2021-10-18 12:46:06 +02:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
2021-11-12 22:13:02 +01:00
|
|
|
import 'package:nc_photos/ci_string.dart';
|
2021-10-18 12:46:06 +02:00
|
|
|
import 'package:nc_photos/debug_util.dart';
|
|
|
|
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/file.dart';
|
|
|
|
import 'package:nc_photos/entity/share.dart';
|
2021-11-11 18:03:36 +01:00
|
|
|
import 'package:nc_photos/entity/sharee.dart';
|
|
|
|
import 'package:nc_photos/or_null.dart';
|
2021-10-18 12:46:06 +02:00
|
|
|
import 'package:nc_photos/use_case/create_share.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 ShareAlbumWithUser {
|
2021-11-11 18:03:36 +01:00
|
|
|
ShareAlbumWithUser(this.shareRepo, this.albumRepo);
|
2021-10-18 12:46:06 +02:00
|
|
|
|
|
|
|
Future<void> call(
|
|
|
|
Account account,
|
|
|
|
Album album,
|
2021-11-11 18:03:36 +01:00
|
|
|
Sharee sharee, {
|
2021-10-18 12:46:06 +02:00
|
|
|
void Function(File)? onShareFileFailed,
|
|
|
|
}) async {
|
|
|
|
assert(album.provider is AlbumStaticProvider);
|
2021-11-11 18:03:36 +01:00
|
|
|
// add the share to album file
|
|
|
|
final newAlbum = album.copyWith(
|
|
|
|
shares: OrNull((album.shares ?? [])
|
|
|
|
..add(AlbumShare(
|
|
|
|
userId: sharee.shareWith,
|
|
|
|
displayName: sharee.shareWithDisplayNameUnique,
|
|
|
|
))),
|
|
|
|
);
|
|
|
|
await UpdateAlbum(albumRepo)(account, newAlbum);
|
|
|
|
|
|
|
|
await _createFileShares(
|
|
|
|
account,
|
|
|
|
newAlbum,
|
|
|
|
sharee.shareWith,
|
|
|
|
onShareFileFailed: onShareFileFailed,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _createFileShares(
|
|
|
|
Account account,
|
|
|
|
Album album,
|
2021-11-12 22:13:02 +01:00
|
|
|
CiString shareWith, {
|
2021-11-11 18:03:36 +01:00
|
|
|
void Function(File)? onShareFileFailed,
|
|
|
|
}) async {
|
2021-10-18 12:46:06 +02:00
|
|
|
final files = AlbumStaticProvider.of(album)
|
|
|
|
.items
|
|
|
|
.whereType<AlbumFileItem>()
|
|
|
|
.map((e) => e.file);
|
2021-11-11 18:03:36 +01:00
|
|
|
try {
|
2021-11-12 22:13:02 +01:00
|
|
|
await CreateUserShare(shareRepo)(
|
|
|
|
account, album.albumFile!, shareWith.raw);
|
2021-11-11 18:03:36 +01:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.severe(
|
|
|
|
"[_createFileShares] Failed sharing album file '${logFilename(album.albumFile?.path)}' with '$shareWith'",
|
|
|
|
e,
|
|
|
|
stackTrace);
|
|
|
|
onShareFileFailed?.call(album.albumFile!);
|
|
|
|
}
|
2021-10-18 12:46:06 +02:00
|
|
|
for (final f in files) {
|
2021-11-11 18:03:36 +01:00
|
|
|
_log.info("[_createFileShares] Sharing '${f.path}' with '$shareWith'");
|
2021-10-18 12:46:06 +02:00
|
|
|
try {
|
2021-11-12 22:13:02 +01:00
|
|
|
await CreateUserShare(shareRepo)(account, f, shareWith.raw);
|
2021-10-18 12:46:06 +02:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.severe(
|
2021-11-11 18:03:36 +01:00
|
|
|
"[_createFileShares] Failed sharing file '${logFilename(f.path)}' with '$shareWith'",
|
2021-10-18 12:46:06 +02:00
|
|
|
e,
|
|
|
|
stackTrace);
|
|
|
|
onShareFileFailed?.call(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final ShareRepo shareRepo;
|
2021-11-11 18:03:36 +01:00
|
|
|
final AlbumRepo albumRepo;
|
2021-10-18 12:46:06 +02:00
|
|
|
|
|
|
|
static final _log =
|
|
|
|
Logger("use_case.share_album_with_user.ShareAlbumWithUser");
|
|
|
|
}
|