chore: store states to archived rooms (#1)

* chore: store states to archived rooms

* chore: PR fix

---------

Co-authored-by: Clemens Toegel <clemens.toegel@x-tention.at>
This commit is contained in:
Clemens-Toegel 2023-10-13 10:14:20 +02:00 committed by The one with the braid
parent 053dddb76d
commit 0ec6a64cc8
1 changed files with 39 additions and 28 deletions

View File

@ -939,48 +939,58 @@ class Client extends MatrixApi {
return _archivedRooms; return _archivedRooms;
} }
Future<void> _storeArchivedRoom(String id, LeftRoomUpdate update) async { /// [_storeArchivedRoom]
final room = update; /// @leftRoom we can pass a room which was left so that we don't loose states
final leftRoom = Room( Future<void> _storeArchivedRoom(
id: id, String id,
membership: Membership.leave, LeftRoomUpdate update, {
client: this, Room? leftRoom,
roomAccountData: }) async {
room.accountData?.asMap().map((k, v) => MapEntry(v.type, v)) ?? final roomUpdate = update;
final archivedRoom = leftRoom ??
Room(
id: id,
membership: Membership.leave,
client: this,
roomAccountData: roomUpdate.accountData
?.asMap()
.map((k, v) => MapEntry(v.type, v)) ??
<String, BasicRoomEvent>{}, <String, BasicRoomEvent>{},
); );
// Set membership of room to leave, in the case we got a left room passed, otherwise
// the left room would have still membership join, which would be wrong for the setState later
archivedRoom.membership = Membership.leave;
final timeline = Timeline( final timeline = Timeline(
room: leftRoom, room: archivedRoom,
chunk: TimelineChunk( chunk: TimelineChunk(
events: room.timeline?.events?.reversed events: roomUpdate.timeline?.events?.reversed
.toList() // we display the event in the other sence .toList() // we display the event in the other sence
.map((e) => Event.fromMatrixEvent(e, leftRoom)) .map((e) => Event.fromMatrixEvent(e, archivedRoom))
.toList() ?? .toList() ??
[])); []));
leftRoom.prev_batch = room.timeline?.prevBatch; archivedRoom.prev_batch = update.timeline?.prevBatch;
room.state?.forEach((event) { update.state?.forEach((event) {
leftRoom.setState(Event.fromMatrixEvent( archivedRoom.setState(Event.fromMatrixEvent(
event, event,
leftRoom, archivedRoom,
)); ));
}); });
room.timeline?.events?.forEach((event) { update.timeline?.events?.forEach((event) {
leftRoom.setState(Event.fromMatrixEvent( archivedRoom.setState(Event.fromMatrixEvent(
event, event,
leftRoom, archivedRoom,
)); ));
}); });
for (var i = 0; i < timeline.events.length; i++) { for (var i = 0; i < timeline.events.length; i++) {
// Try to decrypt encrypted events but don't update the database. // Try to decrypt encrypted events but don't update the database.
if (leftRoom.encrypted && leftRoom.client.encryptionEnabled) { if (archivedRoom.encrypted && archivedRoom.client.encryptionEnabled) {
if (timeline.events[i].type == EventTypes.Encrypted) { if (timeline.events[i].type == EventTypes.Encrypted) {
await leftRoom.client.encryption! await archivedRoom.client.encryption!
.decryptRoomEvent( .decryptRoomEvent(
leftRoom.id, archivedRoom.id,
timeline.events[i], timeline.events[i],
) )
.then( .then(
@ -990,7 +1000,7 @@ class Client extends MatrixApi {
} }
} }
_archivedRooms.add(ArchivedRoom(room: leftRoom, timeline: timeline)); _archivedRooms.add(ArchivedRoom(room: archivedRoom, timeline: timeline));
} }
/// Uploads a file and automatically caches it in the database, if it is small enough /// Uploads a file and automatically caches it in the database, if it is small enough
@ -2175,6 +2185,11 @@ class Client extends MatrixApi {
room.stopStaleCallsChecker(room.id); room.stopStaleCallsChecker(room.id);
rooms.removeAt(roomIndex); rooms.removeAt(roomIndex);
// in order to keep the archive in sync, add left room to archive
if (chatUpdate is LeftRoomUpdate) {
await _storeArchivedRoom(room.id, chatUpdate, leftRoom: room);
}
} }
// Update notification, highlight count and/or additional information // Update notification, highlight count and/or additional information
else if (found && else if (found &&
@ -2209,10 +2224,6 @@ class Client extends MatrixApi {
runInRoot(rooms[roomIndex].requestHistory); runInRoot(rooms[roomIndex].requestHistory);
} }
} }
// in order to keep the archive in sync, add left room to archive
if (chatUpdate is LeftRoomUpdate) {
await _storeArchivedRoom(room.id, chatUpdate);
}
return room; return room;
} }