From 10645bf02babb6b91a2424f9221d48a7d1676761 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Sun, 21 Aug 2022 18:10:03 +0000 Subject: [PATCH] DB: Add Collab tables for recording Recip's and Topic's Accept activities --- migrations/425_2022-08-21_collab_accept.model | 20 ++++ .../426_2022-08-21_collab_accept_mig.model | 101 ++++++++++++++++++ src/Vervis/Access.hs | 33 +++--- src/Vervis/Migration.hs | 89 +++++++++++++++ src/Vervis/Migration/Model.hs | 6 ++ th/models | 35 +++++- 6 files changed, 258 insertions(+), 26 deletions(-) create mode 100644 migrations/425_2022-08-21_collab_accept.model create mode 100644 migrations/426_2022-08-21_collab_accept_mig.model diff --git a/migrations/425_2022-08-21_collab_accept.model b/migrations/425_2022-08-21_collab_accept.model new file mode 100644 index 0000000..94b6474 --- /dev/null +++ b/migrations/425_2022-08-21_collab_accept.model @@ -0,0 +1,20 @@ +CollabTopicAccept + collab CollabId + accept OutboxItemId + + UniqueCollabTopicAcceptCollab collab + UniqueCollabTopicAcceptAccept accept + +CollabRecipLocalAccept + collab CollabRecipLocalId + accept OutboxItemId + + UniqueCollabRecipLocalAcceptCollab collab + UniqueCollabRecipLocalAcceptAccept accept + +CollabRecipRemoteAccept + collab CollabRecipRemoteId + accept RemoteActivityId + + UniqueCollabRecipRemoteAcceptCollab collab + UniqueCollabRecipRemoteAcceptAccept accept diff --git a/migrations/426_2022-08-21_collab_accept_mig.model b/migrations/426_2022-08-21_collab_accept_mig.model new file mode 100644 index 0000000..2758a2d --- /dev/null +++ b/migrations/426_2022-08-21_collab_accept_mig.model @@ -0,0 +1,101 @@ +RemoteActor +RemoteActivity +Role +Workflow +Inbox +FollowerSet + +Collab + +CollabTopicLocalRepo + collab CollabId + repo RepoId + + UniqueCollabTopicLocalRepo collab + +CollabTopicLocalDeck + collab CollabId + deck DeckId + + UniqueCollabTopicLocalDeck collab + +CollabTopicLocalLoom + collab CollabId + loom LoomId + + UniqueCollabTopicLocalLoom collab + +CollabTopicAccept + collab CollabId + accept OutboxItemId + + UniqueCollabTopicAcceptCollab collab + UniqueCollabTopicAcceptAccept accept + +CollabSenderLocal + collab CollabId + activity OutboxItemId + + UniqueCollabSenderLocal collab + UniqueCollabSenderLocalActivity activity + +CollabSenderRemote + collab CollabId + actor RemoteActorId + activity RemoteActivityId + + UniqueCollabSenderRemote collab + UniqueCollabSenderRemoteActivity activity + +Repo + vcs VersionControlSystem + project DeckId Maybe + mainBranch Text + collabUser RoleId Maybe + collabAnon RoleId Maybe + actor ActorId + create OutboxItemId + + UniqueRepoActor actor + UniqueRepoCreate create + +Deck + actor ActorId + workflow WorkflowId + nextTicket Int + wiki RepoId Maybe + collabUser RoleId Maybe + collabAnon RoleId Maybe + create OutboxItemId + + UniqueDeckActor actor + UniqueDeckCreate create + +Loom + nextTicket Int + actor ActorId + repo RepoId + create OutboxItemId + + UniqueLoomActor actor + UniqueLoomRepo repo + UniqueLoomCreate create + +Actor + name Text + desc Text + createdAt UTCTime + inbox InboxId + outbox OutboxId + followers FollowerSetId + + UniqueActorInbox inbox + UniqueActorOutbox outbox + UniqueActorFollowers followers + +Outbox + +OutboxItem + outbox OutboxId + activity PersistJSONObject + published UTCTime diff --git a/src/Vervis/Access.hs b/src/Vervis/Access.hs index 67ede84..ea09f7c 100644 --- a/src/Vervis/Access.hs +++ b/src/Vervis/Access.hs @@ -139,18 +139,15 @@ checkRepoAccess' mpid op repoID = do status <$> roleHasAccess role op where asCollab rid pid = do - fmap (maybe Developer RoleID . E.unValue . snd) . listToMaybe <$> do - E.select $ E.from $ \ (topic `E.InnerJoin` recip `E.LeftOuterJoin` role) -> do - E.on $ E.just (topic E.^. CollabTopicLocalRepoCollab) E.==. role E.?. CollabRoleLocalCollab + fmap (const Developer) . listToMaybe <$> do + E.select $ E.from $ \ (topic `E.InnerJoin` recip `E.InnerJoin` accept) -> do + E.on $ topic E.^. CollabTopicLocalRepoCollab E.==. accept E.^. CollabTopicAcceptCollab E.on $ topic E.^. CollabTopicLocalRepoCollab E.==. recip E.^. CollabRecipLocalCollab E.where_ $ topic E.^. CollabTopicLocalRepoRepo E.==. E.val rid E.&&. recip E.^. CollabRecipLocalPerson E.==. E.val pid E.limit 1 - return - ( topic E.^. CollabTopicLocalRepoCollab - , role E.?. CollabRoleLocalRole - ) + return $ topic E.^. CollabTopicLocalRepoCollab asUser = fmap RoleID . repoCollabUser asAnon = fmap RoleID . repoCollabAnon @@ -176,18 +173,15 @@ checkRepoAccess mpid op repoHash = do status <$> roleHasAccess role op where asCollab rid pid = do - fmap (maybe Developer RoleID . E.unValue . snd) . listToMaybe <$> do - E.select $ E.from $ \ (topic `E.InnerJoin` recip `E.LeftOuterJoin` role) -> do - E.on $ E.just (topic E.^. CollabTopicLocalRepoCollab) E.==. role E.?. CollabRoleLocalCollab + fmap (const Developer) . listToMaybe <$> do + E.select $ E.from $ \ (topic `E.InnerJoin` recip `E.InnerJoin` accept) -> do + E.on $ topic E.^. CollabTopicLocalRepoCollab E.==. accept E.^. CollabTopicAcceptCollab E.on $ topic E.^. CollabTopicLocalRepoCollab E.==. recip E.^. CollabRecipLocalCollab E.where_ $ topic E.^. CollabTopicLocalRepoRepo E.==. E.val rid E.&&. recip E.^. CollabRecipLocalPerson E.==. E.val pid E.limit 1 - return - ( topic E.^. CollabTopicLocalRepoCollab - , role E.?. CollabRoleLocalRole - ) + return $ topic E.^. CollabTopicLocalRepoCollab asUser = fmap RoleID . repoCollabUser asAnon = fmap RoleID . repoCollabAnon @@ -214,17 +208,14 @@ checkProjectAccess mpid op deckHash = do status <$> roleHasAccess role op where asCollab jid pid = do - fmap (maybe Developer RoleID . E.unValue . snd) . listToMaybe <$> do - E.select $ E.from $ \ (topic `E.InnerJoin` recip `E.LeftOuterJoin` role) -> do - E.on $ E.just (topic E.^. CollabTopicLocalDeckCollab) E.==. role E.?. CollabRoleLocalCollab + fmap (const Developer) . listToMaybe <$> do + E.select $ E.from $ \ (topic `E.InnerJoin` recip `E.InnerJoin` accept) -> do + E.on $ topic E.^. CollabTopicLocalDeckCollab E.==. accept E.^. CollabTopicAcceptCollab E.on $ topic E.^. CollabTopicLocalDeckCollab E.==. recip E.^. CollabRecipLocalCollab E.where_ $ topic E.^. CollabTopicLocalDeckDeck E.==. E.val jid E.&&. recip E.^. CollabRecipLocalPerson E.==. E.val pid E.limit 1 - return - ( topic E.^. CollabTopicLocalDeckCollab - , role E.?. CollabRoleLocalRole - ) + return $ topic E.^. CollabTopicLocalDeckCollab asUser = fmap RoleID . deckCollabUser asAnon = fmap RoleID . deckCollabAnon diff --git a/src/Vervis/Migration.hs b/src/Vervis/Migration.hs index 7510620..59c8a60 100644 --- a/src/Vervis/Migration.hs +++ b/src/Vervis/Migration.hs @@ -2343,6 +2343,95 @@ changes hLocal ctx = , removeField "Workflow" "sharer" -- 423 , removeEntity "Sharer" + -- 424 + , removeEntity "CollabRoleLocal" + -- 425 + , addEntities model_425_collab_accept + -- 426 + , unchecked $ lift $ do + repos <- selectList [] [Asc CollabTopicLocalRepo426Id] + for_ repos $ \ (Entity _ (CollabTopicLocalRepo426 collabID repoID)) -> do + repo <- getJust repoID + itemID <- do + mgrant <- runMaybeT $ do + CollabSenderLocal426 _ grantID <- + MaybeT $ maybeRight <$> + requireEitherAlt + (getBy $ UniqueCollabSenderRemote426 collabID) + (getValBy $ UniqueCollabSenderLocal426 collabID) + "No CollabSender*" + "Multiple CollabSender*" + OutboxItem426 outboxID _ _ <- lift $ getJust $ grantID + actorID <- do + mactor <- lift $ getKeyBy $ UniqueActorOutbox426 outboxID + case mactor of + Just a -> return a + Nothing -> error "Outbox with no actor" + guard $ repo426Actor repo == actorID + return grantID + case mgrant of + Just grantID -> return grantID + Nothing -> do + actor <- getJust $ repo426Actor repo + let doc = persistJSONObjectFromDoc $ Doc hLocal emptyActivity + insert $ OutboxItem426 (actor426Outbox actor) doc defaultTime + insert_ $ CollabTopicAccept426 collabID itemID + + decks <- selectList [] [Asc CollabTopicLocalDeck426Id] + for_ decks $ \ (Entity _ (CollabTopicLocalDeck426 collabID deckID)) -> do + deck <- getJust deckID + itemID <- do + mgrant <- runMaybeT $ do + CollabSenderLocal426 _ grantID <- + MaybeT $ maybeRight <$> + requireEitherAlt + (getBy $ UniqueCollabSenderRemote426 collabID) + (getValBy $ UniqueCollabSenderLocal426 collabID) + "No CollabSender*" + "Multiple CollabSender*" + OutboxItem426 outboxID _ _ <- lift $ getJust $ grantID + actorID <- do + mactor <- lift $ getKeyBy $ UniqueActorOutbox426 outboxID + case mactor of + Just a -> return a + Nothing -> error "Outbox with no actor" + guard $ deck426Actor deck == actorID + return grantID + case mgrant of + Just grantID -> return grantID + Nothing -> do + actor <- getJust $ deck426Actor deck + let doc = persistJSONObjectFromDoc $ Doc hLocal emptyActivity + insert $ OutboxItem426 (actor426Outbox actor) doc defaultTime + insert_ $ CollabTopicAccept426 collabID itemID + + looms <- selectList [] [Asc CollabTopicLocalLoom426Id] + for_ looms $ \ (Entity _ (CollabTopicLocalLoom426 collabID loomID)) -> do + loom <- getJust loomID + itemID <- do + mgrant <- runMaybeT $ do + CollabSenderLocal426 _ grantID <- + MaybeT $ maybeRight <$> + requireEitherAlt + (getBy $ UniqueCollabSenderRemote426 collabID) + (getValBy $ UniqueCollabSenderLocal426 collabID) + "No CollabSender*" + "Multiple CollabSender*" + OutboxItem426 outboxID _ _ <- lift $ getJust $ grantID + actorID <- do + mactor <- lift $ getKeyBy $ UniqueActorOutbox426 outboxID + case mactor of + Just a -> return a + Nothing -> error "Outbox with no actor" + guard $ loom426Actor loom == actorID + return grantID + case mgrant of + Just grantID -> return grantID + Nothing -> do + actor <- getJust $ loom426Actor loom + let doc = persistJSONObjectFromDoc $ Doc hLocal emptyActivity + insert $ OutboxItem426 (actor426Outbox actor) doc defaultTime + insert_ $ CollabTopicAccept426 collabID itemID ] migrateDB diff --git a/src/Vervis/Migration/Model.hs b/src/Vervis/Migration/Model.hs index 9c9f644..29ea146 100644 --- a/src/Vervis/Migration/Model.hs +++ b/src/Vervis/Migration/Model.hs @@ -623,3 +623,9 @@ makeEntitiesMigration "414" makeEntitiesMigration "418" $(modelFile "migrations/418_2022-08-06_follow_actor.model") + +model_425_collab_accept :: [Entity SqlBackend] +model_425_collab_accept = $(schema "425_2022-08-21_collab_accept") + +makeEntitiesMigration "426" + $(modelFile "migrations/426_2022-08-21_collab_accept_mig.model") diff --git a/th/models b/th/models index 5ca3521..0e9493a 100644 --- a/th/models +++ b/th/models @@ -588,11 +588,12 @@ Collab -------------------------------- Collab topic -------------------------------- -CollabRoleLocal - collab CollabId - role RoleId - - UniqueCollabRoleLocal collab +-- Removed for now, until I figure out whether/how to federate custom roles +--CollabRoleLocal +-- collab CollabId +-- role RoleId +-- +-- UniqueCollabRoleLocal collab CollabTopicLocalRepo collab CollabId @@ -612,6 +613,16 @@ CollabTopicLocalLoom UniqueCollabTopicLocalLoom collab +CollabTopicAccept + collab CollabId + accept OutboxItemId + + UniqueCollabTopicAcceptCollab collab + UniqueCollabTopicAcceptAccept accept + +-- Do we need this for S2S? Or is this just for the Client, to decide which +-- Grant URI to use as the 'capability'? If latter, look into removing this +-- table... CollabTopicRemote collab CollabId topic RemoteObjectId @@ -644,12 +655,26 @@ CollabRecipLocal UniqueCollabRecipLocal collab +CollabRecipLocalAccept + collab CollabRecipLocalId + accept OutboxItemId + + UniqueCollabRecipLocalAcceptCollab collab + UniqueCollabRecipLocalAcceptAccept accept + CollabRecipRemote collab CollabId actor RemoteActorId UniqueCollabRecipRemote collab +CollabRecipRemoteAccept + collab CollabRecipRemoteId + accept RemoteActivityId + + UniqueCollabRecipRemoteAcceptCollab collab + UniqueCollabRecipRemoteAcceptAccept accept + -------------------------------- Collab reason ------------------------------- CollabFulfillsLocalTopicCreation