S2S: Project: Accept: Separate child and parent modes' code

This commit is contained in:
Pere Lev 2024-05-13 01:08:47 +03:00
parent 3162a6ac28
commit ee30cb9f70
No known key found for this signature in database
GPG key ID: 5252C5C863E5E57D

View file

@ -725,107 +725,56 @@ projectAccept now projectID (Verse authorIdMsig body) accept = do
remoteRecipsGrant fwdHostsGrant grantID actionGrant remoteRecipsGrant fwdHostsGrant grantID actionGrant
doneDB inboxItemID "[Component mode] Forwarded the Accept and maybe published a Grant" doneDB inboxItemID "[Component mode] Forwarded the Accept and maybe published a Grant"
addChildParent cp = do -- Add-a-child mode
addChildParent (Left (sourceID, topic, mode)) = do
idsForAccept <- mode' <-
bitraverse bitraverse
-- Adding-a-new-child mode -- Child-active mode
(\ (sourceID, topic, mode) -> (sourceID,topic,) <$> bitraverse -- Verify we haven't yet seen child's Accept
(\ () -> do
maybeChildAccept <-
lift $ withDB $
case bimap fst fst topic of
Left localID -> (() <$) <$> getBy (UniqueSourceThemAcceptLocal localID)
Right remoteID -> (() <$) <$> getBy (UniqueSourceThemAcceptRemote remoteID)
verifyNothingE maybeChildAccept "I already saw child's Accept"
)
-- Child-active mode -- Child-passive mode
-- Verify we haven't yet seen child's Accept -- Option 1: We haven't seen child's Accept yet
(\ () -> do -- * Verify sender is the child
maybeChildAccept <- -- Option 2: We saw it, but not my collaborator's Accept
lift $ withDB $ -- * Verify the Accept is authorized
case bimap fst fst topic of -- Otherwise respond with error, no Accept is needed
(\ () -> do
(maybeChildAccept, maybeGrant) <-
lift $ withDB $ liftA2 (,)
(case bimap fst fst topic of
Left localID -> (() <$) <$> getBy (UniqueSourceThemAcceptLocal localID) Left localID -> (() <$) <$> getBy (UniqueSourceThemAcceptLocal localID)
Right remoteID -> (() <$) <$> getBy (UniqueSourceThemAcceptRemote remoteID) Right remoteID -> (() <$) <$> getBy (UniqueSourceThemAcceptRemote remoteID)
verifyNothingE maybeChildAccept "I already saw child's Accept" )
) (getBy $ UniqueSourceUsSendDelegator sourceID)
case (isJust maybeChildAccept, isJust maybeGrant) of
-- Child-passive mode (False, True) -> error "Impossible/bug, didn't see child's Accept but sent a Grant"
-- Option 1: We haven't seen child's Accept yet (False, False) -> do
-- * Verify sender is the child unless (theyIsAuthor topic) $
-- Option 2: We saw it, but not my collaborator's Accept throwE "The Accept I'm waiting for is from my new child"
-- * Verify the Accept is authorized return $ Left ()
-- Otherwise respond with error, no Accept is needed (True, False) -> do
(\ () -> do let muCap = AP.activityCapability $ actbActivity body
(maybeChildAccept, maybeGrant) <- uCap <- fromMaybeE muCap "No capability provided"
lift $ withDB $ liftA2 (,) verifyCapability''
(case bimap fst fst topic of uCap
Left localID -> (() <$) <$> getBy (UniqueSourceThemAcceptLocal localID) authorIdMsig
Right remoteID -> (() <$) <$> getBy (UniqueSourceThemAcceptRemote remoteID) (LocalResourceProject projectID)
) AP.RoleAdmin
(getBy $ UniqueSourceUsSendDelegator sourceID) return $ Right ()
case (isJust maybeChildAccept, isJust maybeGrant) of (True, True) -> throwE "Child already enabled, not needing any further Accept"
(False, True) -> error "Impossible/bug, didn't see child's Accept but sent a Grant"
(False, False) -> do
unless (theyIsAuthor topic) $
throwE "The Accept I'm waiting for is from my new child"
return $ Left ()
(True, False) -> do
let muCap = AP.activityCapability $ actbActivity body
uCap <- fromMaybeE muCap "No capability provided"
verifyCapability''
uCap
authorIdMsig
(LocalResourceProject projectID)
AP.RoleAdmin
return $ Right ()
(True, True) -> throwE "Child already enabled, not needing any further Accept"
)
mode
) )
-- Adding-a-new-parent mode mode
(\ (destID, topic, mode) -> case mode of
-- Parent-active mode
-- Respond with error, we aren't supposed to get any Accept
Left () -> throwE "Parent-active (DestOriginUs) mode, I'm not expecting any Accept"
-- Parent-passive mode
-- Option 1: I haven't yet seen parent's Accept
-- * Verify sender is the parent
-- Option 2: I saw it, but not my collaborator's Accept
-- * Verify the accept is authorized
-- Otherwise respond with error, no Accept is needed
Right themID -> (destID,themID,topic,) <$> do
(maybeParentAccept, maybeUsGesture) <-
lift $ withDB $ liftA2 (,)
(case bimap fst fst topic of
Left localID -> (() <$) <$> getBy (UniqueDestThemAcceptLocalTopic localID)
Right remoteID -> (() <$) <$> getBy (UniqueDestThemAcceptRemoteTopic remoteID)
)
(do l <- getBy $ UniqueDestUsGestureLocal destID
r <- getBy $ UniqueDestUsGestureRemote destID
case (isJust l, isJust r) of
(False, False) -> pure Nothing
(False, True) -> pure $ Just ()
(True, False) -> pure $ Just ()
(True, True) -> error "Both DestUsGestureLocal and DestUsGestureRemote"
)
case (isJust maybeParentAccept, isJust maybeUsGesture) of
(False, True) -> error "Impossible/bug, didn't see parent's Accept but recorded my collaborator's Accept"
(False, False) -> do
unless (theyIsAuthor topic) $
throwE "The Accept I'm waiting for is from my new parent"
return $ Left ()
(True, False) -> do
let muCap = AP.activityCapability $ actbActivity body
uCap <- fromMaybeE muCap "No capability provided"
verifyCapability''
uCap
authorIdMsig
(LocalResourceProject projectID)
AP.RoleAdmin
return $ Right ()
(True, True) -> throwE "Just waiting for Grant from parent, or already have it, anyway not needing any further Accept"
)
cp
maybeNew <- withDBExcept $ do maybeNew <- withDBExcept $ do
@ -839,93 +788,60 @@ projectAccept now projectID (Verse authorIdMsig body) accept = do
for maybeAcceptDB $ \ (inboxItemID, acceptDB) -> do for maybeAcceptDB $ \ (inboxItemID, acceptDB) -> do
idsForGrant <- idsForGrant <-
lift $
bitraverse bitraverse
-- Child mode -- Child-active mode
(\ (sourceID, topic, mode) -> lift $ bitraverse -- If sender is child, record the Accept into the
-- Source record & prepare to send degelator-Grant
-- Child-active mode -- Othrerwise do nothing
-- If sender is child, record the Accept into the (\ () ->
-- Source record & prepare to send degelator-Grant if theyIsAuthor topic
-- Othrerwise do nothing then Just <$> do
(\ () ->
if theyIsAuthor topic
then Just <$> do
case (topic, acceptDB) of
(Left (localID, _), Left (_, _, acceptID)) ->
insert_ $ SourceThemAcceptLocal localID acceptID
(Right (remoteID, _), Right (_, _, acceptID)) ->
insert_ $ SourceThemAcceptRemote remoteID acceptID
_ -> error "projectAccept impossible iv"
grantID <- insertEmptyOutboxItem' (actorOutbox recipActor) now
insert_ $ SourceUsSendDelegator sourceID grantID
return (topic, grantID)
else pure Nothing
)
-- Child-passive mode
(\case
-- Getting an Accept from the child
-- Record child's Accept in Source record
Left () -> do
case (topic, acceptDB) of case (topic, acceptDB) of
(Left (localID, _), Left (_, _, acceptID)) -> (Left (localID, _), Left (_, _, acceptID)) ->
insert_ $ SourceThemAcceptLocal localID acceptID insert_ $ SourceThemAcceptLocal localID acceptID
(Right (remoteID, _), Right (_, _, acceptID)) -> (Right (remoteID, _), Right (_, _, acceptID)) ->
insert_ $ SourceThemAcceptRemote remoteID acceptID insert_ $ SourceThemAcceptRemote remoteID acceptID
_ -> error "projectAccept impossible v"
return Nothing
-- Getting an Accept from my collaborator
-- Record my collaborator's Accept
-- Prepare to send delegator-Grant
Right () -> Just <$> do
{-
case (topic, acceptDB) of
(Left (localID, _), Left (_, _, acceptID)) ->
insert_ $ ? localID acceptID
(Right (remoteID, _), Right (_, _, acceptID)) ->
insert_ $ ? remoteID acceptID
_ -> error "projectAccept impossible iv" _ -> error "projectAccept impossible iv"
-}
grantID <- insertEmptyOutboxItem' (actorOutbox recipActor) now grantID <- insertEmptyOutboxItem' (actorOutbox recipActor) now
insert_ $ SourceUsSendDelegator sourceID grantID insert_ $ SourceUsSendDelegator sourceID grantID
return (topic, grantID) return grantID
) else pure Nothing
mode
) )
-- Parent-passive mode -- Child-passive mode
(\ (destID, themID, topic, mode) -> lift $ case mode of (\case
-- Getting an Accept from the parent -- Getting an Accept from the child
-- Record parent's Accept in the Dest record -- Record child's Accept in Source record
Left () -> do Left () -> do
case (topic, acceptDB) of case (topic, acceptDB) of
(Left (localID, _), Left (_, _, acceptID)) -> (Left (localID, _), Left (_, _, acceptID)) ->
insert_ $ DestThemAcceptLocal themID localID acceptID insert_ $ SourceThemAcceptLocal localID acceptID
(Right (remoteID, _), Right (_, _, acceptID)) -> (Right (remoteID, _), Right (_, _, acceptID)) ->
insert_ $ DestThemAcceptRemote themID remoteID acceptID insert_ $ SourceThemAcceptRemote remoteID acceptID
_ -> error "projectAccept impossible v" _ -> error "projectAccept impossible v"
return Nothing return Nothing
-- Getting an Accept from my collaborator -- Getting an Accept from my collaborator
-- Record my collaborator's Accept in the Dest record -- Record my collaborator's Accept
-- Prepare to send my own Accept -- Prepare to send delegator-Grant
Right () -> Just <$> do Right () -> Just <$> do
case acceptDB of {-
Left (_, _, acceptID) -> case (topic, acceptDB) of
insert_ $ DestUsGestureLocal destID acceptID (Left (localID, _), Left (_, _, acceptID)) ->
Right (author, _, acceptID) -> insert_ $ ? localID acceptID
insert_ $ DestUsGestureRemote destID (remoteAuthorId author) acceptID (Right (remoteID, _), Right (_, _, acceptID)) ->
acceptID <- insertEmptyOutboxItem' (actorOutbox recipActor) now insert_ $ ? remoteID acceptID
insert_ $ DestUsAccept destID acceptID _ -> error "projectAccept impossible iv"
return (topic, acceptID) -}
grantID <- insertEmptyOutboxItem' (actorOutbox recipActor) now
insert_ $ SourceUsSendDelegator sourceID grantID
return grantID
) )
idsForAccept mode'
-- Prepare forwarding of Accept to my followers -- Prepare forwarding of Accept to my followers
@ -935,27 +851,20 @@ projectAccept now projectID (Verse authorIdMsig body) accept = do
maybeAct <- maybeAct <-
case idsForGrant of case idsForGrant of
Left (Left mg) -> for mg $ \ (topic, grantID) -> lift $ do Left mg -> for mg $ \ grantID -> lift $ do
grant@(actionGrant, _, _, _) <- grant@(actionGrant, _, _, _) <-
prepareSourceDelegGrant (bimap snd snd topic) False prepareSourceDelegGrant (bimap snd snd topic) False
let recipByKey = LocalActorProject projectID let recipByKey = LocalActorProject projectID
_luGrant <- updateOutboxItem' recipByKey grantID actionGrant _luGrant <- updateOutboxItem' recipByKey grantID actionGrant
return (grantID, grant) return (grantID, grant)
Left (Right mg) -> for mg $ \ (topic, grantID) -> lift $ do Right mg -> for mg $ \ grantID -> lift $ do
grant@(actionGrant, _, _, _) <- grant@(actionGrant, _, _, _) <-
prepareSourceDelegGrant (bimap snd snd topic) True prepareSourceDelegGrant (bimap snd snd topic) True
let recipByKey = LocalActorProject projectID let recipByKey = LocalActorProject projectID
_luGrant <- updateOutboxItem' recipByKey grantID actionGrant _luGrant <- updateOutboxItem' recipByKey grantID actionGrant
return (grantID, grant) return (grantID, grant)
Right ma -> for ma $ \ (topic, acceptID) -> lift $ do
accept@(actionAccept, _, _, _) <-
prepareDestAccept (bimap snd snd topic)
let recipByKey = LocalActorProject projectID
_luAccept <- updateOutboxItem' recipByKey acceptID actionAccept
return (acceptID, accept)
return (recipActorID, sieve, maybeAct, inboxItemID) return (recipActorID, sieve, maybeAct, inboxItemID)
case maybeNew of case maybeNew of
@ -967,7 +876,119 @@ projectAccept now projectID (Verse authorIdMsig body) accept = do
sendActivity sendActivity
recipByID recipActorID localRecipsGrant recipByID recipActorID localRecipsGrant
remoteRecipsGrant fwdHostsGrant grantID actionGrant remoteRecipsGrant fwdHostsGrant grantID actionGrant
doneDB inboxItemID "[Child/Parent mode] Forwarded the Accept and maybe published a Grant/Accept" doneDB inboxItemID "[Child mode] Forwarded the Accept and maybe published a Grant/Accept"
-- Add-a-parent mode
addChildParent (Right (destID, topic, mode)) = do
(themID, mode') <-
case mode of
-- Parent-active mode
-- Respond with error, we aren't supposed to get any Accept
Left () -> throwE "Parent-active (DestOriginUs) mode, I'm not expecting any Accept"
-- Parent-passive mode
-- Option 1: I haven't yet seen parent's Accept
-- * Verify sender is the parent
-- Option 2: I saw it, but not my collaborator's Accept
-- * Verify the accept is authorized
-- Otherwise respond with error, no Accept is needed
Right themID -> (themID,) <$> do
(maybeParentAccept, maybeUsGesture) <-
lift $ withDB $ liftA2 (,)
(case bimap fst fst topic of
Left localID -> (() <$) <$> getBy (UniqueDestThemAcceptLocalTopic localID)
Right remoteID -> (() <$) <$> getBy (UniqueDestThemAcceptRemoteTopic remoteID)
)
(do l <- getBy $ UniqueDestUsGestureLocal destID
r <- getBy $ UniqueDestUsGestureRemote destID
case (isJust l, isJust r) of
(False, False) -> pure Nothing
(False, True) -> pure $ Just ()
(True, False) -> pure $ Just ()
(True, True) -> error "Both DestUsGestureLocal and DestUsGestureRemote"
)
case (isJust maybeParentAccept, isJust maybeUsGesture) of
(False, True) -> error "Impossible/bug, didn't see parent's Accept but recorded my collaborator's Accept"
(False, False) -> do
unless (theyIsAuthor topic) $
throwE "The Accept I'm waiting for is from my new parent"
return $ Left ()
(True, False) -> do
let muCap = AP.activityCapability $ actbActivity body
uCap <- fromMaybeE muCap "No capability provided"
verifyCapability''
uCap
authorIdMsig
(LocalResourceProject projectID)
AP.RoleAdmin
return $ Right ()
(True, True) -> throwE "Just waiting for Grant from parent, or already have it, anyway not needing any further Accept"
maybeNew <- withDBExcept $ do
-- Grab me from DB
(recipActorID, recipActor) <- lift $ do
recip <- getJust projectID
let actorID = projectActor recip
(actorID,) <$> getJust actorID
maybeAcceptDB <- lift $ insertToInbox now authorIdMsig body (actorInbox recipActor) False
for maybeAcceptDB $ \ (inboxItemID, acceptDB) -> do
idsForGrant <-
lift $ case mode' of
-- Getting an Accept from the parent
-- Record parent's Accept in the Dest record
Left () -> do
case (topic, acceptDB) of
(Left (localID, _), Left (_, _, acceptID)) ->
insert_ $ DestThemAcceptLocal themID localID acceptID
(Right (remoteID, _), Right (_, _, acceptID)) ->
insert_ $ DestThemAcceptRemote themID remoteID acceptID
_ -> error "projectAccept impossible v"
return Nothing
-- Getting an Accept from my collaborator
-- Record my collaborator's Accept in the Dest record
-- Prepare to send my own Accept
Right () -> Just <$> do
case acceptDB of
Left (_, _, acceptID) ->
insert_ $ DestUsGestureLocal destID acceptID
Right (author, _, acceptID) ->
insert_ $ DestUsGestureRemote destID (remoteAuthorId author) acceptID
acceptID <- insertEmptyOutboxItem' (actorOutbox recipActor) now
insert_ $ DestUsAccept destID acceptID
return acceptID
-- Prepare forwarding of Accept to my followers
let recipByID = LocalActorProject projectID
recipByHash <- hashLocalActor recipByID
let sieve = makeRecipientSet [] [localActorFollowers recipByHash]
maybeAct <-
for idsForGrant $ \ acceptID -> lift $ do
accept@(actionAccept, _, _, _) <-
prepareDestAccept (bimap snd snd topic)
let recipByKey = LocalActorProject projectID
_luAccept <- updateOutboxItem' recipByKey acceptID actionAccept
return (acceptID, accept)
return (recipActorID, sieve, maybeAct, inboxItemID)
case maybeNew of
Nothing -> done "I already have this activity in my inbox"
Just (recipActorID, sieve, maybeGrant, inboxItemID) -> do
let recipByID = LocalActorProject projectID
forwardActivity authorIdMsig body recipByID recipActorID sieve
lift $ for_ maybeGrant $ \ (grantID, (actionGrant, localRecipsGrant, remoteRecipsGrant, fwdHostsGrant)) ->
sendActivity
recipByID recipActorID localRecipsGrant
remoteRecipsGrant fwdHostsGrant grantID actionGrant
doneDB inboxItemID "[Parent mode] Forwarded the Accept and maybe published a Grant/Accept"
prepareCollabGrant isInvite sender role = do prepareCollabGrant isInvite sender role = do
encodeRouteHome <- getEncodeRouteHome encodeRouteHome <- getEncodeRouteHome