π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Clarify person and contact cache invariants
Neeraj Guptabadf2bd6bfd7
diff --git a/mobile/apps/photos/lib/services/machine_learning/face_ml/person/person_service.dart b/mobile/apps/photos/lib/services/machine_learning/face_ml/person/person_service.dartindex 388f6df83b..cdffcf2091 100644--- a/mobile/apps/photos/lib/services/machine_learning/face_ml/person/person_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/face_ml/person/person_service.dart@@ -59,7 +59,7 @@ class PersonService {Future<List<PersonEntity>>? _cachedPersonsFuture;Map<String, PersonEntity>? _cachedPersonsById;int _personCacheGeneration = 0;- int _lastCacheRefreshTime = 0;+ int _cachedRemoteSyncTime = 0;static PersonService get instance {if (_instance == null) {@@ -91,7 +91,7 @@ class PersonService {void clearCache() {_emailToPartialPersonDataMapCache.clear();_invalidatePersonCache();- _lastCacheRefreshTime = 0;+ _cachedRemoteSyncTime = 0;}void _invalidatePersonCache() {@@ -105,9 +105,7 @@ class PersonService {String source = "",}) async {_invalidatePersonCache();- _lastCacheRefreshTime = lastRemoteSyncTime();- // wait to ensure cache is refreshed- final _ = await getPersons();+ await getPersons();if (notifyListeners) {Bus.instance.fire(PeopleChangedEvent(type: PeopleEventType.syncDone, source: source),@@ -133,8 +131,8 @@ class PersonService {Future<List<PersonEntity>> getPersons() async {final remoteSyncTime = lastRemoteSyncTime();- if (_lastCacheRefreshTime != remoteSyncTime) {- _lastCacheRefreshTime = remoteSyncTime;+ if (_cachedRemoteSyncTime != remoteSyncTime) {+ _cachedRemoteSyncTime = remoteSyncTime;_invalidatePersonCache();}if (_cachedPersonsFuture == null) {@@ -190,7 +188,7 @@ class PersonService {}PersonEntity? getCachedPerson(String id) {- if (_lastCacheRefreshTime != lastRemoteSyncTime()) {+ if (_cachedRemoteSyncTime != lastRemoteSyncTime()) {return null;}return _cachedPersonsById?[id];@@ -804,7 +802,6 @@ class PersonService {String? id,}) async {final result = await entityService.addOrUpdate(type, jsonMap, id: id);- _lastCacheRefreshTime = 0; // Invalidate cache_invalidatePersonCache();return result;}diff --git a/mobile/apps/photos/lib/services/photos_contacts_service.dart b/mobile/apps/photos/lib/services/photos_contacts_service.dartindex 96fcf738f2..3aac8587f7 100644--- a/mobile/apps/photos/lib/services/photos_contacts_service.dart+++ b/mobile/apps/photos/lib/services/photos_contacts_service.dart@@ -32,8 +32,10 @@ class PhotosContactsService {final contacts.ContactsService Function()? _contactsServiceFactory;final _logger = Logger("PhotosContactsService");- final Map<int, contacts.ContactRecord> _contactsByUserId = {};- final Map<String, contacts.ContactRecord> _contactsByNormalizedEmail = {};+ // Deleted records evict entries; these indexes contain active contacts only.+ final Map<int, contacts.ContactRecord> _activeContactsByUserId = {};+ final Map<String, contacts.ContactRecord> _activeContactsByNormalizedEmail =+ {};final Map<int, Uint8List?> _profilePictureBytesByUserId = {};final Set<int> _resolvedProfilePictureUserIds = {};final Map<int, Future<Uint8List?>> _profilePictureLoadsByUserId = {};@@ -128,12 +130,12 @@ class PhotosContactsService {return null;}if (contactUserId != null) {- return _contactsByUserId[contactUserId];+ return _activeContactsByUserId[contactUserId];}final normalizedEmail = normalizeContactLinkEmail(email);return normalizedEmail == null? null- : _contactsByNormalizedEmail[normalizedEmail];+ : _activeContactsByNormalizedEmail[normalizedEmail];}String? getCachedSavedName({int? contactUserId, String? email}) {@@ -206,7 +208,7 @@ class PhotosContactsService {final contactId = contact.id;try {final bytes = await _requireContacts().getProfilePicture(contactId);- final latestContact = _contactsByUserId[contactUserId];+ final latestContact = _activeContactsByUserId[contactUserId];if (latestContact == null ||latestContact.isDeleted ||latestContact.id != contactId ||@@ -338,7 +340,7 @@ class PhotosContactsService {return;}- final cachedUserIdsBeforeHydration = _contactsByUserId.keys.toSet();+ final cachedUserIdsBeforeHydration = _activeContactsByUserId.keys.toSet();final localContacts = await contactsService.getContacts();if (!_isSessionGenerationCurrent(generation, session)) {return;@@ -444,21 +446,22 @@ class PhotosContactsService {void _cacheContact(contacts.ContactRecord contact) {final userId = contact.contactUserId;- final existing = _contactsByUserId[userId];+ final existing = _activeContactsByUserId[userId];final previousEmail = normalizeContactLinkEmail(existing?.email);if (previousEmail != null &&- _contactsByNormalizedEmail[previousEmail]?.contactUserId == userId) {- _contactsByNormalizedEmail.remove(previousEmail);+ _activeContactsByNormalizedEmail[previousEmail]?.contactUserId ==+ userId) {+ _activeContactsByNormalizedEmail.remove(previousEmail);}if (contact.isDeleted) {- _contactsByUserId.remove(userId);+ _activeContactsByUserId.remove(userId);_invalidateProfilePictureCache(userId);return;}- _contactsByUserId[userId] = contact;+ _activeContactsByUserId[userId] = contact;final normalizedEmail = normalizeContactLinkEmail(contact.email);if (normalizedEmail != null) {- _contactsByNormalizedEmail[normalizedEmail] = contact;+ _activeContactsByNormalizedEmail[normalizedEmail] = contact;}if (existing?.profilePictureAttachmentId !=contact.profilePictureAttachmentId) {@@ -467,9 +470,9 @@ class PhotosContactsService {}void _resetSessionState({required bool notify}) {- final hadCachedContacts = _contactsByUserId.isNotEmpty;- _contactsByUserId.clear();- _contactsByNormalizedEmail.clear();+ final hadCachedContacts = _activeContactsByUserId.isNotEmpty;+ _activeContactsByUserId.clear();+ _activeContactsByNormalizedEmail.clear();_profilePictureBytesByUserId.clear();_resolvedProfilePictureUserIds.clear();_profilePictureLoadsByUserId.clear();diff --git a/mobile/apps/photos/test/services/photos_contacts_service_test.dart b/mobile/apps/photos/test/services/photos_contacts_service_test.dartindex 35fa31c9ef..074601e8be 100644--- a/mobile/apps/photos/test/services/photos_contacts_service_test.dart+++ b/mobile/apps/photos/test/services/photos_contacts_service_test.dart@@ -88,6 +88,26 @@ void main() {expect((await service.getContact(email: '[email protected]'))?.id, 'ct_1');});+ test('deletion tombstone evicts contact indexes', () async {+ await service.debugOpenAndSync(session);++ service.debugHydrateContacts(const [+ contacts.ContactRecord(+ id: 'ct_1',+ contactUserId: 7,+ email: '[email protected]',+ data: contacts.ContactData(contactUserId: 7, name: 'Alice'),+ profilePictureAttachmentId: null,+ isDeleted: true,+ createdAt: 1,+ updatedAt: 3,+ ),+ ]);++ expect(service.getCachedContact(contactUserId: 7), isNull);+ expect(service.getCachedContact(email: '[email protected]'), isNull);+ });+test('stale in-flight debugOpenAndSync does not repopulate cache after session switch',() async {