feat: added waitForSync method

This commit is contained in:
Henri Carnot 2022-05-25 10:53:52 +00:00
parent e7910ab725
commit 2d018d3d5f
2 changed files with 52 additions and 24 deletions

View File

@ -587,8 +587,7 @@ class Client extends MatrixApi {
if (waitForSync && getRoomById(roomId) == null) { if (waitForSync && getRoomById(roomId) == null) {
// Wait for room actually appears in sync // Wait for room actually appears in sync
await onSync.stream await waitForRoomInSync(roomId, join: true);
.firstWhere((sync) => sync.rooms?.join?.containsKey(roomId) ?? false);
} }
await Room(id: roomId, client: this).addToDirectChat(mxid); await Room(id: roomId, client: this).addToDirectChat(mxid);
@ -632,13 +631,29 @@ class Client extends MatrixApi {
if (waitForSync) { if (waitForSync) {
if (getRoomById(roomId) == null) { if (getRoomById(roomId) == null) {
// Wait for room actually appears in sync // Wait for room actually appears in sync
await onSync.stream.firstWhere( await waitForRoomInSync(roomId, join: true);
(sync) => sync.rooms?.join?.containsKey(roomId) ?? false);
} }
} }
return roomId; return roomId;
} }
/// Wait for the room to appear into the enabled section of the room sync.
/// By default, the function will listen for room in invite, join and leave
/// sections of the sync.
Future<SyncUpdate> waitForRoomInSync(String roomId,
{bool join = false, bool invite = false, bool leave = false}) async {
if (!join && !invite && !leave) {
join = true;
invite = true;
leave = true;
}
return await onSync.stream.firstWhere((sync) =>
invite && (sync.rooms?.invite?.containsKey(roomId) ?? false) ||
join && (sync.rooms?.join?.containsKey(roomId) ?? false) ||
leave && (sync.rooms?.leave?.containsKey(roomId) ?? false));
}
/// Checks if the given user has encryption keys. May query keys from the /// Checks if the given user has encryption keys. May query keys from the
/// server to answer this. /// server to answer this.
Future<bool> userOwnsEncryptionKeys(String userId) async { Future<bool> userOwnsEncryptionKeys(String userId) async {
@ -657,26 +672,33 @@ class Client extends MatrixApi {
/// room as a space with `room.toSpace()`. /// room as a space with `room.toSpace()`.
/// ///
/// https://github.com/matrix-org/matrix-doc/blob/matthew/msc1772/proposals/1772-groups-as-rooms.md /// https://github.com/matrix-org/matrix-doc/blob/matthew/msc1772/proposals/1772-groups-as-rooms.md
Future<String> createSpace({ Future<String> createSpace(
String? name, {String? name,
String? topic, String? topic,
Visibility visibility = Visibility.public, Visibility visibility = Visibility.public,
String? spaceAliasName, String? spaceAliasName,
List<String>? invite, List<String>? invite,
List<Invite3pid>? invite3pid, List<Invite3pid>? invite3pid,
String? roomVersion, String? roomVersion,
}) => bool waitForSync = false}) async {
createRoom( final id = await createRoom(
name: name, name: name,
topic: topic, topic: topic,
visibility: visibility, visibility: visibility,
roomAliasName: spaceAliasName, roomAliasName: spaceAliasName,
creationContent: {'type': 'm.space'}, creationContent: {'type': 'm.space'},
powerLevelContentOverride: {'events_default': 100}, powerLevelContentOverride: {'events_default': 100},
invite: invite, invite: invite,
invite3pid: invite3pid, invite3pid: invite3pid,
roomVersion: roomVersion, roomVersion: roomVersion,
); );
if (waitForSync) {
await waitForRoomInSync(id, join: true);
}
return id;
}
/// Returns the user's own displayname and avatar url. In Matrix it is possible that /// Returns the user's own displayname and avatar url. In Matrix it is possible that
/// one user can have different displaynames and avatar urls in different rooms. So /// one user can have different displaynames and avatar urls in different rooms. So

View File

@ -519,6 +519,12 @@ class Room {
/// in muted rooms, use [hasNewMessages]. /// in muted rooms, use [hasNewMessages].
bool get isUnread => notificationCount > 0 || markedUnread; bool get isUnread => notificationCount > 0 || markedUnread;
/// Wait for the room to appear in join, leave or invited section of the
/// sync.
Future<SyncUpdate> get waitForSync async {
return await client.waitForRoomInSync(id);
}
/// Sets an unread flag manually for this room. This changes the local account /// Sets an unread flag manually for this room. This changes the local account
/// data model before syncing it to make sure /// data model before syncing it to make sure
/// this works if there is no connection to the homeserver. This does **not** /// this works if there is no connection to the homeserver. This does **not**