Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][contacts] route profile pictures through generic attachments

Neeraj Gupta9381a8d58fc9
diff --git a/mobile/packages/contacts/lib/src/rust/contacts_rust_api.dart b/mobile/packages/contacts/lib/src/rust/contacts_rust_api.dart
index 95d4d2ebcb..c72721079f 100644
--- a/mobile/packages/contacts/lib/src/rust/contacts_rust_api.dart
+++ b/mobile/packages/contacts/lib/src/rust/contacts_rust_api.dart
@@ -14,6 +14,8 @@ class WrappedRootContactKey {
});
}
+enum ContactAttachmentType { profilePicture }
+
enum RootKeySource { cache, server, created }
class OpenContactsContextInput {
@@ -67,6 +69,22 @@ abstract class ContactsRustContext {
Future<void> deleteContact(String contactId);
+ Future<ContactRecord> setAttachment(
+ String contactId,
+ ContactAttachmentType attachmentType,
+ Uint8List attachmentBytes,
+ );
+
+ Future<Uint8List> getAttachment(
+ ContactAttachmentType attachmentType,
+ String attachmentId,
+ );
+
+ Future<ContactRecord> deleteAttachment(
+ String contactId,
+ ContactAttachmentType attachmentType,
+ );
+
Future<ContactRecord> setProfilePicture(
String contactId,
Uint8List profilePicture,
@@ -188,29 +206,78 @@ class _FrbContactsRustContext implements ContactsRustContext {
}
@override
- Future<ContactRecord> setProfilePicture(
+ Future<ContactRecord> setAttachment(
String contactId,
- Uint8List profilePicture,
+ ContactAttachmentType attachmentType,
+ Uint8List attachmentBytes,
) async {
return _fromRustRecord(
- await _inner.setProfilePicture(
+ await _inner.setAttachment(
contactId: contactId,
- profilePicture: profilePicture,
+ attachmentType: _toRustAttachmentType(attachmentType),
+ attachmentBytes: attachmentBytes,
),
);
}
@override
- Future<Uint8List> getProfilePicture(String contactId) {
- return _inner.getProfilePicture(contactId: contactId);
+ Future<Uint8List> getAttachment(
+ ContactAttachmentType attachmentType,
+ String attachmentId,
+ ) {
+ return _inner.getAttachment(
+ attachmentType: _toRustAttachmentType(attachmentType),
+ attachmentId: attachmentId,
+ );
}
@override
- Future<ContactRecord> deleteProfilePicture(String contactId) async {
+ Future<ContactRecord> deleteAttachment(
+ String contactId,
+ ContactAttachmentType attachmentType,
+ ) async {
return _fromRustRecord(
- await _inner.deleteProfilePicture(contactId: contactId),
+ await _inner.deleteAttachment(
+ contactId: contactId,
+ attachmentType: _toRustAttachmentType(attachmentType),
+ ),
);
}
+
+ @override
+ Future<ContactRecord> setProfilePicture(
+ String contactId,
+ Uint8List profilePicture,
+ ) async {
+ return setAttachment(
+ contactId,
+ ContactAttachmentType.profilePicture,
+ profilePicture,
+ );
+ }
+
+ @override
+ Future<Uint8List> getProfilePicture(String contactId) async {
+ final contact = await getContact(contactId);
+ final attachmentId = contact.profilePictureAttachmentId;
+ if (attachmentId == null) {
+ throw StateError('Contact $contactId does not have a profile picture');
+ }
+ return getAttachment(ContactAttachmentType.profilePicture, attachmentId);
+ }
+
+ @override
+ Future<ContactRecord> deleteProfilePicture(String contactId) async {
+ return deleteAttachment(contactId, ContactAttachmentType.profilePicture);
+ }
+}
+
+rust.AttachmentType _toRustAttachmentType(
+ ContactAttachmentType attachmentType,
+) {
+ return switch (attachmentType) {
+ ContactAttachmentType.profilePicture => rust.AttachmentType.profilePicture,
+ };
}
ContactRecord _fromRustRecord(rust.ContactRecord record) {
diff --git a/mobile/packages/contacts/lib/src/service/contacts_service.dart b/mobile/packages/contacts/lib/src/service/contacts_service.dart
index ac0c8ceb0b..c624e008eb 100644
--- a/mobile/packages/contacts/lib/src/service/contacts_service.dart
+++ b/mobile/packages/contacts/lib/src/service/contacts_service.dart
@@ -16,9 +16,9 @@ class ContactsService {
required SharedPreferences preferences,
ContactsDatabase? database,
ContactsRustApi? rustApi,
- }) : _preferences = preferences,
- _database = database ?? ContactsDatabase(),
- _rustApi = rustApi ?? const FrbContactsRustApi();
+ }) : _preferences = preferences,
+ _database = database ?? ContactsDatabase(),
+ _rustApi = rustApi ?? const FrbContactsRustApi();
final SharedPreferences _preferences;
final ContactsDatabase _database;
@@ -115,9 +115,8 @@ class ContactsService {
final ctx = _requireCtx();
await ctx.deleteContact(contactId);
final deleted = await ctx.getDiff(0, _syncLimit);
- final matching = deleted
- .where((element) => element.id == contactId)
- .toList();
+ final matching =
+ deleted.where((element) => element.id == contactId).toList();
if (matching.isNotEmpty) {
await _database.upsertContacts([matching.first]);
} else {
@@ -128,11 +127,36 @@ class ContactsService {
Future<ContactRecord> setProfilePicture(
String contactId,
Uint8List bytes,
+ ) {
+ return _setAttachment(
+ contactId,
+ ContactAttachmentType.profilePicture,
+ bytes,
+ );
+ }
+
+ Future<Uint8List> getProfilePicture(String contactId) {
+ return _getAttachment(contactId, ContactAttachmentType.profilePicture);
+ }
+
+ Future<ContactRecord> deleteProfilePicture(String contactId) {
+ return _deleteAttachment(contactId, ContactAttachmentType.profilePicture);
+ }
+
+ Future<ContactRecord> _setAttachment(
+ String contactId,
+ ContactAttachmentType attachmentType,
+ Uint8List bytes,
) async {
final previousAttachmentId = (await _database.getContact(
contactId,
- ))?.profilePictureAttachmentId;
- final updated = await _requireCtx().setProfilePicture(contactId, bytes);
+ ))
+ ?.profilePictureAttachmentId;
+ final updated = await _requireCtx().setAttachment(
+ contactId,
+ attachmentType,
+ bytes,
+ );
await _database.upsertContacts([updated]);
final nextAttachmentId = updated.profilePictureAttachmentId;
if (nextAttachmentId != null) {
@@ -145,26 +169,37 @@ class ContactsService {
return updated;
}
- Future<Uint8List> getProfilePicture(String contactId) async {
+ Future<Uint8List> _getAttachment(
+ String contactId,
+ ContactAttachmentType attachmentType,
+ ) async {
final contact = await _database.getContact(contactId);
final attachmentId = contact?.profilePictureAttachmentId;
if (attachmentId == null) {
- throw StateError('Contact $contactId does not have a profile picture');
+ throw StateError(
+ 'Contact $contactId does not have attachment $attachmentType',
+ );
}
final cached = await _database.getCachedAttachment(attachmentId);
if (cached != null) {
return cached;
}
- final bytes = await _requireCtx().getProfilePicture(contactId);
+ final bytes =
+ await _requireCtx().getAttachment(attachmentType, attachmentId);
await _database.upsertCachedAttachment(attachmentId, bytes);
return bytes;
}
- Future<ContactRecord> deleteProfilePicture(String contactId) async {
+ Future<ContactRecord> _deleteAttachment(
+ String contactId,
+ ContactAttachmentType attachmentType,
+ ) async {
final previousAttachmentId = (await _database.getContact(
contactId,
- ))?.profilePictureAttachmentId;
- final updated = await _requireCtx().deleteProfilePicture(contactId);
+ ))
+ ?.profilePictureAttachmentId;
+ final updated =
+ await _requireCtx().deleteAttachment(contactId, attachmentType);
await _database.upsertContacts([updated]);
if (previousAttachmentId != null) {
await _database.deleteCachedAttachment(previousAttachmentId);
@@ -203,6 +238,5 @@ class ContactsService {
String _entityKeyPref(int userId) => 'entity_key_contact_$userId';
- String _entityHeaderPref(int userId) =>
- 'entity_key_header_contact_$userId';
+ String _entityHeaderPref(int userId) => 'entity_key_header_contact_$userId';
}
diff --git a/mobile/packages/contacts/test/contacts_service_test.dart b/mobile/packages/contacts/test/contacts_service_test.dart
index 75ad12d670..60979be0de 100644
--- a/mobile/packages/contacts/test/contacts_service_test.dart
+++ b/mobile/packages/contacts/test/contacts_service_test.dart
@@ -110,7 +110,7 @@ void main() {
await service.getProfilePicture(created.id),
Uint8List.fromList([1, 2, 3, 4]),
);
- expect(rustApi.ctx.getProfilePictureCalls, 0);
+ expect(rustApi.ctx.getAttachmentCalls, 0);
final deletedPicture = await service.deleteProfilePicture(created.id);
expect(deletedPicture.profilePictureAttachmentId, isNull);
@@ -150,7 +150,7 @@ void main() {
],
const [],
];
- rustApi.ctx.pictures['ct_1'] = Uint8List.fromList([7, 8, 9]);
+ rustApi.ctx.attachments['att_1'] = Uint8List.fromList([7, 8, 9]);
await service.open(
ContactsSession(
@@ -166,12 +166,12 @@ void main() {
await service.getProfilePicture('ct_1'),
Uint8List.fromList([7, 8, 9]),
);
- expect(rustApi.ctx.getProfilePictureCalls, 1);
+ expect(rustApi.ctx.getAttachmentCalls, 1);
expect(
await service.getProfilePicture('ct_1'),
Uint8List.fromList([7, 8, 9]),
);
- expect(rustApi.ctx.getProfilePictureCalls, 1);
+ expect(rustApi.ctx.getAttachmentCalls, 1);
});
test('replacing profile picture removes stale cached bytes', () async {
@@ -285,9 +285,9 @@ class FakeContactsRustApi implements ContactsRustApi {
class FakeContactsRustContext implements ContactsRustContext {
int userIdValue = 0;
final Map<String, ContactRecord> records = {};
- final Map<String, Uint8List> pictures = {};
+ final Map<String, Uint8List> attachments = {};
List<List<ContactRecord>> diffPages = [];
- int getProfilePictureCalls = 0;
+ int getAttachmentCalls = 0;
String nextAttachmentId = 'att_profile';
@override
@@ -330,7 +330,10 @@ class FakeContactsRustContext implements ContactsRustContext {
}
@override
- Future<ContactRecord> deleteProfilePicture(String contactId) async {
+ Future<ContactRecord> deleteAttachment(
+ String contactId,
+ ContactAttachmentType attachmentType,
+ ) async {
final existing = records[contactId]!;
final updated = ContactRecord(
id: existing.id,
@@ -343,10 +346,18 @@ class FakeContactsRustContext implements ContactsRustContext {
updatedAt: existing.updatedAt + 1,
);
records[contactId] = updated;
- pictures.remove(contactId);
+ final previousAttachmentId = existing.profilePictureAttachmentId;
+ if (previousAttachmentId != null) {
+ attachments.remove(previousAttachmentId);
+ }
return updated;
}
+ @override
+ Future<ContactRecord> deleteProfilePicture(String contactId) {
+ return deleteAttachment(contactId, ContactAttachmentType.profilePicture);
+ }
+
@override
Future<ContactRecord> getContact(String contactId) async =>
records[contactId]!;
@@ -365,15 +376,25 @@ class FakeContactsRustContext implements ContactsRustContext {
}
@override
- Future<Uint8List> getProfilePicture(String contactId) async {
- getProfilePictureCalls += 1;
- return pictures[contactId]!;
+ Future<Uint8List> getAttachment(
+ ContactAttachmentType attachmentType,
+ String attachmentId,
+ ) async {
+ getAttachmentCalls += 1;
+ return attachments[attachmentId]!;
}
@override
- Future<ContactRecord> setProfilePicture(
+ Future<Uint8List> getProfilePicture(String contactId) {
+ final attachmentId = records[contactId]!.profilePictureAttachmentId!;
+ return getAttachment(ContactAttachmentType.profilePicture, attachmentId);
+ }
+
+ @override
+ Future<ContactRecord> setAttachment(
String contactId,
- Uint8List profilePicture,
+ ContactAttachmentType attachmentType,
+ Uint8List attachmentBytes,
) async {
final existing = records[contactId]!;
final updated = ContactRecord(
@@ -387,10 +408,22 @@ class FakeContactsRustContext implements ContactsRustContext {
updatedAt: existing.updatedAt + 1,
);
records[contactId] = updated;
- pictures[contactId] = profilePicture;
+ attachments[nextAttachmentId] = attachmentBytes;
return updated;
}
+ @override
+ Future<ContactRecord> setProfilePicture(
+ String contactId,
+ Uint8List profilePicture,
+ ) {
+ return setAttachment(
+ contactId,
+ ContactAttachmentType.profilePicture,
+ profilePicture,
+ );
+ }
+
@override
Future<void> updateAuthToken(String authToken) async {}