Skip to content

Commit c638ade

Browse files
schoettlTristanCacqueray
authored andcommitted
Refactor: Unify hadling of {}/()
1 parent fc998a9 commit c638ade

File tree

1 file changed

+14
-45
lines changed

1 file changed

+14
-45
lines changed

matrix-client/src/Network/Matrix/Client.hs

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ mkLogoutRequest ClientSession {..} = mkLogoutRequest' baseUrl token
200200

201201
-- | 'logout' allows you to destroy a session token.
202202
logout :: ClientSession -> MatrixIO ()
203-
logout session@ClientSession {..} = do
203+
logout session = do
204204
req <- mkLogoutRequest session
205-
fmap (() <$) $ doRequest' @Value manager req
205+
doRequestExpectEmptyResponse session "logout" req
206206

207207
-- | The session record, use 'createSession' to create it.
208208
data ClientSession = ClientSession
@@ -571,17 +571,17 @@ resolveRoomAlias session r@(RoomAlias alias) = do
571571
setRoomAlias :: ClientSession -> RoomAlias -> RoomID -> MatrixIO ()
572572
setRoomAlias session (RoomAlias alias) (RoomID roomId)= do
573573
request <- mkRequest session True $ "/_matrix/client/v3/directory/room/" <> escapeUriComponent alias
574-
doRequest
575-
session $
574+
doRequestExpectEmptyResponse session "set room alias" $
576575
request { HTTP.method = "PUT"
577576
, HTTP.requestBody = HTTP.RequestBodyLBS $ encode $ object [("room_id" .= roomId)]
578577
}
578+
579579
-- | Delete a mapping of room alias to room ID.
580580
-- https://spec.matrix.org/v1.1/client-server-api/#delete_matrixclientv3directoryroomroomalias
581581
deleteRoomAlias :: ClientSession -> RoomAlias -> MatrixIO ()
582582
deleteRoomAlias session (RoomAlias alias) = do
583583
request <- mkRequest session True $ "/_matrix/client/v3/directory/room/" <> escapeUriComponent alias
584-
doRequest session $ request { HTTP.method = "DELETE" }
584+
doRequestExpectEmptyResponse session "delete room alias" $ request { HTTP.method = "DELETE" }
585585

586586
data ResolvedAliases = ResolvedAliases [RoomAlias]
587587

@@ -674,57 +674,36 @@ knockOnRoom session room servers reason = do
674674
forgetRoom :: ClientSession -> RoomID -> MatrixIO ()
675675
forgetRoom session (RoomID roomId) = do
676676
request <- mkRequest session True $ "/_matrix/client/v3/rooms/" <> roomId <> "/forget"
677-
fmap ensureEmptyObject <$> doRequest session (request {HTTP.method = "POST"})
678-
where
679-
ensureEmptyObject :: Value -> ()
680-
ensureEmptyObject value = case value of
681-
Object xs | xs == mempty -> ()
682-
_anyOther -> error $ "Unknown forget response: " <> show value
683-
677+
doRequestExpectEmptyResponse session "forget" (request {HTTP.method = "POST"})
684678

685679
-- | Stop participating in a particular room.
686680
-- https://spec.matrix.org/v1.1/client-server-api/#post_matrixclientv3roomsroomidleave
687681
leaveRoomById :: ClientSession -> RoomID -> MatrixIO ()
688682
leaveRoomById session (RoomID roomId) = do
689683
request <- mkRequest session True $ "/_matrix/client/r0/rooms/" <> roomId <> "/leave"
690-
fmap ensureEmptyObject <$> doRequest session (request {HTTP.method = "POST"})
691-
where
692-
ensureEmptyObject :: Value -> ()
693-
ensureEmptyObject value = case value of
694-
Object xs | xs == mempty -> ()
695-
_anyOther -> error $ "Unknown leave response: " <> show value
684+
doRequestExpectEmptyResponse session "leave" (request {HTTP.method = "POST"})
696685

697686
-- | Kick a user from the room.
698687
-- https://spec.matrix.org/v1.1/client-server-api/#post_matrixclientv3roomsroomidkick
699688
kickUser :: ClientSession -> RoomID -> UserID -> Maybe T.Text -> MatrixIO ()
700689
kickUser session (RoomID roomId) (UserID uid) reason = do
701690
request <- mkRequest session True $ "/_matrix/client/v3/rooms/" <> roomId <> "/kick"
702691
let body = object $ [("user_id", toJSON uid)] <> catMaybes [fmap (("reason",) . toJSON) reason]
703-
fmap (fmap ensureEmptyObject) $ doRequest session $
692+
doRequestExpectEmptyResponse session "kick" $
704693
request { HTTP.method = "POST"
705694
, HTTP.requestBody = HTTP.RequestBodyLBS $ encode body
706695
}
707-
where
708-
ensureEmptyObject :: Value -> ()
709-
ensureEmptyObject value = case value of
710-
Object xs | xs == mempty -> ()
711-
_anyOther -> error $ "Unknown leave response: " <> show value
712696

