Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob] Add social notificaitons for internal users

Neeraj Gupta66f475437c0f
diff --git a/mobile/apps/photos/lib/services/sync/sync_service.dart b/mobile/apps/photos/lib/services/sync/sync_service.dart
index 61de98cb2a..17e2687274 100644
--- a/mobile/apps/photos/lib/services/sync/sync_service.dart
+++ b/mobile/apps/photos/lib/services/sync/sync_service.dart
@@ -12,9 +12,14 @@ import 'package:photos/core/event_bus.dart';
import 'package:photos/events/subscription_purchased_event.dart';
import 'package:photos/events/sync_status_update_event.dart';
import 'package:photos/events/trigger_logout_event.dart';
+import "package:photos/generated/l10n.dart";
import "package:photos/main.dart";
+import "package:photos/models/social/comment.dart";
import 'package:photos/models/file/file_type.dart';
+import "package:photos/models/social/feed_item.dart";
+import "package:photos/models/social/reaction.dart";
import "package:photos/service_locator.dart";
+import "package:photos/services/app_lifecycle_service.dart";
import "package:photos/services/language_service.dart";
import 'package:photos/services/notification_service.dart';
import 'package:photos/services/social_sync_service.dart';
@@ -22,6 +27,7 @@ import 'package:photos/services/sync/local_sync_service.dart';
import 'package:photos/services/sync/remote_sync_service.dart';
import 'package:photos/utils/file_uploader.dart';
import 'package:shared_preferences/shared_preferences.dart';
+import "package:photos/db/social_db.dart";
class SyncService {
final _logger = Logger("SyncService");
@@ -35,6 +41,8 @@ class SyncService {
static const kLastStorageLimitExceededNotificationPushTime =
"last_storage_limit_exceeded_notification_push_time";
+ static const kLastSocialActivityNotificationTime =
+ "last_social_activity_notification_time";
SyncService._privateConstructor() {
Bus.instance.on<SubscriptionPurchasedEvent>().listen((event) {
@@ -219,6 +227,7 @@ class SyncService {
try {
_logger.info("[SYNC] Starting social sync");
await SocialSyncService.instance.syncAllSharedCollections();
+ await _notifyNewSocialActivity();
} catch (e) {
_logger.warning("[SYNC] Social sync failed, continuing", e);
}
@@ -241,4 +250,179 @@ class SyncService {
);
}
}
+
+ Future<void> _notifyNewSocialActivity() async {
+ if (!_shouldShowSocialNotifications()) {
+ return;
+ }
+ if (!flagService.internalUser) {
+ 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();
+
+ _SocialActivityCandidate? latest;
+ void considerCandidate(_SocialActivityCandidate candidate) {
+ if (candidate.createdAt <= cutoffTime) {
+ return;
+ }
+ if (hiddenCollectionIds.contains(candidate.collectionID)) {
+ return;
+ }
+ if (latest == null || candidate.createdAt > latest!.createdAt) {
+ latest = candidate;
+ }
+ }
+
+ final db = SocialDB.instance;
+
+ final List<Reaction> photoLikes =
+ await db.getReactionsOnFiles(excludeUserID: userID, limit: 1);
+ if (photoLikes.isNotEmpty) {
+ final reaction = photoLikes.first;
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.photoLike,
+ collectionID: reaction.collectionID,
+ fileID: reaction.fileID,
+ createdAt: reaction.createdAt,
+ ),
+ );
+ }
+
+ final List<Comment> fileComments =
+ await db.getCommentsOnFiles(excludeUserID: userID, limit: 1);
+ if (fileComments.isNotEmpty) {
+ final comment = fileComments.first;
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.comment,
+ collectionID: comment.collectionID,
+ fileID: comment.fileID,
+ createdAt: comment.createdAt,
+ ),
+ );
+ }
+
+ final List<Comment> replies =
+ await db.getRepliesToUserComments(targetUserID: userID, limit: 1);
+ if (replies.isNotEmpty) {
+ final reply = replies.first;
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.reply,
+ collectionID: reply.collectionID,
+ fileID: reply.fileID,
+ createdAt: reply.createdAt,
+ ),
+ );
+ }
+
+ final List<Reaction> commentLikes =
+ await db.getReactionsOnUserComments(targetUserID: userID, limit: 1);
+ if (commentLikes.isNotEmpty) {
+ final reaction = commentLikes.first;
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.commentLike,
+ collectionID: reaction.collectionID,
+ fileID: reaction.fileID,
+ createdAt: reaction.createdAt,
+ ),
+ );
+ }
+
+ final List<Reaction> replyLikes =
+ await db.getReactionsOnUserReplies(targetUserID: userID, limit: 1);
+ if (replyLikes.isNotEmpty) {
+ final reaction = replyLikes.first;
+ considerCandidate(
+ _SocialActivityCandidate(
+ type: FeedItemType.replyLike,
+ collectionID: reaction.collectionID,
+ fileID: reaction.fileID,
+ createdAt: reaction.createdAt,
+ ),
+ );
+ }
+
+ if (latest == null) {
+ return;
+ }
+
+ final s = await LanguageService.locals;
+ final message = _getSocialNotificationMessage(latest!.type, s);
+ final collection = collectionsService.getCollectionByID(
+ latest!.collectionID,
+ );
+ final title = collection?.displayName ?? "ente";
+
+ await NotificationService.instance.showNotification(
+ title,
+ message,
+ channelID: "social_activity",
+ channelName: "Activity",
+ payload: "ente://feed",
+ );
+ await _prefs.setInt(
+ kLastSocialActivityNotificationTime,
+ latest!.createdAt,
+ );
+ }
+
+ bool _shouldShowSocialNotifications() {
+ return NotificationService.instance
+ .shouldShowNotificationsForSharedPhotos() &&
+ RemoteSyncService.instance.isFirstRemoteSyncDone() &&
+ !AppLifecycleService.instance.isForeground;
+ }
+
+ String _getSocialNotificationMessage(
+ 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;
+ }
+ }
+}
+
+class _SocialActivityCandidate {
+ final FeedItemType type;
+ final int collectionID;
+ final int? fileID;
+ final int createdAt;
+
+ _SocialActivityCandidate({
+ required this.type,
+ required this.collectionID,
+ required this.createdAt,
+ this.fileID,
+ });
}
diff --git a/mobile/apps/photos/lib/ui/tabs/home_widget.dart b/mobile/apps/photos/lib/ui/tabs/home_widget.dart
index 703cbf9746..07b9108d70 100644
--- a/mobile/apps/photos/lib/ui/tabs/home_widget.dart
+++ b/mobile/apps/photos/lib/ui/tabs/home_widget.dart
@@ -69,6 +69,7 @@ import "package:photos/ui/home/landing_page_widget.dart";
import "package:photos/ui/home/loading_photos_widget.dart";
import "package:photos/ui/home/start_backup_hook_widget.dart";
import "package:photos/ui/notification/update/change_log_page.dart";
+import "package:photos/ui/social/feed_screen.dart";
import "package:photos/ui/rituals/ritual_camera_page.dart";
import "package:photos/ui/rituals/ritual_page.dart";
import "package:photos/ui/settings/app_update_dialog.dart";
@@ -1080,6 +1081,14 @@ class _HomeWidgetState extends State<HomeWidget> {
);
return;
}
+ if (uri != null && uri.host.toLowerCase() == "feed") {
+ if (!Configuration.instance.isLoggedIn()) {
+ return;
+ }
+ // ignore: unawaited_futures
+ routeToPage(context, const FeedScreen());
+ return;
+ }
if (payload.toLowerCase().contains("onthisday")) {
_logger.info("On this day notification received");
// ignore: unawaited_futures