Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][contacts] fix contact result and stale display sync

Neeraj Gupta54df4dcd46aa
diff --git a/mobile/apps/photos/lib/ui/viewer/search/result/contact_result_page.dart b/mobile/apps/photos/lib/ui/viewer/search/result/contact_result_page.dart
index f883d40882..a2fb0530af 100644
--- a/mobile/apps/photos/lib/ui/viewer/search/result/contact_result_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search/result/contact_result_page.dart
@@ -644,7 +644,7 @@ class _ContactResultAppBar extends StatelessWidget
preferredSize: Size.fromHeight(0),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
- child: Flexible(child: RecommendedFiltersForAppbar()),
+ child: RecommendedFiltersForAppbar(),
),
),
);
diff --git a/mobile/packages/contacts/lib/src/service/contacts_display_service.dart b/mobile/packages/contacts/lib/src/service/contacts_display_service.dart
index bbac0e513c..689218cd1c 100644
--- a/mobile/packages/contacts/lib/src/service/contacts_display_service.dart
+++ b/mobile/packages/contacts/lib/src/service/contacts_display_service.dart
@@ -31,6 +31,7 @@ class ContactsDisplayService {
String? _sessionAuthToken;
Future<void>? _readyFuture;
bool _hasHydratedCache = false;
+ int _sessionGeneration = 0;
ValueListenable<int> get changes => _changes;
@@ -57,6 +58,7 @@ class ContactsDisplayService {
}
if (_sessionKey != sessionKey) {
+ _sessionGeneration += 1;
_clearCachedState(notify: true);
}
@@ -65,7 +67,8 @@ class ContactsDisplayService {
_sessionAuthToken = session.authToken;
late final Future<void> readyFuture;
- readyFuture = _openAndSync(session).catchError((
+ final generation = _sessionGeneration;
+ readyFuture = _openAndSync(session, generation).catchError((
Object error,
StackTrace stackTrace,
) {
@@ -108,6 +111,7 @@ class ContactsDisplayService {
Future<void> resetLocalState() async {
await _contacts?.resetLocalState();
+ _sessionGeneration += 1;
_clearCachedState(notify: true);
}
@@ -268,11 +272,17 @@ class ContactsDisplayService {
}
}
- Future<void> _openAndSync(ContactsSession session) async {
+ Future<void> _openAndSync(ContactsSession session, int generation) async {
final contacts = _requireContacts();
await contacts.open(session);
+ if (!_isSessionGenerationCurrent(generation, session)) {
+ return;
+ }
final localContacts = await contacts.getContacts();
+ if (!_isSessionGenerationCurrent(generation, session)) {
+ return;
+ }
final localChanged = _cacheContacts(localContacts);
_hasHydratedCache = true;
if (localChanged.isNotEmpty) {
@@ -280,6 +290,9 @@ class ContactsDisplayService {
}
final diff = await contacts.sync();
+ if (!_isSessionGenerationCurrent(generation, session)) {
+ return;
+ }
final diffChanged = _cacheContacts(diff, invalidateProfilePictures: true);
if (diffChanged.isNotEmpty) {
_notifyChanged();
@@ -421,6 +434,11 @@ class ContactsDisplayService {
return contacts;
}
+ bool _isSessionGenerationCurrent(int generation, ContactsSession session) {
+ return _sessionGeneration == generation &&
+ _sessionKey == _buildSessionKey(session);
+ }
+
void _clearCachedState({required bool notify}) {
final hadCachedState = _contactsByUserId.isNotEmpty ||
_contactsByNormalizedEmail.isNotEmpty ||
diff --git a/mobile/packages/contacts/test/contacts_display_service_test.dart b/mobile/packages/contacts/test/contacts_display_service_test.dart
index 170ba6d786..a82af42272 100644
--- a/mobile/packages/contacts/test/contacts_display_service_test.dart
+++ b/mobile/packages/contacts/test/contacts_display_service_test.dart
@@ -184,18 +184,21 @@ void main() {
final pending = displayService.getProfilePictureBytes(contactUserId: 7);
- displayService.debugHydrateContacts([
- const ContactRecord(
- id: 'ct_1',
- contactUserId: 7,
- email: '[email protected]',
- data: ContactData(contactUserId: 7, name: 'Alice'),
- profilePictureAttachmentId: 'att_new',
- isDeleted: false,
- createdAt: 1,
- updatedAt: 3,
- ),
- ], notify: false,);
+ displayService.debugHydrateContacts(
+ [
+ const ContactRecord(
+ id: 'ct_1',
+ contactUserId: 7,
+ email: '[email protected]',
+ data: ContactData(contactUserId: 7, name: 'Alice'),
+ profilePictureAttachmentId: 'att_new',
+ isDeleted: false,
+ createdAt: 1,
+ updatedAt: 3,
+ ),
+ ],
+ notify: false,
+ );
rustApi.ctx.profilePictureBarrier!.complete();
@@ -205,18 +208,66 @@ void main() {
isNull,
);
});
+
+ test(
+ 'stale in-flight ensureReady does not repopulate cache after session switch',
+ () async {
+ final diffBarrier = Completer<void>();
+ rustApi.ctx.diffBarrier = diffBarrier;
+ rustApi.ctx.diffStarted = Completer<void>();
+ rustApi.diffPages = [
+ [
+ const ContactRecord(
+ id: 'ct_old',
+ contactUserId: 7,
+ email: '[email protected]',
+ data: ContactData(contactUserId: 7, name: 'Alice'),
+ profilePictureAttachmentId: null,
+ isDeleted: false,
+ createdAt: 1,
+ updatedAt: 2,
+ ),
+ ],
+ const [],
+ ];
+
+ final oldEnsureReady = displayService.ensureReady(session);
+ await rustApi.ctx.diffStarted!.future;
+
+ rustApi.nextOpenContext = FakeContactsRustContext();
+ rustApi.diffPages = [const []];
+ final nextSession = ContactsSession(
+ baseUrl: session.baseUrl,
+ authToken: 'token-2',
+ userId: 2,
+ accountKey: Uint8List.fromList([9, 9, 9]),
+ );
+ await displayService.ensureReady(nextSession);
+
+ diffBarrier.complete();
+ await oldEnsureReady;
+
+ expect(displayService.getCachedSavedName(contactUserId: 7), isNull);
+ expect(
+ displayService.getCachedResolvedEmail(email: '[email protected]'),
+ isNull,
+ );
+ });
}
class FakeContactsRustApi implements ContactsRustApi {
FakeContactsRustContext ctx = FakeContactsRustContext();
+ FakeContactsRustContext? nextOpenContext;
List<List<ContactRecord>> diffPages = const [];
@override
Future<OpenContactsContextResult> open(OpenContactsContextInput input) async {
- ctx.userIdValue = input.userId;
- ctx.diffPages = List<List<ContactRecord>>.from(diffPages);
+ final context = nextOpenContext ?? ctx;
+ nextOpenContext = null;
+ context.userIdValue = input.userId;
+ context.diffPages = List<List<ContactRecord>>.from(diffPages);
return OpenContactsContextResult(
- ctx: ctx,
+ ctx: context,
wrappedRootKey: const WrappedRootContactKey(
encryptedKey: 'enc-key',
header: 'enc-header',
@@ -236,6 +287,8 @@ class FakeContactsRustContext implements ContactsRustContext {
int getProfilePictureCalls = 0;
String nextAttachmentId = 'att_profile';
Completer<void>? profilePictureBarrier;
+ Completer<void>? diffBarrier;
+ Completer<void>? diffStarted;
Object? profilePictureError;
@override
@@ -312,6 +365,12 @@ class FakeContactsRustContext implements ContactsRustContext {
@override
Future<List<ContactRecord>> getDiff(int sinceTime, int limit) async {
+ final barrier = diffBarrier;
+ if (barrier != null) {
+ diffStarted?.complete();
+ diffBarrier = null;
+ await barrier.future;
+ }
if (diffPages.isEmpty) {
return const [];
}