Skip to content

Commit 3c55367

Browse files
authored
Merge pull request #6 from unisoncomputing/cp/user-info-tickets-contributions
Add user info to tickets and contributions
2 parents 4479ebc + af26824 commit 3c55367

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+428
-137
lines changed

src/Share/Postgres/Contributions/Queries.hs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Data.List qualified as List
2525
import Data.Map qualified as Map
2626
import Data.Set qualified as Set
2727
import Data.Time (UTCTime)
28+
import Safe (lastMay)
2829
import Share.Contribution (Contribution (..), ContributionStatus (..))
2930
import Share.IDs
3031
import Share.Postgres qualified as PG
@@ -35,7 +36,6 @@ import Share.Utils.API
3536
import Share.Web.Errors
3637
import Share.Web.Share.Contributions.API (ContributionTimelineCursor, ListContributionsCursor)
3738
import Share.Web.Share.Contributions.Types
38-
import Safe (lastMay)
3939

4040
createContribution ::
4141
-- | Author
@@ -108,7 +108,7 @@ contributionByProjectIdAndNumber projectId contributionNumber = do
108108
shareContributionByProjectIdAndNumber ::
109109
ProjectId ->
110110
ContributionNumber ->
111-
PG.Transaction e (Maybe ShareContribution)
111+
PG.Transaction e (Maybe (ShareContribution UserId))
112112
shareContributionByProjectIdAndNumber projectId contributionNumber = do
113113
PG.query1Row
114114
[PG.sql|
@@ -126,7 +126,7 @@ shareContributionByProjectIdAndNumber projectId contributionNumber = do
126126
target_branch_contributor.handle,
127127
contribution.created_at,
128128
contribution.updated_at,
129-
author.handle,
129+
contribution.author_id,
130130
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
131131
FROM contributions AS contribution
132132
JOIN projects AS project ON project.id = contribution.project_id
@@ -135,7 +135,6 @@ shareContributionByProjectIdAndNumber projectId contributionNumber = do
135135
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
136136
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
137137
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
138-
JOIN users AS author ON author.id = contribution.author_id
139138
WHERE contribution.project_id = #{projectId}
140139
AND contribution_number = #{contributionNumber}
141140
|]
@@ -149,7 +148,7 @@ listContributionsByProjectId ::
149148
Maybe UserId ->
150149
Maybe ContributionStatus ->
151150
Maybe ContributionKindFilter ->
152-
PG.Transaction e (Maybe (Cursor ListContributionsCursor), [ShareContribution])
151+
PG.Transaction e (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
153152
listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter mayKindFilter = do
154153
let kindFilter = case mayKindFilter of
155154
Nothing -> "true"
@@ -164,7 +163,7 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
164163
(contribution.updated_at, contribution.id) < (#{beforeTime}, #{contributionId})
165164
|]
166165
addCursor
167-
<$> PG.queryListRows @ShareContribution
166+
<$> PG.queryListRows @(ShareContribution UserId)
168167
[PG.sql|
169168
SELECT
170169
contribution.id,
@@ -180,7 +179,7 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
180179
target_branch_contributor.handle,
181180
contribution.created_at,
182181
contribution.updated_at,
183-
author.handle,
182+
contribution.author_id,
184183
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
185184
FROM contributions AS contribution
186185
JOIN projects AS project ON project.id = contribution.project_id
@@ -189,7 +188,6 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
189188
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
190189
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
191190
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
192-
JOIN users AS author ON author.id = contribution.author_id
193191
WHERE
194192
^{kindFilter}
195193
AND ^{cursorFilter}
@@ -200,7 +198,7 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
200198
LIMIT #{limit}
201199
|]
202200
where
203-
addCursor :: [ShareContribution] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution])
201+
addCursor :: [ShareContribution UserId] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
204202
addCursor xs =
205203
( lastMay xs <&> \(ShareContribution {updatedAt, contributionId}) ->
206204
Cursor (updatedAt, contributionId),
@@ -285,7 +283,7 @@ listContributionsByUserId ::
285283
Maybe (Cursor (UTCTime, ContributionId)) ->
286284
Maybe ContributionStatus ->
287285
Maybe ContributionKindFilter ->
288-
PG.Transaction e (Maybe (Cursor (UTCTime, ContributionId)), [ShareContribution])
286+
PG.Transaction e (Maybe (Cursor (UTCTime, ContributionId)), [ShareContribution UserId])
289287
listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter mayKindFilter = do
290288
let kindFilter = case mayKindFilter of
291289
Nothing -> "true"
@@ -300,7 +298,7 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma
300298
(contribution.updated_at, contribution.id) < (#{beforeTime}, #{contributionId})
301299
|]
302300
addCursor
303-
<$> PG.queryListRows @ShareContribution
301+
<$> PG.queryListRows @(ShareContribution UserId)
304302
[PG.sql|
305303
SELECT
306304
contribution.id,
@@ -316,7 +314,7 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma
316314
target_branch_contributor.handle,
317315
contribution.created_at,
318316
contribution.updated_at,
319-
author.handle
317+
contribution.author_id,
320318
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
321319
FROM contributions AS contribution
322320
JOIN projects AS project ON project.id = contribution.project_id
@@ -336,7 +334,7 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma
336334
LIMIT #{limit}
337335
|]
338336
where
339-
addCursor :: [ShareContribution] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution])
337+
addCursor :: [ShareContribution UserId] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
340338
addCursor xs =
341339
( lastMay xs <&> \(ShareContribution {updatedAt, contributionId}) ->
342340
Cursor (updatedAt, contributionId),
@@ -345,12 +343,12 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma
345343

346344
-- | Note: Doesn't perform auth checks, the assumption is that if you already have access to
347345
-- the branchId you have access to all associated contributions.
348-
shareContributionsByBranchOf :: Traversal s t BranchId [ShareContribution] -> s -> PG.Transaction e t
346+
shareContributionsByBranchOf :: Traversal s t BranchId [ShareContribution UserId] -> s -> PG.Transaction e t
349347
shareContributionsByBranchOf trav s =
350348
s
351349
& unsafePartsOf trav %%~ \branchIds -> do
352350
contributionsByBranch <-
353-
( PG.queryListRows @(PG.Only BranchId PG.:. ShareContribution)
351+
( PG.queryListRows @(PG.Only BranchId PG.:. ShareContribution UserId)
354352
[PG.sql|
355353
WITH source_branches(branch_id) AS (
356354
SELECT * FROM ^{PG.singleColumnTable branchIds}
@@ -370,7 +368,7 @@ shareContributionsByBranchOf trav s =
370368
target_branch_contributor.handle,
371369
contribution.created_at,
372370
contribution.updated_at,
373-
author.handle,
371+
contribution.author_id,
374372
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
375373
FROM source_branches
376374
JOIN project_branches AS source_branch ON source_branch.id = source_branches.branch_id
@@ -380,9 +378,8 @@ shareContributionsByBranchOf trav s =
380378
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
381379
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
382380
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
383-
JOIN users AS author ON author.id = contribution.author_id
384381
|]
385-
<&> ( \(results :: [PG.Only BranchId PG.:. ShareContribution]) ->
382+
<&> ( \(results :: [PG.Only BranchId PG.:. ShareContribution UserId]) ->
386383
-- Group by the source branch Id.
387384
results
388385
& fmap

src/Share/Postgres/Tickets/Queries.hs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Control.Lens
2121
import Control.Monad.Except
2222
import Data.List qualified as List
2323
import Data.Time (UTCTime)
24+
import Safe (lastMay)
2425
import Share.IDs
2526
import Share.Postgres qualified as PG
2627
import Share.Prelude
@@ -30,7 +31,6 @@ import Share.Web.Errors
3031
import Share.Web.Share.Comments
3132
import Share.Web.Share.Tickets.API
3233
import Share.Web.Share.Tickets.Types
33-
import Safe (lastMay)
3434

3535
createTicket ::
3636
-- | Author
@@ -91,9 +91,9 @@ ticketByProjectIdAndNumber projectId ticketNumber = do
9191
shareTicketByProjectIdAndNumber ::
9292
ProjectId ->
9393
TicketNumber ->
94-
PG.Transaction e (Maybe ShareTicket)
94+
PG.Transaction e (Maybe (ShareTicket UserId))
9595
shareTicketByProjectIdAndNumber projectId ticketNumber = do
96-
PG.query1Row @ShareTicket
96+
PG.query1Row @(ShareTicket UserId)
9797
[PG.sql|
9898
SELECT
9999
ticket.id,
@@ -105,12 +105,11 @@ shareTicketByProjectIdAndNumber projectId ticketNumber = do
105105
ticket.status,
106106
ticket.created_at,
107107
ticket.updated_at,
108-
author.handle,
108+
ticket.author_id,
109109
(SELECT COUNT(*) FROM comments comment WHERE comment.ticket_id = ticket.id AND comment.deleted_at IS NULL) as num_comments
110110
FROM tickets AS ticket
111111
JOIN projects AS project ON project.id = ticket.project_id
112112
JOIN users AS project_owner ON project_owner.id = project.owner_user_id
113-
JOIN users AS author ON author.id = ticket.author_id
114113
WHERE ticket.project_id = #{projectId}
115114
AND ticket_number = #{ticketNumber}
116115
|]
@@ -123,7 +122,7 @@ listTicketsByProjectId ::
123122
Maybe (Cursor ListTicketsCursor) ->
124123
Maybe UserId ->
125124
Maybe TicketStatus ->
126-
PG.Transaction e (Maybe (Cursor ListTicketsCursor), [ShareTicket])
125+
PG.Transaction e (Maybe (Cursor ListTicketsCursor), [(ShareTicket UserId)])
127126
listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter = do
128127
let cursorFilter = case mayCursor of
129128
Nothing -> "true"
@@ -132,7 +131,7 @@ listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter =
132131
(ticket.updated_at, ticket.id) < (#{beforeTime}, #{ticketId})
133132
|]
134133
addCursor
135-
<$> PG.queryListRows @ShareTicket
134+
<$> PG.queryListRows @(ShareTicket UserId)
136135
[PG.sql|
137136
SELECT
138137
ticket.id,
@@ -144,12 +143,11 @@ listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter =
144143
ticket.status,
145144
ticket.created_at,
146145
ticket.updated_at,
147-
author.handle,
146+
ticket.author_id,
148147
(SELECT COUNT(*) FROM comments comment WHERE comment.ticket_id = ticket.id AND comment.deleted_at IS NULL) as num_comments
149148
FROM tickets AS ticket
150149
JOIN projects AS project ON project.id = ticket.project_id
151150
JOIN users AS project_owner ON project_owner.id = project.owner_user_id
152-
JOIN users AS author ON author.id = ticket.author_id
153151
WHERE
154152
^{cursorFilter}
155153
AND project.id = #{projectId}
@@ -159,7 +157,7 @@ listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter =
159157
LIMIT #{limit}
160158
|]
161159
where
162-
addCursor :: [ShareTicket] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket])
160+
addCursor :: [ShareTicket UserId] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket UserId])
163161
addCursor xs =
164162
( lastMay xs <&> \(ShareTicket {updatedAt, ticketId}) ->
165163
Cursor (updatedAt, ticketId),
@@ -289,7 +287,7 @@ listTicketsByUserId ::
289287
Limit ->
290288
Maybe (Cursor (UTCTime, TicketId)) ->
291289
Maybe TicketStatus ->
292-
PG.Transaction e (Maybe (Cursor (UTCTime, TicketId)), [ShareTicket])
290+
PG.Transaction e (Maybe (Cursor (UTCTime, TicketId)), [ShareTicket UserId])
293291
listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
294292
let cursorFilter = case mayCursor of
295293
Nothing -> "true"
@@ -298,7 +296,7 @@ listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
298296
(ticket.updated_at, ticket.id) < (#{beforeTime}, #{ticketId})
299297
|]
300298
addCursor
301-
<$> PG.queryListRows @ShareTicket
299+
<$> PG.queryListRows @(ShareTicket UserId)
302300
[PG.sql|
303301
SELECT
304302
ticket.id,
@@ -310,7 +308,7 @@ listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
310308
ticket.status,
311309
ticket.created_at,
312310
ticket.updated_at,
313-
author.handle,
311+
ticket.author_id,
314312
(SELECT COUNT(*) FROM comments comment WHERE comment.ticket_id = ticket.id AND comment.deleted_at IS NULL) as num_comments
315313
FROM tickets AS ticket
316314
JOIN projects AS project ON project.id = ticket.project_id
@@ -329,7 +327,7 @@ listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
329327
LIMIT #{limit}
330328
|]
331329
where
332-
addCursor :: [ShareTicket] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket])
330+
addCursor :: [ShareTicket UserId] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket UserId])
333331
addCursor xs =
334332
( lastMay xs <&> \(ShareTicket {updatedAt, ticketId}) ->
335333
Cursor (updatedAt, ticketId),

src/Share/Web/Share/Branches/Impl.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Share.Postgres.Causal.Queries qualified as CausalQ
2222
import Share.Postgres.Contributions.Queries qualified as ContributionsQ
2323
import Share.Postgres.IDs (CausalId)
2424
import Share.Postgres.Queries qualified as Q
25+
import Share.Postgres.Users.Queries qualified as UsersQ
2526
import Share.Prelude
2627
import Share.Project (Project (..))
2728
import Share.Project qualified as Project
@@ -330,7 +331,9 @@ getProjectBranchDetailsEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandl
330331
pure (project, projectOwner, projectBranchWithCausals)
331332
_authZReceipt <- AuthZ.permissionGuard $ AuthZ.checkProjectBranchRead callerUserId project
332333
PG.runTransaction $ do
333-
branchContributions <- ContributionsQ.shareContributionsByBranchOf id projectBranchId
334+
branchContributions <-
335+
ContributionsQ.shareContributionsByBranchOf id projectBranchId
336+
>>= UsersQ.userDisplayInfoOf (traversed . traversed)
334337
let shareProject = projectToAPI projectOwner project
335338
pure $ API.branchToShareBranch branchShortHand projectBranch shareProject branchContributions
336339

@@ -400,6 +403,7 @@ listBranchesByProjectEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle
400403
branches
401404
& fmap (\(branch@(Branch {branchId}), contributorHandle) -> (branch, branchId, contributorHandle))
402405
& ContributionsQ.shareContributionsByBranchOf (traversed . _2)
406+
>>= UsersQ.userDisplayInfoOf (traversed . _2 . traversed . traversed)
403407
CausalQ.expectCausalHashesByIdsOf (traversed . _1 . branchCausals_) branchesWithContributions
404408

405409
let shareProject = projectToAPI projectOwner project
@@ -484,6 +488,7 @@ listBranchesByUserEndpoint (AuthN.MaybeAuthedUserID callerUserId) contributorHan
484488
& ContributionsQ.shareContributionsByBranchOf (traversed . _2)
485489
branchesWithContributions
486490
& CausalQ.expectCausalHashesByIdsOf (traversed . _1 . branchCausals_)
491+
>>= UsersQ.userDisplayInfoOf (traversed . _2 . traversed . traversed)
487492

488493
let shareBranches =
489494
expandedBranches

src/Share/Web/Share/Branches/Types.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ module Share.Web.Share.Branches.Types where
77

88
import Data.Aeson
99
import Data.Time (UTCTime)
10+
import Servant (FromHttpApiData)
11+
import Servant.API (FromHttpApiData (..))
1012
import Share.Branch (Branch (..))
1113
import Share.IDs
1214
import Share.IDs qualified as IDs
1315
import Share.Postgres.IDs
1416
import Share.Web.Share.Contributions.Types (ShareContribution)
1517
import Share.Web.Share.Projects.Types
16-
import Servant (FromHttpApiData)
17-
import Servant.API (FromHttpApiData (..))
18+
import Share.Web.Share.Types (UserDisplayInfo)
1819

19-
branchToShareBranch :: BranchShortHand -> Branch CausalHash -> APIProject -> [ShareContribution] -> ShareBranch
20+
branchToShareBranch :: BranchShortHand -> Branch CausalHash -> APIProject -> [ShareContribution UserDisplayInfo] -> ShareBranch
2021
branchToShareBranch branchShortHand Branch {createdAt, updatedAt, causal} project contributions = do
2122
ShareBranch
2223
{ branchShortHand,
@@ -33,7 +34,7 @@ data ShareBranch = ShareBranch
3334
updatedAt :: UTCTime,
3435
causalHash :: PrefixedHash "#" CausalHash,
3536
project :: APIProject,
36-
contributions :: [ShareContribution]
37+
contributions :: [ShareContribution UserDisplayInfo]
3738
}
3839

3940
instance ToJSON ShareBranch where

src/Share/Web/Share/Contributions/API.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Share.Web.Share.Contributions.API where
55

66
import Data.Time (UTCTime)
7+
import Servant
78
import Share.Contribution (ContributionStatus)
89
import Share.IDs
910
import Share.Utils.API
@@ -12,7 +13,6 @@ import Share.Web.Share.Comments.API qualified as Comments
1213
import Share.Web.Share.Contributions.Types
1314
import Share.Web.Share.Diffs.Types (ShareNamespaceDiffResponse)
1415
import Share.Web.Share.Types (UserDisplayInfo)
15-
import Servant
1616

1717
type ContributionsByUserAPI = ListContributionsByUserEndpoint
1818

@@ -47,7 +47,7 @@ type ListContributionsByProjectEndpoint =
4747
:> QueryParam "status" ContributionStatus
4848
-- Filter the contributions by the kind of their source branch
4949
:> QueryParam "kind" ContributionKindFilter
50-
:> Get '[JSON] (Paged ListContributionsCursor ShareContribution)
50+
:> Get '[JSON] (Paged ListContributionsCursor (ShareContribution UserDisplayInfo))
5151

5252
type ListContributionsByUserEndpoint =
5353
QueryParam "cursor" (Cursor ListContributionsCursor)
@@ -56,17 +56,17 @@ type ListContributionsByUserEndpoint =
5656
:> QueryParam "status" ContributionStatus
5757
-- Filter the contributions by the kind of their source branch
5858
:> QueryParam "kind" ContributionKindFilter
59-
:> Get '[JSON] (Paged ListContributionsCursor ShareContribution)
59+
:> Get '[JSON] (Paged ListContributionsCursor (ShareContribution UserDisplayInfo))
6060

6161
type CreateContribution =
6262
ReqBody '[JSON] CreateContributionRequest
63-
:> Post '[JSON] ShareContribution
63+
:> Post '[JSON] (ShareContribution UserDisplayInfo)
6464

65-
type GetContributionByNumber = Get '[JSON] ShareContribution
65+
type GetContributionByNumber = Get '[JSON] (ShareContribution UserDisplayInfo)
6666

6767
type UpdateContributionByNumber =
6868
ReqBody '[JSON] UpdateContributionRequest
69-
:> Patch '[JSON] ShareContribution
69+
:> Patch '[JSON] (ShareContribution UserDisplayInfo)
7070

7171
type MergeContribution =
7272
Post '[JSON] ()

0 commit comments

Comments
 (0)