Skip to content

Commit

Permalink
chore: incrementally add left rooms to archive
Browse files Browse the repository at this point in the history
- store left rooms in archive during sync (as well as they are removed
  on join already)
- refactor room archive code
- fix typo

Internal reference: SMC-385

Signed-off-by: The one with the braid <[email protected]>
  • Loading branch information
TheOneWithTheBraid committed Sep 22, 2023
1 parent fa73ab3 commit 2f73120
Showing 1 changed file with 61 additions and 48 deletions.
109 changes: 61 additions & 48 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class Client extends MatrixApi {

bool enableDehydratedDevices = false;

/// Wether read receipts are sent as public receipts by default or just as private receipts.
/// Whether read receipts are sent as public receipts by default or just as private receipts.
bool receiptsPublicByDefault = true;

/// Whether this client supports end-to-end encryption using olm.
Expand Down Expand Up @@ -927,58 +927,66 @@ class Client extends MatrixApi {
final leave = syncResp.rooms?.leave;
if (leave != null) {
for (final entry in leave.entries) {
final id = entry.key;
final room = entry.value;
final leftRoom = Room(
id: id,
membership: Membership.leave,
client: this,
roomAccountData:
room.accountData?.asMap().map((k, v) => MapEntry(v.type, v)) ??
<String, BasicRoomEvent>{},
);
_storeArchivedRoom(entry.key, entry.value);
}
}
return _archivedRooms;
}

final timeline = Timeline(
room: leftRoom,
chunk: TimelineChunk(
events: room.timeline?.events?.reversed
.toList() // we display the event in the other sence
.map((e) => Event.fromMatrixEvent(e, leftRoom))
.toList() ??
[]));

leftRoom.prev_batch = room.timeline?.prevBatch;
room.state?.forEach((event) {
leftRoom.setState(Event.fromMatrixEvent(
event,
leftRoom,
));
});
void _storeArchivedRoom(String id, LeftRoomUpdate update) {
final room = update;
final leftRoom = Room(
id: id,
membership: Membership.leave,
client: this,
roomAccountData:
room.accountData?.asMap().map((k, v) => MapEntry(v.type, v)) ??
<String, BasicRoomEvent>{},
);

room.timeline?.events?.forEach((event) {
leftRoom.setState(Event.fromMatrixEvent(
event,
leftRoom,
));
});
final timeline = Timeline(
room: leftRoom,
chunk: TimelineChunk(
events: room.timeline?.events?.reversed
.toList() // we display the event in the other sence
.map((e) => Event.fromMatrixEvent(e, leftRoom))
.toList() ??
[]));

leftRoom.prev_batch = room.timeline?.prevBatch;
room.state?.forEach((event) {
leftRoom.setState(Event.fromMatrixEvent(
event,
leftRoom,
));
});

for (var i = 0; i < timeline.events.length; i++) {
// Try to decrypt encrypted events but don't update the database.
if (leftRoom.encrypted && leftRoom.client.encryptionEnabled) {
if (timeline.events[i].type == EventTypes.Encrypted) {
timeline.events[i] =
await leftRoom.client.encryption!.decryptRoomEvent(
leftRoom.id,
timeline.events[i],
);
}
}
}
room.timeline?.events?.forEach((event) {
leftRoom.setState(Event.fromMatrixEvent(
event,
leftRoom,
));
});

_archivedRooms.add(ArchivedRoom(room: leftRoom, timeline: timeline));
for (var i = 0; i < timeline.events.length; i++) {
// Try to decrypt encrypted events but don't update the database.
if (leftRoom.encrypted && leftRoom.client.encryptionEnabled) {
if (timeline.events[i].type == EventTypes.Encrypted) {
unawaited(
leftRoom.client.encryption!
.decryptRoomEvent(
leftRoom.id,
timeline.events[i],
)
.then(
(decrypted) => timeline.events[i] = decrypted,
),
);
}
}
}
return _archivedRooms;

_archivedRooms.add(ArchivedRoom(room: leftRoom, timeline: timeline));
}

/// Uploads a file and automatically caches it in the database, if it is small enough
Expand Down Expand Up @@ -2161,9 +2169,14 @@ class Client extends MatrixApi {
// stop stale group call checker for left room.
room.stopStaleCallsChecker(room.id);

// in order to keep the archive in sync, add left room to archive
if (chatUpdate is LeftRoomUpdate) {
_storeArchivedRoom(room.id, chatUpdate);
}

rooms.removeAt(roomIndex);
}
// Update notification, highlight count and/or additional informations
// Update notification, highlight count and/or additional information
else if (found &&
chatUpdate is JoinedRoomUpdate &&
(rooms[roomIndex].membership != membership ||
Expand Down

0 comments on commit 2f73120

Please sign in to comment.