713697
-- | Ban a user in the room. If the user is currently in the room, also kick them.
714698
-- https://spec.matrix.org/v1.1/client-server-api/#post_matrixclientv3roomsroomidban
715699
banUser :: ClientSession -> RoomID -> UserID -> Maybe T.Text -> MatrixIO ()
716700
banUser session (RoomID roomId) (UserID uid) reason = do
717701
request <- mkRequest session True $ "/_matrix/client/v3/rooms/" <> roomId <> "/ban"
718702
let body = object $ [("user_id", toJSON uid)] <> catMaybes [fmap (("reason",) . toJSON) reason]
719-
fmap (fmap ensureEmptyObject) $ doRequest session $
703+
doRequestExpectEmptyResponse session "ban" $
720704
request { HTTP.method = "POST"
721705
, HTTP.requestBody = HTTP.RequestBodyLBS $ encode body
722706
}
723-
where
724-
ensureEmptyObject :: Value -> ()
725-
ensureEmptyObject value = case value of
726-
Object xs | xs == mempty -> ()
727-
_anyOther -> error $ "Unknown leave response: " <> show value
728707

729708
-- | Unban a user from the room. This allows them to be invited to the
730709
-- room, and join if they would otherwise be allowed to join according
@@ -734,15 +713,10 @@ unbanUser :: ClientSession -> RoomID -> UserID -> Maybe T.Text -> MatrixIO ()
734713
unbanUser session (RoomID roomId) (UserID uid) reason = do
735714
request <- mkRequest session True $ "/_matrix/client/v3/rooms/" <> roomId <> "/unban"
736715
let body = object $ [("user_id", toJSON uid)] <> catMaybes [fmap (("reason",) . toJSON) reason]
737-
fmap (fmap ensureEmptyObject) $ doRequest session $
716+
doRequestExpectEmptyResponse session "unban" $
738717
request { HTTP.method = "POST"
739718
, HTTP.requestBody = HTTP.RequestBodyLBS $ encode body
740719
}
741-
where
742-
ensureEmptyObject :: Value -> ()
743-
ensureEmptyObject value = case value of
744-
Object xs | xs == mempty -> ()
745-
_anyOther -> error $ "Unknown leave response: " <> show value
746720

747721
data Visibility = Public | Private
748722
deriving (Show)
@@ -778,15 +752,10 @@ setRoomVisibility :: ClientSession -> RoomID -> Visibility -> MatrixIO ()
778752
setRoomVisibility session (RoomID rid) visibility = do
779753
request <- mkRequest session True $ "/_matrix/client/v3/directory/list/room/" <> rid
780754
let body = object $ [("visibility", toJSON visibility)]
781-
fmap (fmap ensureEmptyObject) $ doRequest session $
755+
doRequestExpectEmptyResponse session "set room visibility" $
782756
request { HTTP.method = "PUT"
783757
, HTTP.requestBody = HTTP.RequestBodyLBS $ encode body
784758
}
785-
where
786-
ensureEmptyObject :: Value -> ()
787-
ensureEmptyObject value = case value of
788-
Object xs | xs == mempty -> ()
789-
_anyOther -> error $ "Unknown setRoomVisibility response: " <> show value
790759

791760
-- | A pagination token from a previous request, allowing clients to
792761
-- get the next (or previous) batch of rooms. The direction of
@@ -1313,11 +1282,10 @@ getAccountData' session userID t =
13131282
setAccountData' :: (ToJSON a) => ClientSession -> UserID -> T.Text -> a -> MatrixIO ()
13141283
setAccountData' session userID t value = do
13151284
request <- mkRequest session True $ accountDataPath userID t
1316-
void <$> (doRequest session $ request
1285+
doRequestExpectEmptyResponse session "set account data" $ request
13171286
{ HTTP.method = "PUT"
13181287
, HTTP.requestBody = HTTP.RequestBodyLBS $ encode value
1319-
} :: MatrixIO Aeson.Object
1320-
)
1288+
}
13211289

13221290
accountDataPath :: UserID -> T.Text -> T.Text
13231291
accountDataPath (UserID userID) t =
@@ -1356,6 +1324,7 @@ tshow = T.pack . show
13561324
escapeUriComponent :: T.Text -> T.Text
13571325
escapeUriComponent = T.pack . URI.escapeURIString URI.isUnreserved . T.unpack
13581326

1327+
13591328
ensureEmptyObject :: String -> Value -> ()
13601329
ensureEmptyObject apiName value = case value of
13611330
Object xs | xs == mempty -> ()

0 commit comments

Comments
 (0)