1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2024-12-27 19:44:50 +09:00

Person: Port the Accept{Follow} handler

This commit is contained in:
Pere Lev 2023-06-05 09:43:28 +03:00
parent b759b87d0f
commit 9b0622cd7a
No known key found for this signature in database
GPG key ID: 5252C5C863E5E57D
5 changed files with 116 additions and 4 deletions

View file

@ -168,7 +168,8 @@ acceptC
-> AP.Accept URIMode -> AP.Accept URIMode
-> ExceptT Text Handler OutboxItemId -> ExceptT Text Handler OutboxItemId
acceptC (Entity senderPersonID senderPerson) senderActor maybeCap localRecips remoteRecips fwdHosts action accept = do acceptC (Entity senderPersonID senderPerson) senderActor maybeCap localRecips remoteRecips fwdHosts action accept = do
error "acceptC temporarily disabled due to actor refactoring"
{-
-- Check input -- Check input
verifyNothingE maybeCap "Capability not needed" verifyNothingE maybeCap "Capability not needed"
acceptee <- parseAccept accept acceptee <- parseAccept accept
@ -374,6 +375,7 @@ acceptC (Entity senderPersonID senderPerson) senderActor maybeCap localRecips re
} }
return (action, recipientSet, remoteActors, fwdHosts) return (action, recipientSet, remoteActors, fwdHosts)
-}
addBundleC addBundleC
:: Entity Person :: Entity Person

View file

@ -217,6 +217,69 @@ personFollow now recipPersonID author body mfwd luFollow follow = do
(\ () -> pure []) (\ () -> pure [])
now recipPersonID author body mfwd luFollow follow now recipPersonID author body mfwd luFollow follow
-- Meaning: A remote actor accepted something
-- Behavior:
-- * Insert to my inbox
-- * If it's a Follow I sent to them, add to my following list in DB
personAccept
:: UTCTime
-> PersonId
-> RemoteAuthor
-> ActivityBody
-> Maybe (RecipientRoutes, ByteString)
-> LocalURI
-> AP.Accept URIMode
-> ActE (Text, Act (), Next)
personAccept now recipPersonID author body mfwd luAccept accept = do
-- Check input
acceptee <- parseAccept accept
maybeAccept <- withDBExcept $ do
-- Grab recipient person from DB
(personRecip, actorRecip) <- lift $ do
p <- getJust recipPersonID
(p,) <$> getJust (personActor p)
mractid <- lift $ insertToInbox now author body (actorInbox actorRecip) luAccept True
for mractid $ \ acceptID -> runMaybeT $ do
-- Find the accepted activity in our DB
accepteeDB <- MaybeT $ getActivity acceptee
tryFollow (personActor personRecip) accepteeDB acceptID
case maybeAccept of
Nothing -> done "I already have this activity in my inbox"
Just Nothing -> done "Not my Follow; Just inserted to my inbox"
Just (Just ()) ->
done "Recorded this Accept on the Follow request I sent"
where
tryFollow actorID (Left (_, _, outboxItemID)) acceptID = do
Entity key val <-
MaybeT $ lift $
getBy $ UniqueFollowRemoteRequestActivity outboxItemID
guard $ followRemoteRequestPerson val == recipPersonID
let uRecip =
fromMaybe
(followRemoteRequestTarget val)
(followRemoteRequestRecip val)
unless (remoteAuthorURI author == uRecip) $
lift $ throwE "You're Accepting a Follow I sent to someone else"
lift $ lift $ delete key
lift $ lift $ insert_ FollowRemote
{ followRemoteActor = actorID
, followRemoteRecip = remoteAuthorId author
, followRemoteTarget = followRemoteRequestTarget val
, followRemotePublic = followRemoteRequestPublic val
, followRemoteFollow = outboxItemID
, followRemoteAccept = acceptID
}
tryFollow _ (Right _) _ = mzero
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- Commenting -- Commenting
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@ -487,6 +550,8 @@ personBehavior now personID (Left event) =
_ -> throwE $ "Unsupported event for Person: " <> T.pack (show event) _ -> throwE $ "Unsupported event for Person: " <> T.pack (show event)
personBehavior now personID (Right (VerseRemote author body mfwd luActivity)) = personBehavior now personID (Right (VerseRemote author body mfwd luActivity)) =
case AP.activitySpecific $ actbActivity body of case AP.activitySpecific $ actbActivity body of
AP.AcceptActivity accept ->
personAccept now personID author body mfwd luActivity accept
AP.CreateActivity (AP.Create obj mtarget) -> AP.CreateActivity (AP.Create obj mtarget) ->
case obj of case obj of
AP.CreateNote _ note -> AP.CreateNote _ note ->

View file

