π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Handle ineligible automatic share recipients
Neeraj Gupta2b3ee0025e12
diff --git a/mobile/apps/photos/lib/core/errors.dart b/mobile/apps/photos/lib/core/errors.dartindex bfc826669e..307d49b380 100644--- a/mobile/apps/photos/lib/core/errors.dart+++ b/mobile/apps/photos/lib/core/errors.dart@@ -94,6 +94,8 @@ class SharingNotPermittedForFreeAccountsError extends Error {}class RecipientIdentityMismatchError extends Error {}+class AutomaticShareRecipientNotEligibleError extends Error {}+class LinkEditNotAllowedError extends Error {}class NoMediaLocationAccessError extends Error {}diff --git a/mobile/apps/photos/lib/services/collections_service.dart b/mobile/apps/photos/lib/services/collections_service.dartindex 3d416eaeaf..faf2be973f 100644--- a/mobile/apps/photos/lib/services/collections_service.dart+++ b/mobile/apps/photos/lib/services/collections_service.dart@@ -924,6 +924,10 @@ class CollectionsService {if (e.response?.data?['code'] == 'RECIPIENT_IDENTITY_MISMATCH') {throw RecipientIdentityMismatchError();}+ if (e.response?.data?['code'] ==+ 'AUTOMATIC_SHARE_RECIPIENT_NOT_ELIGIBLE') {+ throw AutomaticShareRecipientNotEligibleError();+ }rethrow;}}diff --git a/mobile/apps/photos/lib/services/library_sharing_service.dart b/mobile/apps/photos/lib/services/library_sharing_service.dartindex cc4a5a1716..243784d178 100644--- a/mobile/apps/photos/lib/services/library_sharing_service.dart+++ b/mobile/apps/photos/lib/services/library_sharing_service.dart@@ -12,6 +12,7 @@ import 'package:photos/models/collection/collection.dart';import 'package:photos/models/library_sharing/library_sharing_recipient.dart';import 'package:photos/models/metadata/collection_magic.dart';import 'package:photos/models/metadata/common_keys.dart';+import 'package:photos/models/user_details.dart';import 'package:photos/services/account/user_service.dart';import 'package:photos/services/collections_service.dart';import 'package:photos/services/library_sharing_local_store.dart';@@ -345,7 +346,14 @@ class LibrarySharingService implements LibrarySharingRepository {!activeFamilyMemberUserIDs.contains(latest.recipientUserID)) {await _writeConfig(ownerUserID, latest.copyWith(enabled: false));} else {- await _reconcileConfig(ownerUserID, latest);+ try {+ await _reconcileConfig(ownerUserID, latest);+ } on AutomaticShareRecipientNotEligibleError {+ await _refreshFamilyAndHandleIneligibleRecipient(+ ownerUserID,+ latest,+ );+ }}});} catch (error, stackTrace) {@@ -462,6 +470,8 @@ class LibrarySharingService implements LibrarySharingRepository {);} on RecipientIdentityMismatchError {rethrow;+ } on AutomaticShareRecipientNotEligibleError {+ rethrow;} catch (error, stackTrace) {_logger.warning('Bulk library share failed', error, stackTrace);}@@ -563,10 +573,38 @@ class LibrarySharingService implements LibrarySharingRepository {(throw StateError('No email found for library sharing recipient'));}- Set<int>? _activeFamilyMemberUserIDs() {+ Future<void> _refreshFamilyAndHandleIneligibleRecipient(+ int ownerUserID,+ LibrarySharingLocalConfig config,+ ) async {+ UserDetails userDetails;+ try {+ userDetails = await _userService.getUserDetailsV2(memoryCount: false);+ } catch (refreshError, refreshStackTrace) {+ _logger.warning(+ 'Could not refresh family details after automatic share rejection for '+ '${config.recipientUserID}',+ refreshError,+ refreshStackTrace,+ );+ return;+ }+ if (_activeFamilyMemberUserIDs(+ userDetails,+ )!.contains(config.recipientUserID)) {+ _logger.warning(+ 'Automatic share rejected for active family member '+ '${config.recipientUserID}',+ );+ return;+ }+ await _writeConfig(ownerUserID, config.copyWith(enabled: false));+ }++ Set<int>? _activeFamilyMemberUserIDs([UserDetails? userDetails]) {// Temporary best-effort guard while Library Sharing is family-scoped;// cached details can lag membership changes made on another client.- final userDetails = _userService.getCachedUserDetails();+ userDetails ??= _userService.getCachedUserDetails();if (userDetails == null) {return null;}diff --git a/mobile/apps/photos/test/services/library_sharing_service_test.dart b/mobile/apps/photos/test/services/library_sharing_service_test.dartindex 1513edaa7e..7ed0b27a8c 100644--- a/mobile/apps/photos/test/services/library_sharing_service_test.dart+++ b/mobile/apps/photos/test/services/library_sharing_service_test.dart@@ -134,6 +134,57 @@ void main() {expect(fixture.collectionsService.shareBatchSizes, [100]);});++ test(+ 'refreshes only for ineligible recipients and disables after removal',+ () async {+ final albums = [librarySharingTestAlbum(1)];+ final fixture = await _Fixture.create(+ albums,+ activeFamilyMemberUserIDs: {librarySharingTestRecipient.userID},+ );+ await fixture.service.enableAutomaticSharing(+ recipient: librarySharingTestRecipient,+ role: CollectionParticipantRole.viewer,+ );+ albums.addAll([+ for (var id = 2; id <= 102; id++) librarySharingTestAlbum(id),+ ]);++ fixture.collectionsService.shareError = StateError('ordinary failure');+ await fixture.service.reconcile();+ expect(fixture.userService.userDetailsFetches, 0);++ fixture.collectionsService.shareError =+ AutomaticShareRecipientNotEligibleError();+ fixture.userService.refreshedActiveFamilyMemberUserIDs = {+ librarySharingTestRecipient.userID,+ };+ await fixture.service.reconcile();+ expect(fixture.userService.userDetailsFetches, 1);+ expect(await fixture.isAutomaticSharingEnabled(), isTrue);++ fixture.userService.userDetailsError = StateError('refresh failed');+ await fixture.service.reconcile();+ expect(fixture.userService.userDetailsFetches, 2);+ expect(await fixture.isAutomaticSharingEnabled(), isTrue);++ fixture.userService.userDetailsError = null;+ fixture.userService.refreshedActiveFamilyMemberUserIDs = {};+ await fixture.service.reconcile();+ expect(fixture.userService.userDetailsFetches, 3);+ expect(await fixture.isAutomaticSharingEnabled(), isFalse);+ expect(fixture.collectionsService.shareBatchSizes, [+ 1,+ 100,+ 1,+ 100,+ 100,+ 100,+ ]);+ expect(albums.last.sharees, isEmpty);+ },+ );}class _Fixture {@@ -179,6 +230,9 @@ class _Fixture {Future<LibrarySharingLocalConfig> readConfig() async =>(await store.read(1, librarySharingTestRecipient.userID))!;++ Future<bool> isAutomaticSharingEnabled() =>+ service.isAutomaticSharingEnabled(librarySharingTestRecipient.userID);}class _FakeCollectionsService extends Mock implements CollectionsService {@@ -186,7 +240,7 @@ class _FakeCollectionsService extends Mock implements CollectionsService {final List<Collection> albums;final Set<int> blockedIDs;- final Object? shareError;+ Object? shareError;final List<int> shareBatchSizes = [];final List<String> recipientEmails = [];final List<int> shareAttempts = [];@@ -271,6 +325,9 @@ class _FakeUserService extends Mock implements UserService {_FakeUserService(this.activeFamilyMemberUserIDs);Set<int>? activeFamilyMemberUserIDs;+ Set<int>? refreshedActiveFamilyMemberUserIDs;+ Object? userDetailsError;+ int userDetailsFetches = 0;final List<String> publicKeyEmails = [];@override@@ -292,6 +349,23 @@ class _FakeUserService extends Mock implements UserService {final userIDs = activeFamilyMemberUserIDs;return userIDs == null ? null : _FakeUserDetails(userIDs);}++ @override+ Future<UserDetails> getUserDetailsV2({+ bool memoryCount = true,+ bool shouldCache = true,+ }) async {+ userDetailsFetches++;+ if (userDetailsError != null) {+ throw userDetailsError!;+ }+ final userIDs =+ refreshedActiveFamilyMemberUserIDs ??+ activeFamilyMemberUserIDs ??+ const {};+ activeFamilyMemberUserIDs = userIDs;+ return _FakeUserDetails(userIDs);+ }}class _FakeUserDetails extends Mock implements UserDetails {