From 0db710272a7616246b804b147a2c169be36d2c27 Mon Sep 17 00:00:00 2001 From: me Date: Tue, 5 Nov 2024 16:16:40 +0200 Subject: [PATCH] return followers collection --- app/Routes.hs | 4 ++- src/Fedi/Routes/Follow.hs | 59 +++++++++++++++++++++++++++++++++------ src/Fedi/Routes/Outbox.hs | 1 + src/Fedi/Types.hs | 3 ++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/app/Routes.hs b/app/Routes.hs index 9315b38..6c4208d 100644 --- a/app/Routes.hs +++ b/app/Routes.hs @@ -80,7 +80,9 @@ routes db detailsFile = , -- Followers Twain.get (Fedi.matchFollowers $ unsafePerformIO $ fetchUserDetails detailsFile) do details <- liftIO $ fetchUserDetails detailsFile - Fedi.handleFollowers details + followers <- liftIO db.getFollowers + <&> map (\follower -> T.unpack follower.actorId) + Fedi.handleFollowers details followers , -- Following Twain.get (Fedi.matchFollowing $ unsafePerformIO $ fetchUserDetails detailsFile) do details <- liftIO $ fetchUserDetails detailsFile diff --git a/src/Fedi/Routes/Follow.hs b/src/Fedi/Routes/Follow.hs index 7ef2311..4144875 100644 --- a/src/Fedi/Routes/Follow.hs +++ b/src/Fedi/Routes/Follow.hs @@ -15,16 +15,57 @@ matchFollowers :: UserDetails -> Twain.PathPattern matchFollowers details = fromString ("/" <> details.username <> "/followers") -handleFollowers :: UserDetails -> Twain.ResponderM b -handleFollowers details = do +handleFollowers :: UserDetails -> [Url] -> Twain.ResponderM b +handleFollowers details items = do + isPage <- Twain.queryParamMaybe "page" let - collection :: Collection () - collection = - emptyUnorderedCollection - { id = Just $ ObjectId $ actorUrl details <> "/followers" - , summary = Just $ fromString $ details.username <> "'s followers" - } - Twain.send $ jsonLD (A.encode collection) + followersUrl = + "https://" + <> details.domain + <> "/" + <> details.username + <> "/followers" + response = + case isPage of + Just True -> + let + empty = emptyOrderedCollectionPage followersUrl + content :: FollowersPage + content = + empty + { id = Just $ ObjectId $ followersUrl <> "?page=true" + , summary = Just $ fromString $ details.username <> "'s followers" + , otype = + empty.otype + { ctype = + empty.otype.ctype + { partOf = followersUrl + , porderedItems = items + } + } + } + in + A.encode content + _ -> + let + content :: Followers + content = + emptyOrderedCollection + { id = Just $ ObjectId followersUrl + , summary = Just $ fromString $ details.username <> "'s followers" + , otype = + emptyOrderedCollection.otype + { ctype = + emptyOrderedCollection.otype.ctype + { orderedItems = items + } + , first = Just $ followersUrl <> "?page=true" + , last = Just $ followersUrl <> "?page=true" + } + } + in + A.encode content + Twain.send $ jsonLD response -- * Following diff --git a/src/Fedi/Routes/Outbox.hs b/src/Fedi/Routes/Outbox.hs index c597af9..e2ff05d 100644 --- a/src/Fedi/Routes/Outbox.hs +++ b/src/Fedi/Routes/Outbox.hs @@ -34,6 +34,7 @@ handleOutbox details items = do content = empty { id = Just $ ObjectId $ outboxUrl <> "?page=true" + , summary = Just $ fromString $ details.username <> "'s notes" , otype = empty.otype { ctype = diff --git a/src/Fedi/Types.hs b/src/Fedi/Types.hs index 6b216bc..12d8d27 100644 --- a/src/Fedi/Types.hs +++ b/src/Fedi/Types.hs @@ -535,6 +535,9 @@ type Outbox = OrderedCollection AnyActivity type OutboxPage = OrderedCollectionPage AnyActivity +type Followers = OrderedCollection Url +type FollowersPage = OrderedCollectionPage Url + data CollectionType t = CollectionType { ctype :: t