Guard shared album logic with pref flag

This commit is contained in:
Ming Ming 2021-10-30 01:37:11 +08:00
parent 2ad844292a
commit 36dee1a7f1
3 changed files with 53 additions and 42 deletions

View file

@ -6,6 +6,7 @@ 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/pref.dart';
import 'package:nc_photos/use_case/create_share.dart';
import 'package:nc_photos/use_case/list_share.dart';
import 'package:nc_photos/use_case/preprocess_album.dart';
@ -40,10 +41,12 @@ class AddToAlbum {
);
await UpdateAlbum(albumRepo)(account, newAlbum);
final newFiles =
items.whereType<AlbumFileItem>().map((e) => e.file).toList();
if (newFiles.isNotEmpty) {
await _shareFiles(account, newAlbum, newFiles);
if (Pref().isLabEnableSharedAlbumOr(false)) {
final newFiles =
items.whereType<AlbumFileItem>().map((e) => e.file).toList();
if (newFiles.isNotEmpty) {
await _shareFiles(account, newAlbum, newFiles);
}
}
return newAlbum;

View file

@ -5,6 +5,7 @@ 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/pref.dart';
import 'package:nc_photos/use_case/list_share.dart';
import 'package:nc_photos/use_case/remove.dart';
import 'package:nc_photos/use_case/remove_share.dart';
@ -16,34 +17,38 @@ class RemoveAlbum {
/// Remove an album
Future<void> call(Account account, Album album) async {
_log.info("[call] Remove album: $album");
final files = <File>[];
if (album.provider is AlbumStaticProvider) {
files.addAll(AlbumStaticProvider.of(album)
.items
.whereType<AlbumFileItem>()
.map((e) => e.file));
}
final albumShares = (await ListShare(shareRepo)(account, album.albumFile!))
.where((element) => element.shareType == ShareType.user)
.toList();
final albumShareWith = albumShares.map((e) => e.shareWith!).toList();
// remove file shares if necessary
if (files.isNotEmpty && albumShareWith.isNotEmpty) {
try {
await UnshareFileFromAlbum(shareRepo, fileRepo, albumRepo)(
account, album, files, albumShareWith);
} catch (e, stackTrace) {
_log.severe("[call] Failed while UnshareFileFromAlbum", e, stackTrace);
if (Pref().isLabEnableSharedAlbumOr(false)) {
final files = <File>[];
if (album.provider is AlbumStaticProvider) {
files.addAll(AlbumStaticProvider.of(album)
.items
.whereType<AlbumFileItem>()
.map((e) => e.file));
}
}
// then remove the album file
// Nextcloud currently will restore also the shares after restoring from
// trash, but we aren't handling it for the files, so
for (final s in albumShares) {
try {
await RemoveShare(shareRepo)(account, s);
} catch (e, stackTrace) {
_log.severe("[call] Failed while RemoveShare: $s", e, stackTrace);
final albumShares =
(await ListShare(shareRepo)(account, album.albumFile!))
.where((element) => element.shareType == ShareType.user)
.toList();
final albumShareWith = albumShares.map((e) => e.shareWith!).toList();
// remove file shares if necessary
if (files.isNotEmpty && albumShareWith.isNotEmpty) {
try {
await UnshareFileFromAlbum(shareRepo, fileRepo, albumRepo)(
account, album, files, albumShareWith);
} catch (e, stackTrace) {
_log.severe(
"[call] Failed while UnshareFileFromAlbum", e, stackTrace);
}
}
// then remove the album file
// Nextcloud currently will restore also the shares after restoring from
// trash, but we aren't handling it for the files, so
for (final s in albumShares) {
try {
await RemoveShare(shareRepo)(account, s);
} catch (e, stackTrace) {
_log.severe("[call] Failed while RemoveShare: $s", e, stackTrace);
}
}
}
// you can't add an album to another album, so passing null here can save

View file

@ -6,6 +6,7 @@ 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/iterable_extension.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/use_case/list_share.dart';
import 'package:nc_photos/use_case/preprocess_album.dart';
import 'package:nc_photos/use_case/unshare_file_from_album.dart';
@ -46,17 +47,19 @@ class RemoveFromAlbum {
}
await UpdateAlbum(albumRepo)(account, newAlbum);
final removeFiles =
items.whereType<AlbumFileItem>().map((e) => e.file).toList();
if (removeFiles.isNotEmpty) {
final albumShares =
(await ListShare(shareRepo)(account, newAlbum.albumFile!))
.where((element) => element.shareType == ShareType.user)
.map((e) => e.shareWith!)
.toList();
if (albumShares.isNotEmpty) {
await UnshareFileFromAlbum(shareRepo, fileRepo, albumRepo)(
account, newAlbum, removeFiles, albumShares);
if (Pref().isLabEnableSharedAlbumOr(false)) {
final removeFiles =
items.whereType<AlbumFileItem>().map((e) => e.file).toList();
if (removeFiles.isNotEmpty) {
final albumShares =
(await ListShare(shareRepo)(account, newAlbum.albumFile!))
.where((element) => element.shareType == ShareType.user)
.map((e) => e.shareWith!)
.toList();
if (albumShares.isNotEmpty) {
await UnshareFileFromAlbum(shareRepo, fileRepo, albumRepo)(
account, newAlbum, removeFiles, albumShares);
}
}
}