Add support for getOrCreate Uncategorized collection

This commit is contained in:
Neeraj Gupta 2023-01-05 09:08:27 +05:30
parent b3571993eb
commit c79d7e3bc2
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
2 changed files with 40 additions and 0 deletions

View file

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

View file

@ -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,