From bf610a9a3de6a9ceb1277a911c8979216fed0d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Adasiewicz?= Date: Fri, 16 Dec 2022 14:20:40 +0100 Subject: [PATCH 1/3] fix: initializing last synced data --- packages/stream_chat/CHANGELOG.md | 5 +++++ packages/stream_chat/lib/src/client/client.dart | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 2937a2c9..7b0b41ab 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,8 @@ +## Upcomming + +🐞 Fixed +- Fixed initializing last synced date. + ## 5.1.0 ✅ Added diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index a9569c20..4f2dbda3 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -453,6 +453,15 @@ class StreamChatClient { if (persistenceEnabled) { await sync(cids: cids, lastSyncAt: _lastSyncedAt); } + } else { + // channels are empty, assuming it's a fresh start + // and making sure `lastSyncAt` is initialized + if (persistenceEnabled) { + final lastSyncAt = await _chatPersistenceClient?.getLastSyncAt(); + if (lastSyncAt == null) { + await _chatPersistenceClient?.updateLastSyncAt(DateTime.now()); + } + } } handleEvent(Event( type: EventType.connectionRecovered, From cc7231d370f5d5e34ec1d1de48320f2fa6b41cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Adasiewicz?= Date: Fri, 16 Dec 2022 15:08:40 +0100 Subject: [PATCH 2/3] fix tests --- packages/stream_chat/test/src/client/client_test.dart | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index bc6ff843..5389564b 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -516,6 +516,9 @@ void main() { }); setUp(() async { + when(() => persistence.updateLastSyncAt(any())) + .thenAnswer((_) => Future.value()); + when(persistence.getLastSyncAt).thenAnswer((_) async => null); client = StreamChatClient(apiKey, chatApi: api, ws: ws) ..chatPersistenceClient = persistence; await client.connectUser(user, token); @@ -532,9 +535,11 @@ void main() { test( '''should update persistence connectionInfo and lastSync when sync succeeds''', () async { + // persistence.updateLastSyncAt might be called when connecting the user. + // Resetting the logs so we start counting invocations correctly. + reset(persistence); const cids = ['test-cid-1', 'test-cid-2', 'test-cid-3']; final lastSyncAt = DateTime.now(); - when(() => api.general.sync(cids, lastSyncAt)) .thenAnswer((_) async => SyncResponse() ..events = [ @@ -567,6 +572,9 @@ void main() { test( 'should work fine if persistence contains sync params', () async { + // persistence.updateLastSyncAt might be called when connecting the user. + // Resetting the logs so we start counting invocations correctly. + reset(persistence); const cids = ['test-cid-1', 'test-cid-2', 'test-cid-3']; final lastSyncAt = DateTime.now(); From bc5d452782fac2f090035622efcd0c69fbd09c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Adasiewicz?= Date: Fri, 16 Dec 2022 15:38:47 +0100 Subject: [PATCH 3/3] add line break in comments --- packages/stream_chat/test/src/client/client_test.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index 5389564b..7a605557 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -535,7 +535,8 @@ void main() { test( '''should update persistence connectionInfo and lastSync when sync succeeds''', () async { - // persistence.updateLastSyncAt might be called when connecting the user. + // persistence.updateLastSyncAt might be called + // when connecting the user. // Resetting the logs so we start counting invocations correctly. reset(persistence); const cids = ['test-cid-1', 'test-cid-2', 'test-cid-3']; @@ -572,7 +573,8 @@ void main() { test( 'should work fine if persistence contains sync params', () async { - // persistence.updateLastSyncAt might be called when connecting the user. + // persistence.updateLastSyncAt might be called + // when connecting the user. // Resetting the logs so we start counting invocations correctly. reset(persistence); const cids = ['test-cid-1', 'test-cid-2', 'test-cid-3'];