Merge branch 'client-feature-list-public-rooms' into 'master'

[Client] List public rooms

See merge request famedly/famedlysdk!219
This commit is contained in:
Christian Pauly 2020-02-22 17:26:20 +00:00
commit 079f8006de
2 changed files with 104 additions and 3 deletions

View file

@ -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<dynamic> 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<dynamic> 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<PublicRoomsResponse> 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<String, dynamic> 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);
}
}

View file

@ -0,0 +1,67 @@
import '../client.dart';
class PublicRoomsResponse {
List<PublicRoomEntry> 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<String, dynamic> 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<PublicRoomEntry>();
json['chunk'].forEach((v) {
publicRooms.add(PublicRoomEntry.fromJson(v, client));
});
}
}
}
class PublicRoomEntry {
final List<String> 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<void> 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<String, dynamic> json, Client client)
: aliases = json['aliases'].cast<String>(),
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;
}