ente/lib/services/hidden_service.dart

197 lines
7 KiB
Dart
Raw Normal View History

2022-10-27 06:05:39 +00:00
import 'dart:convert';
import 'dart:typed_data';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
2022-10-27 07:23:45 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/files_updated_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
2022-10-27 06:05:39 +00:00
import 'package:photos/models/api/collection/create_request.dart';
import 'package:photos/models/collection.dart';
import 'package:photos/models/file.dart';
import "package:photos/models/metadata/collection_magic.dart";
import "package:photos/models/metadata/common_keys.dart";
2022-10-27 06:05:39 +00:00
import 'package:photos/services/collections_service.dart';
import 'package:photos/services/file_magic_service.dart';
import 'package:photos/utils/crypto_util.dart';
import 'package:photos/utils/dialog_util.dart';
extension HiddenService on CollectionsService {
static final _logger = Logger("HiddenCollectionService");
// getDefaultHiddenCollection will return null if there's no default
// collection
Future<Collection> getDefaultHiddenCollection() async {
if (cachedDefaultHiddenCollection != null) {
2022-12-30 09:27:50 +00:00
return cachedDefaultHiddenCollection!;
2022-10-27 06:05:39 +00:00
}
final int userID = config.getUserID()!;
final Collection? defaultHidden =
collectionIDToCollections.values.firstWhereOrNull(
(element) => element.isDefaultHidden() && element.owner!.id == userID,
);
if (defaultHidden != null) {
cachedDefaultHiddenCollection = defaultHidden;
2022-12-30 09:27:50 +00:00
return cachedDefaultHiddenCollection!;
2022-10-27 06:05:39 +00:00
}
final Collection createdHiddenCollection =
await _createDefaultHiddenAlbum();
2022-10-31 10:54:30 +00:00
cachedDefaultHiddenCollection = createdHiddenCollection;
2022-12-30 09:27:50 +00:00
return cachedDefaultHiddenCollection!;
2022-10-27 06:05:39 +00:00
}
// 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();
}
2022-10-27 06:05:39 +00:00
Future<bool> hideFiles(
BuildContext context,
List<File> filesToHide, {
bool forceHide = false,
}) async {
final int userID = config.getUserID()!;
final List<int> uploadedIDs = <int>[];
final dialog = createProgressDialog(
context,
2022-10-31 09:56:59 +00:00
"Hiding...",
2022-10-27 06:05:39 +00:00
);
await dialog.show();
try {
for (File file in filesToHide) {
if (file.uploadedFileID == null) {
throw AssertionError("Can only hide uploaded files");
}
if (file.ownerID != userID) {
throw AssertionError("Can only hide files owned by user");
}
uploadedIDs.add(file.uploadedFileID!);
}
final defaultHiddenCollection = await getDefaultHiddenCollection();
2022-10-27 06:05:39 +00:00
final Map<int, List<File>> collectionToFilesMap =
await filesDB.getAllFilesGroupByCollectionID(uploadedIDs);
for (MapEntry<int, List<File>> entry in collectionToFilesMap.entries) {
if (entry.key == defaultHiddenCollection.id) {
_logger.finest('file already part of hidden collection');
continue;
}
2022-10-27 06:05:39 +00:00
await move(defaultHiddenCollection.id, entry.key, entry.value);
}
2022-10-27 07:23:45 +00:00
Bus.instance.fire(
LocalPhotosUpdatedEvent(
filesToHide,
type: EventType.hide,
source: "hideFiles",
),
2022-10-31 09:56:59 +00:00
);
2022-10-27 06:05:39 +00:00
await dialog.hide();
} on AssertionError catch (e) {
await dialog.hide();
showErrorDialog(context, "Oops", e.message as String);
2022-12-15 04:01:51 +00:00
return false;
2022-10-27 06:05:39 +00:00
} catch (e, s) {
_logger.severe("Could not hide", e, s);
await dialog.hide();
showGenericErrorDialog(context: context);
2022-10-27 06:05:39 +00:00
return false;
} finally {
await dialog.hide();
}
return true;
}
Future<Collection> _createDefaultHiddenAlbum() async {
final CreateRequest createRequest = await buildCollectionCreateRequest(
".Hidden",
visibility: hiddenVisibility,
subType: subTypeDefaultHidden,
);
_logger.info("Creating Hidden Collection");
2022-12-30 08:46:07 +00:00
final collection = await createAndCacheCollection(createRequest);
_logger.info("Creating Hidden Collection Created Successfully");
final Collection collectionFromServer =
await fetchCollectionByID(collection.id);
_logger.info("Fetched Created Hidden Collection Successfully");
return collectionFromServer;
}
Future<Collection> _createUncategorizedCollection() async {
2023-02-27 10:44:27 +00:00
final uncategorizedCollectionKey = CryptoUtil.generateKey();
final encKey =
CryptoUtil.encryptSync(uncategorizedCollectionKey, config.getKey()!);
final encName = CryptoUtil.encryptSync(
utf8.encode("Uncategorized") as Uint8List,
uncategorizedCollectionKey,
);
final collection = await createAndCacheCollection(
CreateRequest(
2023-02-03 04:41:45 +00:00
encryptedKey: CryptoUtil.bin2base64(encKey.encryptedData!),
keyDecryptionNonce: CryptoUtil.bin2base64(encKey.nonce!),
encryptedName: CryptoUtil.bin2base64(encName.encryptedData!),
nameDecryptionNonce: CryptoUtil.bin2base64(encName.nonce!),
type: CollectionType.uncategorized,
attributes: CollectionAttributes(),
),
);
cachedUncategorizedCollection = collection;
return cachedUncategorizedCollection!;
}
Future<CreateRequest> buildCollectionCreateRequest(
String name, {
required int visibility,
required int subType,
}) async {
2023-02-27 10:44:27 +00:00
final collectionKey = CryptoUtil.generateKey();
final encryptedKeyData =
CryptoUtil.encryptSync(collectionKey, config.getKey()!);
2022-10-27 06:05:39 +00:00
final encryptedName = CryptoUtil.encryptSync(
utf8.encode(name) as Uint8List,
2023-02-27 10:44:27 +00:00
collectionKey,
2022-10-27 06:05:39 +00:00
);
final jsonToUpdate = CollectionMagicMetadata(
visibility: visibility,
subType: subType,
2022-10-27 06:05:39 +00:00
).toJson();
2022-10-31 10:12:04 +00:00
assert(jsonToUpdate.length == 2, "metadata should have two keys");
2022-10-27 06:05:39 +00:00
final encryptedMMd = await CryptoUtil.encryptChaCha(
utf8.encode(jsonEncode(jsonToUpdate)) as Uint8List,
2023-02-27 10:44:27 +00:00
collectionKey,
2022-10-27 06:05:39 +00:00
);
final MetadataRequest metadataRequest = MetadataRequest(
version: 1,
count: jsonToUpdate.length,
2023-02-03 04:41:45 +00:00
data: CryptoUtil.bin2base64(encryptedMMd.encryptedData!),
header: CryptoUtil.bin2base64(encryptedMMd.header!),
2022-10-27 06:05:39 +00:00
);
final CreateRequest createRequest = CreateRequest(
2023-02-03 04:41:45 +00:00
encryptedKey: CryptoUtil.bin2base64(encryptedKeyData.encryptedData!),
keyDecryptionNonce: CryptoUtil.bin2base64(encryptedKeyData.nonce!),
encryptedName: CryptoUtil.bin2base64(encryptedName.encryptedData!),
nameDecryptionNonce: CryptoUtil.bin2base64(encryptedName.nonce!),
2022-12-20 09:36:54 +00:00
type: CollectionType.album,
2022-10-27 06:05:39 +00:00
attributes: CollectionAttributes(),
magicMetadata: metadataRequest,
);
return createRequest;
2022-10-27 06:05:39 +00:00
}
}