fedi/src/Fedi/Activity.hs

118 lines
2.8 KiB
Haskell

module Fedi.Activity where
import Data.Aeson qualified as A
import Data.Text qualified as T
import Fedi.Types
import Fedi.Actor
import Data.Time (UTCTime)
data Activity
= Create
{ createId :: ActivityUrl
, actor :: ActorId
, object :: Object
}
type ActivityUrl = Url
data Object
= NoteObject Note
data Note
= Note
{ noteId :: NoteId
, notePublished :: UTCTime
, noteActor :: ActorId
, noteContent :: T.Text
, noteName :: Maybe String
, noteUrl :: Maybe Url
, noteReplies :: Collection Unordered Note
}
type NoteId = Url
type Followers = [Actor]
type Following = [Actor]
type Inbox = Collection Ordered Activity
type Outbox = Collection Unordered Activity
data Ordered
data Unordered
data Collection order a
= Collection
{ collectionSummary :: String
, collectionItems :: [a]
}
instance A.ToJSON Note where
toJSON note =
A.object $
[ "type" A..= ("Note" :: String)
, "id" A..= note.noteId
, "published" A..= note.notePublished
, "attributedTo" A..= note.noteActor
, "content" A..= note.noteContent
, "name" A..= note.noteName
, "replies" A..= note.noteReplies
]
<> [ "name" A..= name | Just name <- [note.noteName] ]
<> [ "url" A..= url | Just url <- [note.noteUrl] ]
instance A.ToJSON Object where
toJSON = \case
NoteObject note -> A.toJSON note
instance A.ToJSON Activity where
toJSON = \case
create@Create{} ->
A.object
[ "@context" A..=
[ "https://www.w3.org/ns/activitystreams" :: String
]
, "type" A..= ("Create" :: String)
, "id" A..= create.createId
, "actor" A..= create.actor
, "object" A..= create.object
]
instance A.ToJSON a => A.ToJSON (Collection Ordered a) where
toJSON collection =
A.object
[ "@context" A..=
[ "https://www.w3.org/ns/activitystreams" :: String
]
, "type" A..= ("OrderedCollection" :: String)
, "summary" A..= collection.collectionSummary
, "totalItems" A..= length collection.collectionItems
, "orderedItems" A..= collection.collectionItems
]
instance A.ToJSON a => A.ToJSON (Collection Unordered a) where
toJSON collection =
A.object
[ "@context" A..=
[ "https://www.w3.org/ns/activitystreams" :: String
]
, "type" A..= ("Collection" :: String)
, "summary" A..= collection.collectionSummary
, "totalItems" A..= length collection.collectionItems
, "items" A..= collection.collectionItems
]
{-
"@context": "https://www.w3.org/ns/activitystreams",
"summary": "Sally's notes",
"type": "OrderedCollection",
"totalItems": 2,
"orderedItems": [
{
"type": "Note",
"name": "A Simple Note"
},
{
"type": "Note",
"name": "Another Simple Note"
}
]
-}