joinRoomById method

Future<String> joinRoomById(
  1. String roomId, {
  2. String? reason,
  3. ThirdPartySigned? thirdPartySigned,
})

Note that this API requires a room ID, not alias. /join/{roomIdOrAlias} exists if you have a room alias.

This API starts a user participating in a particular room, if that user is allowed to participate in that room. After this call, the client is allowed to see all current state events in the room, and all subsequent events associated with the room until the user leaves the room.

After a user has joined a room, the room will appear as an entry in the response of the /initialSync and /sync APIs.

roomId The room identifier (not alias) to join.

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

thirdPartySigned If supplied, the homeserver must verify that it matches a pending m.room.third_party_invite event in the room, and perform key validity checking if required by the event.

returns room_id: The joined room ID.

Implementation

Future<String> joinRoomById(String roomId,
    {String? reason, ThirdPartySigned? thirdPartySigned}) async {
  final requestUri = Uri(
      path: '_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/join');
  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,
    if (thirdPartySigned != null)
      'third_party_signed': thirdPartySigned.toJson(),
  }));
  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 json['room_id'] as String;
}