Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use stable identities for contacts

Neeraj Gupta48247ca12580
diff --git a/mobile/apps/photos/lib/extensions/user_extension.dart b/mobile/apps/photos/lib/extensions/user_extension.dart
index 909018ab59..1b8a263f37 100644
--- a/mobile/apps/photos/lib/extensions/user_extension.dart
+++ b/mobile/apps/photos/lib/extensions/user_extension.dart
@@ -2,17 +2,16 @@ import "package:photos/models/api/collection/user.dart";
import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
extension UserExtension on User {
+ Map<String, String>? get _partialPersonData => PersonService.instance
+ .getCachedPartialPersonData(userID: id, email: email);
+
//Some initial users have name in name field.
String? get displayName =>
- PersonService
- .instance
- .emailToPartialPersonDataMapCache[email]?[PersonService.kNameKey] ??
+ _partialPersonData?[PersonService.kNameKey] ??
// ignore: deprecated_member_use_from_same_package
((name?.isEmpty ?? true) ? null : name);
- String? get linkedPersonID => PersonService
- .instance
- .emailToPartialPersonDataMapCache[email]?[PersonService.kPersonIDKey];
+ String? get linkedPersonID => _partialPersonData?[PersonService.kPersonIDKey];
String get nameOrEmail {
if (PersonService.isInitialized) {
diff --git a/mobile/apps/photos/lib/services/contacts/direct_contact_users.dart b/mobile/apps/photos/lib/services/contacts/direct_contact_users.dart
index 2d23cf1af1..782d72c4b8 100644
--- a/mobile/apps/photos/lib/services/contacts/direct_contact_users.dart
+++ b/mobile/apps/photos/lib/services/contacts/direct_contact_users.dart
@@ -14,11 +14,11 @@ List<User> buildDirectContactUsers({
final ownerNormalizedEmail = normalizeContactLinkEmail(
knownContactEmailOrNull(ownerEmail),
);
- final usersByEmail = <String, User>{};
+ final usersById = <int, User>{};
void addUser(User user) {
final userId = user.id;
- if (userId == null || userId <= 0) {
+ if (userId == null || userId <= 0 || userId == ownerUserId) {
return;
}
final normalizedEmail = normalizeContactLinkEmail(
@@ -28,7 +28,7 @@ List<User> buildDirectContactUsers({
return;
}
- usersByEmail.putIfAbsent(normalizedEmail, () => user);
+ usersById[userId] = user;
}
for (final collection in collections) {
@@ -54,5 +54,5 @@ List<User> buildDirectContactUsers({
}
}
- return usersByEmail.values.toList(growable: false);
+ return usersById.values.toList(growable: false);
}
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.dart
index cdffcf2091..2618d8a47c 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
@@ -41,6 +41,7 @@ class PersonService {
final SharedPreferences prefs;
final PhotosContactsService _contactsService;
final _emailToPartialPersonDataMapCache = <String, Map<String, String>>{};
+ final _userIdToPartialPersonDataMapCache = <int, Map<String, String>>{};
PersonService(
this.entityService,
@@ -88,8 +89,19 @@ class PersonService {
Map<String, Map<String, String>> get emailToPartialPersonDataMapCache =>
_emailToPartialPersonDataMapCache;
+ Map<String, String>? getCachedPartialPersonData({
+ required int? userID,
+ required String email,
+ }) {
+ final userIdMatch = userID != null && userID > 0
+ ? _userIdToPartialPersonDataMapCache[userID]
+ : null;
+ return userIdMatch ?? _emailToPartialPersonDataMapCache[email];
+ }
+
void clearCache() {
_emailToPartialPersonDataMapCache.clear();
+ _userIdToPartialPersonDataMapCache.clear();
_invalidatePersonCache();
_cachedRemoteSyncTime = 0;
}
@@ -157,12 +169,19 @@ class PersonService {
for (final person in persons) person.remoteID: person,
};
_emailToPartialPersonDataMapCache.clear();
+ _userIdToPartialPersonDataMapCache.clear();
for (PersonEntity person in persons) {
- if (person.data.email != null && person.data.email!.isNotEmpty) {
- _emailToPartialPersonDataMapCache[person.data.email!] = {
- kPersonIDKey: person.remoteID,
- kNameKey: person.data.name,
- };
+ final partialData = {
+ kPersonIDKey: person.remoteID,
+ kNameKey: person.data.name,
+ };
+ final email = person.data.email;
+ if (email != null && email.isNotEmpty) {
+ _emailToPartialPersonDataMapCache[email] = partialData;
+ }
+ final userID = person.data.userID;
+ if (userID != null && userID > 0) {
+ _userIdToPartialPersonDataMapCache[userID] = partialData;
}
}
diff --git a/mobile/apps/photos/lib/services/search_service.dart b/mobile/apps/photos/lib/services/search_service.dart
index f3c9c3048c..c33983d7c9 100644
--- a/mobile/apps/photos/lib/services/search_service.dart
+++ b/mobile/apps/photos/lib/services/search_service.dart
@@ -161,30 +161,16 @@ class SearchService {
final contactsToFiles = <User, List<EnteFile>>{};
final contactsByUserId = <int, User>{};
- final contactsByEmail = <String, User>{};
for (final contact in directContacts) {
contactsToFiles[contact] = [];
- final userId = contact.id;
- if (userId != null && userId > 0) {
- contactsByUserId[userId] = contact;
- }
- final email = resolveKnownEmail(contact)?.toLowerCase();
- if (email != null) {
- contactsByEmail[email] = contact;
- }
+ contactsByUserId[contact.id!] = contact;
}
for (final file in allFiles) {
if (file.isOwner) {
continue;
}
- final fileOwner = _collectionService.getFileOwner(
- file.ownerID!,
- file.collectionID,
- );
- final contact =
- contactsByUserId[file.ownerID] ??
- contactsByEmail[resolveKnownEmail(fileOwner)?.toLowerCase()];
+ final contact = contactsByUserId[file.ownerID];
if (contact != null) {
contactsToFiles[contact]!.add(file);
}
@@ -193,11 +179,11 @@ class SearchService {
return contactsToFiles;
}
- Map<String, List<Collection>> _incomingContactCollectionsByEmail(
+ Map<int, List<Collection>> _incomingContactCollectionsByUserId(
int ownerID, {
required bool excludeArchived,
}) {
- final collectionsByEmail = <String, List<Collection>>{};
+ final collectionsByUserId = <int, List<Collection>>{};
final collections = _collectionService.getCollectionsForUI(
includedShared: true,
includeCollab: true,
@@ -208,16 +194,19 @@ class SearchService {
collection.isOwner(ownerID)) {
continue;
}
- collectionsByEmail
- .putIfAbsent(collection.owner.email, () => [])
- .add(collection);
+ final contactUserId = collection.owner.id;
+ if (contactUserId != null && contactUserId > 0) {
+ collectionsByUserId
+ .putIfAbsent(contactUserId, () => [])
+ .add(collection);
+ }
}
- return collectionsByEmail;
+ return collectionsByUserId;
}
List<GenericSearchResult> _toContactSearchResults(
Iterable<MapEntry<User, List<EnteFile>>> entries,
- Map<String, List<Collection>> collectionsByEmail,
+ Map<int, List<Collection>> collectionsByUserId,
) {
return entries.map((entry) {
final user = entry.key;
@@ -233,7 +222,7 @@ class SearchService {
),
params: _contactSearchParams(
user,
- collectionsByEmail[user.email] ?? const [],
+ collectionsByUserId[user.id] ?? const [],
),
);
}).toList();
@@ -1904,7 +1893,7 @@ class SearchService {
allFiles,
lowerCaseQuery: lowerCaseQuery,
);
- final peopleToSharedAlbums = _incomingContactCollectionsByEmail(
+ final peopleToSharedAlbums = _incomingContactCollectionsByUserId(
ownerID,
excludeArchived: false,
);
@@ -1922,7 +1911,7 @@ class SearchService {
final int ownerID = Configuration.instance.getUserID()!;
final allFiles = await getAllFilesForSearch();
final peopleToSharedFiles = _directContactsWithSharedFiles(allFiles);
- final peopleToSharedAlbums = _incomingContactCollectionsByEmail(
+ final peopleToSharedAlbums = _incomingContactCollectionsByUserId(
ownerID,
excludeArchived: true,
);
diff --git a/mobile/apps/photos/test/services/contacts/direct_contact_users_test.dart b/mobile/apps/photos/test/services/contacts/direct_contact_users_test.dart
index 2bc58127d4..aff15d6ef5 100644
--- a/mobile/apps/photos/test/services/contacts/direct_contact_users_test.dart
+++ b/mobile/apps/photos/test/services/contacts/direct_contact_users_test.dart
@@ -58,19 +58,22 @@ void main() {
expect(users.every((user) => user.id != null && user.id! > 0), isTrue);
});
- test("deduplicates by email and keeps the user id", () {
+ test("deduplicates by user id after an email change", () {
final users = buildDirectContactUsers(
ownerUserId: 1,
ownerEmail: "[email protected]",
- collections: const [],
- familyMembers: [
- _familyMember("[email protected]", FamilyMemberStatus.accepted),
+ collections: [
+ _collection(
+ owner: User(id: 1, email: "[email protected]"),
+ sharees: [User(id: 5, email: "[email protected]")],
+ ),
],
+ familyMembers: const [],
savedContacts: const [
contacts.ContactRecord(
id: "contact-1",
contactUserId: 5,
- email: "[email protected]",
+ email: "[email protected]",
data: contacts.ContactData(contactUserId: 5, name: "Same"),
profilePictureAttachmentId: null,
isDeleted: false,
@@ -82,6 +85,7 @@ void main() {
expect(users, hasLength(1));
expect(users.single.id, 5);
+ expect(users.single.email, "[email protected]");
});
test("excludes candidates without a positive user id", () {