ente/lib/services/favorites_service.dart

238 lines
8.2 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
2022-12-30 08:52:17 +00:00
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
2020-10-24 10:25:02 +00:00
import 'package:photos/db/files_db.dart';
import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/files_updated_event.dart';
2022-12-30 08:46:07 +00:00
import 'package:photos/models/api/collection/create_request.dart';
2023-08-25 04:39:30 +00:00
import 'package:photos/models/collection/collection.dart';
import 'package:photos/models/file/file.dart';
import 'package:photos/services/collections_service.dart';
import 'package:photos/services/remote_sync_service.dart';
import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
import 'package:photos/utils/crypto_util.dart';
2020-05-06 16:43:03 +00:00
2020-10-03 17:56:18 +00:00
class FavoritesService {
2022-12-30 08:52:17 +00:00
late Configuration _config;
2022-12-30 08:52:17 +00:00
late CollectionsService _collectionsService;
late CollectionActions _collectionActions;
2022-12-30 08:52:17 +00:00
late FilesDB _filesDB;
int? _cachedFavoritesCollectionID;
final Set<int> _cachedFavUploadedIDs = {};
final Set<String> _cachedPendingLocalIDs = {};
2022-12-30 08:52:17 +00:00
late StreamSubscription<CollectionUpdatedEvent>
_collectionUpdatesSubscription;
2023-06-13 06:41:31 +00:00
FavoritesService._privateConstructor();
Future<void> initFav() async {
_config = Configuration.instance;
_collectionsService = CollectionsService.instance;
_collectionActions = CollectionActions(_collectionsService);
2020-10-24 10:25:02 +00:00
_filesDB = FilesDB.instance;
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
if (event.collectionID != null &&
_cachedFavoritesCollectionID != null &&
_cachedFavoritesCollectionID == event.collectionID) {
if (event.type == EventType.addedOrUpdated) {
2022-12-17 11:06:43 +00:00
// Note: This source check is a ugly hack because currently we
// don't have any event type related to remove from collection
final bool isAdded = !event.source.contains("remove");
_updateFavoriteFilesCache(event.updatedFiles, favFlag: isAdded);
} else if (event.type == EventType.deletedFromEverywhere ||
event.type == EventType.deletedFromRemote) {
_updateFavoriteFilesCache(event.updatedFiles, favFlag: false);
}
}
});
await _warmUpCache();
}
void dispose() {
_collectionUpdatesSubscription.cancel();
}
Future<void> _warmUpCache() async {
final favCollection = await _getFavoritesCollection();
if (favCollection != null) {
final uploadedIDs =
await FilesDB.instance.getUploadedFileIDs(favCollection.id);
_cachedFavUploadedIDs.addAll(uploadedIDs);
}
}
2023-06-22 09:19:34 +00:00
bool hasFavorites() {
return _cachedFavUploadedIDs.isNotEmpty;
}
static FavoritesService instance = FavoritesService._privateConstructor();
2020-05-06 16:43:03 +00:00
2021-03-17 21:11:31 +00:00
void clearCache() {
_cachedFavoritesCollectionID = null;
}
2023-08-24 16:56:24 +00:00
bool isFavoriteCache(EnteFile file, {bool checkOnlyAlbum = false}) {
if (file.collectionID != null &&
_cachedFavoritesCollectionID != null &&
file.collectionID == _cachedFavoritesCollectionID) {
2022-12-17 11:06:43 +00:00
debugPrint("File ${file.uploadedFileID} is part of favorite collection");
return true;
}
2022-12-15 10:02:46 +00:00
if (checkOnlyAlbum) {
return false;
}
if (file.uploadedFileID != null) {
return _cachedFavUploadedIDs.contains(file.uploadedFileID);
} else if (file.localID != null) {
return _cachedPendingLocalIDs.contains(file.localID);
}
return false;
}
2023-08-24 16:56:24 +00:00
Future<bool> isFavorite(EnteFile file) async {
2020-10-30 20:17:06 +00:00
final collection = await _getFavoritesCollection();
2022-12-30 08:52:17 +00:00
if (collection == null || file.uploadedFileID == null) {
2020-10-30 20:17:06 +00:00
return false;
2020-10-24 10:25:02 +00:00
}
2020-10-30 20:17:06 +00:00
return _filesDB.doesFileExistInCollection(
2022-12-30 08:52:17 +00:00
file.uploadedFileID!,
2022-06-11 08:23:52 +00:00
collection.id,
);
2020-05-06 16:43:03 +00:00
}
2023-10-04 04:18:27 +00:00
void _updateFavoriteFilesCache(
List<EnteFile> files, {
required bool favFlag,
}) {
final Set<int> updatedIDs = {};
final Set<String> localIDs = {};
for (var file in files) {
if (file.uploadedFileID != null) {
2022-12-30 08:52:17 +00:00
updatedIDs.add(file.uploadedFileID!);
} else if (file.localID != null || file.localID != "") {
/* Note: Favorite un-uploaded files
For such files, as we don't have uploaded IDs yet, we will cache
cache the local ID for showing the fav icon in the gallery
*/
2022-12-30 08:52:17 +00:00
localIDs.add(file.localID!);
}
}
if (favFlag) {
_cachedFavUploadedIDs.addAll(updatedIDs);
} else {
_cachedFavUploadedIDs.removeAll(updatedIDs);
}
}
2023-08-24 16:56:24 +00:00
Future<void> addToFavorites(BuildContext context, EnteFile file) async {
final collectionID = await _getOrCreateFavoriteCollectionID();
2023-08-24 16:56:24 +00:00
final List<EnteFile> files = [file];
if (file.uploadedFileID == null) {
file.collectionID = collectionID;
await _filesDB.insert(file);
Bus.instance.fire(CollectionUpdatedEvent(collectionID, files, "addTFav"));
} else {
await _collectionsService.addToCollection(collectionID, files);
}
_updateFavoriteFilesCache(files, favFlag: true);
2023-01-11 07:05:40 +00:00
RemoteSyncService.instance.sync(silently: true).ignore();
}
Future<void> updateFavorites(
2023-03-16 04:43:32 +00:00
BuildContext context,
2023-08-24 16:56:24 +00:00
List<EnteFile> files,
2023-03-16 04:43:32 +00:00
bool favFlag,
) async {
2022-12-30 08:52:17 +00:00
final int currentUserID = Configuration.instance.getUserID()!;
2022-12-15 10:02:46 +00:00
if (files.any((f) => f.uploadedFileID == null)) {
throw AssertionError("Can only favorite uploaded items");
}
if (files.any((f) => f.ownerID != currentUserID)) {
throw AssertionError("Can not favortie files owned by others");
}
final collectionID = await _getOrCreateFavoriteCollectionID();
if (favFlag) {
await _collectionsService.addToCollection(collectionID, files);
} else {
final Collection? favCollection = await _getFavoritesCollection();
await _collectionActions.moveFilesFromCurrentCollection(
context,
favCollection!,
files,
);
2022-12-15 10:02:46 +00:00
}
2022-12-17 11:06:43 +00:00
_updateFavoriteFilesCache(files, favFlag: favFlag);
2022-12-15 10:02:46 +00:00
}
2023-08-24 16:56:24 +00:00
Future<void> removeFromFavorites(BuildContext context, EnteFile file) async {
2022-08-29 14:43:31 +00:00
final fileID = file.uploadedFileID;
2020-10-23 16:18:38 +00:00
if (fileID == null) {
// Do nothing, ignore
} else {
final Collection? favCollection = await _getFavoritesCollection();
2023-09-29 04:05:21 +00:00
// The file might be part of another collection. For unfav, we need to
// move file from the fav collection to the .
if (file.collectionID != favCollection!.id) {
final EnteFile? favFile = await FilesDB.instance.getUploadedFile(
fileID,
favCollection.id,
);
if (favFile != null) {
file = favFile;
}
}
await _collectionActions.moveFilesFromCurrentCollection(
context,
2023-09-29 04:05:21 +00:00
favCollection,
[file],
);
2020-10-23 16:18:38 +00:00
}
_updateFavoriteFilesCache([file], favFlag: false);
2020-10-23 16:18:38 +00:00
}
2022-12-30 08:52:17 +00:00
Future<Collection?> _getFavoritesCollection() async {
2020-10-30 20:17:06 +00:00
if (_cachedFavoritesCollectionID == null) {
final collections = _collectionsService.getActiveCollections();
for (final collection in collections) {
2022-12-30 08:52:17 +00:00
if (collection.owner!.id == _config.getUserID() &&
collection.type == CollectionType.favorites) {
2020-10-30 20:17:06 +00:00
_cachedFavoritesCollectionID = collection.id;
return collection;
}
}
return null;
}
2022-12-30 08:52:17 +00:00
return _collectionsService.getCollectionByID(_cachedFavoritesCollectionID!);
}
Future<int> _getOrCreateFavoriteCollectionID() async {
2020-10-30 20:17:06 +00:00
if (_cachedFavoritesCollectionID != null) {
2022-12-30 08:52:17 +00:00
return _cachedFavoritesCollectionID!;
}
2023-02-27 10:44:27 +00:00
final favoriteCollectionKey = CryptoUtil.generateKey();
final encryptedKeyResult =
CryptoUtil.encryptSync(favoriteCollectionKey, _config.getKey()!);
final encName = CryptoUtil.encryptSync(
utf8.encode("Favorites") as Uint8List,
favoriteCollectionKey,
);
2022-06-11 08:23:52 +00:00
final collection = await _collectionsService.createAndCacheCollection(
2022-12-30 08:46:07 +00:00
CreateRequest(
2023-02-27 10:44:27 +00:00
encryptedKey: CryptoUtil.bin2base64(encryptedKeyResult.encryptedData!),
keyDecryptionNonce: CryptoUtil.bin2base64(encryptedKeyResult.nonce!),
2023-02-03 04:41:45 +00:00
encryptedName: CryptoUtil.bin2base64(encName.encryptedData!),
nameDecryptionNonce: CryptoUtil.bin2base64(encName.nonce!),
2022-12-30 08:46:07 +00:00
type: CollectionType.favorites,
attributes: CollectionAttributes(),
2022-06-11 08:23:52 +00:00
),
);
2020-10-30 20:17:06 +00:00
_cachedFavoritesCollectionID = collection.id;
return collection.id;
}
2020-05-06 16:43:03 +00:00
}