π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Split shared feed notifications and honor shared album toggle
ashilkn08b92a8c6267
diff --git a/mobile/apps/photos/lib/l10n/intl_en.arb b/mobile/apps/photos/lib/l10n/intl_en.arbindex 1f8404d6ca..7b6c5c7112 100644--- a/mobile/apps/photos/lib/l10n/intl_en.arb+++ b/mobile/apps/photos/lib/l10n/intl_en.arb@@ -667,8 +667,8 @@"familyPlans": "Family plans","referrals": "Referrals","notifications": "Notifications",- "sharedPhotoNotifications": "New shared photos",- "sharedPhotoNotificationsExplanation": "Receive notifications when someone adds a photo to a shared album that you're a part of",+ "sharedPhotoNotifications": "New shared photos and albums",+ "sharedPhotoNotificationsExplanation": "Receive notifications when someone adds photos to a shared album and when a new album is shared with you","socialNotifications": "Likes and comments","socialNotificationsExplanation": "Receive notifications for likes on your photos and comments on albums you're a part of","advanced": "Advanced",diff --git a/mobile/apps/photos/lib/models/social/feed_data_provider.dart b/mobile/apps/photos/lib/models/social/feed_data_provider.dartindex 36595e6d69..60c68068c6 100644--- a/mobile/apps/photos/lib/models/social/feed_data_provider.dart+++ b/mobile/apps/photos/lib/models/social/feed_data_provider.dart@@ -4,6 +4,7 @@ import "package:photos/core/configuration.dart";import "package:photos/db/files_db.dart";import "package:photos/db/social_db.dart";import "package:photos/models/collection/collection.dart";+import "package:photos/models/file/extensions/file_props.dart";import "package:photos/models/file/file.dart";import "package:photos/models/social/comment.dart";import "package:photos/models/social/feed_item.dart";@@ -499,6 +500,9 @@ class FeedDataProvider {}continue;}+ if (file.uploaderName != null) {+ continue;+ }oldestFetchedAddedTime = addedTime;groupingState.addFile(file);retainedRows++;diff --git a/mobile/apps/photos/lib/services/notification_service.dart b/mobile/apps/photos/lib/services/notification_service.dartindex 99f959b37e..0720869544 100644--- a/mobile/apps/photos/lib/services/notification_service.dart+++ b/mobile/apps/photos/lib/services/notification_service.dart@@ -176,13 +176,13 @@ class NotificationService {return result ?? false;}- bool shouldShowNotificationsForSharedPhotos() {+ bool shouldShowNotificationsForSharedPhotosAndAlbums() {final result =_preferences.getBool(keyShouldShowNotificationsForSharedPhotos);return result ?? true;}- Future<void> setShouldShowNotificationsForSharedPhotos(bool value) {+ Future<void> setShouldShowNotificationsForSharedPhotosAndAlbums(bool value) {return _preferences.setBool(keyShouldShowNotificationsForSharedPhotos,value,diff --git a/mobile/apps/photos/lib/services/social_notification_coordinator.dart b/mobile/apps/photos/lib/services/social_notification_coordinator.dartindex a79c513628..d6631f072e 100644--- a/mobile/apps/photos/lib/services/social_notification_coordinator.dart+++ b/mobile/apps/photos/lib/services/social_notification_coordinator.dart@@ -6,6 +6,9 @@ import 'package:photos/db/files_db.dart';import 'package:photos/db/social_db.dart';import 'package:photos/extensions/user_extension.dart';import 'package:photos/generated/l10n.dart';+import 'package:photos/models/collection/collection.dart';+import 'package:photos/models/file/extensions/file_props.dart';+import 'package:photos/models/file/file.dart';import 'package:photos/models/file/file_type.dart';import 'package:photos/models/social/comment.dart';import 'package:photos/models/social/feed_item.dart';@@ -28,6 +31,11 @@ class SocialNotificationCoordinator {'last_social_activity_notification_time';static const String kLastSocialSeenTime = 'last_social_seen_time';static const String kIsFirstRemoteSyncDoneKey = 'isFirstRemoteSyncDone';+ static const _kSharedPhotoSessionGapMicros = 1000 * 1000 * 60 * 10;+ static const _kSharedPhotoFetchPageSize = 200;+ static const _kSharedPhotoFetchMaxPages = 5;+ static const _kSharedPhotoFetchMaxRows =+ _kSharedPhotoFetchPageSize * _kSharedPhotoFetchMaxPages;static final SocialNotificationCoordinator instance =SocialNotificationCoordinator._privateConstructor();@@ -79,9 +87,14 @@ class SocialNotificationCoordinator {return _prefs!;}- bool _shouldShowSocialNotifications(SharedPreferences prefs) {+ bool _shouldEvaluateFeedNotifications(+ SharedPreferences prefs, {+ required bool socialNotificationsEnabled,+ required bool sharedPhotosAndAlbumsNotificationsEnabled,+ }) {return prefs.containsKey(kIsFirstRemoteSyncDoneKey) &&- NotificationService.instance.shouldShowSocialNotifications();+ (socialNotificationsEnabled ||+ sharedPhotosAndAlbumsNotificationsEnabled);}int _getCutoffTime(SharedPreferences prefs) {@@ -100,7 +113,17 @@ class SocialNotificationCoordinator {Future<void> _notifyNewSocialActivity() async {final prefs = await _ensurePrefs();- if (!_shouldShowSocialNotifications(prefs)) {+ final socialNotificationsEnabled =+ NotificationService.instance.shouldShowSocialNotifications();+ final sharedPhotosAndAlbumsNotificationsEnabled = NotificationService+ .instance+ .shouldShowNotificationsForSharedPhotosAndAlbums();+ if (!_shouldEvaluateFeedNotifications(+ prefs,+ socialNotificationsEnabled: socialNotificationsEnabled,+ sharedPhotosAndAlbumsNotificationsEnabled:+ sharedPhotosAndAlbumsNotificationsEnabled,+ )) {return;}if (!flagService.isSocialEnabled) {@@ -117,7 +140,12 @@ class SocialNotificationCoordinator {final latestByKey = <String, _SocialActivityCandidate>{};void considerCandidate(_SocialActivityCandidate candidate) {- if (!_isSocialNotificationEnabledForType(candidate.type)) {+ if (!_isFeedNotificationEnabledForType(+ candidate.type,+ socialNotificationsEnabled: socialNotificationsEnabled,+ sharedPhotosAndAlbumsNotificationsEnabled:+ sharedPhotosAndAlbumsNotificationsEnabled,+ )) {return;}if (candidate.createdAt <= cutoffTime) {@@ -126,12 +154,14 @@ class SocialNotificationCoordinator {if (hiddenCollectionIds.contains(candidate.collectionID)) {return;}- if (candidate.fileID == null) {+ if (candidate.fileID == null &&+ candidate.type != FeedItemType.sharedCollection) {return;}final group = _notificationGroupForType(candidate.type);+ final fileIDKey = candidate.fileID ?? 0;final key =- '${candidate.collectionID}_${candidate.fileID}_${group.index}';+ '${candidate.collectionID}_${candidate.type.index}_${fileIDKey}_${group.index}';final existing = latestByKey[key];if (existing == null || candidate.createdAt > existing.createdAt) {latestByKey[key] = candidate;@@ -217,6 +247,15 @@ class SocialNotificationCoordinator {);}+ final sharedCandidates = await _getSharedFeedCandidates(+ userID: userID,+ cutoffTime: cutoffTime,+ hiddenCollectionIds: hiddenCollectionIds,+ );+ for (final candidate in sharedCandidates) {+ considerCandidate(candidate);+ }+if (latestByKey.isEmpty) {return;}@@ -227,11 +266,8 @@ class SocialNotificationCoordinator {int? latestSentNotificationTime;for (final candidate in candidates) {- final fileID = candidate.fileID;- if (fileID == null) {- continue;- }try {+ final fileID = candidate.fileID;final fileType = filesByID[fileID]?.fileType;final isOwn = switch (candidate.type) {FeedItemType.photoLike => isOwnedByUser(fileID),@@ -245,7 +281,7 @@ class SocialNotificationCoordinator {final title = await _getSocialNotificationTitle(candidate);await NotificationService.instance.showNotification(title,- _getSocialNotificationBody(candidate.type, s, fileType, isOwn),+ _getSocialNotificationBody(candidate, s, fileType, isOwn),channelID: 'social_activity',channelName: 'Ente Feed',payload: _buildSocialNotificationPayload(candidate),@@ -273,16 +309,21 @@ class SocialNotificationCoordinator {}}- bool _isSocialNotificationEnabledForType(FeedItemType type) {+ bool _isFeedNotificationEnabledForType(+ FeedItemType type, {+ required bool socialNotificationsEnabled,+ required bool sharedPhotosAndAlbumsNotificationsEnabled,+ }) {switch (type) {case FeedItemType.comment:case FeedItemType.reply:case FeedItemType.photoLike:- return true;- case FeedItemType.commentLike:- case FeedItemType.replyLike:+ return socialNotificationsEnabled;case FeedItemType.sharedPhoto:case FeedItemType.sharedCollection:+ return sharedPhotosAndAlbumsNotificationsEnabled;+ case FeedItemType.commentLike:+ case FeedItemType.replyLike:return false;}}@@ -295,22 +336,23 @@ class SocialNotificationCoordinator {case FeedItemType.photoLike:case FeedItemType.commentLike:case FeedItemType.replyLike:+ return _SocialNotificationGroup.like;case FeedItemType.sharedPhoto:+ return _SocialNotificationGroup.sharedPhoto;case FeedItemType.sharedCollection:- return _SocialNotificationGroup.like;+ return _SocialNotificationGroup.sharedCollection;}}int _buildSocialNotificationId(int collectionID,- int fileID,+ int? fileID,_SocialNotificationGroup group,) {const int base = 0x10000000;int hash = collectionID & 0x7fffffff;- hash = ((hash * 31) ^ fileID) & 0x7fffffff;- hash = ((hash * 31) ^ (group == _SocialNotificationGroup.comment ? 1 : 0)) &- 0x7fffffff;+ hash = ((hash * 31) ^ (fileID ?? 0)) & 0x7fffffff;+ hash = ((hash * 31) ^ group.index) & 0x7fffffff;return base | (hash & 0x0fffffff);}@@ -332,22 +374,22 @@ class SocialNotificationCoordinator {}String _getSocialNotificationBody(- FeedItemType type,+ _SocialActivityCandidate candidate,AppLocalizations s,FileType? fileType,bool isOwn,) {- return _getSocialNotificationDetail(type, s, fileType, isOwn);+ return _getSocialNotificationDetail(candidate, s, fileType, isOwn);}String _getSocialNotificationDetail(- FeedItemType type,+ _SocialActivityCandidate candidate,AppLocalizations s,FileType? fileType,bool isOwn,) {final isVideo = fileType == FileType.video;- switch (type) {+ switch (candidate.type) {case FeedItemType.photoLike:return isVideo ? s.likedYourVideo : s.likedYourPhoto;case FeedItemType.comment:@@ -362,14 +404,118 @@ class SocialNotificationCoordinator {case FeedItemType.replyLike:return s.likedYourReply;case FeedItemType.sharedPhoto:- // Shared photos don't trigger notifications- return '';+ final count = candidate.sharedFileCount;+ final albumName = candidate.collectionName ?? s.albums;+ if (count <= 1) {+ return s.addedAMemoryTo(albumName: albumName);+ }+ return s.addedNMemoriesTo(count: count, albumName: albumName);case FeedItemType.sharedCollection:- // Shared collections don't trigger notifications- return '';+ final albumName = candidate.collectionName ?? s.albums;+ return s.sharedAlbumWithYou(albumName: albumName);}}+ Future<List<_SocialActivityCandidate>> _getSharedFeedCandidates({+ required int userID,+ required int cutoffTime,+ required Set<int> hiddenCollectionIds,+ }) async {+ final context = _SharedCollectionsContext.fromCollections(+ _collectionsService.getCollectionsForUI(+ includedShared: true,+ includeCollab: true,+ ),+ userID: userID,+ );++ final candidates = <_SocialActivityCandidate>[];+ for (final collection in context.incomingSharedCollections) {+ final sharedAt = collection.sharedAt;+ final ownerID = collection.owner.id;+ if (sharedAt == null ||+ sharedAt <= cutoffTime ||+ ownerID == null ||+ hiddenCollectionIds.contains(collection.id)) {+ continue;+ }+ candidates.add(+ _SocialActivityCandidate(+ type: FeedItemType.sharedCollection,+ collectionID: collection.id,+ createdAt: sharedAt,+ actorUserID: ownerID,+ collectionName: context.collectionNames[collection.id],+ ),+ );+ }++ final groupingState = _SharedPhotoGroupingState(+ sessionGapMicros: _kSharedPhotoSessionGapMicros,+ );+ var retainedRows = 0;++ for (var page = 0; page < _kSharedPhotoFetchMaxPages; page++) {+ final pageFiles = await _filesDb.getRecentlySharedFiles(+ currentUserID: userID,+ limit: _kSharedPhotoFetchPageSize,+ offset: page * _kSharedPhotoFetchPageSize,+ addedTimeAfterOrEqualTo: cutoffTime + 1,+ );+ if (pageFiles.isEmpty) {+ break;+ }++ for (final file in pageFiles) {+ final collectionID = file.collectionID;+ final addedTime = file.addedTime;+ if (collectionID == null ||+ addedTime == null ||+ hiddenCollectionIds.contains(collectionID) ||+ file.uploaderName != null) {+ continue;+ }+ final incomingSharedAt =+ context.incomingCollectionSharedAtByID[collectionID];+ if (incomingSharedAt != null && addedTime <= incomingSharedAt) {+ continue;+ }++ groupingState.addFile(file);+ retainedRows++;+ if (retainedRows >= _kSharedPhotoFetchMaxRows) {+ break;+ }+ }++ final reachedEnd = pageFiles.length < _kSharedPhotoFetchPageSize ||+ retainedRows >= _kSharedPhotoFetchMaxRows;+ if (reachedEnd) {+ break;+ }+ }++ final groupedPhotos = groupingState.buildSnapshotSorted();+ for (final group in groupedPhotos) {+ if (group.createdAt <= cutoffTime) {+ continue;+ }+ candidates.add(+ _SocialActivityCandidate(+ type: FeedItemType.sharedPhoto,+ collectionID: group.collectionID,+ fileID: group.sharedFileIDs.first,+ createdAt: group.createdAt,+ actorUserID: group.ownerID,+ sharedFileCount: group.sharedFileIDs.length,+ collectionName: context.collectionNames[group.collectionID],+ ),+ );+ }++ return candidates;+ }+String _buildSocialNotificationPayload(_SocialActivityCandidate candidate) {final params = <String, String>{'type': candidate.type.name,@@ -398,6 +544,8 @@ class _SocialActivityCandidate {final int actorUserID;final String? actorAnonID;final int? parentCommentUserID;+ final int sharedFileCount;+ final String? collectionName;_SocialActivityCandidate({required this.type,@@ -408,10 +556,158 @@ class _SocialActivityCandidate {this.commentID,this.actorAnonID,this.parentCommentUserID,+ this.sharedFileCount = 0,+ this.collectionName,});}enum _SocialNotificationGroup {comment,like,+ sharedPhoto,+ sharedCollection,+}++class _SharedCollectionsContext {+ final Map<int, String> collectionNames;+ final List<Collection> incomingSharedCollections;+ final Map<int, int> incomingCollectionSharedAtByID;++ const _SharedCollectionsContext({+ required this.collectionNames,+ required this.incomingSharedCollections,+ required this.incomingCollectionSharedAtByID,+ });++ factory _SharedCollectionsContext.fromCollections(+ List<Collection> collections, {+ required int userID,+ }) {+ final collectionNames = <int, String>{};+ final incomingSharedCollections = <Collection>[];+ final incomingCollectionSharedAtByID = <int, int>{};++ for (final collection in collections) {+ collectionNames[collection.id] = collection.displayName;+ if (collection.isDeleted || collection.isOwner(userID)) {+ continue;+ }+ incomingSharedCollections.add(collection);+ final sharedAt = collection.sharedAt;+ if (sharedAt != null && sharedAt > 0) {+ incomingCollectionSharedAtByID[collection.id] = sharedAt;+ }+ }++ return _SharedCollectionsContext(+ collectionNames: collectionNames,+ incomingSharedCollections: incomingSharedCollections,+ incomingCollectionSharedAtByID: incomingCollectionSharedAtByID,+ );+ }+}++class _SharedPhotoGroup {+ final int collectionID;+ final int ownerID;+ final int createdAt;+ final int oldestAddedTime;+ final List<int> sharedFileIDs;++ const _SharedPhotoGroup({+ required this.collectionID,+ required this.ownerID,+ required this.createdAt,+ required this.oldestAddedTime,+ required this.sharedFileIDs,+ });+}++class _SharedPhotoGroupBuilder {+ final int collectionID;+ final int ownerID;+ final int createdAt;+ int oldestAddedTime;+ final List<int> sharedFileIDs;++ _SharedPhotoGroupBuilder({+ required this.collectionID,+ required this.ownerID,+ required this.createdAt,+ required int firstFileID,+ }) : oldestAddedTime = createdAt,+ sharedFileIDs = [firstFileID];++ void add(int fileID, int addedTime) {+ sharedFileIDs.add(fileID);+ oldestAddedTime = addedTime;+ }++ _SharedPhotoGroup build() {+ return _SharedPhotoGroup(+ collectionID: collectionID,+ ownerID: ownerID,+ createdAt: createdAt,+ oldestAddedTime: oldestAddedTime,+ sharedFileIDs: List<int>.from(sharedFileIDs),+ );+ }+}++class _SharedPhotoGroupingState {+ final int sessionGapMicros;+ final Map<String, _SharedPhotoGroupBuilder> _activeGroups = {};+ final List<_SharedPhotoGroup> _closedGroups = [];++ _SharedPhotoGroupingState({+ required this.sessionGapMicros,+ });++ void addFile(EnteFile file) {+ final addedTime = file.addedTime;+ final ownerID = file.ownerID;+ final collectionID = file.collectionID;+ final uploadedFileID = file.uploadedFileID;+ if (addedTime == null ||+ ownerID == null ||+ collectionID == null ||+ uploadedFileID == null) {+ return;+ }++ final key = '${collectionID}_$ownerID';+ final currentGroup = _activeGroups[key];+ if (currentGroup == null) {+ _activeGroups[key] = _SharedPhotoGroupBuilder(+ collectionID: collectionID,+ ownerID: ownerID,+ createdAt: addedTime,+ firstFileID: uploadedFileID,+ );+ return;+ }++ final gapFromCurrentGroup = currentGroup.oldestAddedTime - addedTime;+ if (gapFromCurrentGroup <= sessionGapMicros) {+ currentGroup.add(uploadedFileID, addedTime);+ return;+ }++ _closedGroups.add(currentGroup.build());+ _activeGroups[key] = _SharedPhotoGroupBuilder(+ collectionID: collectionID,+ ownerID: ownerID,+ createdAt: addedTime,+ firstFileID: uploadedFileID,+ );+ }++ List<_SharedPhotoGroup> buildSnapshotSorted() {+ final groups = <_SharedPhotoGroup>[+ ..._closedGroups,+ ..._activeGroups.values.map((g) => g.build()),+ ];+ groups.sort((a, b) => b.createdAt.compareTo(a.createdAt));+ return groups;+ }}diff --git a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart b/mobile/apps/photos/lib/services/sync/remote_sync_service.dartindex 423dd71bc0..fb1534e160 100644--- a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart+++ b/mobile/apps/photos/lib/services/sync/remote_sync_service.dart@@ -1007,45 +1007,43 @@ class RemoteSyncService {});}- bool _shouldShowNotification(int collectionID) {+ bool _shouldShowSharedPhotosAndAlbumsNotification(int collectionID) {// TODO: Add option to opt out of notifications for a specific collection// Screen: https://www.figma.com/file/SYtMyLBs5SAOkTbfMMzhqt/ente-Visual-Design?type=design&node-id=7689-52943&t=IyWOfh0Gsb0p7yVC-4final isForeground = AppLifecycleService.instance.isForeground;- final bool showNotification =- NotificationService.instance.shouldShowNotificationsForSharedPhotos() &&- isFirstRemoteSyncDone() &&- !isForeground;+ final bool shouldShowSharedPhotosAndAlbumsNotification = NotificationService+ .instance+ .shouldShowNotificationsForSharedPhotosAndAlbums() &&+ isFirstRemoteSyncDone() &&+ !isForeground;_logger.info(- "[Collection-$collectionID] shouldShow notification: $showNotification, "+ "[Collection-$collectionID] shouldShow notification: "+ "$shouldShowSharedPhotosAndAlbumsNotification, ""isAppInForeground: $isForeground",);- return showNotification;+ return shouldShowSharedPhotosAndAlbumsNotification;}Future<void> _notifyNewFiles(List<int> collectionIDs) async {- final userID = Configuration.instance.getUserID();final appOpenTime = AppLifecycleService.instance.getLastAppOpenTime();for (final collectionID in collectionIDs) {- if (!_shouldShowNotification(collectionID)) {+ if (!_shouldShowSharedPhotosAndAlbumsNotification(collectionID)) {continue;}final files =await _db.getNewFilesInCollection(collectionID, appOpenTime);- final Set<int> sharedFilesIDs = {};- final Set<int> collectedFilesIDs = {};+ final Set<int> collectedFileIDs = {};for (final file in files) {- if (file.isUploaded && file.ownerID != userID) {- sharedFilesIDs.add(file.uploadedFileID!);- } else if (file.isUploaded && file.isCollect) {- collectedFilesIDs.add(file.uploadedFileID!);+ if (file.isUploaded && file.isCollect && file.uploadedFileID != null) {+ collectedFileIDs.add(file.uploadedFileID!);}}- final totalCount = sharedFilesIDs.length + collectedFilesIDs.length;+ final totalCount = collectedFileIDs.length;if (totalCount > 0) {final collection = _collectionsService.getCollectionByID(collectionID);_logger.info('creating notification for ${collection?.displayName} '- 'shared: $sharedFilesIDs, collected: $collectedFilesIDs files',+ 'collected: $collectedFileIDs files',);final s = await LanguageService.locals;// ignore: unawaited_futuresdiff --git a/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dart b/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dartindex 4d587b52e8..643585313d 100644--- a/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dart+++ b/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dart@@ -56,14 +56,14 @@ class NotificationSettingsScreen extends StatelessWidget {NotificationService.instance.hasGrantedPermissions() &&NotificationService.instance- .shouldShowNotificationsForSharedPhotos(),+ .shouldShowNotificationsForSharedPhotosAndAlbums(),onChanged: () async {await NotificationService.instance.requestPermissions();await NotificationService.instance- .setShouldShowNotificationsForSharedPhotos(+ .setShouldShowNotificationsForSharedPhotosAndAlbums(!NotificationService.instance- .shouldShowNotificationsForSharedPhotos(),+ .shouldShowNotificationsForSharedPhotosAndAlbums(),);},),