@ -15,7 +15,9 @@
module Vervis.Data.Actor module Vervis.Data.Actor
( parseLocalActivityURI ( parseLocalActivityURI
, parseLocalActivityURI'
, parseActivityURI , parseActivityURI
, parseActivityURI'
, activityRoute , activityRoute
, stampRoute , stampRoute
, parseStampRoute , parseStampRoute
@ -47,6 +49,9 @@ import Yesod.FedURI
import Yesod.Hashids import Yesod.Hashids
import Yesod.MonadSite import Yesod.MonadSite
import qualified Web.Actor as WA
import qualified Web.Actor.Persist as WAP
import Control.Monad.Trans.Except.Local import Control.Monad.Trans.Except.Local
import Vervis.FedURI import Vervis.FedURI
@ -54,6 +59,8 @@ import Vervis.Foundation
import Vervis.Model import Vervis.Model
import Vervis.Recipient import Vervis.Recipient
import qualified Vervis.Actor as VA
parseLocalActivityURI parseLocalActivityURI
:: (MonadSite m, YesodHashids (SiteEnv m)) :: (MonadSite m, YesodHashids (SiteEnv m))
=> LocalURI => LocalURI
@ -75,6 +82,26 @@ parseLocalActivityURI luAct = do
parseOutboxItemRoute (LoomOutboxItemR l i) = Just (LocalActorLoom l, i) parseOutboxItemRoute (LoomOutboxItemR l i) = Just (LocalActorLoom l, i)
parseOutboxItemRoute _ = Nothing parseOutboxItemRoute _ = Nothing
parseLocalActivityURI'
:: LocalURI
-> VA.ActE (LocalActorBy Key, LocalActorBy KeyHashid, OutboxItemId)
parseLocalActivityURI' luAct = do
route <- fromMaybeE (WA.decodeRouteLocal luAct) "Not a valid route"
(actorHash, outboxItemHash) <-
fromMaybeE
(parseOutboxItemRoute route)
"Valid local route, but not an outbox item route"
outboxItemID <- WAP.decodeKeyHashidE outboxItemHash "Invalid outbox item hash"
actorKey <- VA.unhashLocalActorE actorHash "Invalid actor hash"
return (actorKey, actorHash, outboxItemID)
where
parseOutboxItemRoute (PersonOutboxItemR p i) = Just (LocalActorPerson p, i)
parseOutboxItemRoute (GroupOutboxItemR g i) = Just (LocalActorGroup g, i)
parseOutboxItemRoute (RepoOutboxItemR r i) = Just (LocalActorRepo r, i)
parseOutboxItemRoute (DeckOutboxItemR d i) = Just (LocalActorDeck d, i)
parseOutboxItemRoute (LoomOutboxItemR l i) = Just (LocalActorLoom l, i)
parseOutboxItemRoute _ = Nothing
-- | If the given URI is remote, return as is. If the URI is local, verify that -- | If the given URI is remote, return as is. If the URI is local, verify that
-- it parses as an activity URI, i.e. an outbox item route, and return the -- it parses as an activity URI, i.e. an outbox item route, and return the
-- parsed route. -- parsed route.
@ -92,6 +119,22 @@ parseActivityURI u@(ObjURI h lu) = do
then Left <$> parseLocalActivityURI lu then Left <$> parseLocalActivityURI lu
else pure $ Right u else pure $ Right u
-- | If the given URI is remote, return as is. If the URI is local, verify that
-- it parses as an activity URI, i.e. an outbox item route, and return the
-- parsed route.
parseActivityURI'
:: FedURI
-> VA.ActE
(Either
(LocalActorBy Key, LocalActorBy KeyHashid, OutboxItemId)
FedURI
)
parseActivityURI' u@(ObjURI h lu) = do
hl <- WA.hostIsLocal h
if hl
then Left <$> parseLocalActivityURI' lu
else pure $ Right u
activityRoute :: LocalActorBy KeyHashid -> KeyHashid OutboxItem -> Route App activityRoute :: LocalActorBy KeyHashid -> KeyHashid OutboxItem -> Route App
activityRoute (LocalActorPerson p) = PersonOutboxItemR p activityRoute (LocalActorPerson p) = PersonOutboxItemR p
activityRoute (LocalActorGroup g) = GroupOutboxItemR g activityRoute (LocalActorGroup g) = GroupOutboxItemR g

View file

@ -232,9 +232,9 @@ parseGrant h (AP.Grant object context target mresult mstart mend allows deleg) =
else pure $ Right u else pure $ Right u
parseAccept (AP.Accept object mresult) = do parseAccept (AP.Accept object mresult) = do
verifyNothingE mresult "Accept must not contain 'result'" --verifyNothingE mresult "Accept must not contain 'result'"
first (\ (actor, _, item) -> (actor, item)) <$> first (\ (actor, _, item) -> (actor, item)) <$>
nameExceptT "Accept object" (parseActivityURI object) nameExceptT "Accept object" (parseActivityURI' object)
grantResourceActorID :: GrantResourceBy Identity -> ActorId grantResourceActorID :: GrantResourceBy Identity -> ActorId
grantResourceActorID (GrantResourceRepo (Identity r)) = repoActor r grantResourceActorID (GrantResourceRepo (Identity r)) = repoActor r

View file

@ -357,7 +357,8 @@ topicAcceptF
-> AP.Accept URIMode -> AP.Accept URIMode
-> ExceptT Text Handler (Text, Maybe (ExceptT Text Worker Text)) -> ExceptT Text Handler (Text, Maybe (ExceptT Text Worker Text))
topicAcceptF topicActor topicResource now recipHash author body mfwd luAccept accept = (,Nothing) <$> do topicAcceptF topicActor topicResource now recipHash author body mfwd luAccept accept = (,Nothing) <$> do
error "topicAcceptF temporarily disabled due to actor refactoring"
{-
-- Check input -- Check input
acceptee <- parseAccept accept acceptee <- parseAccept accept
@ -564,6 +565,7 @@ topicAcceptF topicActor topicResource now recipHash author body mfwd luAccept ac
} }
return (action, recipientSet, remoteActors, fwdHosts) return (action, recipientSet, remoteActors, fwdHosts)
-}
repoAcceptF repoAcceptF
:: UTCTime :: UTCTime