浏览代码

Add support for getOrCreate Uncategorized collection

Neeraj Gupta 2 年之前
父节点
当前提交
c79d7e3bc2
共有 2 个文件被更改,包括 40 次插入0 次删除
  1. 1 0
      lib/services/collections_service.dart
  2. 39 0
      lib/services/hidden_service.dart

+ 1 - 0
lib/services/collections_service.dart

@@ -57,6 +57,7 @@ class CollectionsService {
   final _cachedUserIdToUser = <int, User>{};
   Collection? cachedDefaultHiddenCollection;
   Future<List<File>>? _cachedLatestFiles;
+  Collection? cachedUncategorizedCollection;
 
   CollectionsService._privateConstructor() {
     _db = CollectionsDB.instance;

+ 39 - 0
lib/services/hidden_service.dart

@@ -41,6 +41,26 @@ extension HiddenService on CollectionsService {
     return cachedDefaultHiddenCollection!;
   }
 
+  // getUncategorizedCollection will return the uncategorized collection
+  // for the given user
+  Future<Collection> getUncategorizedCollection() async {
+    if (cachedUncategorizedCollection != null) {
+      return cachedUncategorizedCollection!;
+    }
+    final int userID = config.getUserID()!;
+    final Collection? matchedCollection =
+        collectionIDToCollections.values.firstWhereOrNull(
+      (element) =>
+          element.type == CollectionType.uncategorized &&
+          element.owner!.id == userID,
+    );
+    if (matchedCollection != null) {
+      cachedUncategorizedCollection = matchedCollection;
+      return cachedUncategorizedCollection!;
+    }
+    return _createUncategorizedCollection();
+  }
+
   Future<bool> hideFiles(
     BuildContext context,
     List<File> filesToHide, {
@@ -113,6 +133,25 @@ extension HiddenService on CollectionsService {
     return collectionFromServer;
   }
 
+  Future<Collection> _createUncategorizedCollection() async {
+    final key = CryptoUtil.generateKey();
+    final encKey = CryptoUtil.encryptSync(key, config.getKey()!);
+    final encName =
+        CryptoUtil.encryptSync(utf8.encode("Uncategorized") as Uint8List, key);
+    final collection = await createAndCacheCollection(
+      CreateRequest(
+        encryptedKey: Sodium.bin2base64(encKey.encryptedData!),
+        keyDecryptionNonce: Sodium.bin2base64(encKey.nonce!),
+        encryptedName: Sodium.bin2base64(encName.encryptedData!),
+        nameDecryptionNonce: Sodium.bin2base64(encName.nonce!),
+        type: CollectionType.uncategorized,
+        attributes: CollectionAttributes(),
+      ),
+    );
+    cachedUncategorizedCollection = collection;
+    return cachedUncategorizedCollection!;
+  }
+
   Future<CreateRequest> buildCollectionCreateRequest(
     String name, {
     required int visibility,