Skip to content

armgit / ente

Public Imported from GitHub
0

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

Require owned Locker default collections

Neeraj Guptab179d2d874dc
diff --git a/mobile/apps/locker/lib/services/collections/collection_selectors.dart b/mobile/apps/locker/lib/services/collections/collection_selectors.dart
new file mode 100644
index 0000000000..0bd6b742e2
--- /dev/null
+++ b/mobile/apps/locker/lib/services/collections/collection_selectors.dart
@@ -0,0 +1,18 @@
+import 'package:locker/services/collections/models/collection.dart';
+
+Collection? findOwnedCollection(
+ Iterable<Collection> collections, {
+ required int ownerID,
+ required CollectionType type,
+ String? name,
+}) {
+ for (final collection in collections) {
+ if (!collection.isDeleted &&
+ collection.isOwner(ownerID) &&
+ collection.type == type &&
+ (name == null || collection.name == name)) {
+ return collection;
+ }
+ }
+ return null;
+}
diff --git a/mobile/apps/locker/lib/services/collections/collections_service.dart b/mobile/apps/locker/lib/services/collections/collections_service.dart
index 7a860b280b..20fe64c432 100644
--- a/mobile/apps/locker/lib/services/collections/collections_service.dart
+++ b/mobile/apps/locker/lib/services/collections/collections_service.dart
@@ -14,6 +14,7 @@ import "package:flutter/material.dart";
import 'package:locker/core/errors.dart';
import 'package:locker/events/collections_updated_event.dart';
import 'package:locker/events/user_details_refresh_event.dart';
+import "package:locker/services/collections/collection_selectors.dart";
import "package:locker/services/collections/collections_api_client.dart";
import 'package:locker/services/collections/models/collection.dart';
import "package:locker/services/collections/models/files_split.dart";
@@ -373,10 +374,14 @@ class CollectionService {
Future<Collection> getOrCreateImportantCollection() async {
final collections = await getCollections();
- for (final collection in collections) {
- if (collection.type == CollectionType.favorites) {
- return collection;
- }
+ final userID = Configuration.instance.getUserID()!;
+ final ownedCollection = findOwnedCollection(
+ collections,
+ ownerID: userID,
+ type: CollectionType.favorites,
+ );
+ if (ownedCollection != null) {
+ return ownedCollection;
}
_logger.info(
"No favorites collection found, creating important collection.",
@@ -715,11 +720,15 @@ class CollectionService {
Future<Collection> _getOrCreateDocumentsCollection() async {
final collections = await getCollections();
- for (final collection in collections) {
- if (collection.type == CollectionType.folder &&
- collection.name == "Documents") {
- return collection;
- }
+ final userID = Configuration.instance.getUserID()!;
+ final collection = findOwnedCollection(
+ collections,
+ ownerID: userID,
+ type: CollectionType.folder,
+ name: "Documents",
+ );
+ if (collection != null) {
+ return collection;
}
_logger.info(
"No Documents collection found, creating Documents collection.",
diff --git a/mobile/apps/locker/lib/services/favorites_service.dart b/mobile/apps/locker/lib/services/favorites_service.dart
index 44a44ad9cc..d51e60ab16 100644
--- a/mobile/apps/locker/lib/services/favorites_service.dart
+++ b/mobile/apps/locker/lib/services/favorites_service.dart
@@ -2,6 +2,7 @@ import 'dart:async';
import 'package:ente_events/event_bus.dart';
import 'package:locker/events/collections_updated_event.dart';
+import 'package:locker/services/collections/collection_selectors.dart';
import 'package:locker/services/collections/collections_service.dart';
import 'package:locker/services/collections/models/collection.dart';
import 'package:locker/services/configuration.dart';
@@ -58,6 +59,8 @@ class FavoritesService {
}
}
}
+ } else {
+ clearCache();
}
}
@@ -132,18 +135,14 @@ class FavoritesService {
}
Future<void> addToFavorites(EnteFile file) async {
- final collectionID = await _getOrCreateFavoriteCollectionID();
+ final favCollection = await _getOrCreateFavoriteCollection();
final List<EnteFile> files = [file];
if (file.uploadedFileID == null) {
_logger.severe("Cannot favorite file without uploadedFileID");
throw AssertionError("Can only favorite uploaded items");
} else {
- final collection = await _collectionService.getCollectionByID(
- collectionID,
- );
-
- await _collectionService.addToCollection(collection!, files[0]);
+ await _collectionService.addToCollection(favCollection, files[0]);
}
_updateFavoriteFilesCache(files, favFlag: true);
@@ -159,23 +158,19 @@ class FavoritesService {
if (files.any((f) => f.ownerID != currentUserID)) {
throw AssertionError("Can not favorite files owned by others");
}
- final collectionID = await _getOrCreateFavoriteCollectionID();
+ final favCollection = await _getOrCreateFavoriteCollection();
if (favFlag) {
- final collection = await _collectionService.getCollectionByID(
- collectionID,
- );
for (final file in files) {
- await _collectionService.addToCollection(collection!, file);
+ await _collectionService.addToCollection(favCollection, file);
}
} else {
- final Collection? favCollection = await getFavoritesCollection();
for (final file in files) {
final currentCollections = await _collectionService
.getCollectionsForFile(file);
Collection? targetCollection;
for (final col in currentCollections) {
- if (col.id != favCollection!.id) {
+ if (col.id != favCollection.id) {
targetCollection = col;
break;
}
@@ -184,7 +179,7 @@ class FavoritesService {
targetCollection ??= await _collectionService
.getOrCreateUncategorizedCollection();
- await _collectionService.move([file], favCollection!, targetCollection);
+ await _collectionService.move([file], favCollection, targetCollection);
}
}
_updateFavoriteFilesCache(files, favFlag: favFlag);
@@ -195,11 +190,15 @@ class FavoritesService {
if (inUploadID == null) {
} else {
final Collection? favCollection = await getFavoritesCollection();
+ if (favCollection == null) {
+ _updateFavoriteFilesCache([file], favFlag: false);
+ return;
+ }
if (file.ownerID != _config.getUserID() &&
file.hash != null &&
_cachedFavFileHashes.containsKey(file.hash!)) {
- final favFiles = await _db.getFilesInCollection(favCollection!);
+ final favFiles = await _db.getFilesInCollection(favCollection);
final favFile = favFiles.firstWhere(
(f) => f.uploadedFileID == _cachedFavFileHashes[file.hash!],
orElse: () => file,
@@ -207,7 +206,7 @@ class FavoritesService {
file = favFile;
}
- if (file.collectionID != favCollection!.id) {
+ if (file.collectionID != favCollection.id) {
final favFiles = await _db.getFilesInCollection(favCollection);
final favFile = favFiles.firstWhere(
(f) => f.uploadedFileID == file.uploadedFileID,
@@ -238,20 +237,33 @@ class FavoritesService {
}
Future<Collection?> getFavoritesCollection() async {
- if (_cachedFavoritesCollectionID == null) {
- final collections = await _collectionService.getCollections();
-
- for (final collection in collections) {
- if (collection.owner.id == _config.getUserID() &&
- collection.type == CollectionType.favorites) {
- _cachedFavoritesCollectionID = collection.id;
- return collection;
- }
- }
+ final userID = _config.getUserID();
+ if (userID == null) {
+ clearCache();
return null;
}
+ final cachedID = _cachedFavoritesCollectionID;
+ if (cachedID != null) {
+ final cachedCollection = await _collectionService.getCollectionByID(
+ cachedID,
+ );
+ if (cachedCollection != null &&
+ !cachedCollection.isDeleted &&
+ cachedCollection.isOwner(userID) &&
+ cachedCollection.type == CollectionType.favorites) {
+ return cachedCollection;
+ }
+ clearCache();
+ }
- return _collectionService.getCollectionByID(_cachedFavoritesCollectionID!);
+ final collections = await _collectionService.getCollections();
+ final collection = findOwnedCollection(
+ collections,
+ ownerID: userID,
+ type: CollectionType.favorites,
+ );
+ _cachedFavoritesCollectionID = collection?.id;
+ return collection;
}
Future<int?> getFavoriteCollectionID() async {
@@ -259,15 +271,16 @@ class FavoritesService {
return collection?.id;
}
- Future<int> _getOrCreateFavoriteCollectionID() async {
- if (_cachedFavoritesCollectionID != null) {
- return _cachedFavoritesCollectionID!;
+ Future<Collection> _getOrCreateFavoriteCollection() async {
+ final existingCollection = await getFavoritesCollection();
+ if (existingCollection != null) {
+ return existingCollection;
}
final collection = await _collectionService
.getOrCreateImportantCollection();
_cachedFavoritesCollectionID = collection.id;
- return collection.id;
+ return collection;
}
}
diff --git a/mobile/apps/locker/test/services/collections/collection_selectors_test.dart b/mobile/apps/locker/test/services/collections/collection_selectors_test.dart
new file mode 100644
index 0000000000..d987b25cbc
--- /dev/null
+++ b/mobile/apps/locker/test/services/collections/collection_selectors_test.dart
@@ -0,0 +1,89 @@
+import 'package:ente_sharing/models/user.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:locker/services/collections/collection_selectors.dart';
+import 'package:locker/services/collections/models/collection.dart';
+
+Collection collection(
+ int id, {
+ required int ownerID,
+ required CollectionType type,
+ String? name,
+ bool isDeleted = false,
+}) => Collection(
+ id,
+ User(id: ownerID, email: '[email protected]'),
+ '',
+ null,
+ name,
+ null,
+ null,
+ type,
+ CollectionAttributes(),
+ const [],
+ const [],
+ 0,
+ isDeleted: isDeleted,
+);
+
+void main() {
+ for (final (type, name) in const [
+ (CollectionType.favorites, null),
+ (CollectionType.folder, 'Documents'),
+ ]) {
+ group('$type default', () {
+ final foreign = collection(1, ownerID: 2, type: type, name: name);
+ final owned = collection(2, ownerID: 1, type: type, name: name);
+ final deleted = collection(
+ 3,
+ ownerID: 1,
+ type: type,
+ name: name,
+ isDeleted: true,
+ );
+
+ Collection? select(Iterable<Collection> collections, int ownerID) =>
+ findOwnedCollection(
+ collections,
+ ownerID: ownerID,
+ type: type,
+ name: name,
+ );
+
+ test('selects the live owned match', () {
+ final wrongType = collection(
+ 4,
+ ownerID: 1,
+ type: CollectionType.uncategorized,
+ name: name,
+ );
+ expect(select([foreign, deleted, wrongType, owned], 1), same(owned));
+ });
+
+ test('returns no match when only foreign or deleted defaults exist', () {
+ expect(select([foreign, deleted], 1), isNull);
+ });
+
+ test('uses the current account owner', () {
+ expect(select([owned, foreign], 2), same(foreign));
+ });
+ });
+ }
+
+ test('Documents requires its exact folder name', () {
+ final otherFolder = collection(
+ 1,
+ ownerID: 1,
+ type: CollectionType.folder,
+ name: 'Other',
+ );
+ expect(
+ findOwnedCollection(
+ [otherFolder],
+ ownerID: 1,
+ type: CollectionType.folder,
+ name: 'Documents',
+ ),
+ isNull,
+ );
+ });
+}