Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Harden memories social interactions

ashilkn4cc77d013fd3
diff --git a/mobile/apps/photos/lib/services/collections_service.dart b/mobile/apps/photos/lib/services/collections_service.dart
index 41e9581e7c..f65902652b 100644
--- a/mobile/apps/photos/lib/services/collections_service.dart
+++ b/mobile/apps/photos/lib/services/collections_service.dart
@@ -332,50 +332,6 @@ class CollectionsService {
return sharedCollections;
}
- /// Returns the count of shared collections containing this file.
- /// Uses early exit optimization - stops counting at 2.
- ///
- /// Returns:
- /// - 0: File not in any shared collection
- /// - 1: File in exactly one shared collection
- /// - 2: File in multiple shared collections (early exit)
- Future<int> getSharedCollectionCountForFile(int uploadedFileID) async {
- final Set<int> collectionIDs = await _filesDB.getAllCollectionIDsOfFile(
- uploadedFileID,
- );
-
- if (collectionIDs.isEmpty) {
- return 0;
- }
-
- final int? currentUserID = _config.getUserID();
- if (currentUserID == null) {
- return 0;
- }
-
- int sharedCount = 0;
- for (final int collectionID in collectionIDs) {
- final Collection? collection = _collectionIDToCollections[collectionID];
-
- if (collection == null || collection.isDeleted) {
- continue;
- }
-
- // Same logic as isFileInSharedCollection
- if (collection.hasSharees ||
- collection.hasLink ||
- !collection.isOwner(currentUserID)) {
- sharedCount++;
- // Early exit: we only need to distinguish between 0, 1, and >1
- if (sharedCount > 1) {
- return 2;
- }
- }
- }
-
- return sharedCount;
- }
-
Future<List<Collection>> getArchivedCollection() async {
final allCollections = getCollectionsForUI();
return allCollections
diff --git a/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart b/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart
index acdad1bf3a..d234f402f2 100644
--- a/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart
+++ b/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart
@@ -93,24 +93,17 @@ class _LikeCollectionSelectorSheetState
// Load file if not provided (for thumbnail)
_file ??= await FilesDB.instance.getAnyUploadedFile(widget.fileID);
- // Get all collections containing this file
- final collectionIDs = await FilesDB.instance.getAllCollectionIDsOfFile(
- widget.fileID,
- );
-
- // Filter to shared collections only
- final sharedCollections = collectionIDs
- .map((id) => CollectionsService.instance.getCollectionByID(id))
- .whereType<Collection>()
- .where(
- (c) =>
- (c.hasSharees ||
- c.hasLink ||
- !c.isOwner(widget.currentUserID)) &&
- (widget.allowedCollectionIDs == null ||
+ final sharedCollections =
+ (await CollectionsService.instance.getSharedCollectionsForFile(
+ widget.fileID,
+ includeHidden: true,
+ ))
+ .where(
+ (c) =>
+ (widget.allowedCollectionIDs == null ||
widget.allowedCollectionIDs!.contains(c.id)),
- )
- .toList();
+ )
+ .toList();
// If no shared collections, close the sheet
if (sharedCollections.isEmpty) {
diff --git a/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart b/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart
index be7bf552a6..e748bf4292 100644
--- a/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart
+++ b/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart
@@ -55,6 +55,7 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
Comment? _latestComment;
User? _latestCommentAuthor;
int _loadGeneration = 0;
+ int? _reactionUpdateFileID;
@override
void initState() {
@@ -72,6 +73,7 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
oldWidget.currentUserID != widget.currentUserID ||
oldWidget.includeHiddenCollections != widget.includeHiddenCollections) {
_loadGeneration++;
+ _reactionUpdateFileID = null;
_clearSocialState();
unawaited(_refreshSocialState());
}
@@ -175,48 +177,58 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
final file = widget.file;
final fileID = file.uploadedFileID;
final currentUserID = widget.currentUserID;
- if (fileID == null || currentUserID == null || _sharedCollections.isEmpty) {
+ if (fileID == null ||
+ currentUserID == null ||
+ _sharedCollections.isEmpty ||
+ _reactionUpdateFileID == fileID) {
return;
}
- if (_hasLiked) {
- await _unlikeFromAllCollections(fileID, currentUserID);
- return;
- }
+ _reactionUpdateFileID = fileID;
+ try {
+ if (_hasLiked) {
+ await _unlikeFromAllCollections(fileID, currentUserID);
+ return;
+ }
- if (_sharedCollections.length == 1) {
- final previousState = _hasLiked;
- setState(() => _hasLiked = true);
- try {
- await SocialDataProvider.instance.toggleReaction(
- userID: currentUserID,
- collectionID: _sharedCollections.single.id,
- fileID: fileID,
- );
- } catch (error, stackTrace) {
- _logger.severe("Failed to like photo", error, stackTrace);
- if (mounted && widget.file.uploadedFileID == fileID) {
- setState(() => _hasLiked = previousState);
- showShortToast(
- context,
- AppLocalizations.of(context).failedToUpdateLike,
+ if (_sharedCollections.length == 1) {
+ final previousState = _hasLiked;
+ setState(() => _hasLiked = true);
+ try {
+ await SocialDataProvider.instance.toggleReaction(
+ userID: currentUserID,
+ collectionID: _sharedCollections.single.id,
+ fileID: fileID,
);
+ } catch (error, stackTrace) {
+ _logger.severe("Failed to like photo", error, stackTrace);
+ if (mounted && widget.file.uploadedFileID == fileID) {
+ setState(() => _hasLiked = previousState);
+ showShortToast(
+ context,
+ AppLocalizations.of(context).failedToUpdateLike,
+ );
+ }
}
+ return;
}
- return;
- }
- await _runSheetAndRefresh(
- () => showLikeCollectionSelectorSheet(
- context,
- fileID: fileID,
- currentUserID: currentUserID,
- file: file,
- allowedCollectionIDs: _sharedCollections
- .map((collection) => collection.id)
- .toSet(),
- ),
- );
+ await _runSheetAndRefresh(
+ () => showLikeCollectionSelectorSheet(
+ context,
+ fileID: fileID,
+ currentUserID: currentUserID,
+ file: file,
+ allowedCollectionIDs: _sharedCollections
+ .map((collection) => collection.id)
+ .toSet(),
+ ),
+ );
+ } finally {
+ if (_reactionUpdateFileID == fileID) {
+ _reactionUpdateFileID = null;
+ }
+ }
}
Future<void> _unlikeFromAllCollections(int fileID, int currentUserID) async {
@@ -294,7 +306,10 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
await _runSheetAndRefresh(
() => showFileCommentsBottomSheet(
context,
- collectionID: comment?.collectionID ?? _sharedCollections.first.id,
+ collectionID:
+ comment?.collectionID ??
+ _latestComment?.collectionID ??
+ _sharedCollections.first.id,
fileID: fileID,
highlightCommentID: comment?.id,
),