Skip to content

armgit / ente

Public Imported from GitHub
0

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

Auto remove files removed by admin

Neeraj Gupta0da66de98dde
diff --git a/mobile/apps/photos/lib/models/collection/action.dart b/mobile/apps/photos/lib/models/collection/action.dart
new file mode 100644
index 0000000000..c7a1d79a5e
--- /dev/null
+++ b/mobile/apps/photos/lib/models/collection/action.dart
@@ -0,0 +1,63 @@
+class CollectionAction {
+ const CollectionAction({
+ required this.id,
+ required this.userID,
+ required this.actorID,
+ required this.collectionID,
+ this.fileID,
+ required this.action,
+ });
+
+ final String id;
+ final int userID;
+ final int actorID;
+ final int collectionID;
+ final int? fileID;
+ final String action;
+
+ factory CollectionAction.fromJson(Map<String, dynamic> json) {
+ final userID = _parseInt(json["userID"] ?? json["userId"]) ??
+ (throw ArgumentError("CollectionAction.userID is missing"));
+ final actorID = _parseInt(json["actorUserID"] ?? json["actorUserId"]) ??
+ (throw ArgumentError("CollectionAction.actorID is missing"));
+ final collectionID =
+ _parseInt(json["collectionID"] ?? json["collectionId"]) ??
+ (throw ArgumentError("CollectionAction.collectionID is missing"));
+
+ return CollectionAction(
+ id: (json["id"] ?? "").toString(),
+ userID: userID,
+ actorID: actorID,
+ collectionID: collectionID,
+ fileID: _parseInt(json["fileID"] ?? json["fileId"]),
+ action: (json["action"] ?? "").toString(),
+ );
+ }
+
+ Map<String, dynamic> toJson() {
+ return <String, dynamic>{
+ "id": id,
+ "userID": userID,
+ "actorID": actorID,
+ "collectionID": collectionID,
+ if (fileID != null) "fileID": fileID,
+ "action": action,
+ };
+ }
+
+ static int? _parseInt(dynamic value) {
+ if (value == null) {
+ return null;
+ }
+ if (value is int) {
+ return value;
+ }
+ if (value is num) {
+ return value.toInt();
+ }
+ if (value is String) {
+ return int.tryParse(value);
+ }
+ return null;
+ }
+}
diff --git a/mobile/apps/photos/lib/services/collections_service.dart b/mobile/apps/photos/lib/services/collections_service.dart
index 44da931cfb..7653a3a80c 100644
--- a/mobile/apps/photos/lib/services/collections_service.dart
+++ b/mobile/apps/photos/lib/services/collections_service.dart
@@ -30,6 +30,7 @@ import 'package:photos/models/api/collection/create_request.dart';
import "package:photos/models/api/collection/public_url.dart";
import "package:photos/models/api/collection/user.dart";
import "package:photos/models/api/metadata.dart";
+import 'package:photos/models/collection/action.dart';
import 'package:photos/models/collection/collection.dart';
import 'package:photos/models/collection/collection_items.dart';
import 'package:photos/models/file/file.dart';
@@ -1271,6 +1272,29 @@ class CollectionsService {
}
}
+ Future<List<CollectionAction>> fetchPendingRemovalActions() async {
+ try {
+ final response = await _enteDio.get(
+ "/collection-actions/pending-remove/",
+ );
+ final List<dynamic> rawActions =
+ (response.data["actions"] as List<dynamic>?) ?? const [];
+ final actions = rawActions
+ .map(
+ (dynamic action) => CollectionAction.fromJson(
+ Map<String, dynamic>.from(action as Map<dynamic, dynamic>),
+ ),
+ )
+ .toList(growable: false);
+ _logger
+ .info("Fetched ${actions.length} pending collection removal actions");
+ return actions;
+ } catch (e, s) {
+ _logger.warning("Failed to fetch pending removal actions", e, s);
+ rethrow;
+ }
+ }
+
Future<Collection> getCollectionFromPublicLink(
BuildContext context,
Uri uri,
diff --git a/mobile/apps/photos/lib/services/hidden_service.dart b/mobile/apps/photos/lib/services/hidden_service.dart
index 256b8438c8..3067c38927 100644
--- a/mobile/apps/photos/lib/services/hidden_service.dart
+++ b/mobile/apps/photos/lib/services/hidden_service.dart
@@ -13,6 +13,7 @@ import 'package:photos/events/local_photos_updated_event.dart';
import "package:photos/generated/l10n.dart";
import 'package:photos/models/api/collection/create_request.dart';
import "package:photos/models/api/metadata.dart";
+import 'package:photos/models/collection/action.dart';
import 'package:photos/models/collection/collection.dart';
import 'package:photos/models/file/file.dart';
import "package:photos/models/metadata/collection_magic.dart";
@@ -109,6 +110,65 @@ extension HiddenService on CollectionsService {
return _createUncategorizedCollection();
}
+ Future<void> movePendingRemovalActionsToUncategorized() async {
+ try {
+ final pendingActions = await fetchPendingRemovalActions();
+ if (pendingActions.isEmpty) {
+ return;
+ }
+
+ final Map<int, Set<int>> collectionToFileIDs = {};
+ for (final CollectionAction action in pendingActions) {
+ final int? fileID = action.fileID;
+ if (fileID == null) {
+ continue;
+ }
+ collectionToFileIDs
+ .putIfAbsent(action.collectionID, () => <int>{})
+ .add(fileID);
+ }
+
+ if (collectionToFileIDs.isEmpty) {
+ return;
+ }
+
+ final List<int> uploadedFileIDs =
+ collectionToFileIDs.values.expand((ids) => ids).toSet().toList();
+ final Map<int, List<EnteFile>> localFilesByCollection =
+ await filesDB.getAllFilesGroupByCollectionID(uploadedFileIDs);
+ final Collection uncategorizedCollection =
+ await getUncategorizedCollection();
+
+ for (final MapEntry<int, Set<int>> entry in collectionToFileIDs.entries) {
+ final int collectionID = entry.key;
+ final Set<int> pendingFileIDs = entry.value;
+ final List<EnteFile> localFiles =
+ localFilesByCollection[collectionID] ?? <EnteFile>[];
+ final List<EnteFile> filesToMove = localFiles
+ .where(
+ (file) =>
+ file.uploadedFileID != null &&
+ pendingFileIDs.contains(file.uploadedFileID),
+ )
+ .toList();
+ if (filesToMove.isEmpty) {
+ continue;
+ }
+ await move(
+ filesToMove,
+ toCollectionID: uncategorizedCollection.id,
+ fromCollectionID: collectionID,
+ );
+ }
+ } catch (error, stackTrace) {
+ _logger.warning(
+ "Failed to process pending collection removal actions",
+ error,
+ stackTrace,
+ );
+ }
+ }
+
Future<bool> hideFiles(
BuildContext context,
List<EnteFile> filesToHide, {
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 1a8188858e..db5780181a 100644
--- a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart
+++ b/mobile/apps/photos/lib/services/sync/remote_sync_service.dart
@@ -27,6 +27,7 @@ import 'package:photos/models/upload_strategy.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/hidden_service.dart';
import 'package:photos/services/ignored_files_service.dart';
import "package:photos/services/language_service.dart";
import 'package:photos/services/local_file_update_service.dart';
@@ -127,6 +128,9 @@ class RemoteSyncService {
}
await _pullDiff();
await trashSyncService.syncTrash();
+ if (flagService.enableAdminRole) {
+ await _collectionsService.movePendingRemovalActionsToUncategorized();
+ }
if (!hasSyncedBefore) {
await _prefs.setBool(_isFirstRemoteSyncDone, true);
await syncDeviceCollectionFilesForUpload();