2021-11-11 18:03:36 +01:00
|
|
|
import 'package:logging/logging.dart';
|
2021-10-18 12:46:06 +02:00
|
|
|
import 'package:nc_photos/account.dart';
|
2021-11-12 22:13:02 +01:00
|
|
|
import 'package:nc_photos/ci_string.dart';
|
2021-11-11 18:03:36 +01:00
|
|
|
import 'package:nc_photos/debug_util.dart';
|
2021-12-03 18:31:27 +01:00
|
|
|
import 'package:nc_photos/di_container.dart';
|
2021-10-18 12:46:06 +02:00
|
|
|
import 'package:nc_photos/entity/album.dart';
|
|
|
|
import 'package:nc_photos/entity/album/item.dart';
|
|
|
|
import 'package:nc_photos/entity/album/provider.dart';
|
|
|
|
import 'package:nc_photos/entity/share.dart';
|
2021-11-11 18:03:36 +01:00
|
|
|
import 'package:nc_photos/or_null.dart';
|
|
|
|
import 'package:nc_photos/use_case/list_share.dart';
|
2021-10-18 12:46:06 +02:00
|
|
|
import 'package:nc_photos/use_case/remove_share.dart';
|
2021-10-24 10:05:28 +02:00
|
|
|
import 'package:nc_photos/use_case/unshare_file_from_album.dart';
|
2021-11-11 18:03:36 +01:00
|
|
|
import 'package:nc_photos/use_case/update_album.dart';
|
2021-10-18 12:46:06 +02:00
|
|
|
|
|
|
|
class UnshareAlbumWithUser {
|
2021-12-03 18:31:27 +01:00
|
|
|
UnshareAlbumWithUser(this._c)
|
|
|
|
: assert(require(_c)),
|
|
|
|
assert(ListShare.require(_c)),
|
|
|
|
assert(UnshareFileFromAlbum.require(_c));
|
|
|
|
|
|
|
|
static bool require(DiContainer c) =>
|
|
|
|
DiContainer.has(c, DiType.albumRepo) &&
|
|
|
|
DiContainer.has(c, DiType.shareRepo);
|
2021-10-18 12:46:06 +02:00
|
|
|
|
2021-11-25 10:47:49 +01:00
|
|
|
Future<Album> call(
|
2021-10-18 12:46:06 +02:00
|
|
|
Account account,
|
|
|
|
Album album,
|
2021-11-12 22:13:02 +01:00
|
|
|
CiString shareWith, {
|
2021-10-24 10:05:28 +02:00
|
|
|
void Function(Share)? onUnshareFileFailed,
|
2021-10-18 12:46:06 +02:00
|
|
|
}) async {
|
|
|
|
assert(album.provider is AlbumStaticProvider);
|
2021-11-11 18:03:36 +01:00
|
|
|
// remove the share from album file
|
|
|
|
final newShares =
|
|
|
|
album.shares?.where((s) => s.userId != shareWith).toList() ?? [];
|
|
|
|
final newAlbum = album.copyWith(
|
|
|
|
shares: OrNull(newShares.isEmpty ? null : newShares),
|
|
|
|
);
|
2021-12-03 18:31:27 +01:00
|
|
|
await UpdateAlbum(_c.albumRepo)(account, newAlbum);
|
2021-10-18 12:46:06 +02:00
|
|
|
|
2021-11-25 10:47:49 +01:00
|
|
|
try {
|
|
|
|
await _deleteFileShares(
|
|
|
|
account,
|
|
|
|
newAlbum,
|
|
|
|
shareWith,
|
|
|
|
onUnshareFileFailed: onUnshareFileFailed,
|
|
|
|
);
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.shout("[call] Failed while _deleteFileShares", e, stackTrace);
|
|
|
|
}
|
|
|
|
return newAlbum;
|
2021-11-11 18:03:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _deleteFileShares(
|
|
|
|
Account account,
|
|
|
|
Album album,
|
2021-11-12 22:13:02 +01:00
|
|
|
CiString shareWith, {
|
2021-11-11 18:03:36 +01:00
|
|
|
void Function(Share)? onUnshareFileFailed,
|
|
|
|
}) async {
|
|
|
|
// remove share from the album file
|
2021-12-03 18:31:27 +01:00
|
|
|
final albumShares = await ListShare(_c)(account, album.albumFile!);
|
2021-11-11 18:03:36 +01:00
|
|
|
for (final s in albumShares.where((s) => s.shareWith == shareWith)) {
|
|
|
|
try {
|
2021-12-03 18:31:27 +01:00
|
|
|
await RemoveShare(_c.shareRepo)(account, s);
|
2021-11-11 18:03:36 +01:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.severe(
|
|
|
|
"[_deleteFileShares] Failed unsharing album file '${logFilename(album.albumFile?.path)}' with '$shareWith'",
|
|
|
|
e,
|
|
|
|
stackTrace);
|
|
|
|
onUnshareFileFailed?.call(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then remove shares from all files in this album
|
2021-10-24 10:05:28 +02:00
|
|
|
final files = AlbumStaticProvider.of(album)
|
2021-10-18 12:46:06 +02:00
|
|
|
.items
|
|
|
|
.whereType<AlbumFileItem>()
|
|
|
|
.map((e) => e.file)
|
|
|
|
.toList();
|
2021-12-03 18:31:27 +01:00
|
|
|
await UnshareFileFromAlbum(_c)(account, album, files, [shareWith],
|
|
|
|
onUnshareFileFailed: onUnshareFileFailed);
|
2021-10-18 12:46:06 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 18:31:27 +01:00
|
|
|
final DiContainer _c;
|
2021-11-11 18:03:36 +01:00
|
|
|
|
|
|
|
static final _log =
|
|
|
|
Logger("use_case.unshare_album_with_user.UnshareAlbumWithUser");
|
2021-10-18 12:46:06 +02:00
|
|
|
}
|