Skip to content

Commit 6acb9cf

Browse files
authored
[mob][photos] Deletion fixes (#5792)
## Description - Fix issue where user is not able to delete own files in a shared album - Fix issue where deletion of not yet uploaded files leads to showing grey boxes in gallery ## Tests Tested in debug mode on my pixel phone.
2 parents 87e5457 + 5ee2311 commit 6acb9cf

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

mobile/lib/models/gallery_type.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ extension GalleyTypeExtension on GalleryType {
104104
bool showDeleteOption() {
105105
switch (this) {
106106
case GalleryType.ownedCollection:
107+
case GalleryType.sharedCollection:
107108
case GalleryType.searchResults:
108109
case GalleryType.homepage:
109110
case GalleryType.favorite:
@@ -119,7 +120,6 @@ extension GalleyTypeExtension on GalleryType {
119120
case GalleryType.magic:
120121
return true;
121122
case GalleryType.trash:
122-
case GalleryType.sharedCollection:
123123
case GalleryType.sharedPublicCollection:
124124
return false;
125125
}

mobile/lib/services/collections_service.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,16 @@ class CollectionsService {
388388
.toList();
389389
}
390390

391+
List<int> getAllOwnedCollectionIDs() {
392+
final int userID = _config.getUserID()!;
393+
return _collectionIDToCollections.values
394+
.where(
395+
(c) => !c.isDeleted && c.isOwner(userID),
396+
)
397+
.map((e) => e.id)
398+
.toList();
399+
}
400+
391401
SharedCollections getSharedCollections() {
392402
final List<Collection> outgoing = [];
393403
final List<Collection> incoming = [];

mobile/lib/services/sync/trash_sync_service.dart

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import "package:ente_crypto/ente_crypto.dart";
77
import 'package:logging/logging.dart';
88
import 'package:photos/core/constants.dart';
99
import 'package:photos/core/event_bus.dart';
10+
import "package:photos/db/files_db.dart";
1011
import 'package:photos/db/trash_db.dart';
1112
import 'package:photos/events/collection_updated_event.dart';
1213
import 'package:photos/events/force_reload_trash_page_event.dart';
@@ -17,6 +18,7 @@ import 'package:photos/models/file/file.dart';
1718
import 'package:photos/models/file/trash_file.dart';
1819
import 'package:photos/models/ignored_file.dart';
1920
import "package:photos/models/metadata/file_magic.dart";
21+
import "package:photos/services/collections_service.dart";
2022
import 'package:photos/services/ignored_files_service.dart';
2123
import "package:photos/utils/file_key.dart";
2224
import 'package:shared_preferences/shared_preferences.dart';
@@ -108,10 +110,34 @@ class TrashSyncService {
108110
Future<void> trashFilesOnServer(List<TrashRequest> trashRequestItems) async {
109111
final includedFileIDs = <int>{};
110112
final uniqueItems = <TrashRequest>[];
113+
final ownedCollectionIDs =
114+
CollectionsService.instance.getAllOwnedCollectionIDs();
111115
for (final item in trashRequestItems) {
112116
if (!includedFileIDs.contains(item.fileID)) {
113-
uniqueItems.add(item);
114-
includedFileIDs.add(item.fileID);
117+
// Check if the collectionID in the request is owned by the user
118+
if (ownedCollectionIDs.contains(item.collectionID)) {
119+
uniqueItems.add(item);
120+
includedFileIDs.add(item.fileID);
121+
} else {
122+
// If not owned, use a different owned collectionID
123+
final fileCollectionIDs =
124+
await FilesDB.instance.getAllCollectionIDsOfFile(item.fileID);
125+
bool foundAnotherOwnedCollection = false;
126+
for (final collectionID in fileCollectionIDs) {
127+
if (ownedCollectionIDs.contains(collectionID)) {
128+
final newItem = TrashRequest(item.fileID, collectionID);
129+
uniqueItems.add(newItem);
130+
includedFileIDs.add(item.fileID);
131+
foundAnotherOwnedCollection = true;
132+
break;
133+
}
134+
}
135+
if (!foundAnotherOwnedCollection) {
136+
_logger.severe(
137+
"File ${item.fileID} is not owned by the user and has no other owned collection",
138+
);
139+
}
140+
}
115141
}
116142
}
117143
final requestData = <String, dynamic>{};

mobile/lib/ui/viewer/actions/file_selection_actions_widget.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class _FileSelectionActionsWidgetState
134134

135135
final bool anyOwnedFiles =
136136
split.pendingUploads.isNotEmpty || split.ownedByCurrentUser.isNotEmpty;
137+
final bool allOwnedFiles =
138+
ownedAndPendingUploadFilesCount > 0 && split.ownedByOtherUsers.isEmpty;
137139

138140
final bool anyUploadedFiles = split.ownedByCurrentUser.isNotEmpty;
139141
final showCollageOption = CollageCreatorPage.isValidCount(
@@ -259,7 +261,7 @@ class _FileSelectionActionsWidgetState
259261
icon: Icons.delete_outline,
260262
labelText: S.of(context).delete,
261263
onTap: anyOwnedFiles ? _onDeleteClick : null,
262-
shouldShow: ownedAndPendingUploadFilesCount > 0,
264+
shouldShow: allOwnedFiles,
263265
),
264266
);
265267
}

mobile/lib/utils/delete_file_util.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Future<void> deleteFilesOnDeviceOnly(
201201
final List<String> localAssetIDs = [];
202202
final List<String> localSharedMediaIDs = [];
203203
final List<String> alreadyDeletedIDs = []; // to ignore already deleted files
204+
final List<String?> localOnlyIDs = [];
204205
bool hasLocalOnlyFiles = false;
205206
for (final file in files) {
206207
if (file.localID != null) {
@@ -215,6 +216,7 @@ Future<void> deleteFilesOnDeviceOnly(
215216
}
216217
if (file.uploadedFileID == null) {
217218
hasLocalOnlyFiles = true;
219+
localOnlyIDs.add(file.localID);
218220
}
219221
}
220222
if (hasLocalOnlyFiles && Platform.isAndroid) {
@@ -237,8 +239,12 @@ Future<void> deleteFilesOnDeviceOnly(
237239
if (deletedIDs.contains(file.localID) ||
238240
alreadyDeletedIDs.contains(file.localID)) {
239241
deletedFiles.add(file);
240-
file.localID = null;
241-
await FilesDB.instance.update(file);
242+
if (hasLocalOnlyFiles && localOnlyIDs.contains(file.localID)) {
243+
await FilesDB.instance.deleteLocalFile(file);
244+
} else {
245+
file.localID = null;
246+
await FilesDB.instance.update(file);
247+
}
242248
}
243249
}
244250
if (deletedFiles.isNotEmpty || alreadyDeletedIDs.isNotEmpty) {

0 commit comments

Comments
 (0)