ban method

Future<void> ban(
  1. String roomId,
  2. String userId, {
  3. String? reason,
})
inherited

Ban a user in the room. If the user is currently in the room, also kick them.

When a user is banned from a room, they may not join it or be invited to it until they are unbanned.

The caller must have the required power level in order to perform this operation.

roomId The room identifier (not alias) from which the user should be banned.

reason The reason the user has been banned. This will be supplied as the reason on the target's updated m.room.member event.

userId The fully qualified user ID of the user being banned.

Implementation

Future<void> ban(String roomId, String userId, {String? reason}) async {
  final requestUri =
      Uri(path: '_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/ban');
  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,
    'user_id': userId,
  }));
  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);
}