kick method
Kick a user from the room.
The caller must have the required power level in order to perform this operation.
Kicking a user adjusts the target member's membership state to be leave
with an
optional reason
. Like with other membership changes, a user can directly adjust
the target member's state by making a request to /rooms/<room id>/state/m.room.member/<user id>
.
roomId
The room identifier (not alias) from which the user should be kicked.
reason
The reason the user has been kicked. 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 kicked.
Implementation
Future<void> kick(String roomId, String userId, {String? reason}) async {
final requestUri = Uri(
path: '_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/kick',
);
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);
}