From 934c69daae0f9c633d8bb41e0620e3332377c572 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Wed, 26 Oct 2022 10:47:38 +0000 Subject: [PATCH] UI, S2S: Re-implement and re-enable Push activity - When pushing to a repo, a Push activity is now automatically published - The 'actor' is now the repo, and 'attributedTo' specifies the person who pushed - No need for 'context' in the Push anymore, since it's always the 'actor' - 'target' now specifies the branch as a Branch object rather than URI (since Vervis doesn't keep AS2 objects for branches anymore) - I deleted 'pushCommitsC' (from Vervis.API) because the code for preparing and pushing an activity is so simple with the new delivery API, doesn't need a dedicated pushCommitsC function - The generated Push activity does generate an HTML summary, unlike all other generated activities (in which I removed the summary generating code); I'm still unsure whether to bring back those summaries (extra code to write, for a problematic feature that may become useless when the new UI comes) --- src/Vervis/API.hs | 90 --------------- src/Vervis/Handler/Repo.hs | 218 +++++++++++++++++++++++-------------- src/Web/ActivityPub.hs | 27 +++-- 3 files changed, 156 insertions(+), 179 deletions(-) diff --git a/src/Vervis/API.hs b/src/Vervis/API.hs index 50321c9..523d061 100644 --- a/src/Vervis/API.hs +++ b/src/Vervis/API.hs @@ -31,7 +31,6 @@ module Vervis.API --, offerDepC , resolveC , undoC - --, pushCommitsC ) where @@ -3067,92 +3066,3 @@ undoC (Entity senderPersonID senderPerson) senderActor maybeCap localRecips remo , AP.acceptResult = Nothing } } - -pushCommitsC - :: Entity Person - -> Html - -> Push URIMode - -> ShrIdent - -> RpIdent - -> ExceptT Text Handler OutboxItemId -pushCommitsC eperson summary push shrRepo rpRepo = do - error "pushCommitsC temporarily disabled" - -{- - - - let dont = Authority "dont-do.any-forwarding" Nothing - (obiid, doc, remotesHttp) <- runDBExcept $ do - (obiid, doc) <- lift $ insertToOutbox - remoteRecips <- lift $ deliverLocal obiid - federation <- getsYesod $ appFederation . appSettings - unless (federation || null remoteRecips) $ - throwE "Federation disabled but remote collection members found" - remotesHttp <- lift $ deliverRemoteDB' dont obiid [] remoteRecips - return (obiid, doc, remotesHttp) - lift $ forkWorker "pushCommitsC: async HTTP delivery" $ deliverRemoteHttp dont obiid doc remotesHttp - return obiid - where - insertToOutbox :: AppDB (OutboxItemId, Doc Activity URIMode) - insertToOutbox = do - host <- getsYesod siteInstanceHost - encodeRouteLocal <- getEncodeRouteLocal - encodeRouteHome <- getEncodeRouteHome - let shrUser = sharerIdent sharer - aud = map encodeRouteHome - [ SharerFollowersR shrUser - , RepoR shrRepo rpRepo - , RepoTeamR shrRepo rpRepo - , RepoFollowersR shrRepo rpRepo - ] - activity mluAct = Doc host Activity - { activityId = mluAct - , activityActor = encodeRouteLocal $ SharerR shrUser - , activityCapability = Nothing - , activitySummary = - Just $ renderHTML summary - , activityAudience = Audience aud [] [] [] [] [] - , activitySpecific = PushActivity push - } - now <- liftIO getCurrentTime - obiid <- insert OutboxItem - { outboxItemOutbox = personOutbox $ entityVal eperson - , outboxItemActivity = persistJSONObjectFromDoc $ activity Nothing - , outboxItemPublished = now - } - obikhid <- encodeKeyHashid obiid - let luAct = encodeRouteLocal $ SharerOutboxItemR shrUser obikhid - doc = activity $ Just luAct - update obiid [OutboxItemActivity =. persistJSONObjectFromDoc doc] - return (obiid, doc) - - deliverLocal - :: OutboxItemId - -> AppDB - [ ( (InstanceId, Host) - , NonEmpty RemoteRecipient - ) - ] - deliverLocal obiid = do - let pidAuthor = entityKey eperson - (sidRepo, repo) <- do - sid <- getKeyBy404 $ UniqueSharer shrRepo - r <- getValBy404 $ UniqueRepo rpRepo sid - return (sid, r) - (pids, remotes) <- do - (repoPids, repoRemotes) <- getRepoTeam sidRepo - (pfsPids, pfsRemotes) <- - getFollowers $ personFollowers $ entityVal eperson - (rfsPids, rfsRemotes) <- getFollowers $ repoFollowers repo - return - ( L.delete pidAuthor $ union repoPids $ union pfsPids rfsPids - , repoRemotes `unionRemotes` pfsRemotes `unionRemotes` rfsRemotes - ) - ibiid <- insert $ InboxItem False now - insert_ $ InboxItemLocal (repoInbox repo) obiid ibiid - for_ pids $ \ pid -> do - ibid <- personInbox <$> getJust pid - ibiid <- insert $ InboxItem True now - insert_ $ InboxItemLocal ibid obiid ibiid - return remotes --} diff --git a/src/Vervis/Handler/Repo.hs b/src/Vervis/Handler/Repo.hs index 8891cbb..19a65d0 100644 --- a/src/Vervis/Handler/Repo.hs +++ b/src/Vervis/Handler/Repo.hs @@ -129,6 +129,7 @@ import qualified Data.ByteString.Lazy.Internal as BLI import qualified Data.CaseInsensitive as CI (foldedCase) import qualified Data.DList as D import qualified Data.Set as S (member) +import qualified Data.Text.Encoding as TE import qualified Data.Text as T import qualified Data.Text.Lazy.Encoding as L (decodeUtf8With) import qualified Database.Esqueleto as E @@ -137,6 +138,7 @@ import Data.MediaType import Database.Persist.JSON import Development.PatchMediaType import Network.FedURI +import Web.Text import Yesod.ActivityPub import Yesod.FedURI import Yesod.Hashids @@ -150,6 +152,7 @@ import Data.Either.Local import Data.Git.Local import Database.Persist.Local import Text.FilePath.Local (breakExt) +import Web.Hashids.Local import Yesod.Form.Local import Yesod.Persist.Local @@ -157,6 +160,7 @@ import qualified Data.Git.Local as G (createRepo) import qualified Darcs.Local.Repository as D (createRepo) import Vervis.Access +import Vervis.ActivityPub import Vervis.API import Vervis.Federation.Auth import Vervis.Federation.Collab @@ -168,6 +172,7 @@ import Vervis.Path import Vervis.Model import Vervis.Model.Ident import Vervis.Paginate +import Vervis.Persist.Actor import Vervis.Readme import Vervis.Recipient import Vervis.Settings @@ -175,6 +180,7 @@ import Vervis.SourceTree import Vervis.Style import Vervis.Web.Actor import Vervis.Web.Darcs +import Vervis.Web.Delivery import Vervis.Web.Git import qualified Vervis.Client as C @@ -538,111 +544,165 @@ postRepoUnfollowR :: KeyHashid Repo -> Handler () postRepoUnfollowR _ = error "Temporarily disabled" postPostReceiveR :: Handler Text -postPostReceiveR = return "Temporarily disabled, no Push activity published" - {- +postPostReceiveR = do + -- Parse the push object that the hook sent push <- requireCheckJsonBody - (pushAP, shr, rp) <- push2ap push - user <- runDB $ do - p <- getJustEntity $ toSqlKey $ H.pushUser push - s <- getJust $ personIdent $ entityVal p - return (p, s) - let shrUser = sharerIdent $ snd user - summary <- do - let mbranch = H.pushBranch push - total = pushCommitsTotal pushAP - lasts = pushCommitsLast pushAP - rest firsts = total - length firsts - length lasts - hashText (Hash b) = decodeUtf8 b - commitW c = - [hamlet| - - #{commitTitle c} - |] - withUrlRenderer - [hamlet| -

- #{shr2text shrUser} - \ pushed #{total} # - \ #{commitsText mbranch total} to repo # - #{rp2text rp}^{branchText shr rp mbranch}: -