Add support for getOrCreate Uncategorized collection
This commit is contained in:
parent
b3571993eb
commit
c79d7e3bc2
|
@ -57,6 +57,7 @@ class CollectionsService {
|
|||
final _cachedUserIdToUser = <int, User>{};
|
||||
Collection? cachedDefaultHiddenCollection;
|
||||
Future<List<File>>? _cachedLatestFiles;
|
||||
Collection? cachedUncategorizedCollection;
|
||||
|
||||
CollectionsService._privateConstructor() {
|
||||
_db = CollectionsDB.instance;
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue