diff --git a/lib/src/Client.dart b/lib/src/Client.dart index 8b39bae..b87a4aa 100644 --- a/lib/src/Client.dart +++ b/lib/src/Client.dart @@ -97,6 +97,9 @@ class Client { /// A list of all rooms the user is participating or invited. RoomList roomList; + /// A list of all rooms the user is not participating anymore. + RoomList archive; + /// Key/Value store of account data. Map accountData = {}; @@ -251,27 +254,29 @@ class Client { await connection.clear(); } - /// Loads the Rooms from the [store] and creates a new [RoomList] object. - Future getRoomList( + /// Creates a new [RoomList] object. + RoomList getRoomList( {bool onlyLeft = false, - bool onlyDirect = false, - bool onlyGroups = false, onRoomListUpdateCallback onUpdate, onRoomListInsertCallback onInsert, - onRoomListRemoveCallback onRemove}) async { - List rooms = await store.getRoomList( - onlyLeft: onlyLeft, onlyGroups: onlyGroups, onlyDirect: onlyDirect); + onRoomListRemoveCallback onRemove}) { + List rooms = onlyLeft ? archive.rooms : roomList.rooms; return RoomList( client: this, onlyLeft: onlyLeft, - onlyDirect: onlyDirect, - onlyGroups: onlyGroups, onUpdate: onUpdate, onInsert: onInsert, onRemove: onRemove, rooms: rooms); } + /// Searches in the roomList and in the archive for a room with the given [id]. + Room getRoomById(String id) { + Room room = roomList.getRoomById(id); + if (room == null) room = archive.getRoomById(id); + return room; + } + Future joinRoomById(String id) async { return await connection.jsonRequest( type: HTTPType.POST, action: "/client/r0/join/$id"); diff --git a/lib/src/Connection.dart b/lib/src/Connection.dart index 6e632df..c1fa08c 100644 --- a/lib/src/Connection.dart +++ b/lib/src/Connection.dart @@ -152,10 +152,11 @@ class Connection { client.prevBatch = newPrevBatch; List rooms = []; + List archivedRooms = []; if (client.store != null) { client.store.storeClient(); - rooms = await client.store - .getRoomList(onlyLeft: false, onlyGroups: false, onlyDirect: false); + rooms = await client.store.getRoomList(onlyLeft: false); + archivedRooms = await client.store.getRoomList(onlyLeft: true); client.accountData = await client.store.getAccountData(); client.presences = await client.store.getPresences(); } @@ -163,13 +164,19 @@ class Connection { client.roomList = RoomList( client: client, onlyLeft: false, - onlyDirect: false, - onlyGroups: false, onUpdate: null, onInsert: null, onRemove: null, rooms: rooms); + client.archive = RoomList( + client: client, + onlyLeft: true, + onUpdate: null, + onInsert: null, + onRemove: null, + rooms: archivedRooms); + _userEventSub ??= onUserEvent.stream.listen(client.handleUserUpdate); onLoginStateChanged.add(LoginState.logged); diff --git a/lib/src/RoomList.dart b/lib/src/RoomList.dart index 58d5de4..19be2fe 100644 --- a/lib/src/RoomList.dart +++ b/lib/src/RoomList.dart @@ -45,8 +45,6 @@ class RoomList { List rooms = []; final bool onlyLeft; - final bool onlyDirect; - final bool onlyGroups; /// Will be called, when the room list has changed. Can be used e.g. to update /// the state of a StatefulWidget. @@ -67,9 +65,7 @@ class RoomList { this.onUpdate, this.onInsert, this.onRemove, - this.onlyLeft = false, - this.onlyDirect = false, - this.onlyGroups = false}) { + this.onlyLeft = false}) { eventSub ??= client.connection.onEvent.stream.listen(_handleEventUpdate); roomSub ??= client.connection.onRoomUpdate.stream.listen(_handleRoomUpdate); sort(); @@ -125,13 +121,16 @@ class RoomList { rooms.removeAt(j); if (onRemove != null) onRemove(j); } - // Update notification and highlight count + // Update notification, highlight count and/or additional informations else if (found && chatUpdate.membership != Membership.leave && (rooms[j].notificationCount != chatUpdate.notification_count || - rooms[j].highlightCount != chatUpdate.highlight_count)) { + rooms[j].highlightCount != chatUpdate.highlight_count || + chatUpdate.summary != null)) { rooms[j].notificationCount = chatUpdate.notification_count; rooms[j].highlightCount = chatUpdate.highlight_count; + if (chatUpdate.prev_batch != null) + rooms[j].prev_batch = chatUpdate.prev_batch; if (chatUpdate.summary != null) { if (chatUpdate.summary.mHeroes != null) rooms[j].mHeroes = chatUpdate.summary.mHeroes; diff --git a/lib/src/Store.dart b/lib/src/Store.dart index c764472..817d95e 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -357,11 +357,7 @@ class Store { } /// Returns all rooms, the client is participating. Excludes left rooms. - Future> getRoomList( - {bool onlyLeft = false, - bool onlyDirect = false, - bool onlyGroups = false}) async { - if (onlyDirect && onlyGroups) return []; + Future> getRoomList({bool onlyLeft = false}) async { List> res = await db.rawQuery("SELECT * " + " FROM Rooms" + " WHERE membership" + @@ -399,7 +395,8 @@ class Store { /// Searches for the event in the store. Future getEventById(String eventID, Room room) async { List> res = await db.rawQuery( - "SELECT * FROM Events WHERE event_id=? AND room_id=?", [eventID, room.id]); + "SELECT * FROM Events WHERE event_id=? AND room_id=?", + [eventID, room.id]); if (res.length == 0) return null; return Event.fromJson(res[0], room); } diff --git a/test/RoomList_test.dart b/test/RoomList_test.dart index 29ade6e..2ebecfc 100644 --- a/test/RoomList_test.dart +++ b/test/RoomList_test.dart @@ -119,6 +119,17 @@ void main() { limitedTimeline: false, prev_batch: "1234", )); + client.connection.onRoomUpdate.add(RoomUpdate( + id: "1", + membership: Membership.join, + notification_count: 2, + highlight_count: 1, + limitedTimeline: false, + prev_batch: "12345", + summary: RoomSummary( + mHeroes: ["@alice:example.com"], + mJoinedMemberCount: 1, + mInvitedMemberCount: 1))); await new Future.delayed(new Duration(milliseconds: 50)); @@ -126,6 +137,10 @@ void main() { expect(roomList.roomSub != null, true); expect(roomList.rooms[0].id, "1"); expect(roomList.rooms[1].id, "2"); + expect(roomList.rooms[0].prev_batch, "12345"); + expect(roomList.rooms[0].displayname, "alice"); + expect(roomList.rooms[0].mJoinedMemberCount, 1); + expect(roomList.rooms[0].mInvitedMemberCount, 1); ChatTime now = ChatTime.now(); @@ -166,7 +181,7 @@ void main() { await new Future.delayed(new Duration(milliseconds: 50)); - expect(updateCount, 4); + expect(updateCount, 5); expect(roomUpdates, 2); expect(insertList, [0, 1]); expect(removeList, []);