Skip to content

armgit / ente

Public Imported from GitHub
0

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

Auto-create contacts for linked people

Neeraj Gupta0b3af9fe6757
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 2618d8a47c..bd3ee41fcd 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
@@ -7,6 +7,7 @@ import "package:ente_contacts/contacts.dart" as contacts;
import "package:ente_pure_utils/ente_pure_utils.dart";
import "package:flutter/foundation.dart";
import "package:logging/logging.dart";
+import "package:photos/core/configuration.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/db/ml/db.dart";
import "package:photos/events/people_changed_event.dart";
@@ -21,6 +22,7 @@ import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/photos_contacts_service.dart";
import "package:photos/settings/local_settings.dart";
import "package:photos/utils/contact_photo_util.dart";
+import "package:photos/utils/contact_string_util.dart";
import "package:photos/utils/face/face_thumbnail_cache.dart";
import "package:shared_preferences/shared_preferences.dart";
@@ -40,15 +42,25 @@ class PersonService {
final MLDataDB faceMLDataDB;
final SharedPreferences prefs;
final PhotosContactsService _contactsService;
+ final Future<Uint8List?> Function(PersonEntity) _contactPhotoBuilder;
+ final int? Function() _currentUserIDProvider;
final _emailToPartialPersonDataMapCache = <String, Map<String, String>>{};
final _userIdToPartialPersonDataMapCache = <int, Map<String, String>>{};
+ final _autoContactCreationsByUserId =
+ <int, Future<contacts.ContactRecord?>>{};
PersonService(
this.entityService,
this.faceMLDataDB,
this.prefs, {
PhotosContactsService? contactsService,
- }) : _contactsService = contactsService ?? PhotosContactsService.instance;
+ Future<Uint8List?> Function(PersonEntity)? contactPhotoBuilder,
+ int? Function()? currentUserIDProvider,
+ }) : _contactsService = contactsService ?? PhotosContactsService.instance,
+ _contactPhotoBuilder =
+ contactPhotoBuilder ?? buildContactPhotoAttachmentBytesFromPerson,
+ _currentUserIDProvider =
+ currentUserIDProvider ?? Configuration.instance.getUserID;
// instance
static PersonService? _instance;
@@ -744,6 +756,85 @@ class PersonService {
);
}
+ Future<contacts.ContactRecord?> tryAutoCreateContactForLinkedPerson({
+ required PersonEntity person,
+ required int contactUserId,
+ required String email,
+ }) {
+ final pending = _autoContactCreationsByUserId[contactUserId];
+ if (pending != null) {
+ return pending;
+ }
+
+ final creation = _tryAutoCreateContactForLinkedPerson(
+ person: person,
+ contactUserId: contactUserId,
+ email: email,
+ );
+ _autoContactCreationsByUserId[contactUserId] = creation;
+ return creation.whenComplete(() {
+ if (identical(_autoContactCreationsByUserId[contactUserId], creation)) {
+ _autoContactCreationsByUserId.remove(contactUserId);
+ }
+ });
+ }
+
+ Future<contacts.ContactRecord?> _tryAutoCreateContactForLinkedPerson({
+ required PersonEntity person,
+ required int contactUserId,
+ required String email,
+ }) async {
+ final ownerUserID = _currentUserIDProvider();
+ if (ownerUserID == null ||
+ contactUserId <= 0 ||
+ contactUserId == ownerUserID) {
+ return null;
+ }
+
+ final existing = await _contactsService.getContact(
+ contactUserId: contactUserId,
+ );
+ if (existing != null) {
+ return existing;
+ }
+
+ final personUserID = person.data.userID;
+ final personEmail = normalizeContactLinkEmail(person.data.email);
+ final matchesContact = personUserID != null
+ ? personUserID == contactUserId
+ : personEmail != null &&
+ personEmail == normalizeContactLinkEmail(email);
+ final name = person.data.name.trim();
+ if (!matchesContact ||
+ person.data.isIgnored ||
+ !person.data.hasAvatar() ||
+ name.isEmpty) {
+ return null;
+ }
+
+ try {
+ final bytes = await _contactPhotoBuilder(person);
+ if (bytes == null || _currentUserIDProvider() != ownerUserID) {
+ return null;
+ }
+ return await _contactsService.createContactWithProfilePictureIfAbsent(
+ contactUserId: contactUserId,
+ name: name,
+ bytes: bytes,
+ );
+ } catch (e, s) {
+ logger.warning(
+ "Failed to auto-create contact for linked person ${person.remoteID}",
+ e,
+ s,
+ );
+ if (_currentUserIDProvider() != ownerUserID) {
+ return null;
+ }
+ return _contactsService.getContact(contactUserId: contactUserId);
+ }
+ }
+
Future<contacts.ContactRecord?> _getLinkedContact(PersonEntity person) =>
_contactsService.getContact(
contactUserId: person.data.userID,
diff --git a/mobile/apps/photos/lib/services/photos_contacts_service.dart b/mobile/apps/photos/lib/services/photos_contacts_service.dart
index feb5ab44df..0303030504 100644
--- a/mobile/apps/photos/lib/services/photos_contacts_service.dart
+++ b/mobile/apps/photos/lib/services/photos_contacts_service.dart
@@ -257,6 +257,47 @@ class PhotosContactsService {
return contact;
}
+ Future<contacts.ContactRecord?> createContactWithProfilePictureIfAbsent({
+ required int contactUserId,
+ required String name,
+ required Uint8List bytes,
+ }) async {
+ final contactsService = await _ensureReadyForWrite();
+ final generation = _sessionGeneration;
+ final existing = await contactsService.getContactByUserId(
+ contactUserId,
+ includeDeleted: true,
+ );
+ if (generation != _sessionGeneration) {
+ return null;
+ }
+ if (existing != null) {
+ if (!existing.isDeleted) {
+ _cacheContact(existing);
+ return existing;
+ }
+ return null;
+ }
+
+ final contact = await contactsService.createContact(
+ contacts.ContactData(contactUserId: contactUserId, name: name.trim()),
+ );
+ if (generation != _sessionGeneration) {
+ return null;
+ }
+ _cacheContact(contact);
+ _notifyChanged(contact);
+ final updated = await contactsService.setProfilePicture(contact.id, bytes);
+ if (generation != _sessionGeneration) {
+ return null;
+ }
+ _cacheContact(updated);
+ _profilePictureBytesByUserId[updated.contactUserId] = bytes;
+ _resolvedProfilePictureUserIds.add(updated.contactUserId);
+ _notifyChanged(updated);
+ return updated;
+ }
+
Future<contacts.ContactRecord> setProfilePicture({
required String contactId,
required Uint8List bytes,
diff --git a/mobile/apps/photos/lib/ui/family/family_plan_page.dart b/mobile/apps/photos/lib/ui/family/family_plan_page.dart
index 4535773935..6604fff0bc 100644
--- a/mobile/apps/photos/lib/ui/family/family_plan_page.dart
+++ b/mobile/apps/photos/lib/ui/family/family_plan_page.dart
@@ -35,6 +35,7 @@ import 'package:photos/ui/sharing/library_sharing/library_sharing_page.dart';
import 'package:photos/ui/sharing/library_sharing/library_sharing_strings.dart';
import 'package:photos/ui/viewer/search/result/edit_contact_page.dart';
import 'package:photos/utils/dialog_util.dart';
+import 'package:photos/utils/person_contact_linking_util.dart';
class FamilyPlanPage extends StatefulWidget {
const FamilyPlanPage({
@@ -511,9 +512,7 @@ class _FamilyPlanPageState extends State<FamilyPlanPage> {
FamilyMember member,
String fallbackDisplayName,
) async {
- final isCurrentUser =
- member.email.trim().toLowerCase() ==
- _userDetails.email.trim().toLowerCase();
+ final isCurrentUser = _isCurrentMember(member);
final savedContact = await _resolveMemberContact(member);
if (!mounted) {
return;
@@ -671,7 +670,7 @@ class _FamilyPlanPageState extends State<FamilyPlanPage> {
return _contactsByUserId[userID];
}
- final resolved = await _loadMemberContact(userID);
+ final resolved = await _loadMemberContact(member);
if (!mounted) {
return resolved.contact;
}
@@ -717,8 +716,13 @@ class _FamilyPlanPageState extends State<FamilyPlanPage> {
Future<void> _loadMemberContacts() async {
final generation = ++_memberContactsLoadGeneration;
- final userIDs = _familyMemberUserIDs();
- final resolvedContacts = await Future.wait(userIDs.map(_loadMemberContact));
+ final members =
+ _userDetails.familyData?.members
+ ?.where((member) => member.userID != null)
+ .toList() ??
+ const <FamilyMember>[];
+ final userIDs = members.map((member) => member.userID!).toSet();
+ final resolvedContacts = await Future.wait(members.map(_loadMemberContact));
if (!mounted || generation != _memberContactsLoadGeneration) {
return;
}
@@ -776,10 +780,32 @@ class _FamilyPlanPageState extends State<FamilyPlanPage> {
.toSet() ??
const <int>{};
- Future<_ResolvedMemberContact> _loadMemberContact(int userID) async {
- final contact = await PhotosContactsService.instance.getContact(
+ bool _isCurrentMember(FamilyMember member) =>
+ member.email.trim().toLowerCase() ==
+ _userDetails.email.trim().toLowerCase();
+
+ Future<_ResolvedMemberContact> _loadMemberContact(FamilyMember member) async {
+ final userID = member.userID!;
+ var contact = await PhotosContactsService.instance.getContact(
contactUserId: userID,
);
+ if (contact == null &&
+ member.isActive &&
+ !_isCurrentMember(member) &&
+ PersonService.isInitialized) {
+ final person = await findPersonLinkedToContact(
+ contactUserId: userID,
+ email: member.email,
+ );
+ if (person != null) {
+ contact = await PersonService.instance
+ .tryAutoCreateContactForLinkedPerson(
+ person: person,
+ contactUserId: userID,
+ email: member.email,
+ );
+ }
+ }
final profilePicture = contact == null
? null
: await PhotosContactsService.instance.getProfilePictureBytesByUserId(
diff --git a/mobile/apps/photos/lib/ui/social/social_actor_contact_navigation.dart b/mobile/apps/photos/lib/ui/social/social_actor_contact_navigation.dart
index 6fe0c370f8..6779e2c91a 100644
--- a/mobile/apps/photos/lib/ui/social/social_actor_contact_navigation.dart
+++ b/mobile/apps/photos/lib/ui/social/social_actor_contact_navigation.dart
@@ -4,10 +4,12 @@ import "package:logging/logging.dart";
import "package:photos/models/api/collection/user.dart";
import "package:photos/models/search/generic_search_result.dart";
import "package:photos/services/contacts/contact_identity_resolver.dart";
+import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
import "package:photos/services/photos_contacts_service.dart";
import "package:photos/services/search_service.dart";
import "package:photos/ui/viewer/search/result/contact_result_page.dart";
import "package:photos/ui/viewer/search/result/edit_contact_page.dart";
+import "package:photos/utils/person_contact_linking_util.dart";
final _logger = Logger("SocialActorContactNavigation");
@@ -89,6 +91,16 @@ Future<Widget?> _buildDestination(
return _contactResultPageForUser(user);
}
+ if (PersonService.isInitialized) {
+ final person = await findPersonLinkedToContact(
+ contactUserId: userId,
+ email: email,
+ );
+ if (person != null && !person.data.isIgnored && person.data.hasAvatar()) {
+ return _contactResultPageForUser(user);
+ }
+ }
+
return EditContactPage(
contactUserId: userId,
email: email,
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 689e70ef1c..3ffb9538a9 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
@@ -41,6 +41,7 @@ import "package:photos/ui/viewer/gallery/state/selection_state.dart";
import "package:photos/ui/viewer/hierarchicial_search/app_bar_filter_chips.dart";
import "package:photos/ui/viewer/search/contact_avatar_widget.dart";
import "package:photos/ui/viewer/search/result/edit_contact_page.dart";
+import "package:photos/utils/person_contact_linking_util.dart";
class ContactResultPage extends StatefulWidget {
final SearchResult searchResult;
@@ -265,14 +266,34 @@ class _ContactResultPageState extends State<ContactResultPage> {
collections.isEmpty;
Future<void> _refreshSavedContact() async {
- final saved = await PhotosContactsService.instance.getContact(
+ var saved = await PhotosContactsService.instance.getContact(
contactUserId: _contactUserId,
);
+ String? resolvedPersonID;
+ if (saved == null && PersonService.isInitialized) {
+ final personID = _personId;
+ final person = personID == null
+ ? await findPersonLinkedToContact(
+ contactUserId: _contactUserId,
+ email: _contactEmail,
+ )
+ : await PersonService.instance.getPerson(personID);
+ if (person != null) {
+ resolvedPersonID = person.remoteID;
+ saved = await PersonService.instance
+ .tryAutoCreateContactForLinkedPerson(
+ person: person,
+ contactUserId: _contactUserId,
+ email: _contactEmail,
+ );
+ }
+ }
if (!mounted) {
return;
}
setState(() {
_savedContact = saved;
+ _personId ??= resolvedPersonID;
_resolvedSavedContact = true;
});
}
diff --git a/mobile/apps/photos/test/models/ml/face/person_contact_link_test.dart b/mobile/apps/photos/test/models/ml/face/person_contact_link_test.dart
index 36a5617cf4..ffe3ba79d4 100644
--- a/mobile/apps/photos/test/models/ml/face/person_contact_link_test.dart
+++ b/mobile/apps/photos/test/models/ml/face/person_contact_link_test.dart
@@ -1,4 +1,6 @@
+import "dart:async";
import "dart:convert";
+import "dart:typed_data";
import "package:ente_contacts/contacts.dart" as contacts;
import "package:flutter_test/flutter_test.dart";
@@ -46,9 +48,13 @@ void main() {
late _FakeEntityService entityService;
late _FakePhotosContactsService contactsService;
late PersonService personService;
+ late Future<Uint8List?> Function(PersonEntity) contactPhotoBuilder;
+ int? currentUserID;
setUp(() async {
SharedPreferences.setMockInitialValues({});
+ contactPhotoBuilder = (_) async => null;
+ currentUserID = 1;
entityService = _FakeEntityService()
..seed(
PersonEntity(
@@ -74,6 +80,8 @@ void main() {
_FakeMLDataDB(),
await SharedPreferences.getInstance(),
contactsService: contactsService,
+ contactPhotoBuilder: (person) => contactPhotoBuilder(person),
+ currentUserIDProvider: () => currentUserID,
);
});
@@ -143,6 +151,104 @@ void main() {
expect(contactsService.createOrUpdateCalls, 0);
expect(contactsService.lastUpdatedName, isNull);
});
+
+ test(
+ "does not auto-create a contact without a configured person avatar",
+ () async {
+ final person = PersonEntity(
+ "person-1",
+ PersonData(name: "Alex", email: "[email protected]", userID: 7),
+ );
+ var photoBuilds = 0;
+ contactsService.contact = null;
+ contactPhotoBuilder = (_) async {
+ photoBuilds++;
+ return Uint8List.fromList([1, 2, 3]);
+ };
+
+ final contact = await personService.tryAutoCreateContactForLinkedPerson(
+ person: person,
+ contactUserId: 7,
+ email: "[email protected]",
+ );
+
+ expect(contact, isNull);
+ expect(photoBuilds, 0);
+ expect(contactsService.autoCreateCalls, 0);
+ },
+ );
+
+ test("coalesces automatic contact creation for a linked person", () async {
+ final person = PersonEntity(
+ "person-1",
+ PersonData(
+ name: "Alex",
+ avatarFaceID: "face-1",
+ email: "[email protected]",
+ userID: 7,
+ ),
+ );
+ final photoBuildStarted = Completer<void>();
+ final releasePhotoBuild = Completer<void>();
+ var photoBuilds = 0;
+ contactsService.contact = null;
+ contactPhotoBuilder = (_) async {
+ photoBuilds++;
+ photoBuildStarted.complete();
+ await releasePhotoBuild.future;
+ return Uint8List.fromList([1, 2, 3]);
+ };
+
+ final first = personService.tryAutoCreateContactForLinkedPerson(
+ person: person,
+ contactUserId: 7,
+ email: "[email protected]",
+ );
+ await photoBuildStarted.future;
+ final second = personService.tryAutoCreateContactForLinkedPerson(
+ person: person,
+ contactUserId: 7,
+ email: "[email protected]",
+ );
+ releasePhotoBuild.complete();
+ final contacts = await Future.wait([first, second]);
+
+ expect(contacts.every((contact) => contact?.contactUserId == 7), isTrue);
+ expect(photoBuilds, 1);
+ expect(contactsService.autoCreateCalls, 1);
+ });
+
+ test("does not auto-create after the account changes", () async {
+ final person = PersonEntity(
+ "person-1",
+ PersonData(
+ name: "Alex",
+ avatarFaceID: "face-1",
+ email: "[email protected]",
+ userID: 7,
+ ),
+ );
+ final photoBuildStarted = Completer<void>();
+ final releasePhotoBuild = Completer<void>();
+ contactsService.contact = null;
+ contactPhotoBuilder = (_) async {
+ photoBuildStarted.complete();
+ await releasePhotoBuild.future;
+ return Uint8List.fromList([1, 2, 3]);
+ };
+
+ final contact = personService.tryAutoCreateContactForLinkedPerson(
+ person: person,
+ contactUserId: 7,
+ email: "[email protected]",
+ );
+ await photoBuildStarted.future;
+ currentUserID = 2;
+ releasePhotoBuild.complete();
+
+ expect(await contact, isNull);
+ expect(contactsService.autoCreateCalls, 0);
+ });
});
group("contact link conflict rules", () {
@@ -214,6 +320,8 @@ class _TestPersonService extends PersonService {
super.faceMLDataDB,
super.prefs, {
super.contactsService,
+ super.contactPhotoBuilder,
+ super.currentUserIDProvider,
});
@override
@@ -228,6 +336,7 @@ class _FakePhotosContactsService implements PhotosContactsService {
int createOrUpdateCalls = 0;
int? lastUpdatedContactUserId;
String? lastUpdatedName;
+ int autoCreateCalls = 0;
void seed(contacts.ContactRecord value) {
contact = value;
@@ -276,6 +385,29 @@ class _FakePhotosContactsService implements PhotosContactsService {
);
}
+ @override
+ Future<contacts.ContactRecord?> createContactWithProfilePictureIfAbsent({
+ required int contactUserId,
+ required String name,
+ required Uint8List bytes,
+ }) async {
+ autoCreateCalls++;
+ final existing = contact;
+ if (existing != null) {
+ return existing;
+ }
+ return contact = contacts.ContactRecord(
+ id: "contact-$contactUserId",
+ contactUserId: contactUserId,
+ email: "[email protected]",
+ data: contacts.ContactData(contactUserId: contactUserId, name: name),
+ profilePictureAttachmentId: "attachment-$contactUserId",
+ isDeleted: false,
+ createdAt: 1,
+ updatedAt: 2,
+ );
+ }
+
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}