inviteUser method

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

Note that there are two forms of this API, which are documented separately. This version of the API requires that the inviter knows the SDN identifier of the invitee. The other is documented in the third party invites section.

This API invites a user to participate in a particular room. They do not start participating in the room until they actually join the room.

Only users currently in a particular room can invite other users to join that room.

If the user was invited to the room, the node will append a m.room.member event to the room.

roomId The room identifier (not alias) to which to invite the user.

reason Optional reason to be included as the reason on the subsequent membership event.

userId The fully qualified user ID of the invitee.

Implementation

Future<void> inviteUser(String roomId, String userId,
    {String? reason}) async {
  print("roomId=$roomId, userId=$userId");
  final requestUri =
      Uri(path: '_api/client/v3/rooms/${Uri.encodeComponent(roomId)}/invite');
  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();
  final responseString = utf8.decode(responseBody);
  print("responseString$responseString,CODE= ${response.statusCode}");
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final json = jsonDecode(responseString);
  return ignore(json);
}