π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add batch sharing with legacy fallback
AmanRajSinghMourya8fd68e5966a2
diff --git a/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dart b/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dartindex bfe0038194..0d4bd73644 100644--- a/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dart+++ b/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dart@@ -44,6 +44,33 @@ class CollectionShareGateway {return sharees;}+ Future<List<User>> shareBatch({+ required int collectionID,+ required Map<String, String> encryptedKeys,+ required String role,+ }) async {+ final response = await _enteDio.post(+ "/collections/share/batch",+ data: {+ "shares": encryptedKeys.entries+ .map(+ (entry) => {+ "collectionID": collectionID,+ "email": entry.key,+ "encryptedKey": entry.value,+ "role": role,+ },+ )+ .toList(),+ },+ );+ final sharees = <User>[];+ for (final user in response.data["sharees"]) {+ sharees.add(User.fromMap(user));+ }+ return sharees;+ }+Future<List<User>> unshare({required int collectionID,required String email,diff --git a/mobile/apps/photos/lib/gateways/users/users_gateway.dart b/mobile/apps/photos/lib/gateways/users/users_gateway.dartindex 0ffe754ce2..e3d3fae555 100644--- a/mobile/apps/photos/lib/gateways/users/users_gateway.dart+++ b/mobile/apps/photos/lib/gateways/users/users_gateway.dart@@ -70,6 +70,18 @@ class UsersGateway {}}+ Future<List<String>> getPublicKeys(List<String> emails) async {+ final response = await _enteDio.post(+ "/users/public-keys",+ data: {"emails": emails},+ );+ final values = response.data["publicKeys"] as List;+ if (values.length != emails.length) {+ throw StateError("Public-key batch response length mismatch");+ }+ return values.map((value) => value as String).toList();+ }+Future<UserDetails> getUserDetails({bool memoryCount = true}) async {final response = await _enteDio.get("/users/details/v2",diff --git a/mobile/apps/photos/lib/services/account/user_service.dart b/mobile/apps/photos/lib/services/account/user_service.dartindex a1bdeb29cc..e7df1ad414 100644--- a/mobile/apps/photos/lib/services/account/user_service.dart+++ b/mobile/apps/photos/lib/services/account/user_service.dart@@ -58,6 +58,7 @@ class UserService {static const keyUserDetails = "user_details";static const kReferralSource = "referral_source";static const kIsEmailMFAEnabled = "is_email_mfa_enabled";+ static const _publicKeyBatchSize = 10;final SRP6GroupParameters kDefaultSrpGroup = SRP6StandardGroups.rfc5054_4096;final _publicKeyCache = TimedCache<String, String>(@@ -206,6 +207,7 @@ class UserService {}Future<String?> getPublicKey(String email) async {+ email = email.trim().toLowerCase();final String? cachedPubKey = _publicKeyCache.get(email);if (cachedPubKey != null) {return cachedPubKey;@@ -217,6 +219,58 @@ class UserService {return publicKey;}+ Future<Map<String, String?>> getPublicKeys(Iterable<String> emails) async {+ final normalizedEmails = <String>[];+ final seen = <String>{};+ for (final email in emails) {+ final normalizedEmail = email.trim().toLowerCase();+ if (normalizedEmail.isNotEmpty && seen.add(normalizedEmail)) {+ normalizedEmails.add(normalizedEmail);+ }+ }++ final result = <String, String?>{};+ final uncached = <String>[];+ for (final email in normalizedEmails) {+ final publicKey = _publicKeyCache.get(email);+ if (publicKey == null) {+ uncached.add(email);+ } else {+ result[email] = publicKey;+ }+ }++ for (final batch in uncached.chunks(_publicKeyBatchSize)) {+ final publicKeys = await _getPublicKeyBatch(batch);+ for (var index = 0; index < batch.length; index++) {+ final publicKey = publicKeys[index];+ result[batch[index]] = publicKey;+ if (publicKey != null && publicKey.isNotEmpty) {+ _publicKeyCache.set(batch[index], publicKey);+ }+ }+ }+ return result;+ }++ Future<List<String?>> _getPublicKeyBatch(List<String> emails) async {+ if (flagService.internalUser) {+ try {+ return await _gateway.getPublicKeys(emails);+ } on DioException catch (error) {+ if (error.response?.statusCode != 404) {+ rethrow;+ }+ }+ }++ final publicKeys = <String?>[];+ for (final email in emails) {+ publicKeys.add(await _gateway.getPublicKey(email));+ }+ return publicKeys;+ }+UserDetails? getCachedUserDetails() {if (_preferences.containsKey(keyUserDetails)) {return UserDetails.fromJson(_preferences.getString(keyUserDetails)!);diff --git a/mobile/apps/photos/lib/services/collections_service.dart b/mobile/apps/photos/lib/services/collections_service.dartindex 0b5aa2b847..ec0b3db089 100644--- a/mobile/apps/photos/lib/services/collections_service.dart+++ b/mobile/apps/photos/lib/services/collections_service.dart@@ -15,6 +15,7 @@ import 'package:photos/core/configuration.dart';import 'package:photos/core/constants.dart';import 'package:photos/core/errors.dart';import 'package:photos/core/event_bus.dart';+import 'package:photos/core/network/api_response.dart';import 'package:photos/db/collections_db.dart';import 'package:photos/db/device_files_db.dart';import 'package:photos/db/files_db.dart';@@ -65,6 +66,7 @@ class CollectionsService {static const int kMaximumWriteAttempts = 5;static const int _maxSocialCleanupRetries = 3;static const int _collectionKeyLength = 32;+ static const int _shareBatchSize = 10;final _logger = Logger("CollectionsService");@@ -873,6 +875,80 @@ class CollectionsService {}}+ Future<List<User>> shareBatch(+ int collectionID,+ Map<String, String> publicKeys,+ CollectionParticipantRole role,+ ) async {+ var sharees = List<User>.from(+ _collectionIDToCollections[collectionID]!.sharees,+ );+ final collectionKey = getCollectionKey(collectionID);+ final shareRole = role.toStringVal();+ var useLegacyAPI = !flagService.internalUser;+ try {+ for (final batch in publicKeys.entries.toList().chunks(_shareBatchSize)) {+ final encryptedKeys = <String, String>{};+ for (final entry in batch) {+ final encryptedKey = CryptoUtil.sealSync(+ collectionKey,+ CryptoUtil.base642bin(entry.value),+ );+ encryptedKeys[entry.key] = CryptoUtil.bin2base64(encryptedKey);+ }++ if (!useLegacyAPI) {+ try {+ sharees = await collectionShareGateway.shareBatch(+ collectionID: collectionID,+ encryptedKeys: encryptedKeys,+ role: shareRole,+ );+ _cacheSharees(_collectionIDToCollections[collectionID]!, sharees);+ RemoteSyncService.instance.sync(silently: true).ignore();+ continue;+ } on UnexpectedApiResponseException catch (error) {+ if (error.response?.statusCode != 404) rethrow;+ useLegacyAPI = true;+ }+ }++ sharees = await _shareBatchLegacy(+ collectionID,+ encryptedKeys,+ shareRole,+ sharees,+ );+ }+ return sharees;+ } on DioException catch (error) {+ if (error.response?.statusCode == 402) {+ throw SharingNotPermittedForFreeAccountsError();+ }+ rethrow;+ }+ }++ Future<List<User>> _shareBatchLegacy(+ int collectionID,+ Map<String, String> encryptedKeys,+ String role,+ List<User> initialSharees,+ ) async {+ var sharees = initialSharees;+ for (final entry in encryptedKeys.entries) {+ sharees = await collectionShareGateway.share(+ collectionID: collectionID,+ email: entry.key,+ encryptedKey: entry.value,+ role: role,+ );+ _cacheSharees(_collectionIDToCollections[collectionID]!, sharees);+ RemoteSyncService.instance.sync(silently: true).ignore();+ }+ return sharees;+ }+Future<List<User>> unshare(int collectionID, String email) async {try {final sharees = await collectionShareGateway.unshare(