2025-06-30 12:21:09 -05:00
|
|
|
import 'package:drift/drift.dart';
|
2025-07-01 03:38:15 +08:00
|
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
2025-07-02 23:54:37 +05:30
|
|
|
import 'package:immich_mobile/domain/models/exif.model.dart';
|
2025-07-18 10:01:04 +05:30
|
|
|
import 'package:immich_mobile/domain/models/stack.model.dart';
|
2025-07-25 08:07:22 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/entities/exif.entity.dart' hide ExifInfo;
|
2025-07-02 00:52:11 +08:00
|
|
|
import 'package:immich_mobile/infrastructure/entities/exif.entity.drift.dart';
|
2025-07-05 00:38:06 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_asset.entity.dart';
|
2025-06-30 12:21:09 -05:00
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_asset.entity.drift.dart';
|
2025-07-18 10:01:04 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/entities/stack.entity.drift.dart';
|
2025-06-30 12:21:09 -05:00
|
|
|
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
2025-07-02 00:52:11 +08:00
|
|
|
import 'package:maplibre_gl/maplibre_gl.dart';
|
2025-06-30 12:21:09 -05:00
|
|
|
|
2025-07-02 23:54:37 +05:30
|
|
|
class RemoteAssetRepository extends DriftDatabaseRepository {
|
2025-06-30 12:21:09 -05:00
|
|
|
final Drift _db;
|
2025-07-02 23:54:37 +05:30
|
|
|
const RemoteAssetRepository(this._db) : super(_db);
|
|
|
|
|
|
2025-07-07 23:41:37 -05:00
|
|
|
/// For testing purposes
|
|
|
|
|
Future<List<RemoteAsset>> getSome(String userId) {
|
|
|
|
|
final query = _db.remoteAssetEntity.select()
|
|
|
|
|
..where(
|
|
|
|
|
(row) =>
|
|
|
|
|
_db.remoteAssetEntity.ownerId.equals(userId) &
|
|
|
|
|
_db.remoteAssetEntity.deletedAt.isNull() &
|
2025-07-25 08:07:22 +05:30
|
|
|
_db.remoteAssetEntity.visibility.equalsValue(AssetVisibility.timeline),
|
2025-07-07 23:41:37 -05:00
|
|
|
)
|
|
|
|
|
..orderBy([(row) => OrderingTerm.desc(row.createdAt)])
|
|
|
|
|
..limit(10);
|
|
|
|
|
|
|
|
|
|
return query.map((row) => row.toDto()).get();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 12:02:49 -05:00
|
|
|
SingleOrNullSelectable<RemoteAsset?> _assetSelectable(String id) {
|
2025-07-29 00:34:03 +05:30
|
|
|
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);
|
2025-07-25 12:02:49 -05:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<RemoteAsset?> get(String id) {
|
|
|
|
|
return _assetSelectable(id).getSingleOrNull();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 19:08:53 +05:30
|
|
|
Future<RemoteAsset?> getByChecksum(String checksum) {
|
|
|
|
|
final query = _db.remoteAssetEntity.select()..where((row) => row.checksum.equals(checksum));
|
|
|
|
|
|
|
|
|
|
return query.map((row) => row.toDto()).getSingleOrNull();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 10:01:04 +05:30
|
|
|
Future<List<RemoteAsset>> getStackChildren(RemoteAsset asset) {
|
2025-09-18 02:16:14 -04:00
|
|
|
final stackId = asset.stackId;
|
|
|
|
|
if (stackId == null) {
|
|
|
|
|
return Future.value(const []);
|
2025-07-18 10:01:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final query = _db.remoteAssetEntity.select()
|
2025-09-18 02:16:14 -04:00
|
|
|
..where((row) => row.stackId.equals(stackId) & row.id.equals(asset.id).not())
|
2025-07-18 10:01:04 +05:30
|
|
|
..orderBy([(row) => OrderingTerm.desc(row.createdAt)]);
|
|
|
|
|
|
|
|
|
|
return query.map((row) => row.toDto()).get();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 23:54:37 +05:30
|
|
|
Future<ExifInfo?> getExif(String id) {
|
|
|
|
|
return _db.managers.remoteExifEntity
|
|
|
|
|
.filter((row) => row.assetId.id.equals(id))
|
|
|
|
|
.map((row) => row.toDto())
|
|
|
|
|
.getSingleOrNull();
|
|
|
|
|
}
|
2025-06-30 12:21:09 -05:00
|
|
|
|
2025-07-15 23:10:12 +08:00
|
|
|
Future<List<(String, String)>> getPlaces() {
|
|
|
|
|
final asset = Subquery(
|
2025-07-25 08:07:22 +05:30
|
|
|
_db.remoteAssetEntity.select()..orderBy([(row) => OrderingTerm.desc(row.createdAt)]),
|
2025-07-15 23:10:12 +08:00
|
|
|
"asset",
|
|
|
|
|
);
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
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)]);
|
2025-07-15 23:10:12 +08:00
|
|
|
|
|
|
|
|
return query.map((row) {
|
|
|
|
|
final assetId = row.read(_db.remoteExifEntity.assetId);
|
|
|
|
|
final city = row.read(_db.remoteExifEntity.city);
|
|
|
|
|
return (city!, assetId!);
|
|
|
|
|
}).get();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 12:21:09 -05:00
|
|
|
Future<void> updateFavorite(List<String> ids, bool isFavorite) {
|
|
|
|
|
return _db.batch((batch) async {
|
|
|
|
|
for (final id in ids) {
|
|
|
|
|
batch.update(
|
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
|
RemoteAssetEntityCompanion(isFavorite: Value(isFavorite)),
|
|
|
|
|
where: (e) => e.id.equals(id),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-01 03:38:15 +08:00
|
|
|
|
|
|
|
|
Future<void> updateVisibility(List<String> ids, AssetVisibility visibility) {
|
|
|
|
|
return _db.batch((batch) async {
|
|
|
|
|
for (final id in ids) {
|
|
|
|
|
batch.update(
|
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
|
RemoteAssetEntityCompanion(visibility: Value(visibility)),
|
|
|
|
|
where: (e) => e.id.equals(id),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-02 00:52:11 +08:00
|
|
|
|
2025-07-03 01:26:07 +08:00
|
|
|
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),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 11:36:00 -05:00
|
|
|
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),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 01:26:07 +08:00
|
|
|
Future<void> delete(List<String> ids) {
|
2025-09-19 19:17:56 +05:30
|
|
|
return _db.batch((batch) {
|
|
|
|
|
for (final id in ids) {
|
|
|
|
|
batch.deleteWhere(_db.remoteAssetEntity, (row) => row.id.equals(id));
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-07-03 01:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-02 00:52:11 +08:00
|
|
|
Future<void> updateLocation(List<String> ids, LatLng location) {
|
|
|
|
|
return _db.batch((batch) async {
|
|
|
|
|
for (final id in ids) {
|
|
|
|
|
batch.update(
|
|
|
|
|
_db.remoteExifEntity,
|
2025-07-29 00:34:03 +05:30
|
|
|
RemoteExifEntityCompanion(latitude: Value(location.latitude), longitude: Value(location.longitude)),
|
2025-07-02 00:52:11 +08:00
|
|
|
where: (e) => e.assetId.equals(id),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-18 10:01:04 +05:30
|
|
|
|
2025-07-30 14:40:13 -05:00
|
|
|
Future<void> updateDateTime(List<String> ids, DateTime dateTime) {
|
|
|
|
|
return _db.batch((batch) async {
|
|
|
|
|
for (final id in ids) {
|
|
|
|
|
batch.update(
|
|
|
|
|
_db.remoteExifEntity,
|
|
|
|
|
RemoteExifEntityCompanion(dateTimeOriginal: Value(dateTime)),
|
|
|
|
|
where: (e) => e.assetId.equals(id),
|
|
|
|
|
);
|
|
|
|
|
batch.update(
|
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
|
RemoteAssetEntityCompanion(createdAt: Value(dateTime)),
|
|
|
|
|
where: (e) => e.id.equals(id),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 10:01:04 +05:30
|
|
|
Future<void> stack(String userId, StackResponse stack) {
|
|
|
|
|
return _db.transaction(() async {
|
|
|
|
|
final stackIds = await _db.managers.stackEntity
|
2025-07-22 01:50:41 +05:30
|
|
|
.filter((row) => row.primaryAssetId.isIn(stack.assetIds))
|
2025-07-18 10:01:04 +05:30
|
|
|
.map((row) => row.id)
|
|
|
|
|
.get();
|
|
|
|
|
|
2025-09-19 19:17:56 +05:30
|
|
|
await _db.batch((batch) {
|
|
|
|
|
for (final stackId in stackIds) {
|
|
|
|
|
batch.deleteWhere(_db.stackEntity, (row) => row.id.equals(stackId));
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-07-18 10:01:04 +05:30
|
|
|
|
|
|
|
|
await _db.batch((batch) {
|
2025-07-29 00:34:03 +05:30
|
|
|
final companion = StackEntityCompanion(ownerId: Value(userId), primaryAssetId: Value(stack.primaryAssetId));
|
2025-07-18 10:01:04 +05:30
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
batch.insert(_db.stackEntity, companion.copyWith(id: Value(stack.id)), onConflict: DoUpdate((_) => companion));
|
2025-07-18 10:01:04 +05:30
|
|
|
|
|
|
|
|
for (final assetId in stack.assetIds) {
|
|
|
|
|
batch.update(
|
|
|
|
|
_db.remoteAssetEntity,
|
2025-07-29 00:34:03 +05:30
|
|
|
RemoteAssetEntityCompanion(stackId: Value(stack.id)),
|
2025-07-18 10:01:04 +05:30
|
|
|
where: (e) => e.id.equals(assetId),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> unStack(List<String> stackIds) {
|
|
|
|
|
return _db.transaction(() async {
|
2025-09-19 19:17:56 +05:30
|
|
|
await _db.batch((batch) {
|
|
|
|
|
for (final stackId in stackIds) {
|
|
|
|
|
batch.deleteWhere(_db.stackEntity, (row) => row.id.equals(stackId));
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-07-18 10:01:04 +05:30
|
|
|
|
|
|
|
|
// TODO: delete this after adding foreign key on stackId
|
|
|
|
|
await _db.batch((batch) {
|
2025-09-19 19:17:56 +05:30
|
|
|
for (final stackId in stackIds) {
|
|
|
|
|
batch.update(
|
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
|
const RemoteAssetEntityCompanion(stackId: Value(null)),
|
|
|
|
|
where: (e) => e.stackId.equals(stackId),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-07-18 10:01:04 +05:30
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-22 11:24:32 -05:00
|
|
|
|
2025-07-29 16:17:33 -05:00
|
|
|
Future<void> updateDescription(String assetId, String description) async {
|
|
|
|
|
await (_db.remoteExifEntity.update()..where((row) => row.assetId.equals(assetId))).write(
|
|
|
|
|
RemoteExifEntityCompanion(description: Value(description)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 11:24:32 -05:00
|
|
|
Future<int> getCount() {
|
|
|
|
|
return _db.managers.remoteAssetEntity.count();
|
|
|
|
|
}
|
2025-06-30 12:21:09 -05:00
|
|
|
}
|