diff --git a/lib/src/client.dart b/lib/src/client.dart index 50ef0be..615a360 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -33,6 +33,7 @@ import 'package:famedlysdk/src/sync/user_update.dart'; import 'package:famedlysdk/src/utils/device_keys_list.dart'; import 'package:famedlysdk/src/utils/matrix_file.dart'; import 'package:famedlysdk/src/utils/open_id_credentials.dart'; +import 'package:famedlysdk/src/utils/public_rooms_response.dart'; import 'package:famedlysdk/src/utils/room_key_request.dart'; import 'package:famedlysdk/src/utils/session_key.dart'; import 'package:famedlysdk/src/utils/to_device_event.dart'; @@ -428,9 +429,12 @@ class Client { return archiveList; } - Future joinRoomById(String id) async { - return await this - .jsonRequest(type: HTTPType.POST, action: "/client/r0/join/$id"); + /// 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. + Future joinRoomById(String roomIdOrAlias) async { + return await this.jsonRequest( + type: HTTPType.POST, action: "/client/r0/join/$roomIdOrAlias"); } /// Loads the contact list for this user excluding the user itself. @@ -1870,4 +1874,34 @@ class Client { ); return; } + + /// Lists the public rooms on the server, with optional filter. + Future requestPublicRooms({ + int limit, + String since, + String genericSearchTerm, + String server, + bool includeAllNetworks, + String thirdPartyInstanceId, + }) async { + String action = "/client/r0/publicRooms"; + if (server != null) { + action += "?server=$server"; + } + final Map response = await jsonRequest( + type: HTTPType.POST, + action: action, + data: { + if (limit != null) "limit": 10, + if (since != null) "since": since, + if (genericSearchTerm != null) + "filter": {"generic_search_term": genericSearchTerm}, + if (includeAllNetworks != null) + "include_all_networks": includeAllNetworks, + if (thirdPartyInstanceId != null) + "third_party_instance_id": thirdPartyInstanceId, + }, + ); + return PublicRoomsResponse.fromJson(response, this); + } } diff --git a/lib/src/utils/public_rooms_response.dart b/lib/src/utils/public_rooms_response.dart new file mode 100644 index 0000000..b395485 --- /dev/null +++ b/lib/src/utils/public_rooms_response.dart @@ -0,0 +1,67 @@ +import '../client.dart'; + +class PublicRoomsResponse { + List publicRooms; + final String nextBatch; + final String prevBatch; + final int totalRoomCountEstimate; + Client client; + + PublicRoomsResponse({ + this.publicRooms, + this.nextBatch, + this.prevBatch, + this.totalRoomCountEstimate, + this.client, + }); + + PublicRoomsResponse.fromJson(Map json, Client client) + : nextBatch = json['next_batch'], + prevBatch = json['prev_batch'], + client = client, + totalRoomCountEstimate = json['total_room_count_estimate'] { + if (json['chunk'] != null) { + publicRooms = List(); + json['chunk'].forEach((v) { + publicRooms.add(PublicRoomEntry.fromJson(v, client)); + }); + } + } +} + +class PublicRoomEntry { + final List aliases; + final String avatarUrl; + final bool guestCanJoin; + final String name; + final int numJoinedMembers; + final String roomId; + final String topic; + final bool worldReadable; + Client client; + + Future join(Client client) => client.joinRoomById(roomId); + + PublicRoomEntry({ + this.aliases, + this.avatarUrl, + this.guestCanJoin, + this.name, + this.numJoinedMembers, + this.roomId, + this.topic, + this.worldReadable, + this.client, + }); + + PublicRoomEntry.fromJson(Map json, Client client) + : aliases = json['aliases'].cast(), + avatarUrl = json['avatar_url'], + guestCanJoin = json['guest_can_join'], + name = json['name'], + numJoinedMembers = json['num_joined_members'], + roomId = json['room_id'], + topic = json['topic'], + worldReadable = json['world_readable'], + client = client; +}