π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Share generated Contacts values across native clients
Manav Rathife03f049ce8a
diff --git a/mobile/apps/locker/lib/services/contacts.dart b/mobile/apps/locker/lib/services/contacts.dartindex db8e887275..fb31eb8879 100644--- a/mobile/apps/locker/lib/services/contacts.dart+++ b/mobile/apps/locker/lib/services/contacts.dart@@ -1,137 +1,9 @@-import 'dart:typed_data';--import 'package:ente_contacts/contacts.dart';-import 'package:locker/src/rust/third_party/ente_frb_lib/contacts.dart' as rust;-import 'package:locker/src/rust/third_party/ente_frb_lib/session.dart';--Future<ContactOutput<ContactRecord>> createContact(- Session session,- WrappedRootContactKey? key,- ContactData data,-) async {- final output = await rust.createContact(- session: session,- wrappedRootContactKey: _toRustKey(key),- data: rust.ContactData(contactUserId: data.contactUserId, name: data.name),- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<ContactOutput<List<ContactRecord>>> getDiff(- Session session,- WrappedRootContactKey? key,- int sinceTime,- int limit,-) async {- final output = await rust.getDiff(- session: session,- wrappedRootContactKey: _toRustKey(key),- sinceTime: sinceTime,- limit: limit,- );- return _output(- output.records.map(_fromRustRecord).toList(growable: false),- output.wrappedRootContactKey,- );-}--Future<ContactOutput<ContactRecord>> updateContact(- Session session,- WrappedRootContactKey? key,- String contactId,- ContactData data,-) async {- final output = await rust.updateContact(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- data: rust.ContactData(contactUserId: data.contactUserId, name: data.name),- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<void> deleteContact(Session session, String contactId) =>- rust.deleteContact(session: session, contactId: contactId);--Future<ContactOutput<ContactRecord>> setAttachment(- Session session,- WrappedRootContactKey? key,- String contactId,- ContactAttachmentType attachmentType,- Uint8List bytes,-) async {- final output = await rust.setAttachment(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- attachmentType: _toRustAttachmentType(attachmentType),- attachmentBytes: bytes,- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<ContactOutput<ContactRecord>> deleteAttachment(- Session session,- WrappedRootContactKey? key,- String contactId,- ContactAttachmentType attachmentType,-) async {- final output = await rust.deleteAttachment(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- attachmentType: _toRustAttachmentType(attachmentType),- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<ContactOutput<Uint8List>> getProfilePicture(- Session session,- WrappedRootContactKey? key,- String contactId,-) async {- final output = await rust.getProfilePicture(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- );- return _output(output.bytes, output.wrappedRootContactKey);-}--ContactOutput<T> _output<T>(T value, rust.WrappedRootContactKey? key) =>- ContactOutput(value: value, wrappedRootContactKey: _fromRustKey(key));--rust.WrappedRootContactKey? _toRustKey(WrappedRootContactKey? key) =>- key == null- ? null- : rust.WrappedRootContactKey(- encryptedKey: key.encryptedKey,- header: key.header,- );--WrappedRootContactKey? _fromRustKey(rust.WrappedRootContactKey? key) =>- key == null- ? null- : WrappedRootContactKey(encryptedKey: key.encryptedKey, header: key.header);--rust.AttachmentType _toRustAttachmentType(- ContactAttachmentType attachmentType,-) => switch (attachmentType) {- ContactAttachmentType.profilePicture => rust.AttachmentType.profilePicture,-};--ContactRecord _fromRustRecord(rust.ContactRecord record) {- final data = record.isDeleted- ? null- : ContactData(contactUserId: record.contactUserId, name: record.name!);- return ContactRecord(- id: record.id,- contactUserId: record.contactUserId,- email: record.email,- data: data,- profilePictureAttachmentId: record.profilePictureAttachmentId,- isDeleted: record.isDeleted,- createdAt: record.createdAt,- updatedAt: record.updatedAt,- );-}+export 'package:locker/src/rust/third_party/ente_frb_lib/contacts.dart'+ show+ createContact,+ deleteAttachment,+ deleteContact,+ getDiff,+ getProfilePicture,+ setAttachment,+ updateContact;diff --git a/mobile/apps/locker/lib/services/contacts_display_service.dart b/mobile/apps/locker/lib/services/contacts_display_service.dartindex 86fe397fa7..a8ed2d7045 100644--- a/mobile/apps/locker/lib/services/contacts_display_service.dart+++ b/mobile/apps/locker/lib/services/contacts_display_service.dart@@ -17,18 +17,43 @@ class LockerContactsDisplayService {final session = authenticatedSession();return contacts.ContactsService(preferences: preferences,- createContact: (key, data) => createContact(session, key, data),- getDiff: (key, sinceTime, limit) =>- getDiff(session, key, sinceTime, limit),- updateContact: (key, contactId, data) =>- updateContact(session, key, contactId, data),- deleteContact: (contactId) => deleteContact(session, contactId),- setAttachment: (key, contactId, type, bytes) =>- setAttachment(session, key, contactId, type, bytes),- deleteAttachment: (key, contactId, type) =>- deleteAttachment(session, key, contactId, type),- getProfilePicture: (key, contactId) =>- getProfilePicture(session, key, contactId),+ createContact: (key, data) => createContact(+ session: session,+ wrappedRootContactKey: key,+ data: data,+ ),+ getDiff: (key, sinceTime, limit) => getDiff(+ session: session,+ wrappedRootContactKey: key,+ sinceTime: sinceTime,+ limit: limit,+ ),+ updateContact: (key, contactId, data) => updateContact(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ data: data,+ ),+ deleteContact: (contactId) =>+ deleteContact(session: session, contactId: contactId),+ setAttachment: (key, contactId, type, bytes) => setAttachment(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ attachmentType: type,+ attachmentBytes: bytes,+ ),+ deleteAttachment: (key, contactId, type) => deleteAttachment(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ attachmentType: type,+ ),+ getProfilePicture: (key, contactId) => getProfilePicture(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ ),);},);diff --git a/mobile/apps/locker/pubspec.yaml b/mobile/apps/locker/pubspec.yamlindex 5bb5aec1aa..44347654e1 100644--- a/mobile/apps/locker/pubspec.yaml+++ b/mobile/apps/locker/pubspec.yaml@@ -39,6 +39,8 @@ dependencies:path: ../../packages/eventsente_feature_flag:path: ../../packages/feature_flag+ ente_frb:+ path: ../../packages/frbente_icons:path: ../../packages/ente_iconsente_install_source:diff --git a/mobile/apps/photos/lib/services/contacts.dart b/mobile/apps/photos/lib/services/contacts.dartindex f144a5be26..a339607309 100644--- a/mobile/apps/photos/lib/services/contacts.dart+++ b/mobile/apps/photos/lib/services/contacts.dart@@ -1,137 +1,9 @@-import 'dart:typed_data';--import 'package:ente_contacts/contacts.dart';-import 'package:photos/src/rust/third_party/ente_frb_lib/contacts.dart' as rust;-import 'package:photos/src/rust/third_party/ente_frb_lib/session.dart';--Future<ContactOutput<ContactRecord>> createContact(- Session session,- WrappedRootContactKey? key,- ContactData data,-) async {- final output = await rust.createContact(- session: session,- wrappedRootContactKey: _toRustKey(key),- data: rust.ContactData(contactUserId: data.contactUserId, name: data.name),- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<ContactOutput<List<ContactRecord>>> getDiff(- Session session,- WrappedRootContactKey? key,- int sinceTime,- int limit,-) async {- final output = await rust.getDiff(- session: session,- wrappedRootContactKey: _toRustKey(key),- sinceTime: sinceTime,- limit: limit,- );- return _output(- output.records.map(_fromRustRecord).toList(growable: false),- output.wrappedRootContactKey,- );-}--Future<ContactOutput<ContactRecord>> updateContact(- Session session,- WrappedRootContactKey? key,- String contactId,- ContactData data,-) async {- final output = await rust.updateContact(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- data: rust.ContactData(contactUserId: data.contactUserId, name: data.name),- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<void> deleteContact(Session session, String contactId) =>- rust.deleteContact(session: session, contactId: contactId);--Future<ContactOutput<ContactRecord>> setAttachment(- Session session,- WrappedRootContactKey? key,- String contactId,- ContactAttachmentType attachmentType,- Uint8List bytes,-) async {- final output = await rust.setAttachment(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- attachmentType: _toRustAttachmentType(attachmentType),- attachmentBytes: bytes,- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<ContactOutput<ContactRecord>> deleteAttachment(- Session session,- WrappedRootContactKey? key,- String contactId,- ContactAttachmentType attachmentType,-) async {- final output = await rust.deleteAttachment(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- attachmentType: _toRustAttachmentType(attachmentType),- );- return _output(_fromRustRecord(output.record), output.wrappedRootContactKey);-}--Future<ContactOutput<Uint8List>> getProfilePicture(- Session session,- WrappedRootContactKey? key,- String contactId,-) async {- final output = await rust.getProfilePicture(- session: session,- wrappedRootContactKey: _toRustKey(key),- contactId: contactId,- );- return _output(output.bytes, output.wrappedRootContactKey);-}--ContactOutput<T> _output<T>(T value, rust.WrappedRootContactKey? key) =>- ContactOutput(value: value, wrappedRootContactKey: _fromRustKey(key));--rust.WrappedRootContactKey? _toRustKey(WrappedRootContactKey? key) =>- key == null- ? null- : rust.WrappedRootContactKey(- encryptedKey: key.encryptedKey,- header: key.header,- );--WrappedRootContactKey? _fromRustKey(rust.WrappedRootContactKey? key) =>- key == null- ? null- : WrappedRootContactKey(encryptedKey: key.encryptedKey, header: key.header);--rust.AttachmentType _toRustAttachmentType(- ContactAttachmentType attachmentType,-) => switch (attachmentType) {- ContactAttachmentType.profilePicture => rust.AttachmentType.profilePicture,-};--ContactRecord _fromRustRecord(rust.ContactRecord record) {- final data = record.isDeleted- ? null- : ContactData(contactUserId: record.contactUserId, name: record.name!);- return ContactRecord(- id: record.id,- contactUserId: record.contactUserId,- email: record.email,- data: data,- profilePictureAttachmentId: record.profilePictureAttachmentId,- isDeleted: record.isDeleted,- createdAt: record.createdAt,- updatedAt: record.updatedAt,- );-}+export 'package:photos/src/rust/third_party/ente_frb_lib/contacts.dart'+ show+ createContact,+ deleteAttachment,+ deleteContact,+ getDiff,+ getProfilePicture,+ setAttachment,+ updateContact;diff --git a/mobile/apps/photos/lib/services/contacts/contact_identity_resolver.dart b/mobile/apps/photos/lib/services/contacts/contact_identity_resolver.dartindex 4584da37ac..c64b1df5d8 100644--- a/mobile/apps/photos/lib/services/contacts/contact_identity_resolver.dart+++ b/mobile/apps/photos/lib/services/contacts/contact_identity_resolver.dart@@ -20,7 +20,7 @@ ResolvedUserIdentity resolveSuggestionIdentity(UserSuggestion suggestion) {knownContactEmailOrNull(contact?.email) ??knownContactEmailOrNull(suggestion.email);final resolvedDisplayName =- trimToNull(contact?.data?.name) ??+ trimToNull(contact?.name) ??trimToNull(suggestion.displayName) ??knownEmail;return (diff --git a/mobile/apps/photos/lib/services/photos_contacts_service.dart b/mobile/apps/photos/lib/services/photos_contacts_service.dartindex a126f5ea9e..8a76f94cec 100644--- a/mobile/apps/photos/lib/services/photos_contacts_service.dart+++ b/mobile/apps/photos/lib/services/photos_contacts_service.dart@@ -17,18 +17,43 @@ class PhotosContactsService {final session = authenticatedSession();return contacts.ContactsService(preferences: ServiceLocator.instance.prefs,- createContact: (key, data) => createContact(session, key, data),- getDiff: (key, sinceTime, limit) =>- getDiff(session, key, sinceTime, limit),- updateContact: (key, contactId, data) =>- updateContact(session, key, contactId, data),- deleteContact: (contactId) => deleteContact(session, contactId),- setAttachment: (key, contactId, type, bytes) =>- setAttachment(session, key, contactId, type, bytes),- deleteAttachment: (key, contactId, type) =>- deleteAttachment(session, key, contactId, type),- getProfilePicture: (key, contactId) =>- getProfilePicture(session, key, contactId),+ createContact: (key, data) => createContact(+ session: session,+ wrappedRootContactKey: key,+ data: data,+ ),+ getDiff: (key, sinceTime, limit) => getDiff(+ session: session,+ wrappedRootContactKey: key,+ sinceTime: sinceTime,+ limit: limit,+ ),+ updateContact: (key, contactId, data) => updateContact(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ data: data,+ ),+ deleteContact: (contactId) =>+ deleteContact(session: session, contactId: contactId),+ setAttachment: (key, contactId, type, bytes) => setAttachment(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ attachmentType: type,+ attachmentBytes: bytes,+ ),+ deleteAttachment: (key, contactId, type) => deleteAttachment(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ attachmentType: type,+ ),+ getProfilePicture: (key, contactId) => getProfilePicture(+ session: session,+ wrappedRootContactKey: key,+ contactId: contactId,+ ),);},onContactsChanged: _notifyContactsChanged,diff --git a/mobile/apps/photos/lib/ui/family/family_dashboard.dart b/mobile/apps/photos/lib/ui/family/family_dashboard.dartindex 91ff3b954a..231f8f324d 100644--- a/mobile/apps/photos/lib/ui/family/family_dashboard.dart+++ b/mobile/apps/photos/lib/ui/family/family_dashboard.dart@@ -183,7 +183,7 @@ class FamilyDashboard extends StatelessWidget {member.email.split('@').first;String? _savedNameFor(FamilyMember member) {- final savedName = contactsByUserId[member.userID]?.data?.name.trim();+ final savedName = contactsByUserId[member.userID]?.name?.trim();return savedName == null || savedName.isEmpty ? null : savedName;}diff --git a/mobile/apps/photos/lib/ui/family/family_plan_page.dart b/mobile/apps/photos/lib/ui/family/family_plan_page.dartindex ffda4f7c31..118625a203 100644--- a/mobile/apps/photos/lib/ui/family/family_plan_page.dart+++ b/mobile/apps/photos/lib/ui/family/family_plan_page.dart@@ -526,7 +526,7 @@ class _FamilyPlanPageState extends State<FamilyPlanPage> {return;}- final savedContactName = savedContact?.data?.name.trim();+ final savedContactName = savedContact?.name?.trim();final displayName = savedContactName == null || savedContactName.isEmpty? fallbackDisplayName: savedContactName;diff --git a/mobile/apps/photos/lib/ui/viewer/people/person_face_widget.dart b/mobile/apps/photos/lib/ui/viewer/people/person_face_widget.dartindex 73ea1cb103..507852edf5 100644--- a/mobile/apps/photos/lib/ui/viewer/people/person_face_widget.dart+++ b/mobile/apps/photos/lib/ui/viewer/people/person_face_widget.dart@@ -296,7 +296,7 @@ class _PersonFaceWidgetState extends State<PersonFaceWidget>email: personData.email,);_personIdentity = AvatarIdentity.account(- label: trimToNull(contact?.data?.name) ?? personData.name,+ label: trimToNull(contact?.name) ?? personData.name,email: trimToNull(contact?.email) ?? personData.email,userID: personData.userID,personID: personEntity.remoteID,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.dartindex 9ad73b27b8..38f3a4b9f8 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@@ -319,7 +319,7 @@ class _ContactResultPageState extends State<ContactResultPage> {}String get _savedContactDisplayName {- final savedName = _savedContact?.data?.name.trim();+ final savedName = _savedContact?.name?.trim();if (savedName != null && savedName.isNotEmpty) {return savedName;}diff --git a/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart b/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dartindex 673d6ce88b..b37347450b 100644--- a/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart+++ b/mobile/apps/photos/lib/ui/viewer/search/result/edit_contact_page.dart@@ -70,7 +70,7 @@ class _EditContactPageState extends State<EditContactPage> {void initState() {super.initState();_nameController =- TextEditingController(text: widget.existingContact?.data?.name ?? "")+ TextEditingController(text: widget.existingContact?.name ?? "")..addListener(() {if (mounted) {setState(() {});@@ -89,7 +89,7 @@ class _EditContactPageState extends State<EditContactPage> {}bool get _canSave => !_isSaving && _nameController.text.trim().isNotEmpty;- String get _initialName => (widget.existingContact?.data?.name ?? "").trim();+ String get _initialName => (widget.existingContact?.name ?? "").trim();bool get _hasUnsavedChanges =>_nameController.text.trim() != _initialName || _photoDirty || _linkDirty;bool get _hasLinkedPersonDraft =>diff --git a/mobile/apps/photos/pubspec.yaml b/mobile/apps/photos/pubspec.yamlindex f2758331e8..456516032b 100644--- a/mobile/apps/photos/pubspec.yaml+++ b/mobile/apps/photos/pubspec.yaml@@ -48,6 +48,8 @@ dependencies:path: ../../packages/ente_crypto_apiente_feature_flag:path: ../../packages/feature_flag+ ente_frb:+ path: ../../packages/frbente_icons:path: ../../packages/ente_iconsente_install_source: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.dartindex f3d5f43cb6..23696df4f1 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@@ -39,7 +39,7 @@ void main() {id: "contact-1",contactUserId: 3,email: "[email protected]",- data: contacts.ContactData(contactUserId: 3, name: "Old Contact"),+ name: "Old Contact",profilePictureAttachmentId: null,isDeleted: false,createdAt: 1,@@ -348,10 +348,7 @@ class _FakePhotosContactsService implements PhotosContactsService {id: saved?.id ?? "contact-$contactUserId",contactUserId: contactUserId,email: saved?.email,- data: contacts.ContactData(- contactUserId: contactUserId,- name: name.trim(),- ),+ name: name.trim(),profilePictureAttachmentId: saved?.profilePictureAttachmentId,isDeleted: false,createdAt: saved?.createdAt ?? 1,@@ -374,7 +371,7 @@ class _FakePhotosContactsService implements PhotosContactsService {id: "contact-$contactUserId",contactUserId: contactUserId,email: "[email protected]",- data: contacts.ContactData(contactUserId: contactUserId, name: name),+ name: name,profilePictureAttachmentId: "attachment-$contactUserId",isDeleted: false,createdAt: 1,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.dartindex 554d0cabaf..b10499eae9 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@@ -83,7 +83,7 @@ contacts.ContactRecord _savedContact({id: id,contactUserId: userID,email: email,- data: contacts.ContactData(contactUserId: userID, name: name),+ name: name,profilePictureAttachmentId: null,isDeleted: false,createdAt: 1,diff --git a/mobile/apps/photos/test/services/photos_contacts_service_test.dart b/mobile/apps/photos/test/services/photos_contacts_service_test.dartindex 1ebf874c27..87bd4ec8f9 100644--- a/mobile/apps/photos/test/services/photos_contacts_service_test.dart+++ b/mobile/apps/photos/test/services/photos_contacts_service_test.dart@@ -41,7 +41,7 @@ void main() {id: 'ct_1',contactUserId: 7,email: '[email protected]',- data: contacts.ContactData(contactUserId: 7, name: 'Alice'),+ name: 'Alice',profilePictureAttachmentId: null,isDeleted: false,createdAt: 1,@@ -63,7 +63,7 @@ void main() {id: 'ct_1',contactUserId: 7,email: '[email protected]',- data: contacts.ContactData(contactUserId: 7, name: 'Alice'),+ name: 'Alice',profilePictureAttachmentId: null,isDeleted: true,createdAt: 1,@@ -83,7 +83,7 @@ void main() {id: 'ct_old',contactUserId: 7,email: '[email protected]',- data: contacts.ContactData(contactUserId: 7, name: 'Alice'),+ name: 'Alice',profilePictureAttachmentId: null,isDeleted: false,createdAt: 1,@@ -97,7 +97,7 @@ void main() {id: 'ct_new',contactUserId: 9,email: '[email protected]',- data: contacts.ContactData(contactUserId: 9, name: 'Bob'),+ name: 'Bob',profilePictureAttachmentId: null,isDeleted: false,createdAt: 3,@@ -152,7 +152,7 @@ void main() {id: 'ct_1',contactUserId: 7,email: '[email protected]',- data: contacts.ContactData(contactUserId: 7, name: 'Alice'),+ name: 'Alice',profilePictureAttachmentId: 'att_1',isDeleted: false,createdAt: 1,diff --git a/mobile/apps/photos/test/ui/family/family_dashboard_test.dart b/mobile/apps/photos/test/ui/family/family_dashboard_test.dartindex 7c39f050fd..ee486c1e90 100644--- a/mobile/apps/photos/test/ui/family/family_dashboard_test.dart+++ b/mobile/apps/photos/test/ui/family/family_dashboard_test.dart@@ -220,7 +220,7 @@ contacts.ContactRecord _contact(FamilyMember member, {required String name}) {id: 'contact-${member.userID}',contactUserId: member.userID!,email: member.email,- data: contacts.ContactData(contactUserId: member.userID!, name: name),+ name: name,profilePictureAttachmentId: null,isDeleted: false,createdAt: 1,diff --git a/mobile/packages/contacts/lib/contacts.dart b/mobile/packages/contacts/lib/contacts.dartindex 0ff83ecd67..35677e03be 100644--- a/mobile/packages/contacts/lib/contacts.dart+++ b/mobile/packages/contacts/lib/contacts.dart@@ -1,9 +1,8 @@library;+export 'package:ente_frb/contacts.dart';+export 'src/db/contacts_database.dart';-export 'src/models/contact_data.dart';-export 'src/models/contact_output.dart';-export 'src/models/contact_record.dart';export 'src/service/contact_directory.dart';export 'src/service/contacts_display_service.dart';export 'src/service/contacts_service.dart';diff --git a/mobile/packages/contacts/lib/src/db/contacts_database.dart b/mobile/packages/contacts/lib/src/db/contacts_database.dartindex c740771a36..9784f4ddc0 100644--- a/mobile/packages/contacts/lib/src/db/contacts_database.dart+++ b/mobile/packages/contacts/lib/src/db/contacts_database.dart@@ -3,8 +3,7 @@ import 'dart:convert';import 'dart:io';import 'dart:typed_data';-import 'package:ente_contacts/src/models/contact_data.dart';-import 'package:ente_contacts/src/models/contact_record.dart';+import 'package:ente_frb/contacts.dart';import 'package:path/path.dart' as p;import 'package:path_provider/path_provider.dart';import 'package:sqflite_common_ffi/sqflite_ffi.dart';@@ -63,9 +62,12 @@ class ContactsDatabase {'id': contact.id,'contact_user_id': contact.contactUserId,'email': contact.email,- 'data_json': contact.data == null+ 'data_json': contact.name == null? null- : jsonEncode(contact.data!.toJson()),+ : jsonEncode({+ 'contactUserId': contact.contactUserId,+ 'name': contact.name,+ }),'profile_picture_attachment_id': contact.profilePictureAttachmentId,'is_deleted': contact.isDeleted ? 1 : 0,'created_at': contact.createdAt,@@ -295,9 +297,9 @@ class ContactsDatabase {id: row['id']! as String,contactUserId: row['contact_user_id']! as int,email: row['email'] as String?,- data: dataJson == null+ name: dataJson == null? null- : ContactData.fromJson(jsonDecode(dataJson) as Map<String, dynamic>),+ : (jsonDecode(dataJson) as Map<String, dynamic>)['name'] as String,profilePictureAttachmentId:row['profile_picture_attachment_id'] as String?,isDeleted: (row['is_deleted'] as int? ?? 0) == 1,diff --git a/mobile/packages/contacts/lib/src/models/contact_data.dart b/mobile/packages/contacts/lib/src/models/contact_data.dartdeleted file mode 100644index 1f6a43faf8..0000000000--- a/mobile/packages/contacts/lib/src/models/contact_data.dart+++ /dev/null@@ -1,25 +0,0 @@-import 'dart:convert';--class ContactData {- final int contactUserId;- final String name;-- const ContactData({required this.contactUserId, required this.name});-- Map<String, dynamic> toJson() => {- 'contactUserId': contactUserId,- 'name': name,- };-- factory ContactData.fromJson(Map<String, dynamic> json) {- return ContactData(- contactUserId: json['contactUserId'] as int,- name: json['name'] as String,- );- }-- String toEncodedJson() => jsonEncode(toJson());-- factory ContactData.fromEncodedJson(String jsonValue) =>- ContactData.fromJson(jsonDecode(jsonValue) as Map<String, dynamic>);-}diff --git a/mobile/packages/contacts/lib/src/models/contact_output.dart b/mobile/packages/contacts/lib/src/models/contact_output.dartdeleted file mode 100644index ad1efa02a0..0000000000--- a/mobile/packages/contacts/lib/src/models/contact_output.dart+++ /dev/null@@ -1,19 +0,0 @@-class WrappedRootContactKey {- final String encryptedKey;- final String header;-- const WrappedRootContactKey({- required this.encryptedKey,- required this.header,- });-}--class ContactOutput<T> {- final T value;- final WrappedRootContactKey? wrappedRootContactKey;-- const ContactOutput({- required this.value,- required this.wrappedRootContactKey,- });-}diff --git a/mobile/packages/contacts/lib/src/models/contact_record.dart b/mobile/packages/contacts/lib/src/models/contact_record.dartdeleted file mode 100644index ff82dff9a4..0000000000--- a/mobile/packages/contacts/lib/src/models/contact_record.dart+++ /dev/null@@ -1,23 +0,0 @@-import 'package:ente_contacts/src/models/contact_data.dart';--class ContactRecord {- final String id;- final int contactUserId;- final String? email;- final ContactData? data;- final String? profilePictureAttachmentId;- final bool isDeleted;- final int createdAt;- final int updatedAt;-- const ContactRecord({- required this.id,- required this.contactUserId,- required this.email,- required this.data,- required this.profilePictureAttachmentId,- required this.isDeleted,- required this.createdAt,- required this.updatedAt,- });-}diff --git a/mobile/packages/contacts/lib/src/service/contact_directory.dart b/mobile/packages/contacts/lib/src/service/contact_directory.dartindex 84e153f53f..96b959ddf0 100644--- a/mobile/packages/contacts/lib/src/service/contact_directory.dart+++ b/mobile/packages/contacts/lib/src/service/contact_directory.dart@@ -1,8 +1,7 @@import 'dart:async';-import 'package:ente_contacts/src/models/contact_data.dart';-import 'package:ente_contacts/src/models/contact_record.dart';import 'package:ente_contacts/src/service/contacts_service.dart';+import 'package:ente_frb/contacts.dart';import 'package:flutter/foundation.dart';import 'package:logging/logging.dart';@@ -92,10 +91,7 @@ class ContactDirectory {String? getCachedSavedName({int? contactUserId, String? email}) =>_trimToNull(- getCachedContact(- contactUserId: contactUserId,- email: email,- )?.data?.name,+ getCachedContact(contactUserId: contactUserId, email: email)?.name,);String? getCachedResolvedEmail({int? contactUserId, String? email}) =>@@ -372,7 +368,7 @@ class ContactDirectory {a.updatedAt == b.updatedAt &&a.isDeleted == b.isDeleted &&a.email == b.email &&- a.data?.name == b.data?.name &&+ a.name == b.name &&a.profilePictureAttachmentId == b.profilePictureAttachmentId;void _cacheAndNotify(ContactRecord contact) {diff --git a/mobile/packages/contacts/lib/src/service/contacts_display_service.dart b/mobile/packages/contacts/lib/src/service/contacts_display_service.dartindex 2e6a7f6e8d..7ba31bcea2 100644--- a/mobile/packages/contacts/lib/src/service/contacts_display_service.dart+++ b/mobile/packages/contacts/lib/src/service/contacts_display_service.dart@@ -1,6 +1,6 @@-import 'package:ente_contacts/src/models/contact_record.dart';import 'package:ente_contacts/src/service/contact_directory.dart';import 'package:ente_contacts/src/service/contacts_service.dart';+import 'package:ente_frb/contacts.dart';import 'package:flutter/foundation.dart';class ContactsDisplayService {diff --git a/mobile/packages/contacts/lib/src/service/contacts_service.dart b/mobile/packages/contacts/lib/src/service/contacts_service.dartindex ac5fe2dbc0..bd19c8e044 100644--- a/mobile/packages/contacts/lib/src/service/contacts_service.dart+++ b/mobile/packages/contacts/lib/src/service/contacts_service.dart@@ -2,47 +2,43 @@ import 'dart:math';import 'dart:typed_data';import 'package:ente_contacts/src/db/contacts_database.dart';-import 'package:ente_contacts/src/models/contact_data.dart';-import 'package:ente_contacts/src/models/contact_output.dart';-import 'package:ente_contacts/src/models/contact_record.dart';+import 'package:ente_frb/contacts.dart';import 'package:logging/logging.dart';import 'package:shared_preferences/shared_preferences.dart';-enum ContactAttachmentType { profilePicture }-typedef CreateContact =- Future<ContactOutput<ContactRecord>> Function(+ Future<ContactRecordOutput> Function(WrappedRootContactKey? wrappedRootContactKey,ContactData data,);typedef GetContactDiff =- Future<ContactOutput<List<ContactRecord>>> Function(+ Future<ContactDiffOutput> Function(WrappedRootContactKey? wrappedRootContactKey,int sinceTime,int limit,);typedef UpdateContact =- Future<ContactOutput<ContactRecord>> Function(+ Future<ContactRecordOutput> Function(WrappedRootContactKey? wrappedRootContactKey,String contactId,ContactData data,);typedef DeleteContact = Future<void> Function(String contactId);typedef SetContactAttachment =- Future<ContactOutput<ContactRecord>> Function(+ Future<ContactRecordOutput> Function(WrappedRootContactKey? wrappedRootContactKey,String contactId,- ContactAttachmentType attachmentType,+ AttachmentType attachmentType,Uint8List attachmentBytes,);typedef DeleteContactAttachment =- Future<ContactOutput<ContactRecord>> Function(+ Future<ContactRecordOutput> Function(WrappedRootContactKey? wrappedRootContactKey,String contactId,- ContactAttachmentType attachmentType,+ AttachmentType attachmentType,);typedef GetContactProfilePicture =- Future<ContactOutput<Uint8List>> Function(+ Future<ProfilePictureOutput> Function(WrappedRootContactKey? wrappedRootContactKey,String contactId,);@@ -101,9 +97,13 @@ class ContactsService {var previousSinceTime = -1;List<String>? previousPageIds;while (true) {- final diff = await _value(- await _getRemoteDiff(_wrappedRootContactKey, sinceTime, limit),+ final output = await _getRemoteDiff(+ _wrappedRootContactKey,+ sinceTime,+ limit,);+ await _saveWrappedRootContactKey(output.wrappedRootContactKey);+ final diff = output.records;if (diff.isEmpty) {break;}@@ -159,9 +159,9 @@ class ContactsService {Future<ContactRecord> createContact(ContactData data) async {_requireOpen();- final created = await _value(- await _createRemoteContact(_wrappedRootContactKey, data),- );+ final output = await _createRemoteContact(_wrappedRootContactKey, data);+ await _saveWrappedRootContactKey(output.wrappedRootContactKey);+ final created = output.record;await _database.upsertContacts([created]);return created;}@@ -171,9 +171,13 @@ class ContactsService {ContactData data,) async {_requireOpen();- final updated = await _value(- await _updateRemoteContact(_wrappedRootContactKey, contactId, data),+ final output = await _updateRemoteContact(+ _wrappedRootContactKey,+ contactId,+ data,);+ await _saveWrappedRootContactKey(output.wrappedRootContactKey);+ final updated = output.record;await _database.upsertContacts([updated]);return updated;}@@ -181,9 +185,9 @@ class ContactsService {Future<void> deleteContact(String contactId) async {_requireOpen();await _deleteRemoteContact(contactId);- final deleted = await _value(- await _getRemoteDiff(_wrappedRootContactKey, 0, _syncLimit),- );+ final output = await _getRemoteDiff(_wrappedRootContactKey, 0, _syncLimit);+ await _saveWrappedRootContactKey(output.wrappedRootContactKey);+ final deleted = output.records;final matching = deleted.where((element) => element.id == contactId).toList();@@ -195,11 +199,7 @@ class ContactsService {}Future<ContactRecord> setProfilePicture(String contactId, Uint8List bytes) {- return _setAttachment(- contactId,- ContactAttachmentType.profilePicture,- bytes,- );+ return _setAttachment(contactId, AttachmentType.profilePicture, bytes);}Future<Uint8List> getProfilePicture(String contactId) {@@ -207,26 +207,26 @@ class ContactsService {}Future<ContactRecord> deleteProfilePicture(String contactId) {- return _deleteAttachment(contactId, ContactAttachmentType.profilePicture);+ return _deleteAttachment(contactId, AttachmentType.profilePicture);}Future<ContactRecord> _setAttachment(String contactId,- ContactAttachmentType attachmentType,+ AttachmentType attachmentType,Uint8List bytes,) async {final previousAttachmentId = (await _database.getContact(contactId,))?.profilePictureAttachmentId;_requireOpen();- final updated = await _value(- await _setRemoteAttachment(- _wrappedRootContactKey,- contactId,- attachmentType,- bytes,- ),+ final output = await _setRemoteAttachment(+ _wrappedRootContactKey,+ contactId,+ attachmentType,+ bytes,);+ await _saveWrappedRootContactKey(output.wrappedRootContactKey);+ final updated = output.record;await _database.upsertContacts([updated]);final nextAttachmentId = updated.profilePictureAttachmentId;if (nextAttachmentId != null) {@@ -250,28 +250,31 @@ class ContactsService {return cached;}_requireOpen();- final bytes = await _value(- await _getRemoteProfilePicture(_wrappedRootContactKey, contactId),+ final output = await _getRemoteProfilePicture(+ _wrappedRootContactKey,+ contactId,);+ await _saveWrappedRootContactKey(output.wrappedRootContactKey);+ final bytes = output.bytes;await _database.upsertCachedAttachment(attachmentId, bytes);return bytes;}Future<ContactRecord> _deleteAttachment(String contactId,- ContactAttachmentType attachmentType,+ AttachmentType attachmentType,) async {final previousAttachmentId = (await _database.getContact(contactId,))?.profilePictureAttachmentId;_requireOpen();- final updated = await _value(- await _deleteRemoteAttachment(- _wrappedRootContactKey,- contactId,- attachmentType,- ),+ final output = await _deleteRemoteAttachment(+ _wrappedRootContactKey,+ contactId,+ attachmentType,);+ await _saveWrappedRootContactKey(output.wrappedRootContactKey);+ final updated = output.record;await _database.upsertContacts([updated]);if (previousAttachmentId != null) {await _database.deleteCachedAttachment(previousAttachmentId);@@ -319,24 +322,15 @@ class ContactsService {return WrappedRootContactKey(encryptedKey: encryptedKey, header: header);}- Future<void> _persistWrappedRootContactKey(- int userId,- WrappedRootContactKey key,- ) async {+ Future<void> _saveWrappedRootContactKey(WrappedRootContactKey? key) async {+ final userId = _userId;+ if (userId == null || key == null) return;++ _wrappedRootContactKey = key;await _preferences.setString(_entityKeyPref(userId), key.encryptedKey);await _preferences.setString(_entityHeaderPref(userId), key.header);}- Future<T> _value<T>(ContactOutput<T> output) async {- final userId = _userId;- final key = output.wrappedRootContactKey;- if (userId != null && key != null) {- _wrappedRootContactKey = key;- await _persistWrappedRootContactKey(userId, key);- }- return output.value;- }-String _entityKeyPref(int userId) => 'entity_key_contact_$userId';String _entityHeaderPref(int userId) => 'entity_key_header_contact_$userId';diff --git a/mobile/packages/contacts/pubspec.yaml b/mobile/packages/contacts/pubspec.yamlindex 43ed7db54e..688b542687 100644--- a/mobile/packages/contacts/pubspec.yaml+++ b/mobile/packages/contacts/pubspec.yaml@@ -7,6 +7,8 @@ environment:flutter: ">=1.17.0"dependencies:+ ente_frb:+ path: ../frbflutter:sdk: flutterlogging: 1.3.0diff --git a/mobile/packages/contacts/test/contact_data_test.dart b/mobile/packages/contacts/test/contact_data_test.dartdeleted file mode 100644index c490e86910..0000000000--- a/mobile/packages/contacts/test/contact_data_test.dart+++ /dev/null@@ -1,14 +0,0 @@-import 'package:ente_contacts/contacts.dart';-import 'package:flutter_test/flutter_test.dart';--void main() {- test('ignores legacy birth date', () {- final data = ContactData.fromJson({- 'contactUserId': 2,- 'name': 'B',- 'birthDate': '2001-04-02',- });-- expect(data.toJson(), {'contactUserId': 2, 'name': 'B'});- });-}diff --git a/mobile/packages/contacts/test/contacts_database_test.dart b/mobile/packages/contacts/test/contacts_database_test.dartindex 686b6006a0..c6b72c6fb0 100644--- a/mobile/packages/contacts/test/contacts_database_test.dart+++ b/mobile/packages/contacts/test/contacts_database_test.dart@@ -31,7 +31,7 @@ void main() {id: 'ct_1',contactUserId: 200,email: '[email protected]',- data: ContactData(contactUserId: 200, name: 'A'),+ name: 'A',profilePictureAttachmentId: null,isDeleted: false,createdAt: 10,@@ -50,6 +50,56 @@ void main() {expect((await database.getContactByUserId(200))?.id, 'ct_1');});+ test('reads and updates contacts stored by older clients', () async {+ final oldDatabase = await databaseFactoryFfi.openDatabase(+ '${tempDir.path}/ente.contacts.1.db',+ options: OpenDatabaseOptions(+ version: 1,+ onCreate: (db, _) => db.execute('''+ CREATE TABLE contacts (+ id TEXT PRIMARY KEY,+ contact_user_id INTEGER NOT NULL,+ email TEXT,+ data_json TEXT,+ profile_picture_attachment_id TEXT,+ is_deleted INTEGER NOT NULL DEFAULT 0,+ created_at INTEGER NOT NULL,+ updated_at INTEGER NOT NULL+ )+ '''),+ ),+ );+ await oldDatabase.insert('contacts', {+ 'id': 'ct_old',+ 'contact_user_id': 200,+ 'email': '[email protected]',+ 'data_json': '{"contactUserId":200,"name":"A","birthDate":"2001-04-02"}',+ 'profile_picture_attachment_id': 'att_old',+ 'is_deleted': 0,+ 'created_at': 10,+ 'updated_at': 20,+ });+ await oldDatabase.close();++ await database.configure(userId: 1);+ final contact = (await database.getContact('ct_old'))!;+ expect(contact.name, 'A');+ expect(contact.profilePictureAttachmentId, 'att_old');+ await database.upsertContacts([contact]);++ final rows = await (await database.database).query('contacts');+ expect(rows.single, {+ 'id': 'ct_old',+ 'contact_user_id': 200,+ 'email': '[email protected]',+ 'data_json': '{"contactUserId":200,"name":"A"}',+ 'profile_picture_attachment_id': 'att_old',+ 'is_deleted': 0,+ 'created_at': 10,+ 'updated_at': 20,+ });+ });+test('clearTable removes all contacts databases', () async {await database.configure(userId: 1);await database.upsertContacts([@@ -57,7 +107,7 @@ void main() {id: 'ct_1',contactUserId: 200,email: '[email protected]',- data: ContactData(contactUserId: 200, name: 'A'),+ name: 'A',profilePictureAttachmentId: null,isDeleted: false,createdAt: 10,@@ -70,7 +120,7 @@ void main() {id: 'ct_2',contactUserId: 201,email: '[email protected]',- data: ContactData(contactUserId: 201, name: 'B'),+ name: 'B',profilePictureAttachmentId: null,isDeleted: false,createdAt: 11,@@ -95,7 +145,7 @@ void main() {id: 'ct_1',contactUserId: 200,email: '[email protected]',- data: ContactData(contactUserId: 200, name: 'A'),+ name: 'A',profilePictureAttachmentId: 'att_keep',isDeleted: false,createdAt: 10,diff --git a/mobile/packages/contacts/test/contacts_display_service_test.dart b/mobile/packages/contacts/test/contacts_display_service_test.dartindex efa07f734d..49c4a59348 100644--- a/mobile/packages/contacts/test/contacts_display_service_test.dart+++ b/mobile/packages/contacts/test/contacts_display_service_test.dart@@ -256,7 +256,7 @@ ContactRecord _contact({id: id,contactUserId: userID,email: email,- data: ContactData(contactUserId: userID, name: name),+ name: name,profilePictureAttachmentId: attachmentID,isDeleted: false,createdAt: 1,@@ -264,6 +264,11 @@ ContactRecord _contact({);class FakeContacts {+ static const _key = WrappedRootContactKey(+ encryptedKey: 'enc-key',+ header: 'enc-header',+ );+final Map<String, Uint8List> profilePictureBytesByContactId = {};List<List<ContactRecord>> diffPages = [];int getProfilePictureCalls = 0;@@ -289,25 +294,26 @@ class FakeContacts {deleteAttachment: (_, _, _) => throw UnimplementedError(),);- Future<ContactOutput<ContactRecord>> createContact(+ Future<ContactRecordOutput> createContact(WrappedRootContactKey? wrappedRootContactKey,ContactData data,) async {- return _output(- ContactRecord(+ return ContactRecordOutput(+ record: ContactRecord(id: 'ct_created',contactUserId: data.contactUserId,email: '[email protected]',- data: data,+ name: data.name,profilePictureAttachmentId: null,isDeleted: false,createdAt: 1,updatedAt: 1,),+ wrappedRootContactKey: _key,);}- Future<ContactOutput<List<ContactRecord>>> getDiff(+ Future<ContactDiffOutput> getDiff(WrappedRootContactKey? wrappedRootContactKey,int sinceTime,int limit,@@ -324,14 +330,14 @@ class FakeContacts {throw error;}if (diffPages.isEmpty) {- return _output(const []);+ return const ContactDiffOutput(records: [], wrappedRootContactKey: _key);}final first = diffPages.first;diffPages = diffPages.sublist(1);- return _output(first);+ return ContactDiffOutput(records: first, wrappedRootContactKey: _key);}- Future<ContactOutput<Uint8List>> getProfilePicture(+ Future<ProfilePictureOutput> getProfilePicture(WrappedRootContactKey? wrappedRootContactKey,String contactId,) async {@@ -344,14 +350,9 @@ class FakeContacts {if (error != null) {throw error;}- return _output(profilePictureBytesByContactId[contactId]!);+ return ProfilePictureOutput(+ bytes: profilePictureBytesByContactId[contactId]!,+ wrappedRootContactKey: _key,+ );}-- ContactOutput<T> _output<T>(T value) => ContactOutput(- value: value,- wrappedRootContactKey: const WrappedRootContactKey(- encryptedKey: 'enc-key',- header: 'enc-header',- ),- );}diff --git a/mobile/packages/contacts/test/contacts_service_test.dart b/mobile/packages/contacts/test/contacts_service_test.dartindex 477e39817c..e29c73d150 100644--- a/mobile/packages/contacts/test/contacts_service_test.dart+++ b/mobile/packages/contacts/test/contacts_service_test.dart@@ -54,7 +54,7 @@ void main() {id: 'ct_1',contactUserId: 2,email: '[email protected]',- data: ContactData(contactUserId: 2, name: 'B'),+ name: 'B',profilePictureAttachmentId: 'att_1',isDeleted: false,createdAt: 10,@@ -87,7 +87,7 @@ void main() {id: 'ct_1',contactUserId: 2,email: '[email protected]',- data: ContactData(contactUserId: 2, name: 'B'),+ name: 'B',profilePictureAttachmentId: null,isDeleted: false,createdAt: 10,@@ -121,7 +121,7 @@ void main() {);expect(preferences.getString('entity_key_contact_1'), 'enc-key');expect(preferences.getString('entity_key_header_contact_1'), 'enc-header');- expect((await service.getContact(created.id))!.data!.name, 'B');+ expect((await service.getContact(created.id))!.name, 'B');expect((await service.getContactByUserId(2))!.id, created.id);final updated = await service.setProfilePicture(@@ -153,7 +153,7 @@ void main() {id: 'ct_1',contactUserId: 2,email: '[email protected]',- data: ContactData(contactUserId: 2, name: 'B'),+ name: 'B',profilePictureAttachmentId: 'att_1',isDeleted: false,createdAt: 10,@@ -219,7 +219,7 @@ void main() {id: 'ct_1',contactUserId: 2,email: '[email protected]',- data: ContactData(contactUserId: 2, name: 'B'),+ name: 'B',profilePictureAttachmentId: 'att_keep',isDeleted: false,createdAt: 10,@@ -256,7 +256,7 @@ void main() {id: 'ct_$i',contactUserId: i + 2,email: '[email protected]',- data: ContactData(contactUserId: i + 2, name: 'User $i'),+ name: 'User $i',profilePictureAttachmentId: null,isDeleted: false,createdAt: 10,@@ -285,6 +285,11 @@ void main() {}class FakeContacts {+ static const _key = WrappedRootContactKey(+ encryptedKey: 'enc-key',+ header: 'enc-header',+ );+WrappedRootContactKey? lastWrappedRootContactKey;final Map<String, ContactRecord> records = {};final Map<String, Uint8List> attachments = {};@@ -296,7 +301,7 @@ class FakeContacts {int getProfilePictureCalls = 0;String nextAttachmentId = 'att_profile';- Future<ContactOutput<ContactRecord>> createContact(+ Future<ContactRecordOutput> createContact(WrappedRootContactKey? wrappedRootContactKey,ContactData data,) async {@@ -304,14 +309,14 @@ class FakeContacts {id: 'ct_created',contactUserId: data.contactUserId,email: '[email protected]',- data: data,+ name: data.name,profilePictureAttachmentId: null,isDeleted: false,createdAt: 1,updatedAt: 1,);records[record.id] = record;- return _output(record);+ return ContactRecordOutput(record: record, wrappedRootContactKey: _key);}Future<void> deleteContact(String contactId) async {@@ -321,7 +326,7 @@ class FakeContacts {id: existing.id,contactUserId: existing.contactUserId,email: existing.email,- data: null,+ name: null,profilePictureAttachmentId: null,isDeleted: true,createdAt: existing.createdAt,@@ -330,17 +335,17 @@ class FakeContacts {}}- Future<ContactOutput<ContactRecord>> deleteAttachment(+ Future<ContactRecordOutput> deleteAttachment(WrappedRootContactKey? wrappedRootContactKey,String contactId,- ContactAttachmentType attachmentType,+ AttachmentType attachmentType,) async {final existing = records[contactId]!;final updated = ContactRecord(id: existing.id,contactUserId: existing.contactUserId,email: existing.email,- data: existing.data,+ name: existing.name,profilePictureAttachmentId: null,isDeleted: existing.isDeleted,createdAt: existing.createdAt,@@ -351,10 +356,10 @@ class FakeContacts {if (previousAttachmentId != null) {attachments.remove(previousAttachmentId);}- return _output(updated);+ return ContactRecordOutput(record: updated, wrappedRootContactKey: _key);}- Future<ContactOutput<List<ContactRecord>>> getDiff(+ Future<ContactDiffOutput> getDiff(WrappedRootContactKey? wrappedRootContactKey,int sinceTime,int limit,@@ -368,36 +373,39 @@ class FakeContacts {for (final record in page) {records[record.id] = record;}- return _output(page);+ return ContactDiffOutput(records: page, wrappedRootContactKey: _key);}if (diffPages.isEmpty) {- return _output(const []);+ return const ContactDiffOutput(records: [], wrappedRootContactKey: _key);}final first = diffPages.first;diffPages = diffPages.sublist(1);for (final record in first) {records[record.id] = record;}- return _output(first);+ return ContactDiffOutput(records: first, wrappedRootContactKey: _key);}- Future<ContactOutput<Uint8List>> getProfilePicture(+ Future<ProfilePictureOutput> getProfilePicture(WrappedRootContactKey? wrappedRootContactKey,String contactId,) async {getProfilePictureCalls += 1;final picture = profilePictures[contactId];if (picture != null) {- return _output(picture);+ return ProfilePictureOutput(bytes: picture, wrappedRootContactKey: _key);}final attachmentId = records[contactId]!.profilePictureAttachmentId!;- return _output(attachments[attachmentId]!);+ return ProfilePictureOutput(+ bytes: attachments[attachmentId]!,+ wrappedRootContactKey: _key,+ );}- Future<ContactOutput<ContactRecord>> setAttachment(+ Future<ContactRecordOutput> setAttachment(WrappedRootContactKey? wrappedRootContactKey,String contactId,- ContactAttachmentType attachmentType,+ AttachmentType attachmentType,Uint8List attachmentBytes,) async {final existing = records[contactId]!;@@ -405,7 +413,7 @@ class FakeContacts {id: existing.id,contactUserId: existing.contactUserId,email: existing.email,- data: existing.data,+ name: existing.name,profilePictureAttachmentId: nextAttachmentId,isDeleted: existing.isDeleted,createdAt: existing.createdAt,@@ -414,10 +422,10 @@ class FakeContacts {records[contactId] = updated;attachments[nextAttachmentId] = attachmentBytes;profilePictures[contactId] = attachmentBytes;- return _output(updated);+ return ContactRecordOutput(record: updated, wrappedRootContactKey: _key);}- Future<ContactOutput<ContactRecord>> updateContact(+ Future<ContactRecordOutput> updateContact(WrappedRootContactKey? wrappedRootContactKey,String contactId,ContactData data,@@ -427,21 +435,13 @@ class FakeContacts {id: existing.id,contactUserId: existing.contactUserId,email: existing.email,- data: data,+ name: data.name,profilePictureAttachmentId: existing.profilePictureAttachmentId,isDeleted: existing.isDeleted,createdAt: existing.createdAt,updatedAt: existing.updatedAt + 1,);records[contactId] = updated;- return _output(updated);+ return ContactRecordOutput(record: updated, wrappedRootContactKey: _key);}-- ContactOutput<T> _output<T>(T value) => ContactOutput(- value: value,- wrappedRootContactKey: const WrappedRootContactKey(- encryptedKey: 'enc-key',- header: 'enc-header',- ),- );}diff --git a/mobile/packages/frb/lib/.gitignore b/mobile/packages/frb/lib/.gitignorenew file mode 100644index 0000000000..d6b7ef32c8--- /dev/null+++ b/mobile/packages/frb/lib/.gitignore@@ -0,0 +1,2 @@+*+!.gitignorediff --git a/mobile/packages/frb/pubspec.yaml b/mobile/packages/frb/pubspec.yamlnew file mode 100644index 0000000000..b1b0feccb1--- /dev/null+++ b/mobile/packages/frb/pubspec.yaml@@ -0,0 +1,10 @@+name: ente_frb+description: Shared generated Dart value types for Ente native bindings+publish_to: none++resolution: workspace+environment:+ sdk: ">=3.10.0 <4.0.0"++dependencies:+ flutter_rust_bridge: 2.12.0diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yamlindex 27340720da..e1c82ea422 100644--- a/mobile/pubspec.yaml+++ b/mobile/pubspec.yaml@@ -30,6 +30,7 @@ workspace:- packages/ente_screen_brightness- packages/events- packages/feature_flag+ - packages/frb- packages/legacy- packages/local_auth_linux- packages/lock_screendiff --git a/rust/apps/codegen/src/main.rs b/rust/apps/codegen/src/main.rsindex 69ae7f0791..8302c9adbc 100644--- a/rust/apps/codegen/src/main.rs+++ b/rust/apps/codegen/src/main.rs@@ -164,11 +164,34 @@ fn generate_frb(target: FrbTarget) -> Result<(), DynError> {.parent().ok_or("failed to resolve repo root from rust/apps/codegen")?;- if matches!(target, FrbTarget::All | FrbTarget::Photos) {- generate_frb_package(&repo_root.join("mobile/apps/photos"))?;- }- if matches!(target, FrbTarget::All | FrbTarget::Locker) {- generate_frb_package(&repo_root.join("mobile/apps/locker"))?;+ let apps: &[&str] = match target {+ FrbTarget::All => &["photos", "locker"],+ FrbTarget::Photos => &["photos"],+ FrbTarget::Locker => &["locker"],+ };+ let shared_dir = repo_root.join("mobile/packages/frb/lib");+ write_generated_gitignore(&shared_dir)?;+ let mut shared_types = None;+ for app in apps {+ let package_dir = repo_root.join("mobile/apps").join(app);+ generate_frb_package(&package_dir)?;+ let types_path =+ package_dir.join("lib/src/rust/third_party/ente_frb_lib/contacts/types.dart");+ let types =+ fs::read_to_string(&types_path)?.replace("import '../../../frb_generated.dart';\n", "");+ if shared_types+ .as_ref()+ .is_some_and(|previous| previous != &types)+ {+ return Err("Photos and Locker generated different shared Contacts types".into());+ }+ fs::write(shared_dir.join("contacts.dart"), &types)?;+ fs::write(+ types_path,+ "// This file is automatically generated, so please do not edit it.\n\n\+ export 'package:ente_frb/contacts.dart';\n",+ )?;+ shared_types = Some(types);}format_frb_bindings(target)}diff --git a/rust/bindings/frb/lib/src/contacts.rs b/rust/bindings/frb/lib/src/contacts.rsindex 0712011a82..74a34553a3 100644--- a/rust/bindings/frb/lib/src/contacts.rs+++ b/rust/bindings/frb/lib/src/contacts.rs@@ -2,6 +2,12 @@ use flutter_rust_bridge::frb;use crate::session::Session;+pub mod types;+pub use types::{+ AttachmentType, ContactData, ContactDiffOutput, ContactRecord, ContactRecordOutput,+ ProfilePictureOutput, WrappedRootContactKey,+};+#[frb(non_opaque)]pub enum ContactsError {Other { message: String },@@ -15,107 +21,6 @@ impl From<ente_contacts::Error> for ContactsError {}}-#[frb]-#[derive(Clone)]-pub struct WrappedRootContactKey {- pub encrypted_key: String,- pub header: String,-}--impl From<ente_contacts::WrappedRootContactKey> for WrappedRootContactKey {- fn from(value: ente_contacts::WrappedRootContactKey) -> Self {- Self {- encrypted_key: value.encrypted_key,- header: value.header,- }- }-}--impl From<WrappedRootContactKey> for ente_contacts::WrappedRootContactKey {- fn from(value: WrappedRootContactKey) -> Self {- Self {- encrypted_key: value.encrypted_key,- header: value.header,- }- }-}--#[frb]-#[derive(Clone)]-pub struct ContactData {- pub contact_user_id: i64,- pub name: String,-}--impl From<ContactData> for ente_contacts::ContactData {- fn from(value: ContactData) -> Self {- Self {- contact_user_id: value.contact_user_id,- name: value.name,- }- }-}--#[frb]-#[derive(Clone)]-pub struct ContactRecord {- pub id: String,- pub contact_user_id: i64,- pub email: Option<String>,- pub name: Option<String>,- pub profile_picture_attachment_id: Option<String>,- pub is_deleted: bool,- pub created_at: i64,- pub updated_at: i64,-}--impl From<ente_contacts::ContactRecord> for ContactRecord {- fn from(value: ente_contacts::ContactRecord) -> Self {- Self {- id: value.id,- contact_user_id: value.contact_user_id,- email: value.email,- name: value.name,- profile_picture_attachment_id: value.profile_picture_attachment_id,- is_deleted: value.is_deleted,- created_at: value.created_at,- updated_at: value.updated_at,- }- }-}--#[frb]-#[derive(Clone, Copy)]-pub enum AttachmentType {- ProfilePicture,-}--impl From<AttachmentType> for ente_contacts::AttachmentType {- fn from(value: AttachmentType) -> Self {- match value {- AttachmentType::ProfilePicture => ente_contacts::AttachmentType::ProfilePicture,- }- }-}--#[frb]-pub struct ContactRecordOutput {- pub record: ContactRecord,- pub wrapped_root_contact_key: Option<WrappedRootContactKey>,-}--#[frb]-pub struct ContactDiffOutput {- pub records: Vec<ContactRecord>,- pub wrapped_root_contact_key: Option<WrappedRootContactKey>,-}--#[frb]-pub struct ProfilePictureOutput {- pub bytes: Vec<u8>,- pub wrapped_root_contact_key: Option<WrappedRootContactKey>,-}-pub async fn create_contact(session: &Session,wrapped_root_contact_key: Option<WrappedRootContactKey>,diff --git a/rust/bindings/frb/lib/src/contacts/types.rs b/rust/bindings/frb/lib/src/contacts/types.rsnew file mode 100644index 0000000000..264fd4b040--- /dev/null+++ b/rust/bindings/frb/lib/src/contacts/types.rs@@ -0,0 +1,102 @@+use flutter_rust_bridge::frb;++#[frb(unignore)]+#[derive(Clone)]+pub struct ContactData {+ pub contact_user_id: i64,+ pub name: String,+}++impl From<ContactData> for ente_contacts::ContactData {+ fn from(value: ContactData) -> Self {+ Self {+ contact_user_id: value.contact_user_id,+ name: value.name,+ }+ }+}++#[frb(unignore)]+#[derive(Clone)]+pub struct ContactRecord {+ pub id: String,+ pub contact_user_id: i64,+ pub email: Option<String>,+ pub name: Option<String>,+ pub profile_picture_attachment_id: Option<String>,+ pub is_deleted: bool,+ pub created_at: i64,+ pub updated_at: i64,+}++impl From<ente_contacts::ContactRecord> for ContactRecord {+ fn from(value: ente_contacts::ContactRecord) -> Self {+ Self {+ id: value.id,+ contact_user_id: value.contact_user_id,+ email: value.email,+ name: value.name,+ profile_picture_attachment_id: value.profile_picture_attachment_id,+ is_deleted: value.is_deleted,+ created_at: value.created_at,+ updated_at: value.updated_at,+ }+ }+}++#[frb(unignore)]+#[derive(Clone, Copy)]+pub enum AttachmentType {+ ProfilePicture,+}++impl From<AttachmentType> for ente_contacts::AttachmentType {+ fn from(value: AttachmentType) -> Self {+ match value {+ AttachmentType::ProfilePicture => ente_contacts::AttachmentType::ProfilePicture,+ }+ }+}++#[frb(unignore)]+#[derive(Clone)]+pub struct WrappedRootContactKey {+ pub encrypted_key: String,+ pub header: String,+}++impl From<ente_contacts::WrappedRootContactKey> for WrappedRootContactKey {+ fn from(value: ente_contacts::WrappedRootContactKey) -> Self {+ Self {+ encrypted_key: value.encrypted_key,+ header: value.header,+ }+ }+}++impl From<WrappedRootContactKey> for ente_contacts::WrappedRootContactKey {+ fn from(value: WrappedRootContactKey) -> Self {+ Self {+ encrypted_key: value.encrypted_key,+ header: value.header,+ }+ }+}++#[frb(unignore)]+pub struct ContactRecordOutput {+ pub record: ContactRecord,+ pub wrapped_root_contact_key: Option<WrappedRootContactKey>,+}++#[frb(unignore)]+pub struct ContactDiffOutput {+ pub records: Vec<ContactRecord>,+ pub wrapped_root_contact_key: Option<WrappedRootContactKey>,+}++#[frb(unignore)]+pub struct ProfilePictureOutput {+ pub bytes: Vec<u8>,+ pub wrapped_root_contact_key: Option<WrappedRootContactKey>,+}