Skip to content

Commit

Permalink
Merge branch 'henri/fragmented-timeline-v2' into 'main'
Browse files Browse the repository at this point in the history
 Henri/fragmented timeline v2

Closes #293

See merge request famedly/company/frontend/famedlysdk!1038
  • Loading branch information
henri2h committed May 16, 2022
2 parents 4fec950 + bfdd4c8 commit be0adde
Show file tree
Hide file tree
Showing 15 changed files with 1,030 additions and 129 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.9.5] - 13th May 2022
- fix: Fix deep copy issue in the fragmented timeline feature and restored it

## [0.9.4] - 12th May 2022
- fix: Revert fragmented timeline feature temporarily to fix SENDING timeline

Expand Down
54 changes: 27 additions & 27 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ class Client extends MatrixApi {
await roomsLoading;
await _accountDataLoading;
_currentTransaction = database.transaction(() async {
await _handleSync(syncResp);
await _handleSync(syncResp, direction: Direction.f);
if (prevBatch != syncResp.nextBatch) {
await database.storePrevBatch(syncResp.nextBatch);
}
Expand All @@ -1401,7 +1401,7 @@ class Client extends MatrixApi {
);
onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp));
} else {
await _handleSync(syncResp);
await _handleSync(syncResp, direction: Direction.f);
}
if (_disposed || _aborted) return;
if (prevBatch == null) {
Expand Down Expand Up @@ -1445,13 +1445,13 @@ class Client extends MatrixApi {
}

/// Use this method only for testing utilities!
Future<void> handleSync(SyncUpdate sync, {bool sortAtTheEnd = false}) async {
Future<void> handleSync(SyncUpdate sync, {Direction? direction}) async {
// ensure we don't upload keys because someone forgot to set a key count
sync.deviceOneTimeKeysCount ??= {'signed_curve25519': 100};
await _handleSync(sync, sortAtTheEnd: sortAtTheEnd);
await _handleSync(sync, direction: direction);
}

Future<void> _handleSync(SyncUpdate sync, {bool sortAtTheEnd = false}) async {
Future<void> _handleSync(SyncUpdate sync, {Direction? direction}) async {
final syncToDevice = sync.toDevice;
if (syncToDevice != null) {
await _handleToDeviceEvents(syncToDevice);
Expand All @@ -1460,15 +1460,15 @@ class Client extends MatrixApi {
if (sync.rooms != null) {
final join = sync.rooms?.join;
if (join != null) {
await _handleRooms(join, sortAtTheEnd: sortAtTheEnd);
await _handleRooms(join, direction: direction);
}
final invite = sync.rooms?.invite;
if (invite != null) {
await _handleRooms(invite, sortAtTheEnd: sortAtTheEnd);
await _handleRooms(invite, direction: direction);
}
final leave = sync.rooms?.leave;
if (leave != null) {
await _handleRooms(leave, sortAtTheEnd: sortAtTheEnd);
await _handleRooms(leave, direction: direction);
}
}
for (final newPresence in sync.presence ?? []) {
Expand Down Expand Up @@ -1532,7 +1532,7 @@ class Client extends MatrixApi {
}

Future<void> _handleRooms(Map<String, SyncRoomUpdate> rooms,
{bool sortAtTheEnd = false}) async {
{Direction? direction}) async {
var handledRooms = 0;
for (final entry in rooms.entries) {
onSyncStatus.add(SyncStatusUpdate(
Expand All @@ -1548,20 +1548,26 @@ class Client extends MatrixApi {
/// Handle now all room events and save them in the database
if (room is JoinedRoomUpdate) {
final state = room.state;

if (state != null && state.isNotEmpty) {
// TODO: This method seems to be comperatively slow for some updates
await _handleRoomEvents(
id, state.map((i) => i.toJson()).toList(), EventUpdateType.state,
sortAtTheEnd: sortAtTheEnd);
id,
state.map((i) => i.toJson()).toList(),
EventUpdateType.state,
);
}

final timelineEvents = room.timeline?.events;
if (timelineEvents != null && timelineEvents.isNotEmpty) {
await _handleRoomEvents(
id,
timelineEvents.map((i) => i.toJson()).toList(),
sortAtTheEnd ? EventUpdateType.history : EventUpdateType.timeline,
sortAtTheEnd: sortAtTheEnd);
direction != null
? (direction == Direction.b
? EventUpdateType.history
: EventUpdateType.timeline)
: EventUpdateType.timeline);
}

final ephemeral = room.ephemeral;
Expand All @@ -1584,10 +1590,10 @@ class Client extends MatrixApi {
final timelineEvents = room.timeline?.events;
if (timelineEvents != null && timelineEvents.isNotEmpty) {
await _handleRoomEvents(
id,
timelineEvents.map((i) => i.toJson()).toList(),
EventUpdateType.timeline,
sortAtTheEnd: sortAtTheEnd);
id,
timelineEvents.map((i) => i.toJson()).toList(),
EventUpdateType.timeline,
);
}
final accountData = room.accountData;
if (accountData != null && accountData.isNotEmpty) {
Expand Down Expand Up @@ -1658,16 +1664,14 @@ class Client extends MatrixApi {
}

Future<void> _handleRoomEvents(
String chat_id, List<dynamic> events, EventUpdateType type,
{bool sortAtTheEnd = false}) async {
String chat_id, List<dynamic> events, EventUpdateType type) async {
for (final event in events) {
await _handleEvent(event, chat_id, type, sortAtTheEnd: sortAtTheEnd);
await _handleEvent(event, chat_id, type);
}
}

Future<void> _handleEvent(
Map<String, dynamic> event, String roomID, EventUpdateType type,
{bool sortAtTheEnd = false}) async {
Map<String, dynamic> event, String roomID, EventUpdateType type) async {
if (event['type'] is String && event['content'] is Map<String, dynamic>) {
// The client must ignore any new m.room.encryption event to prevent
// man-in-the-middle attacks!
Expand All @@ -1680,11 +1684,7 @@ class Client extends MatrixApi {
return;
}

var update = EventUpdate(
roomID: roomID,
type: type,
content: event,
);
var update = EventUpdate(roomID: roomID, type: type, content: event);
if (event['type'] == EventTypes.Encrypted && encryptionEnabled) {
update = await update.decrypt(room);
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/database/database_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ abstract class DatabaseApi {
Future<List<Event>> getEventList(
Room room, {
int start = 0,
bool onlySending,
int limit,
});

Expand Down
3 changes: 2 additions & 1 deletion lib/src/database/fluffybox_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ class FluffyBoxDatabase extends DatabaseApi {
Future<List<Event>> getEventList(
Room room, {
int start = 0,
bool onlySending = false,
int? limit,
}) =>
runBenchmarked<List<Event>>('Get event list', () async {
Expand All @@ -367,7 +368,7 @@ class FluffyBoxDatabase extends DatabaseApi {
final end = min(timelineEventIds.length,
start + (limit ?? timelineEventIds.length));
final eventIds = sendingEventIds +
(start < timelineEventIds.length
(start < timelineEventIds.length && !onlySending
? timelineEventIds.getRange(start, end).toList()
: []);

Expand Down
3 changes: 2 additions & 1 deletion lib/src/database/hive_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
Future<List<Event>> getEventList(
Room room, {
int start = 0,
bool onlySending = false,
int? limit,
}) =>
runBenchmarked<List<Event>>('Get event list', () async {
Expand All @@ -410,7 +411,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
final end = min(timelineEventIds.length,
start + (limit ?? timelineEventIds.length));
final eventIds = sendingEventIds +
(start < timelineEventIds.length
(start < timelineEventIds.length && !onlySending
? timelineEventIds.getRange(start, end).toList()
: []);

Expand Down
10 changes: 10 additions & 0 deletions lib/src/models/timeline_chunk.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../../matrix.dart';

class TimelineChunk {
String prevBatch; // pos of the first event of the database timeline chunk
String nextBatch;

List<Event> events;
TimelineChunk(
{required this.events, this.prevBatch = '', this.nextBatch = ''});
}
Loading

0 comments on commit be0adde

Please sign in to comment.