Skip to content

Commit

Permalink
fix: Workaround for too big sqflite tables
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Dec 20, 2023
1 parent e1d4af8 commit be4048c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class Client extends MatrixApi {
this.shareKeysWithUnverifiedDevices = true,
this.enableDehydratedDevices = false,
this.receiptsPublicByDefault = true,
this.maxStoreReadReceiptsPerRoom = 100,
}) : syncFilter = syncFilter ??
Filter(
room: RoomFilter(
Expand Down Expand Up @@ -217,6 +218,13 @@ class Client extends MatrixApi {
registerDefaultCommands();
}

/// Sets a maximum amount of stored read receipts per room. Own read receipts
/// will always be excluded from this. This will stop persisting receipts
/// after the maximum is reached. For each user and thread there is one
/// read receipt stored.
/// Workaround for Workaround for https://github.com/famedly/matrix-dart-sdk/issues/1642
final int maxStoreReadReceiptsPerRoom;

/// The required name for this client.
final String clientName;

Expand Down
22 changes: 21 additions & 1 deletion lib/src/models/receipts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ class LatestReceiptState {
this.byThread = const {},
});

int get receiptCount {
var count = global.otherUsers.length;
count += (mainThread?.otherUsers.length ?? 0);
for (final thread in byThread.values) {
count += thread.otherUsers.length;
}
return count;
}

factory LatestReceiptState.fromJson(Map<String, dynamic> json) {
final global = json['global'] ?? <String, dynamic>{};
final Map<String, dynamic> main = json['main'] ?? <String, dynamic>{};
Expand Down Expand Up @@ -246,6 +255,8 @@ class LatestReceiptState {
) async {
final List<LatestReceiptStateForTimeline> updatedTimelines = [];
final ownUserid = room.client.userID!;
var count = receiptCount;
final receiptsMaxLength = room.client.maxStoreReadReceiptsPerRoom;

content.receipts.forEach((eventId, receiptsByType) {
receiptsByType.forEach((receiptType, receiptsByUser) {
Expand All @@ -271,7 +282,16 @@ class LatestReceiptState {
}
updatedTimelines.add(timeline);
} else {
timeline.otherUsers[user] = receiptData;
if (count > receiptsMaxLength) {
// Workaround for https://github.com/famedly/matrix-dart-sdk/issues/1642
Logs().w(
'Reached maximum amount of storable read receipts',
receiptsMaxLength,
);
} else {
count++;
timeline.otherUsers[user] = receiptData;
}
}
});
});
Expand Down

0 comments on commit be4048c

Please sign in to comment.