2021-09-25 07:58:30 +02:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
2021-12-03 18:31:27 +01:00
|
|
|
import 'package:nc_photos/di_container.dart';
|
2021-09-25 07:58:30 +02:00
|
|
|
import 'package:nc_photos/entity/album.dart';
|
2021-11-05 12:18:29 +01:00
|
|
|
import 'package:nc_photos/entity/album/cover_provider.dart';
|
2021-09-25 07:58:30 +02:00
|
|
|
import 'package:nc_photos/entity/album/item.dart';
|
|
|
|
import 'package:nc_photos/entity/album/provider.dart';
|
2021-09-25 18:22:19 +02:00
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2021-09-25 07:58:30 +02:00
|
|
|
import 'package:nc_photos/iterable_extension.dart';
|
2021-09-29 13:00:00 +02:00
|
|
|
import 'package:nc_photos/use_case/preprocess_album.dart';
|
2021-10-24 17:32:36 +02:00
|
|
|
import 'package:nc_photos/use_case/unshare_file_from_album.dart';
|
2021-09-25 07:58:30 +02:00
|
|
|
import 'package:nc_photos/use_case/update_album.dart';
|
2021-09-25 18:22:19 +02:00
|
|
|
import 'package:nc_photos/use_case/update_album_with_actual_items.dart';
|
2021-09-25 07:58:30 +02:00
|
|
|
|
|
|
|
class RemoveFromAlbum {
|
2021-12-03 18:31:27 +01:00
|
|
|
RemoveFromAlbum(this._c)
|
|
|
|
: assert(require(_c)),
|
|
|
|
assert(UnshareFileFromAlbum.require(_c));
|
|
|
|
|
|
|
|
static bool require(DiContainer c) =>
|
|
|
|
DiContainer.has(c, DiType.albumRepo) && DiContainer.has(c, DiType.appDb);
|
2021-09-25 07:58:30 +02:00
|
|
|
|
|
|
|
/// Remove a list of AlbumItems from [album]
|
|
|
|
///
|
|
|
|
/// The items are compared with [identical], so it must come from [album] for
|
|
|
|
/// it to work
|
2021-12-03 18:31:27 +01:00
|
|
|
///
|
|
|
|
/// If [shouldUnshare] is false, files will not be unshared after removing
|
|
|
|
/// from the album
|
2021-09-25 07:58:30 +02:00
|
|
|
Future<Album> call(
|
2021-12-03 18:31:27 +01:00
|
|
|
Account account,
|
|
|
|
Album album,
|
|
|
|
List<AlbumItem> items, {
|
|
|
|
bool shouldUnshare = true,
|
|
|
|
}) async {
|
2021-09-25 07:58:30 +02:00
|
|
|
_log.info("[call] Remove ${items.length} items from album '${album.name}'");
|
|
|
|
assert(album.provider is AlbumStaticProvider);
|
|
|
|
final provider = album.provider as AlbumStaticProvider;
|
|
|
|
final newItems = provider.items
|
|
|
|
.where((element) => !items.containsIdentical(element))
|
|
|
|
.toList();
|
|
|
|
var newAlbum = album.copyWith(
|
|
|
|
provider: AlbumStaticProvider.of(album).copyWith(
|
|
|
|
items: newItems,
|
|
|
|
),
|
|
|
|
);
|
2021-11-05 12:18:29 +01:00
|
|
|
newAlbum = await _fixAlbumPostRemove(account, newAlbum, items);
|
2021-12-03 18:31:27 +01:00
|
|
|
await UpdateAlbum(_c.albumRepo)(account, newAlbum);
|
2021-10-24 17:32:36 +02:00
|
|
|
|
2021-12-03 18:31:27 +01:00
|
|
|
if (!shouldUnshare) {
|
|
|
|
_log.info("[call] Skip unsharing files");
|
2021-12-02 11:47:37 +01:00
|
|
|
} else {
|
|
|
|
if (album.shares?.isNotEmpty == true) {
|
|
|
|
final removeFiles =
|
|
|
|
items.whereType<AlbumFileItem>().map((e) => e.file).toList();
|
|
|
|
if (removeFiles.isNotEmpty) {
|
|
|
|
await _unshareFiles(account, newAlbum, removeFiles);
|
|
|
|
}
|
2021-10-24 17:32:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 07:58:30 +02:00
|
|
|
return newAlbum;
|
|
|
|
}
|
|
|
|
|
2021-11-05 12:18:29 +01:00
|
|
|
/// Update the album accordingly if any of the removed items is interesting
|
|
|
|
/// (e.g., cover, latest item, etc)
|
|
|
|
Future<Album> _fixAlbumPostRemove(
|
|
|
|
Account account, Album newAlbum, List<AlbumItem> items) async {
|
|
|
|
bool isNeedUpdate = false;
|
|
|
|
for (final fileItem in items.whereType<AlbumFileItem>()) {
|
|
|
|
if (newAlbum.coverProvider
|
|
|
|
.getCover(newAlbum)
|
|
|
|
?.compareServerIdentity(fileItem.file) ==
|
|
|
|
true) {
|
|
|
|
// revert to auto cover so [UpdateAutoAlbumCover] can do its work
|
|
|
|
newAlbum = newAlbum.copyWith(
|
|
|
|
coverProvider: AlbumAutoCoverProvider(),
|
|
|
|
);
|
|
|
|
isNeedUpdate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fileItem.file.bestDateTime == newAlbum.provider.latestItemTime) {
|
|
|
|
isNeedUpdate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isNeedUpdate) {
|
|
|
|
return newAlbum;
|
|
|
|
}
|
|
|
|
|
|
|
|
_log.info(
|
|
|
|
"[_fixAlbumPostRemove] Resync as interesting item is being removed");
|
|
|
|
// need to update the album properties
|
2021-12-03 18:31:27 +01:00
|
|
|
final newItemsSynced = await PreProcessAlbum(_c.appDb)(account, newAlbum);
|
2021-11-05 12:18:29 +01:00
|
|
|
newAlbum = await UpdateAlbumWithActualItems(null)(
|
|
|
|
account,
|
|
|
|
newAlbum,
|
|
|
|
newItemsSynced,
|
|
|
|
);
|
|
|
|
return newAlbum;
|
|
|
|
}
|
|
|
|
|
2021-11-11 18:03:36 +01:00
|
|
|
Future<void> _unshareFiles(
|
|
|
|
Account account, Album album, List<File> files) async {
|
|
|
|
final albumShares = (album.shares!.map((e) => e.userId).toList()
|
|
|
|
..add(album.albumFile!.ownerId ?? account.username))
|
2021-11-12 22:13:02 +01:00
|
|
|
.where((element) => element != account.username)
|
2021-11-11 18:03:36 +01:00
|
|
|
.toList();
|
|
|
|
if (albumShares.isNotEmpty) {
|
2021-12-03 18:31:27 +01:00
|
|
|
await UnshareFileFromAlbum(_c)(account, album, files, albumShares);
|
2021-11-11 18:03:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 18:31:27 +01:00
|
|
|
final DiContainer _c;
|
2021-09-25 07:58:30 +02:00
|
|
|
|
|
|
|
static final _log = Logger("use_case.remove_from_album.RemoveFromAlbum");
|
|
|
|
}
|