From 36dee1a7f1264ee5eaa45f0ae7d0348374364536 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sat, 30 Oct 2021 01:37:11 +0800 Subject: [PATCH] Guard shared album logic with pref flag --- lib/use_case/add_to_album.dart | 11 ++++-- lib/use_case/remove_album.dart | 59 ++++++++++++++++------------- lib/use_case/remove_from_album.dart | 25 ++++++------ 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/lib/use_case/add_to_album.dart b/lib/use_case/add_to_album.dart index 7b871743..3ea207e8 100644 --- a/lib/use_case/add_to_album.dart +++ b/lib/use_case/add_to_album.dart @@ -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().map((e) => e.file).toList(); - if (newFiles.isNotEmpty) { - await _shareFiles(account, newAlbum, newFiles); + if (Pref().isLabEnableSharedAlbumOr(false)) { + final newFiles = + items.whereType().map((e) => e.file).toList(); + if (newFiles.isNotEmpty) { + await _shareFiles(account, newAlbum, newFiles); + } } return newAlbum; diff --git a/lib/use_case/remove_album.dart b/lib/use_case/remove_album.dart index b7b6cf3f..d43e4151 100644 --- a/lib/use_case/remove_album.dart +++ b/lib/use_case/remove_album.dart @@ -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 call(Account account, Album album) async { _log.info("[call] Remove album: $album"); - final files = []; - if (album.provider is AlbumStaticProvider) { - files.addAll(AlbumStaticProvider.of(album) - .items - .whereType() - .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 = []; + if (album.provider is AlbumStaticProvider) { + files.addAll(AlbumStaticProvider.of(album) + .items + .whereType() + .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 diff --git a/lib/use_case/remove_from_album.dart b/lib/use_case/remove_from_album.dart index e6f71425..60949fb4 100644 --- a/lib/use_case/remove_from_album.dart +++ b/lib/use_case/remove_from_album.dart @@ -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().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().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); + } } }