2022-11-20 10:59:13 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2022-11-22 10:46:51 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2023-01-05 11:17:34 +00:00
|
|
|
import 'package:photos/db/files_db.dart';
|
2022-11-20 10:59:13 +00:00
|
|
|
import 'package:photos/ente_theme_data.dart';
|
2022-12-22 17:51:59 +00:00
|
|
|
import 'package:photos/models/api/collection/create_request.dart';
|
2022-11-20 10:59:13 +00:00
|
|
|
import 'package:photos/models/collection.dart';
|
2022-12-19 09:36:27 +00:00
|
|
|
import 'package:photos/models/file.dart';
|
2023-01-05 11:17:34 +00:00
|
|
|
import 'package:photos/models/files_split.dart';
|
2022-12-22 17:51:59 +00:00
|
|
|
import 'package:photos/models/magic_metadata.dart';
|
2022-11-20 10:59:13 +00:00
|
|
|
import 'package:photos/services/collections_service.dart';
|
2022-12-22 17:51:59 +00:00
|
|
|
import 'package:photos/services/hidden_service.dart';
|
2022-11-22 10:46:51 +00:00
|
|
|
import 'package:photos/services/user_service.dart';
|
2022-11-22 07:12:18 +00:00
|
|
|
import 'package:photos/theme/ente_theme.dart';
|
2022-11-20 10:59:13 +00:00
|
|
|
import 'package:photos/ui/common/dialogs.dart';
|
|
|
|
import 'package:photos/ui/payment/subscription.dart';
|
2022-12-19 10:05:46 +00:00
|
|
|
import 'package:photos/utils/date_time_util.dart';
|
2022-11-20 10:59:13 +00:00
|
|
|
import 'package:photos/utils/dialog_util.dart';
|
2022-11-22 10:46:51 +00:00
|
|
|
import 'package:photos/utils/email_util.dart';
|
|
|
|
import 'package:photos/utils/share_util.dart';
|
2022-11-21 09:11:09 +00:00
|
|
|
import 'package:photos/utils/toast_util.dart';
|
2022-11-20 10:59:13 +00:00
|
|
|
|
2022-12-15 10:02:46 +00:00
|
|
|
class CollectionActions {
|
|
|
|
final Logger logger = Logger((CollectionActions).toString());
|
2022-11-20 10:59:13 +00:00
|
|
|
final CollectionsService collectionsService;
|
|
|
|
|
2022-12-15 10:02:46 +00:00
|
|
|
CollectionActions(this.collectionsService);
|
2022-11-20 10:59:13 +00:00
|
|
|
|
|
|
|
Future<bool> publicLinkToggle(
|
|
|
|
BuildContext context,
|
|
|
|
Collection collection,
|
|
|
|
bool enable,
|
|
|
|
) async {
|
|
|
|
// confirm if user wants to disable the url
|
|
|
|
if (!enable) {
|
|
|
|
final choice = await showChoiceDialog(
|
|
|
|
context,
|
|
|
|
'Remove public link?',
|
|
|
|
'This will remove the public link for accessing "${collection.name}".',
|
|
|
|
firstAction: 'Yes, remove',
|
|
|
|
secondAction: 'Cancel',
|
|
|
|
actionType: ActionType.critical,
|
|
|
|
);
|
|
|
|
if (choice != DialogUserChoice.firstChoice) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final dialog = createProgressDialog(
|
|
|
|
context,
|
|
|
|
enable ? "Creating link..." : "Disabling link...",
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
await dialog.show();
|
|
|
|
enable
|
|
|
|
? await CollectionsService.instance.createShareUrl(collection)
|
|
|
|
: await CollectionsService.instance.disableShareUrl(collection);
|
|
|
|
dialog.hide();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
dialog.hide();
|
|
|
|
if (e is SharingNotPermittedForFreeAccountsError) {
|
|
|
|
_showUnSupportedAlert(context);
|
|
|
|
} else {
|
2022-12-15 10:02:46 +00:00
|
|
|
logger.severe("Failed to update shareUrl collection", e);
|
2022-12-26 11:09:29 +00:00
|
|
|
showGenericErrorDialog(context: context);
|
2022-11-20 10:59:13 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 09:36:27 +00:00
|
|
|
Future<Collection?> createSharedCollectionLink(
|
|
|
|
BuildContext context,
|
|
|
|
List<File> files,
|
|
|
|
) async {
|
|
|
|
final dialog =
|
|
|
|
createProgressDialog(context, "Creating link...", isDismissible: true);
|
|
|
|
dialog.show();
|
|
|
|
try {
|
|
|
|
// create album with emptyName, use collectionCreationTime on UI to
|
|
|
|
// show name
|
|
|
|
logger.finest("creating album for sharing files");
|
2022-12-22 11:35:55 +00:00
|
|
|
final File fileWithMinCreationTime = files.reduce(
|
|
|
|
(a, b) => (a.creationTime ?? 0) < (b.creationTime ?? 0) ? a : b,
|
|
|
|
);
|
|
|
|
final File fileWithMaxCreationTime = files.reduce(
|
|
|
|
(a, b) => (a.creationTime ?? 0) > (b.creationTime ?? 0) ? a : b,
|
|
|
|
);
|
|
|
|
final String dummyName = getNameForDateRange(
|
|
|
|
fileWithMinCreationTime.creationTime!,
|
|
|
|
fileWithMaxCreationTime.creationTime!,
|
|
|
|
);
|
2022-12-22 17:51:59 +00:00
|
|
|
final CreateRequest req =
|
|
|
|
await collectionsService.buildCollectionCreateRequest(
|
|
|
|
dummyName,
|
|
|
|
visibility: visibilityVisible,
|
|
|
|
subType: subTypeSharedFilesCollection,
|
|
|
|
);
|
|
|
|
final collection = await collectionsService.createAndCacheCollection(
|
2022-12-30 08:46:07 +00:00
|
|
|
req,
|
2022-12-22 17:51:59 +00:00
|
|
|
);
|
2022-12-19 09:36:27 +00:00
|
|
|
logger.finest("adding files to share to new album");
|
|
|
|
await collectionsService.addToCollection(collection.id, files);
|
|
|
|
logger.finest("creating public link for the newly created album");
|
|
|
|
await CollectionsService.instance.createShareUrl(collection);
|
|
|
|
dialog.hide();
|
|
|
|
return collection;
|
|
|
|
} catch (e, s) {
|
|
|
|
dialog.hide();
|
2022-12-26 11:50:52 +00:00
|
|
|
showGenericErrorDialog(context: context);
|
2022-12-19 09:36:27 +00:00
|
|
|
logger.severe("Failing to create link for selected files", e, s);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-11-21 09:11:09 +00:00
|
|
|
// removeParticipant remove the user from a share album
|
2022-11-22 07:12:18 +00:00
|
|
|
Future<bool?> removeParticipant(
|
2022-11-21 09:11:09 +00:00
|
|
|
BuildContext context,
|
|
|
|
Collection collection,
|
|
|
|
User user,
|
|
|
|
) async {
|
2022-11-22 07:12:18 +00:00
|
|
|
final result = await showChoiceDialog(
|
|
|
|
context,
|
|
|
|
"Remove?",
|
|
|
|
"${user.email} will be removed "
|
2022-11-22 08:02:54 +00:00
|
|
|
"from this shared album.\n\nAny photos and videos added by them will also be removed from the album.",
|
2022-11-22 07:12:18 +00:00
|
|
|
firstAction: "Yes, remove",
|
|
|
|
secondAction: "Cancel",
|
|
|
|
secondActionColor: getEnteColorScheme(context).strokeBase,
|
|
|
|
actionType: ActionType.critical,
|
|
|
|
);
|
|
|
|
if (result != DialogUserChoice.firstChoice) {
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
2022-11-21 09:11:09 +00:00
|
|
|
final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
await dialog.show();
|
|
|
|
try {
|
2022-11-25 10:32:20 +00:00
|
|
|
final newSharees =
|
|
|
|
await CollectionsService.instance.unshare(collection.id, user.email);
|
2022-11-25 11:04:35 +00:00
|
|
|
collection.updateSharees(newSharees);
|
2022-11-21 09:11:09 +00:00
|
|
|
await dialog.hide();
|
2022-12-26 05:14:36 +00:00
|
|
|
showShortToast(context, "Stopped sharing with " + user.email + ".");
|
2022-11-21 09:11:09 +00:00
|
|
|
return true;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logger("EmailItemWidget").severe(e, s);
|
|
|
|
await dialog.hide();
|
2022-12-26 11:09:29 +00:00
|
|
|
await showGenericErrorDialog(context: context);
|
2022-11-21 09:11:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:46:51 +00:00
|
|
|
Future<bool?> addEmailToCollection(
|
|
|
|
BuildContext context,
|
|
|
|
Collection collection,
|
|
|
|
String email, {
|
2022-11-22 17:43:36 +00:00
|
|
|
CollectionParticipantRole role = CollectionParticipantRole.viewer,
|
2022-11-22 10:46:51 +00:00
|
|
|
String? publicKey,
|
|
|
|
}) async {
|
|
|
|
if (!isValidEmail(email)) {
|
|
|
|
await showErrorDialog(
|
|
|
|
context,
|
|
|
|
"Invalid email address",
|
|
|
|
"Please enter a valid email address.",
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
} else if (email == Configuration.instance.getEmail()) {
|
|
|
|
await showErrorDialog(context, "Oops", "You cannot share with yourself");
|
|
|
|
return null;
|
2022-11-22 17:43:36 +00:00
|
|
|
} else {
|
|
|
|
// if (collection.getSharees().any((user) => user.email == email)) {
|
|
|
|
// showErrorDialog(
|
|
|
|
// context,
|
|
|
|
// "Oops",
|
|
|
|
// "You're already sharing this with " + email,
|
|
|
|
// );
|
|
|
|
// return null;
|
|
|
|
// }
|
2022-11-22 10:46:51 +00:00
|
|
|
}
|
|
|
|
if (publicKey == null) {
|
|
|
|
final dialog = createProgressDialog(context, "Searching for user...");
|
|
|
|
await dialog.show();
|
|
|
|
try {
|
|
|
|
publicKey = await UserService.instance.getPublicKey(email);
|
|
|
|
await dialog.hide();
|
|
|
|
} catch (e) {
|
2022-12-15 10:02:46 +00:00
|
|
|
logger.severe("Failed to get public key", e);
|
2022-12-26 11:09:29 +00:00
|
|
|
showGenericErrorDialog(context: context);
|
2022-11-22 10:46:51 +00:00
|
|
|
await dialog.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// getPublicKey can return null
|
|
|
|
// ignore: unnecessary_null_comparison
|
|
|
|
if (publicKey == null || publicKey == '') {
|
|
|
|
final dialog = AlertDialog(
|
|
|
|
title: const Text("Invite to ente?"),
|
|
|
|
content: Text(
|
|
|
|
"Looks like " +
|
|
|
|
email +
|
|
|
|
" hasn't signed up for ente yet. would you like to invite them?",
|
|
|
|
style: const TextStyle(
|
|
|
|
height: 1.4,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(
|
|
|
|
"Invite",
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.greenAlternative,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
shareText(
|
|
|
|
"Hey, I have some photos to share. Please install https://ente.io so that I can share them privately.",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return dialog;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
final dialog = createProgressDialog(context, "Sharing...");
|
|
|
|
await dialog.show();
|
|
|
|
try {
|
2022-11-25 10:32:20 +00:00
|
|
|
final newSharees = await CollectionsService.instance
|
2022-11-22 17:43:36 +00:00
|
|
|
.share(collection.id, email, publicKey, role);
|
2022-11-25 11:04:35 +00:00
|
|
|
collection.updateSharees(newSharees);
|
2022-11-22 10:46:51 +00:00
|
|
|
await dialog.hide();
|
|
|
|
showShortToast(context, "Shared successfully!");
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
if (e is SharingNotPermittedForFreeAccountsError) {
|
|
|
|
_showUnSupportedAlert(context);
|
|
|
|
} else {
|
2022-12-15 10:02:46 +00:00
|
|
|
logger.severe("failed to share collection", e);
|
2022-12-26 11:09:29 +00:00
|
|
|
showGenericErrorDialog(context: context);
|
2022-11-22 10:46:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:17:34 +00:00
|
|
|
/*
|
|
|
|
_moveFilesFromCurrentCollection removes the file from the current
|
|
|
|
collection. Based on the file and collection ownership, files will be
|
|
|
|
either moved to different collection (Case A). or will just get removed
|
|
|
|
from current collection (Case B).
|
|
|
|
-------------------------------
|
|
|
|
Case A: Files and collection belong to the same user. Such files
|
|
|
|
will be moved to a collection which belongs to the user and removed from
|
|
|
|
the current collection as part of move operation.
|
|
|
|
Note: Even files are present in the
|
|
|
|
destination collection, we need to make move API call on the server
|
|
|
|
so that the files are removed from current collection and are actually
|
|
|
|
moved to a collection owned by the user.
|
|
|
|
-------------------------------
|
|
|
|
Case B: Owner of files and collections are different. In such cases,
|
|
|
|
we will just remove (not move) the files from the given collection.
|
|
|
|
*/
|
|
|
|
Future<void> moveFilesFromCurrentCollection(
|
|
|
|
BuildContext context,
|
|
|
|
Collection collection,
|
|
|
|
Iterable<File> files,
|
|
|
|
) async {
|
|
|
|
final int currentUserID = Configuration.instance.getUserID()!;
|
|
|
|
final isCollectionOwner = collection.owner!.id == currentUserID;
|
|
|
|
if (!isCollectionOwner) {
|
|
|
|
// Todo: Support for removing own files from a collection owner by
|
|
|
|
// someone else will be added along with collaboration changes
|
|
|
|
showShortToast(context, "Only collection owner can remove");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final FilesSplit split = FilesSplit.split(
|
|
|
|
files,
|
|
|
|
Configuration.instance.getUserID()!,
|
|
|
|
);
|
|
|
|
if (split.ownedByOtherUsers.isNotEmpty) {
|
|
|
|
// Todo: Support for removing own files from a collection owner by
|
|
|
|
// someone else will be added along with collaboration changes
|
|
|
|
showShortToast(context, "Can only remove files owned by you");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pendingAssignMap keeps a track of files which are yet to be assigned to
|
|
|
|
// to destination collection.
|
|
|
|
final Map<int, File> pendingAssignMap = {};
|
|
|
|
// destCollectionToFilesMap contains the destination collection and
|
|
|
|
// files entry which needs to be moved in destination.
|
|
|
|
// After the end of mapping logic, the number of files entries in
|
|
|
|
// pendingAssignMap should be equal to files in destCollectionToFilesMap
|
|
|
|
final Map<int, List<File>> destCollectionToFilesMap = {};
|
|
|
|
final List<int> uploadedIDs = [];
|
|
|
|
for (File f in split.ownedByCurrentUser) {
|
|
|
|
if (f.uploadedFileID != null) {
|
|
|
|
pendingAssignMap[f.uploadedFileID!] = f;
|
|
|
|
uploadedIDs.add(f.uploadedFileID!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final Map<int, List<File>> collectionToFilesMap =
|
|
|
|
await FilesDB.instance.getAllFilesGroupByCollectionID(uploadedIDs);
|
|
|
|
|
|
|
|
// Find and map the files from current collection to to entries in other
|
|
|
|
// collections. This mapping is done to avoid moving all the files to
|
|
|
|
// uncategorized during remove from album.
|
|
|
|
for (MapEntry<int, List<File>> entry in collectionToFilesMap.entries) {
|
|
|
|
if (!_isAutoMoveCandidate(collection.id, entry.key, currentUserID)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final targetCollection = collectionsService.getCollectionByID(entry.key)!;
|
|
|
|
// for each file which already exist in the destination collection
|
|
|
|
// add entries in the moveDestCollectionToFiles map
|
|
|
|
for (File file in entry.value) {
|
|
|
|
// Check if the uploaded file is still waiting to be mapped
|
|
|
|
if (pendingAssignMap.containsKey(file.uploadedFileID)) {
|
|
|
|
if (!destCollectionToFilesMap.containsKey(targetCollection.id)) {
|
|
|
|
destCollectionToFilesMap[targetCollection.id] = <File>[];
|
|
|
|
}
|
|
|
|
destCollectionToFilesMap[targetCollection.id]!
|
|
|
|
.add(pendingAssignMap[file.uploadedFileID!]!);
|
|
|
|
pendingAssignMap.remove(file.uploadedFileID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Move the remaining files to uncategorized collection
|
|
|
|
if (pendingAssignMap.isNotEmpty) {
|
|
|
|
final Collection uncategorizedCollection =
|
|
|
|
await collectionsService.getUncategorizedCollection();
|
|
|
|
final int toCollectionID = uncategorizedCollection.id;
|
|
|
|
for (MapEntry<int, File> entry in pendingAssignMap.entries) {
|
|
|
|
final file = entry.value;
|
|
|
|
if (pendingAssignMap.containsKey(file.uploadedFileID)) {
|
|
|
|
if (!destCollectionToFilesMap.containsKey(toCollectionID)) {
|
|
|
|
destCollectionToFilesMap[toCollectionID] = <File>[];
|
|
|
|
}
|
|
|
|
destCollectionToFilesMap[toCollectionID]!
|
|
|
|
.add(pendingAssignMap[file.uploadedFileID!]!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all files are mapped.
|
|
|
|
int mappedFilesCount = 0;
|
|
|
|
destCollectionToFilesMap.forEach((key, value) {
|
|
|
|
mappedFilesCount += value.length;
|
|
|
|
});
|
|
|
|
if (mappedFilesCount == uploadedIDs.length) {
|
|
|
|
throw AssertionError(
|
|
|
|
"Failed to map all files toMap: ${uploadedIDs.length} and mapped "
|
|
|
|
"$mappedFilesCount",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MapEntry<int, List<File>> entry in destCollectionToFilesMap.entries) {
|
|
|
|
await collectionsService.move(entry.key, collection.id, entry.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method returns true if the given destination collection is a good
|
|
|
|
// target to moving files during file remove or delete collection but keey
|
|
|
|
// photos action. Uncategorized or favorite type of collections are not
|
|
|
|
// good auto-move candidates. Uncategorized will be fall back for all files
|
|
|
|
// which could not be mapped to a potential target collection
|
|
|
|
bool _isAutoMoveCandidate(int fromCollectionID, toCollectionID, int userID) {
|
|
|
|
if (fromCollectionID == toCollectionID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final Collection? targetCollection =
|
|
|
|
collectionsService.getCollectionByID(toCollectionID);
|
|
|
|
// ignore non-cached collections, uncategorized and favorite
|
|
|
|
// collections and collections ignored by others
|
|
|
|
if (targetCollection == null ||
|
|
|
|
(CollectionType.uncategorized == targetCollection.type ||
|
|
|
|
targetCollection.type == CollectionType.favorites) ||
|
|
|
|
targetCollection.owner!.id != userID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-20 10:59:13 +00:00
|
|
|
void _showUnSupportedAlert(BuildContext context) {
|
|
|
|
final AlertDialog alert = AlertDialog(
|
|
|
|
title: const Text("Sorry"),
|
|
|
|
content: const Text(
|
2022-12-20 06:44:08 +00:00
|
|
|
"Looks like your subscription has expired. Please subscribe to enable"
|
|
|
|
" sharing.",
|
2022-11-20 10:59:13 +00:00
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(
|
|
|
|
"Subscribe",
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.greenAlternative,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
2022-12-20 04:59:31 +00:00
|
|
|
Navigator.of(context, rootNavigator: true).pop();
|
2022-11-20 10:59:13 +00:00
|
|
|
Navigator.of(context).pushReplacement(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return getSubscriptionPage();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
child: Text(
|
|
|
|
"Ok",
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurface,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context, rootNavigator: true).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return alert;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|