module TD.Data.ChatEvent
(ChatEvent(..)) where
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.MessageSender as MessageSender
import qualified TD.Data.ChatEventAction as ChatEventAction
data ChatEvent
= ChatEvent
{ ChatEvent -> Maybe Int
_id :: Maybe Int
, ChatEvent -> Maybe Int
date :: Maybe Int
, ChatEvent -> Maybe MessageSender
member_id :: Maybe MessageSender.MessageSender
, ChatEvent -> Maybe ChatEventAction
action :: Maybe ChatEventAction.ChatEventAction
}
deriving (ChatEvent -> ChatEvent -> Bool
(ChatEvent -> ChatEvent -> Bool)
-> (ChatEvent -> ChatEvent -> Bool) -> Eq ChatEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatEvent -> ChatEvent -> Bool
== :: ChatEvent -> ChatEvent -> Bool
$c/= :: ChatEvent -> ChatEvent -> Bool
/= :: ChatEvent -> ChatEvent -> Bool
Eq, Int -> ChatEvent -> ShowS
[ChatEvent] -> ShowS
ChatEvent -> String
(Int -> ChatEvent -> ShowS)
-> (ChatEvent -> String)
-> ([ChatEvent] -> ShowS)
-> Show ChatEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatEvent -> ShowS
showsPrec :: Int -> ChatEvent -> ShowS
$cshow :: ChatEvent -> String
show :: ChatEvent -> String
$cshowList :: [ChatEvent] -> ShowS
showList :: [ChatEvent] -> ShowS
Show)
instance I.ShortShow ChatEvent where
shortShow :: ChatEvent -> String
shortShow ChatEvent
{ _id :: ChatEvent -> Maybe Int
_id = Maybe Int
_id_
, date :: ChatEvent -> Maybe Int
date = Maybe Int
date_
, member_id :: ChatEvent -> Maybe MessageSender
member_id = Maybe MessageSender
member_id_
, action :: ChatEvent -> Maybe ChatEventAction
action = Maybe ChatEventAction
action_
}
= String
"ChatEvent"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
, String
"date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
, String
"member_id" String -> Maybe MessageSender -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe MessageSender
member_id_
, String
"action" String -> Maybe ChatEventAction -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ChatEventAction
action_
]
instance AT.FromJSON ChatEvent where
parseJSON :: Value -> Parser ChatEvent
parseJSON v :: Value
v@(AT.Object Object
obj) = do
String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String
case String
t of
String
"chatEvent" -> Value -> Parser ChatEvent
parseChatEvent Value
v
String
_ -> Parser ChatEvent
forall a. Monoid a => a
mempty
where
parseChatEvent :: A.Value -> AT.Parser ChatEvent
parseChatEvent :: Value -> Parser ChatEvent
parseChatEvent = String -> (Object -> Parser ChatEvent) -> Value -> Parser ChatEvent
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatEvent" ((Object -> Parser ChatEvent) -> Value -> Parser ChatEvent)
-> (Object -> Parser ChatEvent) -> Value -> Parser ChatEvent
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
_id_ <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"id"
Maybe Int
date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"date"
Maybe MessageSender
member_id_ <- Object
o Object -> Key -> Parser (Maybe MessageSender)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"member_id"
Maybe ChatEventAction
action_ <- Object
o Object -> Key -> Parser (Maybe ChatEventAction)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"action"
ChatEvent -> Parser ChatEvent
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatEvent -> Parser ChatEvent) -> ChatEvent -> Parser ChatEvent
forall a b. (a -> b) -> a -> b
$ ChatEvent
{ _id :: Maybe Int
_id = Maybe Int
_id_
, date :: Maybe Int
date = Maybe Int
date_
, member_id :: Maybe MessageSender
member_id = Maybe MessageSender
member_id_
, action :: Maybe ChatEventAction
action = Maybe ChatEventAction
action_
}
parseJSON Value
_ = Parser ChatEvent
forall a. Monoid a => a
mempty