π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix cases of missing notifications
laurenspriemce0b5ab95428
diff --git a/mobile/apps/photos/lib/db/social_db.dart b/mobile/apps/photos/lib/db/social_db.dartindex 7ecc178020..0c69a58414 100644--- a/mobile/apps/photos/lib/db/social_db.dart+++ b/mobile/apps/photos/lib/db/social_db.dart@@ -575,6 +575,99 @@ class SocialDB {return rows.map(_rowToReaction).toList();}+ /// Gets all reactions on files after [sinceTime], excluding the current user.+ /// Returns reactions sorted by created_at DESC.+ Future<List<Reaction>> getReactionsOnFilesSince({+ required int excludeUserID,+ required int sinceTime,+ }) async {+ final db = await database;+ final rows = await db.query(+ _reactionsTable,+ where: 'file_id IS NOT NULL AND comment_id IS NULL '+ 'AND is_deleted = 0 AND user_id != ? AND created_at > ?',+ whereArgs: [excludeUserID, sinceTime],+ orderBy: 'created_at DESC',+ );+ return rows.map(_rowToReaction).toList();+ }++ /// Gets all comments on files after [sinceTime], excluding the current user.+ /// Returns comments sorted by created_at DESC.+ Future<List<Comment>> getCommentsOnFilesSince({+ required int excludeUserID,+ required int sinceTime,+ }) async {+ final db = await database;+ final rows = await db.query(+ _commentsTable,+ where: 'file_id IS NOT NULL AND parent_comment_id IS NULL '+ 'AND is_deleted = 0 AND user_id != ? AND created_at > ?',+ whereArgs: [excludeUserID, sinceTime],+ orderBy: 'created_at DESC',+ );+ return rows.map(_rowToComment).toList();+ }++ /// Gets all replies after [sinceTime], excluding the current user.+ /// Returns replies sorted by created_at DESC.+ Future<List<Comment>> getRepliesToUserCommentsSince({+ required int targetUserID,+ required int sinceTime,+ }) async {+ final db = await database;+ final rows = await db.query(+ _commentsTable,+ where: 'parent_comment_id IS NOT NULL AND is_deleted = 0 '+ 'AND user_id != ? AND created_at > ?',+ whereArgs: [targetUserID, sinceTime],+ orderBy: 'created_at DESC',+ );+ return rows.map(_rowToComment).toList();+ }++ /// Gets all reactions on top-level comments after [sinceTime], excluding the current user.+ /// Returns reactions sorted by created_at DESC.+ Future<List<Reaction>> getReactionsOnUserCommentsSince({+ required int targetUserID,+ required int sinceTime,+ }) async {+ final db = await database;+ final rows = await db.rawQuery(+ '''+ SELECT r.*, c.file_id AS linked_file_id FROM $_reactionsTable r+ INNER JOIN $_commentsTable c ON r.comment_id = c.id+ WHERE r.comment_id IS NOT NULL AND r.is_deleted = 0 AND r.user_id != ?+ AND r.created_at > ?+ AND c.is_deleted = 0 AND c.parent_comment_id IS NULL+ ORDER BY r.created_at DESC+ ''',+ [targetUserID, sinceTime],+ );+ return rows.map(_rowToReactionWithLinkedFileID).toList();+ }++ /// Gets all reactions on replies after [sinceTime], excluding the current user.+ /// Returns reactions sorted by created_at DESC.+ Future<List<Reaction>> getReactionsOnUserRepliesSince({+ required int targetUserID,+ required int sinceTime,+ }) async {+ final db = await database;+ final rows = await db.rawQuery(+ '''+ SELECT r.*, c.file_id AS linked_file_id FROM $_reactionsTable r+ INNER JOIN $_commentsTable c ON r.comment_id = c.id+ WHERE r.comment_id IS NOT NULL AND r.is_deleted = 0 AND r.user_id != ?+ AND r.created_at > ?+ AND c.is_deleted = 0 AND c.parent_comment_id IS NOT NULL+ ORDER BY r.created_at DESC+ ''',+ [targetUserID, sinceTime],+ );+ return rows.map(_rowToReactionWithLinkedFileID).toList();+ }+// ============ Cleanup Methods ============Future<void> deleteCollectionData(int collectionID) async {@@ -683,6 +776,17 @@ class SocialDB {);}+ Reaction _rowToReactionWithLinkedFileID(Map<String, dynamic> row) {+ final reaction = _rowToReaction(row);+ final linkedFileID = row['linked_file_id'] as int?;+ if (linkedFileID == null) {+ return reaction;+ }+ return reaction.fileID == null+ ? reaction.copyWith(fileID: linkedFileID)+ : reaction;+ }+Map<String, dynamic> _anonProfileToRow(AnonProfile profile) {return {'anon_user_id': profile.anonUserID,diff --git a/mobile/apps/photos/lib/services/app_lifecycle_service.dart b/mobile/apps/photos/lib/services/app_lifecycle_service.dartindex e75174da72..d97ab2de22 100644--- a/mobile/apps/photos/lib/services/app_lifecycle_service.dart+++ b/mobile/apps/photos/lib/services/app_lifecycle_service.dart@@ -33,10 +33,14 @@ class AppLifecycleService {void onAppInBackground(String reason) {_logger.info("App in background $reason");- _preferences.setInt(- keyLastAppOpenTime,- DateTime.now().microsecondsSinceEpoch,- );+ if (isForeground) {+ _preferences.setInt(+ keyLastAppOpenTime,+ DateTime.now().microsecondsSinceEpoch,+ );+ } else {+ _logger.info("App already in background, skipping open time update");+ }isForeground = false;}diff --git a/mobile/apps/photos/lib/services/notification_service.dart b/mobile/apps/photos/lib/services/notification_service.dartindex 79decc0475..6cbbcf76e2 100644--- a/mobile/apps/photos/lib/services/notification_service.dart+++ b/mobile/apps/photos/lib/services/notification_service.dart@@ -160,6 +160,7 @@ class NotificationService {Future<void> showNotification(String title,String message, {+ int? id,String channelID = "io.ente.photos",String channelName = "ente",String payload = "ente://home",@@ -179,7 +180,7 @@ class NotificationService {final platformChannelSpecs =NotificationDetails(android: androidSpecs, iOS: iosSpecs);await _notificationsPlugin.show(- channelName.hashCode,+ id ?? channelName.hashCode,title,message,platformChannelSpecs,diff --git a/mobile/apps/photos/lib/services/social_sync_service.dart b/mobile/apps/photos/lib/services/social_sync_service.dartindex fee0e92db5..f8b7ad7de8 100644--- a/mobile/apps/photos/lib/services/social_sync_service.dart+++ b/mobile/apps/photos/lib/services/social_sync_service.dart@@ -20,6 +20,10 @@ class SocialSyncService {final _logger = Logger('SocialSyncService');final _db = SocialDB.instance;final _api = SocialService.instance;+ final Map<int, int> _commentsEqualSeenCache = {};+ final Map<int, int> _reactionsEqualSeenCache = {};+ final Map<int, int> _commentsEqualSyncedCache = {};+ final Map<int, int> _reactionsEqualSyncedCache = {};bool _isSyncing = false;@@ -27,8 +31,11 @@ class SocialSyncService {////// Uses diff-based sync with separate sinceTime for comments and reactions.Future<void> syncCollection(int collectionID) async {- int commentsSinceTime = await _db.getCommentsSyncTime(collectionID);- int reactionsSinceTime = await _db.getReactionsSyncTime(collectionID);+ int normalizeSinceTime(int syncTime) => syncTime > 0 ? syncTime - 1 : 0;+ int commentsSinceTime =+ normalizeSinceTime(await _db.getCommentsSyncTime(collectionID));+ int reactionsSinceTime =+ normalizeSinceTime(await _db.getReactionsSyncTime(collectionID));int maxCommentsUpdatedAt = commentsSinceTime;int maxReactionsUpdatedAt = reactionsSinceTime;@@ -188,10 +195,27 @@ class SocialSyncService {final localReactionsSyncTime =await _db.getReactionsSyncTime(update.collectionID);+ final forceCommentsEqualSync = _shouldForceEqualSync(+ _commentsEqualSeenCache,+ _commentsEqualSyncedCache,+ update.collectionID,+ update.commentsUpdatedAt,+ localCommentsSyncTime,+ );+ final forceReactionsEqualSync = _shouldForceEqualSync(+ _reactionsEqualSeenCache,+ _reactionsEqualSyncedCache,+ update.collectionID,+ update.reactionsUpdatedAt,+ localReactionsSyncTime,+ );+final needsCommentsSync = update.commentsUpdatedAt != null &&- update.commentsUpdatedAt! > localCommentsSyncTime;+ (update.commentsUpdatedAt! > localCommentsSyncTime ||+ forceCommentsEqualSync);final needsReactionsSync = update.reactionsUpdatedAt != null &&- update.reactionsUpdatedAt! > localReactionsSyncTime;+ (update.reactionsUpdatedAt! > localReactionsSyncTime ||+ forceReactionsEqualSync);if (needsCommentsSync || needsReactionsSync) {await syncCollection(update.collectionID);@@ -234,6 +258,32 @@ class SocialSyncService {}}+ bool _shouldForceEqualSync(+ Map<int, int> seenCache,+ Map<int, int> syncedCache,+ int collectionID,+ int? remoteUpdatedAt,+ int localSyncTime,+ ) {+ if (remoteUpdatedAt == null || remoteUpdatedAt <= 0) {+ return false;+ }+ if (remoteUpdatedAt != localSyncTime) {+ seenCache.remove(collectionID);+ syncedCache.remove(collectionID);+ return false;+ }+ if (syncedCache[collectionID] == remoteUpdatedAt) {+ return false;+ }+ if (seenCache[collectionID] == remoteUpdatedAt) {+ syncedCache[collectionID] = remoteUpdatedAt;+ return true;+ }+ seenCache[collectionID] = remoteUpdatedAt;+ return false;+ }+/// Syncs anonymous profiles for a collection.Future<void> syncAnonProfiles(int collectionID) async {try {diff --git a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart b/mobile/apps/photos/lib/services/sync/remote_sync_service.dartindex 726a6fe81b..0a1904fcb6 100644--- a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart+++ b/mobile/apps/photos/lib/services/sync/remote_sync_service.dart@@ -1096,8 +1096,8 @@ class RemoteSyncService {lastNotifiedTime > appOpenTime ? lastNotifiedTime : appOpenTime;final hiddenCollectionIds = _collectionsService.getHiddenCollectionIds();+ final latestByKey = <String, _SocialActivityCandidate>{};- _SocialActivityCandidate? latest;void considerCandidate(_SocialActivityCandidate candidate) {if (candidate.createdAt <= cutoffTime) {return;@@ -1105,17 +1105,25 @@ class RemoteSyncService {if (hiddenCollectionIds.contains(candidate.collectionID)) {return;}- if (latest == null || candidate.createdAt > latest!.createdAt) {- latest = candidate;+ 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<Reaction> photoLikes =- await db.getReactionsOnFiles(excludeUserID: userID, limit: 1);- if (photoLikes.isNotEmpty) {- final reaction = photoLikes.first;+ final List<Reaction> photoLikes = await db.getReactionsOnFilesSince(+ excludeUserID: userID,+ sinceTime: cutoffTime,+ );+ for (final reaction in photoLikes) {considerCandidate(_SocialActivityCandidate(type: FeedItemType.photoLike,@@ -1126,10 +1134,11 @@ class RemoteSyncService {);}- final List<Comment> fileComments =- await db.getCommentsOnFiles(excludeUserID: userID, limit: 1);- if (fileComments.isNotEmpty) {- final comment = fileComments.first;+ final List<Comment> fileComments = await db.getCommentsOnFilesSince(+ excludeUserID: userID,+ sinceTime: cutoffTime,+ );+ for (final comment in fileComments) {considerCandidate(_SocialActivityCandidate(type: FeedItemType.comment,@@ -1142,10 +1151,11 @@ class RemoteSyncService {);}- final List<Comment> replies =- await db.getRepliesToUserComments(targetUserID: userID, limit: 1);- if (replies.isNotEmpty) {- final reply = replies.first;+ final List<Comment> replies = await db.getRepliesToUserCommentsSince(+ targetUserID: userID,+ sinceTime: cutoffTime,+ );+ for (final reply in replies) {considerCandidate(_SocialActivityCandidate(type: FeedItemType.reply,@@ -1159,9 +1169,11 @@ class RemoteSyncService {}final List<Reaction> commentLikes =- await db.getReactionsOnUserComments(targetUserID: userID, limit: 1);- if (commentLikes.isNotEmpty) {- final reaction = commentLikes.first;+ await db.getReactionsOnUserCommentsSince(+ targetUserID: userID,+ sinceTime: cutoffTime,+ );+ for (final reaction in commentLikes) {considerCandidate(_SocialActivityCandidate(type: FeedItemType.commentLike,@@ -1173,10 +1185,11 @@ class RemoteSyncService {);}- final List<Reaction> replyLikes =- await db.getReactionsOnUserReplies(targetUserID: userID, limit: 1);- if (replyLikes.isNotEmpty) {- final reaction = replyLikes.first;+ final List<Reaction> replyLikes = await db.getReactionsOnUserRepliesSince(+ targetUserID: userID,+ sinceTime: cutoffTime,+ );+ for (final reaction in replyLikes) {considerCandidate(_SocialActivityCandidate(type: FeedItemType.replyLike,@@ -1188,24 +1201,35 @@ class RemoteSyncService {);}- if (latest == null) {+ if (latestByKey.isEmpty) {return;}+ final candidates = latestByKey.values.toList()+ ..sort((a, b) => b.createdAt.compareTo(a.createdAt));final s = await LanguageService.locals;- final title = _getSocialNotificationTitle(latest!.type, s);- final message = _getSocialNotificationBody(latest!, s);-- await NotificationService.instance.showNotification(- title,- message,- channelID: "social_activity",- channelName: "Activity",- payload: _buildSocialNotificationPayload(latest!),- );++ for (final candidate in candidates) {+ final fileID = candidate.fileID;+ if (fileID == null) {+ continue;+ }+ await NotificationService.instance.showNotification(+ _getSocialNotificationTitle(candidate.type, s),+ _getSocialNotificationBody(candidate, s),+ channelID: "social_activity",+ channelName: "Activity",+ payload: _buildSocialNotificationPayload(candidate),+ id: _buildSocialNotificationId(+ candidate.collectionID,+ fileID,+ _notificationGroupForType(candidate.type),+ ),+ );+ }await _prefs.setInt(kLastSocialActivityNotificationTime,- latest!.createdAt,+ candidates.first.createdAt,);}@@ -1216,6 +1240,31 @@ class RemoteSyncService {!AppLifecycleService.instance.isForeground;}+ _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);+ }+String _getSocialNotificationTitle(FeedItemType type, AppLocalizations s) {switch (type) {case FeedItemType.comment:@@ -1308,3 +1357,8 @@ class _SocialActivityCandidate {this.commentText,});}++enum _SocialNotificationGroup {+ comment,+ like,+}