11{-# LANGUAGE OverloadedStrings #-}
22
33-- | Matrix event data type
4- module Network.Matrix.Events
5- ( MessageTextType (.. ),
4+ module Network.Matrix.Events (
5+ MessageTextType (.. ),
66 MessageText (.. ),
77 RoomMessage (.. ),
88 Event (.. ),
99 EventID (.. ),
1010 eventType ,
11- )
11+ )
1212where
1313
1414import Control.Applicative ((<|>) )
@@ -18,140 +18,140 @@ import Data.Aeson.Types (Pair)
1818import Data.Text (Text )
1919
2020data MessageTextType
21- = TextType
22- | EmoteType
23- | NoticeType
24- deriving (Eq , Show )
21+ = TextType
22+ | EmoteType
23+ | NoticeType
24+ deriving (Eq , Show )
2525
2626instance FromJSON MessageTextType where
27- parseJSON (String name) = case name of
28- " m.text" -> pure TextType
29- " m.emote" -> pure EmoteType
30- " m.notice" -> pure NoticeType
31- _ -> mzero
32- parseJSON _ = mzero
27+ parseJSON (String name) = case name of
28+ " m.text" -> pure TextType
29+ " m.emote" -> pure EmoteType
30+ " m.notice" -> pure NoticeType
31+ _ -> mzero
32+ parseJSON _ = mzero
3333
3434instance ToJSON MessageTextType where
35- toJSON mt = String $ case mt of
36- TextType -> " m.text"
37- EmoteType -> " m.emote"
38- NoticeType -> " m.notice"
35+ toJSON mt = String $ case mt of
36+ TextType -> " m.text"
37+ EmoteType -> " m.emote"
38+ NoticeType -> " m.notice"
3939
4040data MessageText = MessageText
41- { mtBody :: Text ,
42- mtType :: MessageTextType ,
43- mtFormat :: Maybe Text ,
44- mtFormattedBody :: Maybe Text
45- }
46- deriving (Show , Eq )
41+ { mtBody :: Text
42+ , mtType :: MessageTextType
43+ , mtFormat :: Maybe Text
44+ , mtFormattedBody :: Maybe Text
45+ }
46+ deriving (Show , Eq )
4747
4848instance FromJSON MessageText where
49- parseJSON (Object v) =
50- MessageText
51- <$> v .: " body"
52- <*> v .: " msgtype"
53- <*> v .:? " format"
54- <*> v .:? " formatted_body"
55- parseJSON _ = mzero
49+ parseJSON (Object v) =
50+ MessageText
51+ <$> v .: " body"
52+ <*> v .: " msgtype"
53+ <*> v .:? " format"
54+ <*> v .:? " formatted_body"
55+ parseJSON _ = mzero
5656
5757messageTextAttr :: MessageText -> [Pair ]
5858messageTextAttr msg =
59- [" body" .= mtBody msg, " msgtype" .= mtType msg] <> format <> formattedBody
59+ [" body" .= mtBody msg, " msgtype" .= mtType msg] <> format <> formattedBody
6060 where
6161 omitNull k vM = maybe [] (\ v -> [k .= v]) vM
6262 format = omitNull " format" $ mtFormat msg
6363 formattedBody = omitNull " formatted_body" $ mtFormattedBody msg
6464
6565instance ToJSON MessageText where
66- toJSON = object . messageTextAttr
66+ toJSON = object . messageTextAttr
6767
6868newtype RoomMessage
69- = RoomMessageText MessageText
70- deriving (Show , Eq )
69+ = RoomMessageText MessageText
70+ deriving (Show , Eq )
7171
7272roomMessageAttr :: RoomMessage -> [Pair ]
7373roomMessageAttr rm = case rm of
74- RoomMessageText mt -> messageTextAttr mt
74+ RoomMessageText mt -> messageTextAttr mt
7575
7676instance ToJSON RoomMessage where
77- toJSON msg = case msg of
78- RoomMessageText mt -> toJSON mt
77+ toJSON msg = case msg of
78+ RoomMessageText mt -> toJSON mt
7979
8080instance FromJSON RoomMessage where
81- parseJSON x = RoomMessageText <$> parseJSON x
81+ parseJSON x = RoomMessageText <$> parseJSON x
8282
8383data RelatedMessage = RelatedMessage
84- { rmMessage :: RoomMessage ,
85- rmRelatedTo :: EventID
86- }
87- deriving (Show , Eq )
84+ { rmMessage :: RoomMessage
85+ , rmRelatedTo :: EventID
86+ }
87+ deriving (Show , Eq )
8888
8989data Event
90- = EventRoomMessage RoomMessage
91- | -- | A reply defined by the parent event id and the reply message
92- EventRoomReply EventID RoomMessage
93- | -- | An edit defined by the original message and the new message
94- EventRoomEdit (EventID , RoomMessage ) RoomMessage
95- | EventUnknown Object
96- deriving (Eq , Show )
90+ = EventRoomMessage RoomMessage
91+ | -- | A reply defined by the parent event id and the reply message
92+ EventRoomReply EventID RoomMessage
93+ | -- | An edit defined by the original message and the new message
94+ EventRoomEdit (EventID , RoomMessage ) RoomMessage
95+ | EventUnknown Object
96+ deriving (Eq , Show )
9797
9898instance ToJSON Event where
99- toJSON event = case event of
100- EventRoomMessage msg -> toJSON msg
101- EventRoomReply eventID msg ->
102- let replyAttr =
103- [ " m.relates_to"
104- .= object
105- [ " m.in_reply_to" .= toJSON eventID
106- ]
107- ]
108- in object $ replyAttr <> roomMessageAttr msg
109- EventRoomEdit (EventID eventID, msg) newMsg ->
110- let editAttr =
111- [ " m.relates_to"
112- .= object
113- [ " rel_type" .= (" m.replace" :: Text ),
114- " event_id" .= eventID
115- ],
116- " m.new_content" .= object (roomMessageAttr newMsg)
117- ]
118- in object $ editAttr <> roomMessageAttr msg
119- EventUnknown v -> Object v
99+ toJSON event = case event of
100+ EventRoomMessage msg -> toJSON msg
101+ EventRoomReply eventID msg ->
102+ let replyAttr =
103+ [ " m.relates_to"
104+ .= object
105+ [ " m.in_reply_to" .= toJSON eventID
106+ ]
107+ ]
108+ in object $ replyAttr <> roomMessageAttr msg
109+ EventRoomEdit (EventID eventID, msg) newMsg ->
110+ let editAttr =
111+ [ " m.relates_to"
112+ .= object
113+ [ " rel_type" .= (" m.replace" :: Text )
114+ , " event_id" .= eventID
115+ ]
116+ , " m.new_content" .= object (roomMessageAttr newMsg)
117+ ]
118+ in object $ editAttr <> roomMessageAttr msg
119+ EventUnknown v -> Object v
120120
121121instance FromJSON Event where
122- parseJSON (Object content) =
123- parseRelated <|> parseMessage <|> pure (EventUnknown content)
124- where
125- parseMessage = EventRoomMessage <$> parseJSON (Object content)
126- parseRelated = do
127- relateM <- content .: " m.relates_to"
128- case relateM of
129- Object relate -> parseReply relate <|> parseReplace relate
130- _ -> mzero
131- parseReply relate =
132- EventRoomReply <$> relate .: " m.in_reply_to" <*> parseJSON (Object content)
133- parseReplace relate = do
134- rel_type <- relate .: " rel_type"
135- if rel_type == (" m.replace" :: Text )
136- then do
137- ev <- EventID <$> relate .: " event_id"
138- msg <- parseJSON (Object content)
139- EventRoomEdit (ev, msg) <$> content .: " m.new_content"
140- else mzero
141- parseJSON _ = mzero
122+ parseJSON (Object content) =
123+ parseRelated <|> parseMessage <|> pure (EventUnknown content)
124+ where
125+ parseMessage = EventRoomMessage <$> parseJSON (Object content)
126+ parseRelated = do
127+ relateM <- content .: " m.relates_to"
128+ case relateM of
129+ Object relate -> parseReply relate <|> parseReplace relate
130+ _ -> mzero
131+ parseReply relate =
132+ EventRoomReply <$> relate .: " m.in_reply_to" <*> parseJSON (Object content)
133+ parseReplace relate = do
134+ rel_type <- relate .: " rel_type"
135+ if rel_type == (" m.replace" :: Text )
136+ then do
137+ ev <- EventID <$> relate .: " event_id"
138+ msg <- parseJSON (Object content)
139+ EventRoomEdit (ev, msg) <$> content .: " m.new_content"
140+ else mzero
141+ parseJSON _ = mzero
142142
143143eventType :: Event -> Text
144144eventType event = case event of
145- EventRoomMessage _ -> " m.room.message"
146- EventRoomReply _ _ -> " m.room.message"
147- EventRoomEdit _ _ -> " m.room.message"
148- EventUnknown _ -> error $ " Event is not implemented: " <> show event
145+ EventRoomMessage _ -> " m.room.message"
146+ EventRoomReply _ _ -> " m.room.message"
147+ EventRoomEdit _ _ -> " m.room.message"
148+ EventUnknown _ -> error $ " Event is not implemented: " <> show event
149149
150150newtype EventID = EventID { unEventID :: Text } deriving (Show , Eq , Ord )
151151
152152instance FromJSON EventID where
153- parseJSON (Object v) = EventID <$> v .: " event_id"
154- parseJSON _ = mzero
153+ parseJSON (Object v) = EventID <$> v .: " event_id"
154+ parseJSON _ = mzero
155155
156156instance ToJSON EventID where
157- toJSON (EventID v) = object [" event_id" .= v]
157+ toJSON (EventID v) = object [" event_id" .= v]
0 commit comments