leaveRoom method
This API stops a user participating in a particular room.
If the user was already in the room, they will no longer be able to see new events in the room. If the room requires an invite to join, they will need to be re-invited before they can re-join.
If the user was invited to the room, but had not joined, this call serves to reject the invite.
Servers MAY additionally forget the room when this endpoint is called –
just as if the user had also invoked /forget.
Servers that do this, MUST inform clients about this behavior using the
m.forget_forced_upon_leave
capability.
If the server doesn't automatically forget the room, the user will still be allowed to retrieve history from the room which they were previously allowed to see.
roomId The room identifier to leave.
reason Optional reason to be included as the reason on the subsequent
membership event.
Implementation
Future<void> leaveRoom(String roomId, {String? reason}) async {
final requestUri = Uri(
path: '_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/leave',
);
final request = Request('POST', baseUri!.resolveUri(requestUri));
request.headers['authorization'] = 'Bearer ${bearerToken!}';
request.headers['content-type'] = 'application/json';
request.bodyBytes = utf8.encode(
jsonEncode({if (reason != null) 'reason': reason}),
);
final response = await httpClient.send(request);
final responseBody = await response.stream.toBytes();
if (response.statusCode != 200) unexpectedResponse(response, responseBody);
final responseString = utf8.decode(responseBody);
final json = jsonDecode(responseString);
return ignore(json);
}