Skip to content

armgit / ente

Public Imported from GitHub
0

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

social notification refactor

laurenspriembc64fa8d71f7
diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dart
index 76066ba137..c07d3bc1a7 100644
--- a/mobile/apps/photos/lib/main.dart
+++ b/mobile/apps/photos/lib/main.dart
@@ -40,6 +40,7 @@ import 'package:photos/services/memory_lane/memory_lane_service.dart';
import "package:photos/services/notification_service.dart";
import 'package:photos/services/push_service.dart';
import 'package:photos/services/search_service.dart';
+import 'package:photos/services/social_notification_coordinator.dart';
import 'package:photos/services/sync/local_sync_service.dart';
import 'package:photos/services/sync/remote_sync_service.dart';
import "package:photos/services/sync/sync_service.dart";
@@ -197,6 +198,7 @@ Future<void> _runMinimally(String taskId, TimeLogger tlog) async {
// Misc Services
await UserService.instance.init();
NotificationService.instance.init(prefs);
+ SocialNotificationCoordinator.instance.init(prefs);
await NotificationService.instance.initializeForBackground();
// Begin Execution
@@ -284,6 +286,7 @@ Future<void> _init(bool isBackground, {String via = ''}) async {
_logger.info("CollectionsService init $tlog");
await CollectionsService.instance.init(preferences);
_logger.info("CollectionsService init done $tlog");
+ SocialNotificationCoordinator.instance.init(preferences);
FavoritesService.instance.initFav().ignore();
LocalFileUpdateService.instance.init(preferences);
diff --git a/mobile/apps/photos/lib/models/social/feed_data_provider.dart b/mobile/apps/photos/lib/models/social/feed_data_provider.dart
index 5e943dae04..b05da720f8 100644
--- a/mobile/apps/photos/lib/models/social/feed_data_provider.dart
+++ b/mobile/apps/photos/lib/models/social/feed_data_provider.dart
@@ -6,6 +6,7 @@ import "package:photos/models/social/comment.dart";
import "package:photos/models/social/feed_item.dart";
import "package:photos/models/social/reaction.dart";
import "package:photos/services/collections_service.dart";
+import 'package:photos/services/social_notification_coordinator.dart';
import "package:photos/services/social_sync_service.dart";
/// Provider for feed data.
@@ -101,6 +102,9 @@ class FeedDataProvider {
Future<void> syncAllSharedCollections() async {
try {
await SocialSyncService.instance.syncAllSharedCollections();
+ await SocialNotificationCoordinator.instance.notifyAfterSocialSync(
+ trigger: SocialNotificationTrigger.feedRefresh,
+ );
} catch (e) {
_logger.warning('Failed to sync shared collections', e);
}
diff --git a/mobile/apps/photos/lib/services/social_notification_coordinator.dart b/mobile/apps/photos/lib/services/social_notification_coordinator.dart
new file mode 100644
index 0000000000..1db3851ef0
--- /dev/null
+++ b/mobile/apps/photos/lib/services/social_notification_coordinator.dart
@@ -0,0 +1,420 @@
+import 'dart:math';
+
+import 'package:logging/logging.dart';
+import 'package:photos/core/configuration.dart';
+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/social/comment.dart';
+import 'package:photos/models/social/feed_item.dart';
+import 'package:photos/models/social/reaction.dart';
+import 'package:photos/models/social/social_data_provider.dart';
+import 'package:photos/service_locator.dart';
+import 'package:photos/services/app_lifecycle_service.dart';
+import 'package:photos/services/collections_service.dart';
+import 'package:photos/services/language_service.dart';
+import 'package:photos/services/notification_service.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+enum SocialNotificationTrigger {
+ remoteSync,
+ feedRefresh,
+}
+
+class SocialNotificationCoordinator {
+ static const String kLastSocialActivityNotificationTime =
+ 'last_social_activity_notification_time';
+ static const String kLastSocialSeenTime = 'last_social_seen_time';
+ static const String kIsFirstRemoteSyncDoneKey = 'isFirstRemoteSyncDone';
+
+ static final SocialNotificationCoordinator instance =
+ SocialNotificationCoordinator._privateConstructor();
+
+ SocialNotificationCoordinator._privateConstructor();
+
+ final _logger = Logger('SocialNotificationCoordinator');
+ late final FilesDB _filesDb = FilesDB.instance;
+ late final CollectionsService _collectionsService =
+ CollectionsService.instance;
+
+ SharedPreferences? _prefs;
+ bool _isNotifying = false;
+
+ void init(SharedPreferences preferences) {
+ _prefs = preferences;
+ }
+
+ Future<void> markSocialSeen({int? timestampMicros}) async {
+ final prefs = await _ensurePrefs();
+ final now = timestampMicros ?? DateTime.now().microsecondsSinceEpoch;
+ final lastSeen = prefs.getInt(kLastSocialSeenTime) ?? 0;
+ if (now > lastSeen) {
+ await prefs.setInt(kLastSocialSeenTime, now);
+ }
+ }
+
+ Future<void> notifyAfterSocialSync({
+ required SocialNotificationTrigger trigger,
+ }) async {
+ if (_isNotifying) {
+ _logger.info('Social notification already in progress; skipping');
+ return;
+ }
+ _isNotifying = true;
+ try {
+ _logger.info('Evaluating social notifications after ${trigger.name}');
+ await _notifyNewSocialActivity();
+ } finally {
+ _isNotifying = false;
+ }
+ }
+
+ Future<SharedPreferences> _ensurePrefs() async {
+ if (_prefs != null) {
+ return _prefs!;
+ }
+ _prefs = await SharedPreferences.getInstance();
+ return _prefs!;
+ }
+
+ bool _shouldShowSocialNotifications(SharedPreferences prefs) {
+ return prefs.containsKey(kIsFirstRemoteSyncDoneKey) &&
+ (NotificationService.instance.shouldShowCommentNotifications() ||
+ NotificationService.instance.shouldShowLikeNotifications() ||
+ NotificationService.instance.shouldShowReplyNotifications());
+ }
+
+ int _getCutoffTime(SharedPreferences prefs) {
+ final lastNotified = prefs.getInt(kLastSocialActivityNotificationTime) ?? 0;
+ final hasSeenKey = prefs.containsKey(kLastSocialSeenTime);
+ final lastSeen = hasSeenKey ? (prefs.getInt(kLastSocialSeenTime) ?? 0) : 0;
+ final lastAppOpen =
+ prefs.getInt(AppLifecycleService.keyLastAppOpenTime) ?? 0;
+ final fallback = hasSeenKey ? lastSeen : lastAppOpen;
+ final cutoff = max(lastNotified, fallback);
+ if (cutoff == 0) {
+ return DateTime.now().microsecondsSinceEpoch;
+ }
+ return cutoff;
+ }
+
+ Future<void> _notifyNewSocialActivity() async {
+ final prefs = await _ensurePrefs();
+ if (!_shouldShowSocialNotifications(prefs)) {
+ return;
+ }
+ if (!flagService.isSocialEnabled) {
+ return;
+ }
+ final userID = Configuration.instance.getUserID();
+ if (userID == null) {
+ return;
+ }
+
+ final cutoffTime = _getCutoffTime(prefs);
+
+ final hiddenCollectionIds = _collectionsService.getHiddenCollectionIds();
+ final latestByKey = <String, _SocialActivityCandidate>{};
+ final bool enableCommentNotifications =
+ NotificationService.instance.shouldShowCommentNotifications();
+ final bool enableLikeNotifications =
+ NotificationService.instance.shouldShowLikeNotifications();
+ final bool enableReplyNotifications =
+ NotificationService.instance.shouldShowReplyNotifications();
+
+ void considerCandidate(_SocialActivityCandidate candidate) {
+ if (!_isSocialNotificationEnabledForType(candidate.type)) {
+ return;
+ }
+ if (candidate.createdAt <= cutoffTime) {
+ return;
+ }
+ if (hiddenCollectionIds.contains(candidate.collectionID)) {
+ return;
+ }
+ if (candidate.fileID == null) {
+ return;
+ }
+ final group = _notificationGroupForType(candidate.type);
+ final key =
+ '${candidate.collectionID}_${candidate.fileID}_${group.index}';
+ final existing = latestByKey[key];
+ if (existing == null || candidate.createdAt > existing.createdAt) {
+ latestByKey[key] = candidate;
+ }
+ }
+
+ final db = SocialDB.instance;
+
+ final List<Comment> fileComments = enableCommentNotifications
+ ? await db.getCommentsOnFilesSince(
+ excludeUserID: userID,
+ sinceTime: cutoffTime,
+ )
+ : <Comment>[];
+ for (final comment in fileComments) {
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.comment,
+ collectionID: comment.collectionID,
+ fileID: comment.fileID,
+ commentID: comment.id,
+ createdAt: comment.createdAt,
+ actorUserID: comment.userID,
+ actorAnonID: comment.anonUserID,
+ ),
+ );
+ }
+
+ final List<Comment> replies = enableReplyNotifications
+ ? await db.getRepliesToUserCommentsSince(
+ targetUserID: userID,
+ sinceTime: cutoffTime,
+ )
+ : <Comment>[];
+ final List<Reaction> photoLikes = enableLikeNotifications
+ ? await db.getReactionsOnFilesSince(
+ excludeUserID: userID,
+ sinceTime: cutoffTime,
+ )
+ : <Reaction>[];
+
+ final repliesNeedingOwnerCheck = <Comment>[];
+ final fileIDsNeedingOwnership = <int>{};
+
+ for (final reply in replies) {
+ if (reply.parentCommentUserID == userID) {
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.reply,
+ collectionID: reply.collectionID,
+ fileID: reply.fileID,
+ commentID: reply.id,
+ createdAt: reply.createdAt,
+ actorUserID: reply.userID,
+ actorAnonID: reply.anonUserID,
+ ),
+ );
+ } else if (reply.fileID != null) {
+ repliesNeedingOwnerCheck.add(reply);
+ fileIDsNeedingOwnership.add(reply.fileID!);
+ }
+ }
+
+ for (final reaction in photoLikes) {
+ if (reaction.fileID != null) {
+ fileIDsNeedingOwnership.add(reaction.fileID!);
+ }
+ }
+
+ final filesByID =
+ await _filesDb.getFileIDToFileFromIDs(fileIDsNeedingOwnership.toList());
+
+ bool isOwnedByUser(int? fileID) {
+ if (fileID == null) {
+ return false;
+ }
+ final file = filesByID[fileID];
+ return file != null && file.ownerID == userID;
+ }
+
+ for (final reaction in photoLikes) {
+ if (!isOwnedByUser(reaction.fileID)) {
+ continue;
+ }
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.photoLike,
+ collectionID: reaction.collectionID,
+ fileID: reaction.fileID,
+ createdAt: reaction.createdAt,
+ actorUserID: reaction.userID,
+ actorAnonID: reaction.anonUserID,
+ ),
+ );
+ }
+
+ for (final reply in repliesNeedingOwnerCheck) {
+ if (!isOwnedByUser(reply.fileID)) {
+ continue;
+ }
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.reply,
+ collectionID: reply.collectionID,
+ fileID: reply.fileID,
+ commentID: reply.id,
+ createdAt: reply.createdAt,
+ actorUserID: reply.userID,
+ actorAnonID: reply.anonUserID,
+ ),
+ );
+ }
+
+ if (latestByKey.isEmpty) {
+ return;
+ }
+
+ final candidates = latestByKey.values.toList()
+ ..sort((a, b) => b.createdAt.compareTo(a.createdAt));
+ final s = await LanguageService.locals;
+
+ for (final candidate in candidates) {
+ final fileID = candidate.fileID;
+ if (fileID == null) {
+ continue;
+ }
+ try {
+ final title = await _getSocialNotificationTitle(candidate);
+ await NotificationService.instance.showNotification(
+ title,
+ _getSocialNotificationBody(candidate.type, s),
+ channelID: 'social_activity',
+ channelName: 'Ente Feed',
+ payload: _buildSocialNotificationPayload(candidate),
+ id: _buildSocialNotificationId(
+ candidate.collectionID,
+ fileID,
+ _notificationGroupForType(candidate.type),
+ ),
+ );
+ } catch (e, stackTrace) {
+ _logger.severe(
+ 'Failed to prepare social notification',
+ e,
+ stackTrace,
+ );
+ }
+ }
+
+ await prefs.setInt(
+ kLastSocialActivityNotificationTime,
+ candidates.first.createdAt,
+ );
+ }
+
+ bool _isSocialNotificationEnabledForType(FeedItemType type) {
+ switch (type) {
+ case FeedItemType.comment:
+ return NotificationService.instance.shouldShowCommentNotifications();
+ case FeedItemType.reply:
+ return NotificationService.instance.shouldShowReplyNotifications();
+ case FeedItemType.photoLike:
+ return NotificationService.instance.shouldShowLikeNotifications();
+ case FeedItemType.commentLike:
+ case FeedItemType.replyLike:
+ return false; // Currently not notifying for comment/reply likes
+ }
+ }
+
+ _SocialNotificationGroup _notificationGroupForType(FeedItemType type) {
+ switch (type) {
+ case FeedItemType.comment:
+ case FeedItemType.reply:
+ return _SocialNotificationGroup.comment;
+ case FeedItemType.photoLike:
+ case FeedItemType.commentLike:
+ case FeedItemType.replyLike:
+ return _SocialNotificationGroup.like;
+ }
+ }
+
+ int _buildSocialNotificationId(
+ int collectionID,
+ 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;
+ return base | (hash & 0x0fffffff);
+ }
+
+ Future<String> _getSocialNotificationTitle(
+ _SocialActivityCandidate candidate,
+ ) async {
+ final userID = candidate.actorUserID;
+ final anonID = candidate.actorAnonID;
+ if (userID <= 0 && anonID != null) {
+ return SocialDataProvider.instance.getAnonDisplayName(
+ anonID,
+ candidate.collectionID,
+ fallback: anonID,
+ );
+ }
+ final user =
+ _collectionsService.getFileOwner(userID, candidate.collectionID);
+ return user.nameOrEmail;
+ }
+
+ String _getSocialNotificationBody(
+ FeedItemType type,
+ AppLocalizations s,
+ ) {
+ return _getSocialNotificationDetail(type, s);
+ }
+
+ String _getSocialNotificationDetail(
+ FeedItemType type,
+ AppLocalizations s,
+ ) {
+ switch (type) {
+ case FeedItemType.photoLike:
+ return s.likedYourPhoto;
+ case FeedItemType.comment:
+ return s.commentedOnYourPhoto;
+ case FeedItemType.reply:
+ return s.repliedToYourComment;
+ case FeedItemType.commentLike:
+ return s.likedYourComment;
+ case FeedItemType.replyLike:
+ return s.likedYourReply;
+ }
+ }
+
+ String _buildSocialNotificationPayload(_SocialActivityCandidate candidate) {
+ final params = <String, String>{
+ 'type': candidate.type.name,
+ 'collectionID': candidate.collectionID.toString(),
+ };
+ if (candidate.fileID != null) {
+ params['fileID'] = candidate.fileID.toString();
+ }
+ if (candidate.commentID != null) {
+ params['commentID'] = candidate.commentID!;
+ }
+ return Uri(
+ scheme: 'ente',
+ host: 'feed',
+ queryParameters: params,
+ ).toString();
+ }
+}
+
+class _SocialActivityCandidate {
+ final FeedItemType type;
+ final int collectionID;
+ final int? fileID;
+ final String? commentID;
+ final int createdAt;
+ final int actorUserID;
+ final String? actorAnonID;
+
+ _SocialActivityCandidate({
+ required this.type,
+ required this.collectionID,
+ required this.createdAt,
+ required this.actorUserID,
+ this.fileID,
+ this.commentID,
+ this.actorAnonID,
+ });
+}
+
+enum _SocialNotificationGroup {
+ comment,
+ like,
+}
diff --git a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart b/mobile/apps/photos/lib/services/sync/remote_sync_service.dart
index 2b037aec3c..ef0dadfab3 100644
--- a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart
+++ b/mobile/apps/photos/lib/services/sync/remote_sync_service.dart
@@ -10,7 +10,6 @@ import 'package:photos/core/event_bus.dart';
import 'package:photos/db/device_files_db.dart';
import 'package:photos/db/file_updation_db.dart';
import 'package:photos/db/files_db.dart';
-import 'package:photos/db/social_db.dart';
import 'package:photos/events/backup_folders_updated_event.dart';
import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/diff_sync_complete_event.dart';
@@ -18,17 +17,11 @@ import 'package:photos/events/files_updated_event.dart';
import 'package:photos/events/force_reload_home_gallery_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/events/sync_status_update_event.dart';
-import 'package:photos/extensions/user_extension.dart';
-import 'package:photos/generated/l10n.dart';
import 'package:photos/main.dart' show isProcessBg;
import 'package:photos/models/device_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';
-import 'package:photos/models/social/reaction.dart';
-import 'package:photos/models/social/social_data_provider.dart';
import 'package:photos/models/upload_strategy.dart';
import 'package:photos/service_locator.dart';
import 'package:photos/services/app_lifecycle_service.dart';
@@ -38,6 +31,7 @@ import 'package:photos/services/ignored_files_service.dart';
import 'package:photos/services/language_service.dart';
import 'package:photos/services/local_file_update_service.dart';
import 'package:photos/services/notification_service.dart';
+import 'package:photos/services/social_notification_coordinator.dart';
import 'package:photos/services/social_sync_service.dart';
import 'package:photos/services/sync/diff_fetcher.dart';
import 'package:photos/services/sync/sync_service.dart';
@@ -84,9 +78,6 @@ class RemoteSyncService {
static const kEditTimeFeatureReleaseTime = 1635460000000000;
static const kMaximumPermissibleUploadsInThrottledMode = 4;
- static const kLastSocialActivityNotificationTime =
- "last_social_activity_notification_time";
-
static final RemoteSyncService instance =
RemoteSyncService._privateConstructor();
@@ -1099,342 +1090,11 @@ class RemoteSyncService {
try {
_logger.info("Starting social sync");
await SocialSyncService.instance.syncAllSharedCollections();
- await _notifyNewSocialActivity();
+ await SocialNotificationCoordinator.instance.notifyAfterSocialSync(
+ trigger: SocialNotificationTrigger.remoteSync,
+ );
} catch (e) {
_logger.severe("Social sync failed, continuing", e);
}
}
-
- Future<void> _notifyNewSocialActivity() async {
- if (!_shouldShowSocialNotifications()) {
- return;
- }
- if (!flagService.isSocialEnabled) {
- return;
- }
- final userID = Configuration.instance.getUserID();
- if (userID == null) {
- return;
- }
- final appOpenTime = AppLifecycleService.instance.getLastAppOpenTime();
- if (appOpenTime <= 0) {
- return;
- }
-
- final lastNotifiedTime =
- _prefs.getInt(kLastSocialActivityNotificationTime) ?? 0;
- final cutoffTime =
- lastNotifiedTime > appOpenTime ? lastNotifiedTime : appOpenTime;
-
- final hiddenCollectionIds = _collectionsService.getHiddenCollectionIds();
- final latestByKey = <String, _SocialActivityCandidate>{};
- final bool enableCommentNotifications =
- NotificationService.instance.shouldShowCommentNotifications();
- final bool enableLikeNotifications =
- NotificationService.instance.shouldShowLikeNotifications();
- final bool enableReplyNotifications =
- NotificationService.instance.shouldShowReplyNotifications();
-
- void considerCandidate(_SocialActivityCandidate candidate) {
- if (!_isSocialNotificationEnabledForType(candidate.type)) {
- return;
- }
- if (candidate.createdAt <= cutoffTime) {
- return;
- }
- if (hiddenCollectionIds.contains(candidate.collectionID)) {
- return;
- }
- if (candidate.fileID == null) {
- return;
- }
- final group = _notificationGroupForType(candidate.type);
- final key =
- '${candidate.collectionID}_${candidate.fileID}_${group.index}';
- final existing = latestByKey[key];
- if (existing == null || candidate.createdAt > existing.createdAt) {
- latestByKey[key] = candidate;
- }
- }
-
- final db = SocialDB.instance;
-
- final List<Comment> fileComments = enableCommentNotifications
- ? await db.getCommentsOnFilesSince(
- excludeUserID: userID,
- sinceTime: cutoffTime,
- )
- : <Comment>[];
- for (final comment in fileComments) {
- considerCandidate(
- _SocialActivityCandidate(
- type: FeedItemType.comment,
- collectionID: comment.collectionID,
- fileID: comment.fileID,
- commentID: comment.id,
- createdAt: comment.createdAt,
- actorUserID: comment.userID,
- actorAnonID: comment.anonUserID,
- ),
- );
- }
-
- final List<Comment> replies = enableReplyNotifications
- ? await db.getRepliesToUserCommentsSince(
- targetUserID: userID,
- sinceTime: cutoffTime,
- )
- : <Comment>[];
- final List<Reaction> photoLikes = enableLikeNotifications
- ? await db.getReactionsOnFilesSince(
- excludeUserID: userID,
- sinceTime: cutoffTime,
- )
- : <Reaction>[];
-
- final repliesNeedingOwnerCheck = <Comment>[];
- final fileIDsNeedingOwnership = <int>{};
-
- for (final reply in replies) {
- if (reply.parentCommentUserID == userID) {
- considerCandidate(
- _SocialActivityCandidate(
- type: FeedItemType.reply,
- collectionID: reply.collectionID,
- fileID: reply.fileID,
- commentID: reply.id,
- createdAt: reply.createdAt,
- actorUserID: reply.userID,
- actorAnonID: reply.anonUserID,
- ),
- );
- } else if (reply.fileID != null) {
- repliesNeedingOwnerCheck.add(reply);
- fileIDsNeedingOwnership.add(reply.fileID!);
- }
- }
-
- for (final reaction in photoLikes) {
- if (reaction.fileID != null) {
- fileIDsNeedingOwnership.add(reaction.fileID!);
- }
- }
-
- final filesByID =
- await _db.getFileIDToFileFromIDs(fileIDsNeedingOwnership.toList());
-
- bool isOwnedByUser(int? fileID) {
- if (fileID == null) {
- return false;
- }
- final file = filesByID[fileID];
- return file != null && file.ownerID == userID;
- }
-
- for (final reaction in photoLikes) {
- if (!isOwnedByUser(reaction.fileID)) {
- continue;
- }
- considerCandidate(
- _SocialActivityCandidate(
- type: FeedItemType.photoLike,
- collectionID: reaction.collectionID,
- fileID: reaction.fileID,
- createdAt: reaction.createdAt,
- actorUserID: reaction.userID,
- actorAnonID: reaction.anonUserID,
- ),
- );
- }
-
- for (final reply in repliesNeedingOwnerCheck) {
- if (!isOwnedByUser(reply.fileID)) {
- continue;
- }
- considerCandidate(
- _SocialActivityCandidate(
- type: FeedItemType.reply,
- collectionID: reply.collectionID,
- fileID: reply.fileID,
- commentID: reply.id,
- createdAt: reply.createdAt,
- actorUserID: reply.userID,
- actorAnonID: reply.anonUserID,
- ),
- );
- }
-
- if (latestByKey.isEmpty) {
- return;
- }
-
- final candidates = latestByKey.values.toList()
- ..sort((a, b) => b.createdAt.compareTo(a.createdAt));
- final s = await LanguageService.locals;
-
- for (final candidate in candidates) {
- final fileID = candidate.fileID;
- if (fileID == null) {
- continue;
- }
- try {
- final title = await _getSocialNotificationTitle(candidate);
- await _showNotificationSafely(
- title: title,
- message: _getSocialNotificationBody(candidate.type, s),
- channelID: "social_activity",
- channelName: "Ente Feed",
- payload: _buildSocialNotificationPayload(candidate),
- id: _buildSocialNotificationId(
- candidate.collectionID,
- fileID,
- _notificationGroupForType(candidate.type),
- ),
- context:
- 'social type=${candidate.type.name} collection=${candidate.collectionID} file=$fileID',
- );
- } catch (e, stackTrace) {
- _logger.severe(
- "Failed to prepare social notification",
- e,
- stackTrace,
- );
- }
- }
- await _prefs.setInt(
- kLastSocialActivityNotificationTime,
- candidates.first.createdAt,
- );
- }
-
- bool _shouldShowSocialNotifications() {
- return isFirstRemoteSyncDone() &&
- (NotificationService.instance.shouldShowCommentNotifications() ||
- NotificationService.instance.shouldShowLikeNotifications() ||
- NotificationService.instance.shouldShowReplyNotifications());
- }
-
- bool _isSocialNotificationEnabledForType(FeedItemType type) {
- switch (type) {
- case FeedItemType.comment:
- return NotificationService.instance.shouldShowCommentNotifications();
- case FeedItemType.reply:
- return NotificationService.instance.shouldShowReplyNotifications();
- case FeedItemType.photoLike:
- return NotificationService.instance.shouldShowLikeNotifications();
- case FeedItemType.commentLike:
- case FeedItemType.replyLike:
- return false; // Currently not notifying for comment/reply likes
- }
- }
-
- _SocialNotificationGroup _notificationGroupForType(FeedItemType type) {
- switch (type) {
- case FeedItemType.comment:
- case FeedItemType.reply:
- return _SocialNotificationGroup.comment;
- case FeedItemType.photoLike:
- case FeedItemType.commentLike:
- case FeedItemType.replyLike:
- return _SocialNotificationGroup.like;
- }
- }
-
- int _buildSocialNotificationId(
- int collectionID,
- 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;
- return base | (hash & 0x0fffffff);
- }
-
- Future<String> _getSocialNotificationTitle(
- _SocialActivityCandidate candidate,
- ) async {
- final userID = candidate.actorUserID;
- final anonID = candidate.actorAnonID;
- if (userID <= 0 && anonID != null) {
- return SocialDataProvider.instance.getAnonDisplayName(
- anonID,
- candidate.collectionID,
- fallback: anonID,
- );
- }
- final user =
- _collectionsService.getFileOwner(userID, candidate.collectionID);
- return user.nameOrEmail;
- }
-
- String _getSocialNotificationBody(
- FeedItemType type,
- AppLocalizations s,
- ) {
- return _getSocialNotificationDetail(type, s);
- }
-
- String _getSocialNotificationDetail(
- FeedItemType type,
- AppLocalizations s,
- ) {
- switch (type) {
- case FeedItemType.photoLike:
- return s.likedYourPhoto;
- case FeedItemType.comment:
- return s.commentedOnYourPhoto;
- case FeedItemType.reply:
- return s.repliedToYourComment;
- case FeedItemType.commentLike:
- return s.likedYourComment;
- case FeedItemType.replyLike:
- return s.likedYourReply;
- }
- }
-
- String _buildSocialNotificationPayload(_SocialActivityCandidate candidate) {
- final params = <String, String>{
- 'type': candidate.type.name,
- 'collectionID': candidate.collectionID.toString(),
- };
- if (candidate.fileID != null) {
- params['fileID'] = candidate.fileID.toString();
- }
- if (candidate.commentID != null) {
- params['commentID'] = candidate.commentID!;
- }
- return Uri(
- scheme: 'ente',
- host: 'feed',
- queryParameters: params,
- ).toString();
- }
-}
-
-class _SocialActivityCandidate {
- final FeedItemType type;
- final int collectionID;
- final int? fileID;
- final String? commentID;
- final int createdAt;
- final int actorUserID;
- final String? actorAnonID;
-
- _SocialActivityCandidate({
- required this.type,
- required this.collectionID,
- required this.createdAt,
- required this.actorUserID,
- this.fileID,
- this.commentID,
- this.actorAnonID,
- });
-}
-
-enum _SocialNotificationGroup {
- comment,
- like,
}
diff --git a/mobile/apps/photos/lib/ui/social/comments_screen.dart b/mobile/apps/photos/lib/ui/social/comments_screen.dart
index 3449595685..aea30f0d67 100644
--- a/mobile/apps/photos/lib/ui/social/comments_screen.dart
+++ b/mobile/apps/photos/lib/ui/social/comments_screen.dart
@@ -13,6 +13,7 @@ import "package:photos/models/social/comment.dart";
import "package:photos/models/social/reaction.dart";
import "package:photos/models/social/social_data_provider.dart";
import "package:photos/services/collections_service.dart";
+import 'package:photos/services/social_notification_coordinator.dart';
import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/components/buttons/icon_button_widget.dart";
@@ -147,6 +148,7 @@ class _FileCommentsBottomSheetState extends State<FileCommentsBottomSheet> {
@override
void initState() {
super.initState();
+ unawaited(SocialNotificationCoordinator.instance.markSocialSeen());
_textController = TextEditingController();
_inputFocusNode = FocusNode()..addListener(_onInputFocusChange);
_scrollController = ScrollController()..addListener(_onScroll);
diff --git a/mobile/apps/photos/lib/ui/social/feed_screen.dart b/mobile/apps/photos/lib/ui/social/feed_screen.dart
index 7c1ef1242e..6689d37636 100644
--- a/mobile/apps/photos/lib/ui/social/feed_screen.dart
+++ b/mobile/apps/photos/lib/ui/social/feed_screen.dart
@@ -9,6 +9,7 @@ import "package:photos/generated/l10n.dart";
import "package:photos/models/social/feed_data_provider.dart";
import "package:photos/models/social/feed_item.dart";
import "package:photos/models/social/social_data_provider.dart";
+import 'package:photos/services/social_notification_coordinator.dart';
import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/components/buttons/icon_button_widget.dart";
@@ -98,6 +99,7 @@ class _FeedScreenState extends State<FeedScreen> {
super.initState();
_currentUserID = Configuration.instance.getUserID() ?? 0;
_pendingNavigationTarget = widget.initialTarget;
+ unawaited(SocialNotificationCoordinator.instance.markSocialSeen());
_loadFeedItems();
}
diff --git a/mobile/apps/photos/lib/ui/social/likes_bottom_sheet.dart b/mobile/apps/photos/lib/ui/social/likes_bottom_sheet.dart
index 998a986cbe..cf76779de6 100644
--- a/mobile/apps/photos/lib/ui/social/likes_bottom_sheet.dart
+++ b/mobile/apps/photos/lib/ui/social/likes_bottom_sheet.dart
@@ -11,6 +11,7 @@ import "package:photos/models/collection/collection.dart";
import "package:photos/models/social/reaction.dart";
import "package:photos/models/social/social_data_provider.dart";
import "package:photos/services/collections_service.dart";
+import 'package:photos/services/social_notification_coordinator.dart';
import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/components/buttons/icon_button_widget.dart";
@@ -65,6 +66,7 @@ class _LikesBottomSheetState extends State<LikesBottomSheet> {
@override
void initState() {
super.initState();
+ unawaited(SocialNotificationCoordinator.instance.markSocialSeen());
_currentUserID = Configuration.instance.getUserID()!;
_selectedCollectionID = widget.initialCollectionID;
_loadSharedCollections();