* fix: watch asset delete/archive in search * fix: restore via toast * chore: format * refactor: simplify search asset pruning * chore: implement suggestions * chore: more suggestions * fix: mounted check (cherry picked from commit f7b8ae5da370f947223b006618e5b4e5fb42fd33)
258 lines
8.8 KiB
Dart
258 lines
8.8 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:immich_mobile/data/db/main/database.dart';
|
|
import 'package:immich_mobile/data/db/main/table/asset/edit.dart';
|
|
import 'package:immich_mobile/data/db/main/table/remote/asset.dart';
|
|
import 'package:immich_mobile/data/db/main/table/remote/asset.drift.dart';
|
|
import 'package:immich_mobile/data/db/main/table/remote/exif.dart';
|
|
import 'package:immich_mobile/data/db/main/table/remote/exif.drift.dart';
|
|
import 'package:immich_mobile/data/db/main/table/remote/stack.drift.dart';
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
|
import 'package:immich_mobile/domain/models/asset_edit.model.dart';
|
|
import 'package:immich_mobile/domain/models/exif.model.dart';
|
|
import 'package:immich_mobile/domain/models/stack.model.dart';
|
|
import 'package:immich_mobile/infrastructure/repositories/remote_asset.repository.drift.dart';
|
|
import 'package:immich_mobile/utils/option.dart';
|
|
|
|
@DriftAccessor()
|
|
class RemoteAssetRepository extends DatabaseAccessor<Drift> with $RemoteAssetRepositoryMixin {
|
|
RemoteAssetRepository(super.attachedDatabase);
|
|
|
|
Drift get _db => attachedDatabase;
|
|
|
|
SingleOrNullSelectable<RemoteAsset?> _assetSelectable(String id) {
|
|
final query =
|
|
_db.remoteAssetEntity.select().addColumns([_db.localAssetEntity.id]).join([
|
|
leftOuterJoin(
|
|
_db.localAssetEntity,
|
|
_db.remoteAssetEntity.checksum.equalsExp(_db.localAssetEntity.checksum),
|
|
useColumns: false,
|
|
),
|
|
])
|
|
..where(_db.remoteAssetEntity.id.equals(id))
|
|
..limit(1);
|
|
|
|
return query.map((row) {
|
|
final asset = row.readTable(_db.remoteAssetEntity).toDto();
|
|
return asset.copyWith(localId: row.read(_db.localAssetEntity.id));
|
|
});
|
|
}
|
|
|
|
Stream<RemoteAsset?> watch(String id) {
|
|
return _assetSelectable(id).watchSingleOrNull();
|
|
}
|
|
|
|
Stream<Set<String>> watchDeletedAssetIds(Iterable<String> ids, AssetVisibility visibility) {
|
|
final idList = ids.toList(growable: false);
|
|
if (idList.isEmpty) {
|
|
return Stream.value(const <String>{});
|
|
}
|
|
|
|
final query = _db.remoteAssetEntity.selectOnly()
|
|
..addColumns([_db.remoteAssetEntity.id])
|
|
..where(
|
|
_db.remoteAssetEntity.id.isIn(idList) &
|
|
(_db.remoteAssetEntity.deletedAt.isNotNull() |
|
|
_db.remoteAssetEntity.visibility.equalsValue(visibility).not()),
|
|
);
|
|
|
|
return query.map((row) => row.read(_db.remoteAssetEntity.id)!).watch().map((rows) => rows.toSet());
|
|
}
|
|
|
|
Future<RemoteAsset?> get(String id) {
|
|
return _assetSelectable(id).getSingleOrNull();
|
|
}
|
|
|
|
Future<List<RemoteAsset>> getAllDebugForChecksum(String checksum) {
|
|
final query = _db.remoteAssetEntity.select()..where((row) => row.checksum.equals(checksum));
|
|
|
|
return query.map((row) => row.toDto()).get();
|
|
}
|
|
|
|
Future<List<RemoteAsset>> getStackChildren(RemoteAsset asset) {
|
|
final stackId = asset.stackId;
|
|
if (stackId == null) {
|
|
return Future.value(const []);
|
|
}
|
|
|
|
final query = _db.remoteAssetEntity.select()
|
|
..where((row) => row.stackId.equals(stackId) & row.id.equals(asset.id).not())
|
|
..orderBy([(row) => OrderingTerm.desc(row.createdAt)]);
|
|
|
|
return query.map((row) => row.toDto()).get();
|
|
}
|
|
|
|
Stream<ExifInfo?> watchExif(String id) => _db.managers.remoteExifEntity
|
|
.filter((row) => row.assetId.id.equals(id))
|
|
.map((row) => row.toDto())
|
|
.watchSingleOrNull();
|
|
|
|
Future<List<(String, String)>> getPlaces(String userId) {
|
|
final asset = Subquery(
|
|
_db.remoteAssetEntity.select()
|
|
..where((row) => row.ownerId.equals(userId))
|
|
..orderBy([(row) => OrderingTerm.desc(row.createdAt)]),
|
|
"asset",
|
|
);
|
|
|
|
final query =
|
|
asset.selectOnly().join([
|
|
innerJoin(
|
|
_db.remoteExifEntity,
|
|
_db.remoteExifEntity.assetId.equalsExp(asset.ref(_db.remoteAssetEntity.id)),
|
|
useColumns: false,
|
|
),
|
|
])
|
|
..addColumns([_db.remoteExifEntity.city, _db.remoteExifEntity.assetId])
|
|
..where(
|
|
_db.remoteExifEntity.city.isNotNull() &
|
|
asset.ref(_db.remoteAssetEntity.deletedAt).isNull() &
|
|
asset.ref(_db.remoteAssetEntity.visibility).equals(AssetVisibility.timeline.index),
|
|
)
|
|
..groupBy([_db.remoteExifEntity.city])
|
|
..orderBy([OrderingTerm.asc(_db.remoteExifEntity.city)]);
|
|
|
|
return query.map((row) {
|
|
final assetId = row.read(_db.remoteExifEntity.assetId);
|
|
final city = row.read(_db.remoteExifEntity.city);
|
|
return (city!, assetId!);
|
|
}).get();
|
|
}
|
|
|
|
Future<void> trash(List<String> ids) {
|
|
return _db.batch((batch) async {
|
|
for (final id in ids) {
|
|
batch.update(
|
|
_db.remoteAssetEntity,
|
|
RemoteAssetEntityCompanion(deletedAt: Value(DateTime.now())),
|
|
where: (e) => e.id.equals(id),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> restoreTrash(List<String> ids) {
|
|
return _db.batch((batch) async {
|
|
for (final id in ids) {
|
|
batch.update(
|
|
_db.remoteAssetEntity,
|
|
const RemoteAssetEntityCompanion(deletedAt: Value(null)),
|
|
where: (e) => e.id.equals(id),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> emptyTrash(String ownerId) async {
|
|
await _db.remoteAssetEntity.deleteWhere((t) => t.deletedAt.isNotNull() & t.ownerId.equals(ownerId));
|
|
}
|
|
|
|
Future<void> restoreAllTrash(String ownerId) async {
|
|
await (_db.remoteAssetEntity.update()..where((t) => t.deletedAt.isNotNull() & t.ownerId.equals(ownerId))).write(
|
|
const RemoteAssetEntityCompanion(deletedAt: Value(null)),
|
|
);
|
|
}
|
|
|
|
Future<void> deleteAssets(List<String> ids) {
|
|
return _db.batch((batch) {
|
|
for (final id in ids) {
|
|
batch.deleteWhere(_db.remoteAssetEntity, (row) => row.id.equals(id));
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> stack(String userId, StackResponse stack) {
|
|
return _db.transaction(() async {
|
|
final stackIds = await _db.managers.stackEntity
|
|
.filter((row) => row.primaryAssetId.isIn(stack.assetIds))
|
|
.map((row) => row.id)
|
|
.get();
|
|
|
|
await _db.batch((batch) {
|
|
for (final stackId in stackIds) {
|
|
batch.deleteWhere(_db.stackEntity, (row) => row.id.equals(stackId));
|
|
}
|
|
});
|
|
|
|
await _db.batch((batch) {
|
|
final companion = StackEntityCompanion(ownerId: Value(userId), primaryAssetId: Value(stack.primaryAssetId));
|
|
|
|
batch.insert(_db.stackEntity, companion.copyWith(id: Value(stack.id)), onConflict: DoUpdate((_) => companion));
|
|
|
|
for (final assetId in stack.assetIds) {
|
|
batch.update(
|
|
_db.remoteAssetEntity,
|
|
RemoteAssetEntityCompanion(stackId: Value(stack.id)),
|
|
where: (e) => e.id.equals(assetId),
|
|
);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
Future<void> unStack(List<String> stackIds) {
|
|
return _db.transaction(() async {
|
|
await _db.batch((batch) {
|
|
for (final stackId in stackIds) {
|
|
batch.deleteWhere(_db.stackEntity, (row) => row.id.equals(stackId));
|
|
}
|
|
});
|
|
|
|
// TODO: delete this after adding foreign key on stackId
|
|
await _db.batch((batch) {
|
|
for (final stackId in stackIds) {
|
|
batch.update(
|
|
_db.remoteAssetEntity,
|
|
const RemoteAssetEntityCompanion(stackId: Value(null)),
|
|
where: (e) => e.stackId.equals(stackId),
|
|
);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
Future<void> updateDescription(String assetId, String description) async {
|
|
await (_db.remoteExifEntity.update()..where((row) => row.assetId.equals(assetId))).write(
|
|
RemoteExifEntityCompanion(description: Value(description)),
|
|
);
|
|
}
|
|
|
|
Future<void> updateRating(String assetId, int? rating) async {
|
|
await (_db.remoteExifEntity.update()..where((row) => row.assetId.equals(assetId))).write(
|
|
RemoteExifEntityCompanion(rating: Value(rating)),
|
|
);
|
|
}
|
|
|
|
Future<int> getCount() {
|
|
return _db.managers.remoteAssetEntity.count();
|
|
}
|
|
|
|
Future<List<AssetEdit>> getAssetEdits(String assetId) {
|
|
final query = _db.assetEditEntity.select()
|
|
..where((row) => row.assetId.equals(assetId) & row.action.equals(AssetEditAction.other.index).not())
|
|
..orderBy([(row) => OrderingTerm.asc(row.sequence)]);
|
|
return query.map((row) => row.toDto()!).get();
|
|
}
|
|
|
|
Future<void> updateAssets(
|
|
List<String> remoteIds, {
|
|
Option<bool> isFavorite = const .none(),
|
|
Option<AssetVisibility> visibility = const .none(),
|
|
Option<DateTime> createdAt = const .none(),
|
|
}) async {
|
|
if ([isFavorite, visibility, createdAt].every((option) => option.isNone)) {
|
|
return;
|
|
}
|
|
|
|
final companion = RemoteAssetEntityCompanion(
|
|
visibility: visibility.toDriftValue(),
|
|
isFavorite: isFavorite.toDriftValue(),
|
|
createdAt: createdAt.toDriftValue(),
|
|
);
|
|
return _db.batch((batch) {
|
|
for (final remoteId in remoteIds) {
|
|
batch.update(_db.remoteAssetEntity, companion, where: (e) => e.id.equals(remoteId));
|
|
}
|
|
});
|
|
}
|
|
}
|