fedi/src/Fedi/Routes/Follow.hs
2024-12-17 10:47:00 +02:00

85 lines
2.6 KiB
Haskell

module Fedi.Routes.Follow where
import Data.Aeson qualified as A
import Fedi.Helpers
import Fedi.Routes.Helpers
import Fedi.Types
import Fedi.Types.Helpers
import Fedi.UserDetails
import Web.Twain qualified as Twain
import Web.Twain.Types qualified as Twain
-- * Followers
matchFollowers :: UserDetails -> Twain.PathPattern
matchFollowers details =
fromString ("/" <> details.username <> "/followers")
handleFollowers :: UserDetails -> [Url] -> Twain.ResponderM b
handleFollowers details items = do
isPage <- Twain.queryParamMaybe "page"
let
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
matchFollowing :: UserDetails -> Twain.PathPattern
matchFollowing details =
fromString ("/" <> details.username <> "/following")
handleFollowing :: UserDetails -> Twain.ResponderM b
handleFollowing details = do
let
collection :: Collection ()
collection =
emptyUnorderedCollection
{ id = Just $ ObjectId $ actorUrl details <> "/following"
, summary = Just $ fromString $ details.username <> " is following"
}
Twain.send $ jsonLD (A.encode collection)