Skip to content

Commit

Permalink
Merge pull request #6 from unisoncomputing/cp/user-info-tickets-contr…
Browse files Browse the repository at this point in the history
…ibutions

Add user info to tickets and contributions
  • Loading branch information
ChrisPenner authored Jul 1, 2024
2 parents 4479ebc + af26824 commit 3c55367
Show file tree
Hide file tree
Showing 42 changed files with 428 additions and 137 deletions.
33 changes: 15 additions & 18 deletions src/Share/Postgres/Contributions/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Data.List qualified as List
import Data.Map qualified as Map
import Data.Set qualified as Set
import Data.Time (UTCTime)
import Safe (lastMay)
import Share.Contribution (Contribution (..), ContributionStatus (..))
import Share.IDs
import Share.Postgres qualified as PG
Expand All @@ -35,7 +36,6 @@ import Share.Utils.API
import Share.Web.Errors
import Share.Web.Share.Contributions.API (ContributionTimelineCursor, ListContributionsCursor)
import Share.Web.Share.Contributions.Types
import Safe (lastMay)

createContribution ::
-- | Author
Expand Down Expand Up @@ -108,7 +108,7 @@ contributionByProjectIdAndNumber projectId contributionNumber = do
shareContributionByProjectIdAndNumber ::
ProjectId ->
ContributionNumber ->
PG.Transaction e (Maybe ShareContribution)
PG.Transaction e (Maybe (ShareContribution UserId))
shareContributionByProjectIdAndNumber projectId contributionNumber = do
PG.query1Row
[PG.sql|
Expand All @@ -126,7 +126,7 @@ shareContributionByProjectIdAndNumber projectId contributionNumber = do
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
author.handle,
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM contributions AS contribution
JOIN projects AS project ON project.id = contribution.project_id
Expand All @@ -135,7 +135,6 @@ shareContributionByProjectIdAndNumber projectId contributionNumber = do
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
JOIN users AS author ON author.id = contribution.author_id
WHERE contribution.project_id = #{projectId}
AND contribution_number = #{contributionNumber}
|]
Expand All @@ -149,7 +148,7 @@ listContributionsByProjectId ::
Maybe UserId ->
Maybe ContributionStatus ->
Maybe ContributionKindFilter ->
PG.Transaction e (Maybe (Cursor ListContributionsCursor), [ShareContribution])
PG.Transaction e (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter mayKindFilter = do
let kindFilter = case mayKindFilter of
Nothing -> "true"
Expand All @@ -164,7 +163,7 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
(contribution.updated_at, contribution.id) < (#{beforeTime}, #{contributionId})
|]
addCursor
<$> PG.queryListRows @ShareContribution
<$> PG.queryListRows @(ShareContribution UserId)
[PG.sql|
SELECT
contribution.id,
Expand All @@ -180,7 +179,7 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
author.handle,
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM contributions AS contribution
JOIN projects AS project ON project.id = contribution.project_id
Expand All @@ -189,7 +188,6 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
JOIN users AS author ON author.id = contribution.author_id
WHERE
^{kindFilter}
AND ^{cursorFilter}
Expand All @@ -200,7 +198,7 @@ listContributionsByProjectId projectId limit mayCursor mayUserFilter mayStatusFi
LIMIT #{limit}
|]
where
addCursor :: [ShareContribution] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution])
addCursor :: [ShareContribution UserId] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
addCursor xs =
( lastMay xs <&> \(ShareContribution {updatedAt, contributionId}) ->
Cursor (updatedAt, contributionId),
Expand Down Expand Up @@ -285,7 +283,7 @@ listContributionsByUserId ::
Maybe (Cursor (UTCTime, ContributionId)) ->
Maybe ContributionStatus ->
Maybe ContributionKindFilter ->
PG.Transaction e (Maybe (Cursor (UTCTime, ContributionId)), [ShareContribution])
PG.Transaction e (Maybe (Cursor (UTCTime, ContributionId)), [ShareContribution UserId])
listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter mayKindFilter = do
let kindFilter = case mayKindFilter of
Nothing -> "true"
Expand All @@ -300,7 +298,7 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma
(contribution.updated_at, contribution.id) < (#{beforeTime}, #{contributionId})
|]
addCursor
<$> PG.queryListRows @ShareContribution
<$> PG.queryListRows @(ShareContribution UserId)
[PG.sql|
SELECT
contribution.id,
Expand All @@ -316,7 +314,7 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
author.handle
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM contributions AS contribution
JOIN projects AS project ON project.id = contribution.project_id
Expand All @@ -336,7 +334,7 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma
LIMIT #{limit}
|]
where
addCursor :: [ShareContribution] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution])
addCursor :: [ShareContribution UserId] -> (Maybe (Cursor ListContributionsCursor), [ShareContribution UserId])
addCursor xs =
( lastMay xs <&> \(ShareContribution {updatedAt, contributionId}) ->
Cursor (updatedAt, contributionId),
Expand All @@ -345,12 +343,12 @@ listContributionsByUserId callerUserId userId limit mayCursor mayStatusFilter ma

-- | Note: Doesn't perform auth checks, the assumption is that if you already have access to
-- the branchId you have access to all associated contributions.
shareContributionsByBranchOf :: Traversal s t BranchId [ShareContribution] -> s -> PG.Transaction e t
shareContributionsByBranchOf :: Traversal s t BranchId [ShareContribution UserId] -> s -> PG.Transaction e t
shareContributionsByBranchOf trav s =
s
& unsafePartsOf trav %%~ \branchIds -> do
contributionsByBranch <-
( PG.queryListRows @(PG.Only BranchId PG.:. ShareContribution)
( PG.queryListRows @(PG.Only BranchId PG.:. ShareContribution UserId)
[PG.sql|
WITH source_branches(branch_id) AS (
SELECT * FROM ^{PG.singleColumnTable branchIds}
Expand All @@ -370,7 +368,7 @@ shareContributionsByBranchOf trav s =
target_branch_contributor.handle,
contribution.created_at,
contribution.updated_at,
author.handle,
contribution.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.contribution_id = contribution.id AND comment.deleted_at IS NULL) as num_comments
FROM source_branches
JOIN project_branches AS source_branch ON source_branch.id = source_branches.branch_id
Expand All @@ -380,9 +378,8 @@ shareContributionsByBranchOf trav s =
LEFT JOIN users AS source_branch_contributor ON source_branch_contributor.id = source_branch.contributor_id
JOIN project_branches AS target_branch ON target_branch.id = contribution.target_branch
LEFT JOIN users AS target_branch_contributor ON target_branch_contributor.id = target_branch.contributor_id
JOIN users AS author ON author.id = contribution.author_id
|]
<&> ( \(results :: [PG.Only BranchId PG.:. ShareContribution]) ->
<&> ( \(results :: [PG.Only BranchId PG.:. ShareContribution UserId]) ->
-- Group by the source branch Id.
results
& fmap
Expand Down
26 changes: 12 additions & 14 deletions src/Share/Postgres/Tickets/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Control.Lens
import Control.Monad.Except
import Data.List qualified as List
import Data.Time (UTCTime)
import Safe (lastMay)
import Share.IDs
import Share.Postgres qualified as PG
import Share.Prelude
Expand All @@ -30,7 +31,6 @@ import Share.Web.Errors
import Share.Web.Share.Comments
import Share.Web.Share.Tickets.API
import Share.Web.Share.Tickets.Types
import Safe (lastMay)

createTicket ::
-- | Author
Expand Down Expand Up @@ -91,9 +91,9 @@ ticketByProjectIdAndNumber projectId ticketNumber = do
shareTicketByProjectIdAndNumber ::
ProjectId ->
TicketNumber ->
PG.Transaction e (Maybe ShareTicket)
PG.Transaction e (Maybe (ShareTicket UserId))
shareTicketByProjectIdAndNumber projectId ticketNumber = do
PG.query1Row @ShareTicket
PG.query1Row @(ShareTicket UserId)
[PG.sql|
SELECT
ticket.id,
Expand All @@ -105,12 +105,11 @@ shareTicketByProjectIdAndNumber projectId ticketNumber = do
ticket.status,
ticket.created_at,
ticket.updated_at,
author.handle,
ticket.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.ticket_id = ticket.id AND comment.deleted_at IS NULL) as num_comments
FROM tickets AS ticket
JOIN projects AS project ON project.id = ticket.project_id
JOIN users AS project_owner ON project_owner.id = project.owner_user_id
JOIN users AS author ON author.id = ticket.author_id
WHERE ticket.project_id = #{projectId}
AND ticket_number = #{ticketNumber}
|]
Expand All @@ -123,7 +122,7 @@ listTicketsByProjectId ::
Maybe (Cursor ListTicketsCursor) ->
Maybe UserId ->
Maybe TicketStatus ->
PG.Transaction e (Maybe (Cursor ListTicketsCursor), [ShareTicket])
PG.Transaction e (Maybe (Cursor ListTicketsCursor), [(ShareTicket UserId)])
listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter = do
let cursorFilter = case mayCursor of
Nothing -> "true"
Expand All @@ -132,7 +131,7 @@ listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter =
(ticket.updated_at, ticket.id) < (#{beforeTime}, #{ticketId})
|]
addCursor
<$> PG.queryListRows @ShareTicket
<$> PG.queryListRows @(ShareTicket UserId)
[PG.sql|
SELECT
ticket.id,
Expand All @@ -144,12 +143,11 @@ listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter =
ticket.status,
ticket.created_at,
ticket.updated_at,
author.handle,
ticket.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.ticket_id = ticket.id AND comment.deleted_at IS NULL) as num_comments
FROM tickets AS ticket
JOIN projects AS project ON project.id = ticket.project_id
JOIN users AS project_owner ON project_owner.id = project.owner_user_id
JOIN users AS author ON author.id = ticket.author_id
WHERE
^{cursorFilter}
AND project.id = #{projectId}
Expand All @@ -159,7 +157,7 @@ listTicketsByProjectId projectId limit mayCursor mayUserFilter mayStatusFilter =
LIMIT #{limit}
|]
where
addCursor :: [ShareTicket] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket])
addCursor :: [ShareTicket UserId] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket UserId])
addCursor xs =
( lastMay xs <&> \(ShareTicket {updatedAt, ticketId}) ->
Cursor (updatedAt, ticketId),
Expand Down Expand Up @@ -289,7 +287,7 @@ listTicketsByUserId ::
Limit ->
Maybe (Cursor (UTCTime, TicketId)) ->
Maybe TicketStatus ->
PG.Transaction e (Maybe (Cursor (UTCTime, TicketId)), [ShareTicket])
PG.Transaction e (Maybe (Cursor (UTCTime, TicketId)), [ShareTicket UserId])
listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
let cursorFilter = case mayCursor of
Nothing -> "true"
Expand All @@ -298,7 +296,7 @@ listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
(ticket.updated_at, ticket.id) < (#{beforeTime}, #{ticketId})
|]
addCursor
<$> PG.queryListRows @ShareTicket
<$> PG.queryListRows @(ShareTicket UserId)
[PG.sql|
SELECT
ticket.id,
Expand All @@ -310,7 +308,7 @@ listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
ticket.status,
ticket.created_at,
ticket.updated_at,
author.handle,
ticket.author_id,
(SELECT COUNT(*) FROM comments comment WHERE comment.ticket_id = ticket.id AND comment.deleted_at IS NULL) as num_comments
FROM tickets AS ticket
JOIN projects AS project ON project.id = ticket.project_id
Expand All @@ -329,7 +327,7 @@ listTicketsByUserId callerUserId userId limit mayCursor mayStatusFilter = do
LIMIT #{limit}
|]
where
addCursor :: [ShareTicket] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket])
addCursor :: [ShareTicket UserId] -> (Maybe (Cursor ListTicketsCursor), [ShareTicket UserId])
addCursor xs =
( lastMay xs <&> \(ShareTicket {updatedAt, ticketId}) ->
Cursor (updatedAt, ticketId),
Expand Down
7 changes: 6 additions & 1 deletion src/Share/Web/Share/Branches/Impl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Share.Postgres.Causal.Queries qualified as CausalQ
import Share.Postgres.Contributions.Queries qualified as ContributionsQ
import Share.Postgres.IDs (CausalId)
import Share.Postgres.Queries qualified as Q
import Share.Postgres.Users.Queries qualified as UsersQ
import Share.Prelude
import Share.Project (Project (..))
import Share.Project qualified as Project
Expand Down Expand Up @@ -330,7 +331,9 @@ getProjectBranchDetailsEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandl
pure (project, projectOwner, projectBranchWithCausals)
_authZReceipt <- AuthZ.permissionGuard $ AuthZ.checkProjectBranchRead callerUserId project
PG.runTransaction $ do
branchContributions <- ContributionsQ.shareContributionsByBranchOf id projectBranchId
branchContributions <-
ContributionsQ.shareContributionsByBranchOf id projectBranchId
>>= UsersQ.userDisplayInfoOf (traversed . traversed)
let shareProject = projectToAPI projectOwner project
pure $ API.branchToShareBranch branchShortHand projectBranch shareProject branchContributions

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

let shareProject = projectToAPI projectOwner project
Expand Down Expand Up @@ -484,6 +488,7 @@ listBranchesByUserEndpoint (AuthN.MaybeAuthedUserID callerUserId) contributorHan
& ContributionsQ.shareContributionsByBranchOf (traversed . _2)
branchesWithContributions
& CausalQ.expectCausalHashesByIdsOf (traversed . _1 . branchCausals_)
>>= UsersQ.userDisplayInfoOf (traversed . _2 . traversed . traversed)

let shareBranches =
expandedBranches
Expand Down
9 changes: 5 additions & 4 deletions src/Share/Web/Share/Branches/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ module Share.Web.Share.Branches.Types where

import Data.Aeson
import Data.Time (UTCTime)
import Servant (FromHttpApiData)
import Servant.API (FromHttpApiData (..))
import Share.Branch (Branch (..))
import Share.IDs
import Share.IDs qualified as IDs
import Share.Postgres.IDs
import Share.Web.Share.Contributions.Types (ShareContribution)
import Share.Web.Share.Projects.Types
import Servant (FromHttpApiData)
import Servant.API (FromHttpApiData (..))
import Share.Web.Share.Types (UserDisplayInfo)

branchToShareBranch :: BranchShortHand -> Branch CausalHash -> APIProject -> [ShareContribution] -> ShareBranch
branchToShareBranch :: BranchShortHand -> Branch CausalHash -> APIProject -> [ShareContribution UserDisplayInfo] -> ShareBranch
branchToShareBranch branchShortHand Branch {createdAt, updatedAt, causal} project contributions = do
ShareBranch
{ branchShortHand,
Expand All @@ -33,7 +34,7 @@ data ShareBranch = ShareBranch
updatedAt :: UTCTime,
causalHash :: PrefixedHash "#" CausalHash,
project :: APIProject,
contributions :: [ShareContribution]
contributions :: [ShareContribution UserDisplayInfo]
}

instance ToJSON ShareBranch where
Expand Down
12 changes: 6 additions & 6 deletions src/Share/Web/Share/Contributions/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Share.Web.Share.Contributions.API where

import Data.Time (UTCTime)
import Servant
import Share.Contribution (ContributionStatus)
import Share.IDs
import Share.Utils.API
Expand All @@ -12,7 +13,6 @@ import Share.Web.Share.Comments.API qualified as Comments
import Share.Web.Share.Contributions.Types
import Share.Web.Share.Diffs.Types (ShareNamespaceDiffResponse)
import Share.Web.Share.Types (UserDisplayInfo)
import Servant

type ContributionsByUserAPI = ListContributionsByUserEndpoint

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

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

type CreateContribution =
ReqBody '[JSON] CreateContributionRequest
:> Post '[JSON] ShareContribution
:> Post '[JSON] (ShareContribution UserDisplayInfo)

type GetContributionByNumber = Get '[JSON] ShareContribution
type GetContributionByNumber = Get '[JSON] (ShareContribution UserDisplayInfo)

type UpdateContributionByNumber =
ReqBody '[JSON] UpdateContributionRequest
:> Patch '[JSON] ShareContribution
:> Patch '[JSON] (ShareContribution UserDisplayInfo)

type MergeContribution =
Post '[JSON] ()
Expand Down
Loading

0 comments on commit 3c55367

Please sign in to comment.