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

52 lines
1.8 KiB
Dart
Raw Normal View History

2022-01-31 05:24:48 +01:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2022-01-31 05:24:48 +01:00
import 'package:nc_photos/use_case/cache_favorite.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
import 'package:np_db/np_db.dart';
2022-01-31 05:24:48 +01:00
2022-12-16 16:01:04 +01:00
part 'sync_favorite.g.dart';
@npLog
2022-01-31 05:24:48 +01:00
class SyncFavorite {
SyncFavorite(this._c) : assert(require(_c));
2022-01-31 05:24:48 +01:00
static bool require(DiContainer c) => DiContainer.has(c, DiType.favoriteRepo);
2022-01-31 05:24:48 +01:00
/// Sync favorites in cache db with remote server
///
/// Return number of files updated
Future<DbSyncIdResult> call(Account account) async {
2022-01-31 05:24:48 +01:00
_log.info("[call] Sync favorites with remote");
final remote = await _getRemoteFavoriteFileIds(account);
return await CacheFavorite(_c)(account, remote);
}
Future<List<int>> _getRemoteFavoriteFileIds(Account account) async {
final settings = AccountPref.of(account);
final shareDir =
File(path: file_util.unstripPath(account, settings.getShareFolderOr()));
bool isShareDirIncluded = false;
final fileIds = <int>[];
for (final r in account.roots) {
final dir = File(path: file_util.unstripPath(account, r));
final favorites = await _c.favoriteRepo.list(account, dir);
fileIds.addAll(favorites.map((f) => f.fileId));
isShareDirIncluded |= file_util.isOrUnderDir(shareDir, dir);
}
if (!isShareDirIncluded) {
_log.info("[_getRemoteFavoriteFileIds] Explicitly querying share folder");
final favorites = await _c.favoriteRepo.list(account, shareDir);
fileIds.addAll(favorites.map((f) => f.fileId));
}
return fileIds;
2022-01-31 05:24:48 +01:00
}
final DiContainer _c;
}