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/album/provider.dart';
import 'package:nc_photos/entity/file.dart'; import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/share.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/create_share.dart';
import 'package:nc_photos/use_case/list_share.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/preprocess_album.dart';
@ -40,10 +41,12 @@ class AddToAlbum {
); );
await UpdateAlbum(albumRepo)(account, newAlbum); await UpdateAlbum(albumRepo)(account, newAlbum);
final newFiles = if (Pref().isLabEnableSharedAlbumOr(false)) {
items.whereType<AlbumFileItem>().map((e) => e.file).toList(); final newFiles =
if (newFiles.isNotEmpty) { items.whereType<AlbumFileItem>().map((e) => e.file).toList();
await _shareFiles(account, newAlbum, newFiles); if (newFiles.isNotEmpty) {
await _shareFiles(account, newAlbum, newFiles);
}
} }
return newAlbum; 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/album/provider.dart';
import 'package:nc_photos/entity/file.dart'; import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/share.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/list_share.dart';
import 'package:nc_photos/use_case/remove.dart'; import 'package:nc_photos/use_case/remove.dart';
import 'package:nc_photos/use_case/remove_share.dart'; import 'package:nc_photos/use_case/remove_share.dart';
@ -16,34 +17,38 @@ class RemoveAlbum {
/// Remove an album /// Remove an album
Future<void> call(Account account, Album album) async { Future<void> call(Account account, Album album) async {
_log.info("[call] Remove album: $album"); _log.info("[call] Remove album: $album");
final files = <File>[]; if (Pref().isLabEnableSharedAlbumOr(false)) {
if (album.provider is AlbumStaticProvider) { final files = <File>[];
files.addAll(AlbumStaticProvider.of(album) if (album.provider is AlbumStaticProvider) {
.items files.addAll(AlbumStaticProvider.of(album)
.whereType<AlbumFileItem>() .items
.map((e) => e.file)); .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);
} }
} final albumShares =
// then remove the album file (await ListShare(shareRepo)(account, album.albumFile!))
// Nextcloud currently will restore also the shares after restoring from .where((element) => element.shareType == ShareType.user)
// trash, but we aren't handling it for the files, so .toList();
for (final s in albumShares) { final albumShareWith = albumShares.map((e) => e.shareWith!).toList();
try { // remove file shares if necessary
await RemoveShare(shareRepo)(account, s); if (files.isNotEmpty && albumShareWith.isNotEmpty) {
} catch (e, stackTrace) { try {
_log.severe("[call] Failed while RemoveShare: $s", e, stackTrace); 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 // 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/file.dart';
import 'package:nc_photos/entity/share.dart'; import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/iterable_extension.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/list_share.dart';
import 'package:nc_photos/use_case/preprocess_album.dart'; import 'package:nc_photos/use_case/preprocess_album.dart';
import 'package:nc_photos/use_case/unshare_file_from_album.dart'; import 'package:nc_photos/use_case/unshare_file_from_album.dart';
@ -46,17 +47,19 @@ class RemoveFromAlbum {
} }
await UpdateAlbum(albumRepo)(account, newAlbum); await UpdateAlbum(albumRepo)(account, newAlbum);
final removeFiles = if (Pref().isLabEnableSharedAlbumOr(false)) {
items.whereType<AlbumFileItem>().map((e) => e.file).toList(); final removeFiles =
if (removeFiles.isNotEmpty) { items.whereType<AlbumFileItem>().map((e) => e.file).toList();
final albumShares = if (removeFiles.isNotEmpty) {
(await ListShare(shareRepo)(account, newAlbum.albumFile!)) final albumShares =
.where((element) => element.shareType == ShareType.user) (await ListShare(shareRepo)(account, newAlbum.albumFile!))
.map((e) => e.shareWith!) .where((element) => element.shareType == ShareType.user)
.toList(); .map((e) => e.shareWith!)
if (albumShares.isNotEmpty) { .toList();
await UnshareFileFromAlbum(shareRepo, fileRepo, albumRepo)( if (albumShares.isNotEmpty) {
account, newAlbum, removeFiles, albumShares); await UnshareFileFromAlbum(shareRepo, fileRepo, albumRepo)(
account, newAlbum, removeFiles, albumShares);
}
} }
} }