4r/4r-api/Api/Types.hs

104 lines
2.4 KiB
Haskell

{-# LANGUAGE Strict #-}
module Api.Types (
GetSections (GetSections)
, Section (..)
, GetTopics (GetTopics)
, TopicInfo (..)
, GetTopic (..)
, Topic (..)
, ThreadHead (..)
, ThreadPost (..)
, threadHeadToPost
) where
import Relude
import Data.Aeson (FromJSON (parseJSON), Value (Object), withObject, (.:))
import Data.HashMap.Strict qualified as M
import Web.FormUrlEncoded (Form (Form), ToForm (toForm))
data GetSections = GetSections
deriving stock (Eq)
instance Hashable GetSections where
hashWithSalt = const
instance ToForm GetSections where
toForm GetSections = Form $ M.fromList [("type", ["sections"]), ("key", ["anon"])]
data Section = Section
{ id :: Integer
, thread_group_name, description :: Text
}
deriving stock (Generic, Show)
instance NFData Section
instance FromJSON Section
data GetTopics = GetTopics
deriving stock (Eq)
instance Hashable GetTopics where
hashWithSalt = const
instance ToForm GetTopics where
toForm GetTopics = Form $ M.fromList [("type", ["threads"]), ("key", ["anon"])]
data TopicInfo = TopicInfo
{ id, group_id, nbr_ansfers :: Integer
, thrname, date, name, last_activity_date, last_activity_user :: Text
}
deriving stock (Generic, Show)
instance NFData TopicInfo
instance FromJSON TopicInfo
newtype GetTopic = GetTopic {topic_id :: Integer}
deriving stock (Eq)
deriving newtype (Hashable)
instance ToForm GetTopic where
toForm GetTopic{topic_id} =
Form $
M.fromList
[ ("type", ["topic"])
, ("key", ["anon"])
, ("id", [show topic_id])
]
data Topic = Topic
{ thread :: ThreadHead
, post :: [ThreadPost]
}
deriving stock (Generic, Show)
instance NFData Topic
instance FromJSON Topic
data ThreadHead = ThreadHead
{ info :: TopicInfo
, message :: Text
}
deriving stock (Generic, Show)
instance NFData ThreadHead
instance FromJSON ThreadHead where
parseJSON = withObject "ThreadHead" \o ->
ThreadHead <$> parseJSON (Object o)
<*> o .: "message"
threadHeadToPost :: ThreadHead -> ThreadPost
threadHeadToPost ThreadHead{info, message} =
ThreadPost
{ id = info.id
, topic_id = info.id
, name = info.name
, date = info.date
, message
}
data ThreadPost = ThreadPost
{ id, topic_id :: Integer
, name, date, message :: Text
}
deriving stock (Generic, Show)
instance NFData ThreadPost
instance FromJSON ThreadPost