π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Reuse Locker collection selection utility
Neeraj Gupta0b14397436bf
diff --git a/mobile/apps/locker/lib/services/collections/collection_selectors.dart b/mobile/apps/locker/lib/services/collections/collection_selectors.dartdeleted file mode 100644index 0bd6b742e2..0000000000--- a/mobile/apps/locker/lib/services/collections/collection_selectors.dart+++ /dev/null@@ -1,18 +0,0 @@-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.dartindex 20fe64c432..8298f031b5 100644--- a/mobile/apps/locker/lib/services/collections/collections_service.dart+++ b/mobile/apps/locker/lib/services/collections/collections_service.dart@@ -14,7 +14,6 @@ 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";diff --git a/mobile/apps/locker/lib/services/favorites_service.dart b/mobile/apps/locker/lib/services/favorites_service.dartindex d51e60ab16..93490ed686 100644--- a/mobile/apps/locker/lib/services/favorites_service.dart+++ b/mobile/apps/locker/lib/services/favorites_service.dart@@ -2,12 +2,12 @@ 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';import 'package:locker/services/db/locker_db.dart';import 'package:locker/services/files/sync/models/file.dart';+import 'package:locker/utils/collection_list_util.dart';import 'package:logging/logging.dart';class FavoritesService {diff --git a/mobile/apps/locker/lib/utils/collection_list_util.dart b/mobile/apps/locker/lib/utils/collection_list_util.dartindex 59d6e7483f..0a1e3b1810 100644--- a/mobile/apps/locker/lib/utils/collection_list_util.dart+++ b/mobile/apps/locker/lib/utils/collection_list_util.dart@@ -17,3 +17,19 @@ Collection? findUserUncategorizedCollection(}return null;}++Collection? findOwnedCollection(+ Iterable<Collection> collections, {+ required int ownerID,+ required CollectionType type,+ String? name,+}) {+ for (final collection in collections) {+ if (collection.isOwner(ownerID) &&+ collection.type == type &&+ (name == null || collection.name == name)) {+ return collection;+ }+ }+ return null;+}diff --git a/mobile/apps/locker/test/services/collections/collection_selectors_test.dart b/mobile/apps/locker/test/services/collections/collection_selectors_test.dartdeleted file mode 100644index d987b25cbc..0000000000--- a/mobile/apps/locker/test/services/collections/collection_selectors_test.dart+++ /dev/null@@ -1,89 +0,0 @@-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,- );- });-}diff --git a/mobile/apps/locker/test/utils/collection_list_util_test.dart b/mobile/apps/locker/test/utils/collection_list_util_test.dartnew file mode 100644index 0000000000..3e520ef576--- /dev/null+++ b/mobile/apps/locker/test/utils/collection_list_util_test.dart@@ -0,0 +1,64 @@+import 'package:ente_sharing/models/user.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:locker/services/collections/models/collection.dart';+import 'package:locker/utils/collection_list_util.dart';++Collection collection(+ int id,+ int ownerID,+ CollectionType type,+ String? name,+) => Collection(+ id,+ User(id: ownerID, email: '[email protected]'),+ '',+ null,+ name,+ null,+ null,+ type,+ CollectionAttributes(),+ [if (ownerID != 1) User(id: 1, email: '[email protected]', role: 'COLLABORATOR')],+ const [],+ 0,+);++void main() {+ for (final (type, name) in const [+ (CollectionType.favorites, null),+ (CollectionType.folder, 'Documents'),+ ]) {+ group('$type default', () {+ final foreign = collection(1, 2, type, name);+ final owned = collection(2, 1, type, name ?? 'Renamed favorite');+ final wrongType = collection(3, 1, CollectionType.uncategorized, name);++ Collection? select(Iterable<Collection> collections) =>+ findOwnedCollection(collections, ownerID: 1, type: type, name: name);++ test(+ 'selects the owned match over an incoming collaborator collection',+ () {+ expect(foreign.canAdd(1), isTrue);+ expect(select([foreign, wrongType, owned]), same(owned));+ },+ );++ test('returns no match when only an incoming collection exists', () {+ expect(select([foreign]), isNull);+ });+ });+ }++ test('Documents requires its exact folder name', () {+ expect(+ findOwnedCollection(+ [collection(1, 1, CollectionType.folder, 'Other')],+ ownerID: 1,+ type: CollectionType.folder,+ name: 'Documents',+ ),+ isNull,+ );+ });+}