nc-photos/app/lib/use_case/share_album_with_user.dart

91 lines
2.8 KiB
Dart
Raw Normal View History

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