joinRoom method
Note that this API takes either a room ID or alias, unlike /rooms/{roomId}/join
.
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.
roomIdOrAlias
The room identifier or alias to join.
serverName
The servers to attempt to join the room through. One of the servers
must be participating in the room.
via
The servers to attempt to join the room through. One of the servers
must be participating in the room.
reason
Optional reason to be included as the reason
on the subsequent
membership event.
thirdPartySigned
If a third_party_signed
was 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> joinRoom(
String roomIdOrAlias, {
List<String>? serverName,
List<String>? via,
String? reason,
ThirdPartySigned? thirdPartySigned,
}) async {
final requestUri = Uri(
path: '_matrix/client/v3/join/${Uri.encodeComponent(roomIdOrAlias)}',
queryParameters: {
if (serverName != null) 'server_name': serverName,
if (via != null) 'via': via,
},
);
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;
}