leaveRoom method

Future<void> leaveRoom(
  1. String roomId, {
  2. String? reason,
})

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.

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);
}