2025-07-15 20:37:44 -05:00
|
|
|
import 'dart:async';
|
|
|
|
|
2025-06-26 19:20:39 +05:30
|
|
|
import 'package:drift/drift.dart';
|
|
|
|
import 'package:immich_mobile/domain/models/album/album.model.dart';
|
2025-07-15 20:37:44 -05:00
|
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
|
|
|
import 'package:immich_mobile/domain/models/user.model.dart';
|
2025-06-26 19:20:39 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_album.entity.drift.dart';
|
2025-07-10 11:59:15 -05:00
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_album_asset.entity.drift.dart';
|
2025-07-15 20:37:44 -05:00
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_album_user.entity.drift.dart';
|
|
|
|
import 'package:immich_mobile/infrastructure/entities/remote_asset.entity.dart';
|
2025-06-26 19:20:39 +05:30
|
|
|
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
|
|
|
|
2025-07-10 11:59:15 -05:00
|
|
|
enum SortRemoteAlbumsBy { id, updatedAt }
|
2025-06-26 19:20:39 +05:30
|
|
|
|
|
|
|
class DriftRemoteAlbumRepository extends DriftDatabaseRepository {
|
|
|
|
final Drift _db;
|
|
|
|
const DriftRemoteAlbumRepository(this._db) : super(_db);
|
|
|
|
|
2025-07-10 10:13:46 -05:00
|
|
|
Future<List<RemoteAlbum>> getAll({
|
2025-07-10 11:59:15 -05:00
|
|
|
Set<SortRemoteAlbumsBy> sortBy = const {SortRemoteAlbumsBy.updatedAt},
|
2025-07-10 10:13:46 -05:00
|
|
|
}) {
|
2025-06-30 21:24:50 -05:00
|
|
|
final assetCount = _db.remoteAlbumAssetEntity.assetId.count();
|
|
|
|
|
|
|
|
final query = _db.remoteAlbumEntity.select().join([
|
|
|
|
leftOuterJoin(
|
|
|
|
_db.remoteAlbumAssetEntity,
|
|
|
|
_db.remoteAlbumAssetEntity.albumId.equalsExp(_db.remoteAlbumEntity.id),
|
|
|
|
useColumns: false,
|
|
|
|
),
|
2025-07-08 21:54:29 +08:00
|
|
|
leftOuterJoin(
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
_db.remoteAssetEntity.id.equalsExp(_db.remoteAlbumAssetEntity.assetId),
|
|
|
|
useColumns: false,
|
|
|
|
),
|
2025-06-30 21:24:50 -05:00
|
|
|
leftOuterJoin(
|
|
|
|
_db.userEntity,
|
|
|
|
_db.userEntity.id.equalsExp(_db.remoteAlbumEntity.ownerId),
|
2025-07-08 21:54:29 +08:00
|
|
|
useColumns: false,
|
2025-06-30 21:24:50 -05:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
query
|
2025-07-08 21:54:29 +08:00
|
|
|
..where(_db.remoteAssetEntity.deletedAt.isNull())
|
2025-06-30 21:24:50 -05:00
|
|
|
..addColumns([assetCount])
|
2025-07-08 21:54:29 +08:00
|
|
|
..addColumns([_db.userEntity.name])
|
2025-06-30 21:24:50 -05:00
|
|
|
..groupBy([_db.remoteAlbumEntity.id]);
|
2025-06-26 19:20:39 +05:30
|
|
|
|
|
|
|
if (sortBy.isNotEmpty) {
|
2025-06-30 21:24:50 -05:00
|
|
|
final orderings = <OrderingTerm>[];
|
2025-06-26 19:20:39 +05:30
|
|
|
for (final sort in sortBy) {
|
|
|
|
orderings.add(
|
|
|
|
switch (sort) {
|
2025-06-30 21:24:50 -05:00
|
|
|
SortRemoteAlbumsBy.id => OrderingTerm.asc(_db.remoteAlbumEntity.id),
|
2025-07-10 11:59:15 -05:00
|
|
|
SortRemoteAlbumsBy.updatedAt =>
|
|
|
|
OrderingTerm.desc(_db.remoteAlbumEntity.updatedAt),
|
2025-06-26 19:20:39 +05:30
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
query.orderBy(orderings);
|
|
|
|
}
|
|
|
|
|
2025-06-30 21:24:50 -05:00
|
|
|
return query
|
|
|
|
.map(
|
|
|
|
(row) => row.readTable(_db.remoteAlbumEntity).toDto(
|
|
|
|
assetCount: row.read(assetCount) ?? 0,
|
2025-07-08 21:54:29 +08:00
|
|
|
ownerName: row.read(_db.userEntity.name)!,
|
2025-06-30 21:24:50 -05:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.get();
|
2025-06-26 19:20:39 +05:30
|
|
|
}
|
2025-07-10 11:59:15 -05:00
|
|
|
|
|
|
|
Future<void> create(
|
|
|
|
RemoteAlbum album,
|
|
|
|
List<String> assetIds,
|
|
|
|
) async {
|
|
|
|
await _db.transaction(() async {
|
|
|
|
final entity = RemoteAlbumEntityCompanion(
|
|
|
|
id: Value(album.id),
|
|
|
|
name: Value(album.name),
|
|
|
|
ownerId: Value(album.ownerId),
|
|
|
|
createdAt: Value(album.createdAt),
|
|
|
|
updatedAt: Value(album.updatedAt),
|
|
|
|
description: Value(album.description),
|
|
|
|
thumbnailAssetId: Value(album.thumbnailAssetId),
|
|
|
|
isActivityEnabled: Value(album.isActivityEnabled),
|
|
|
|
order: Value(album.order),
|
|
|
|
);
|
|
|
|
|
|
|
|
await _db.remoteAlbumEntity.insertOne(entity);
|
|
|
|
|
|
|
|
if (assetIds.isNotEmpty) {
|
|
|
|
final albumAssets = assetIds.map(
|
|
|
|
(assetId) => RemoteAlbumAssetEntityCompanion(
|
|
|
|
albumId: Value(album.id),
|
|
|
|
assetId: Value(assetId),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await _db.batch((batch) {
|
|
|
|
batch.insertAll(
|
|
|
|
_db.remoteAlbumAssetEntity,
|
|
|
|
albumAssets,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2025-07-11 10:06:53 -05:00
|
|
|
|
2025-07-15 20:37:44 -05:00
|
|
|
Future<void> update(RemoteAlbum album) async {
|
|
|
|
await _db.remoteAlbumEntity.update().replace(
|
|
|
|
RemoteAlbumEntityCompanion(
|
|
|
|
id: Value(album.id),
|
|
|
|
name: Value(album.name),
|
|
|
|
ownerId: Value(album.ownerId),
|
|
|
|
createdAt: Value(album.createdAt),
|
|
|
|
updatedAt: Value(album.updatedAt),
|
|
|
|
description: Value(album.description),
|
|
|
|
thumbnailAssetId: Value(album.thumbnailAssetId),
|
|
|
|
isActivityEnabled: Value(album.isActivityEnabled),
|
|
|
|
order: Value(album.order),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-07-11 10:06:53 -05:00
|
|
|
Future<int> removeAssets(String albumId, List<String> assetIds) {
|
|
|
|
return _db.remoteAlbumAssetEntity.deleteWhere(
|
|
|
|
(tbl) => tbl.albumId.equals(albumId) & tbl.assetId.isIn(assetIds),
|
|
|
|
);
|
|
|
|
}
|
2025-07-15 20:37:44 -05:00
|
|
|
|
|
|
|
FutureOr<(DateTime, DateTime)> getDateRange(String albumId) {
|
|
|
|
final query = _db.remoteAlbumAssetEntity.selectOnly()
|
|
|
|
..where(_db.remoteAlbumAssetEntity.albumId.equals(albumId))
|
|
|
|
..addColumns([
|
|
|
|
_db.remoteAssetEntity.createdAt.min(),
|
|
|
|
_db.remoteAssetEntity.createdAt.max(),
|
|
|
|
])
|
|
|
|
..join([
|
|
|
|
innerJoin(
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
_db.remoteAssetEntity.id
|
|
|
|
.equalsExp(_db.remoteAlbumAssetEntity.assetId),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return query.map((row) {
|
|
|
|
final minDate = row.read(_db.remoteAssetEntity.createdAt.min());
|
|
|
|
final maxDate = row.read(_db.remoteAssetEntity.createdAt.max());
|
|
|
|
return (minDate ?? DateTime.now(), maxDate ?? DateTime.now());
|
|
|
|
}).getSingle();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<UserDto>> getSharedUsers(String albumId) async {
|
|
|
|
final albumUserRows = await (_db.select(_db.remoteAlbumUserEntity)
|
|
|
|
..where((row) => row.albumId.equals(albumId)))
|
|
|
|
.get();
|
|
|
|
|
|
|
|
if (albumUserRows.isEmpty) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
final userIds = albumUserRows.map((row) => row.userId);
|
|
|
|
|
|
|
|
return (_db.select(_db.userEntity)..where((row) => row.id.isIn(userIds)))
|
|
|
|
.map(
|
|
|
|
(user) => UserDto(
|
|
|
|
id: user.id,
|
|
|
|
email: user.email,
|
|
|
|
name: user.name,
|
|
|
|
profileImagePath: user.profileImagePath?.isEmpty == true
|
|
|
|
? null
|
|
|
|
: user.profileImagePath,
|
|
|
|
isAdmin: user.isAdmin,
|
|
|
|
updatedAt: user.updatedAt,
|
|
|
|
quotaSizeInBytes: user.quotaSizeInBytes ?? 0,
|
|
|
|
quotaUsageInBytes: user.quotaUsageInBytes,
|
|
|
|
memoryEnabled: true,
|
|
|
|
inTimeline: false,
|
|
|
|
isPartnerSharedBy: false,
|
|
|
|
isPartnerSharedWith: false,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<RemoteAsset>> getAssets(String albumId) {
|
|
|
|
final query = _db.remoteAlbumAssetEntity.select().join([
|
|
|
|
innerJoin(
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
_db.remoteAssetEntity.id.equalsExp(_db.remoteAlbumAssetEntity.assetId),
|
|
|
|
),
|
|
|
|
])
|
|
|
|
..where(_db.remoteAlbumAssetEntity.albumId.equals(albumId));
|
|
|
|
|
|
|
|
return query
|
|
|
|
.map((row) => row.readTable(_db.remoteAssetEntity).toDto())
|
|
|
|
.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> addAssets(String albumId, List<String> assetIds) async {
|
|
|
|
final albumAssets = assetIds.map(
|
|
|
|
(assetId) => RemoteAlbumAssetEntityCompanion(
|
|
|
|
albumId: Value(albumId),
|
|
|
|
assetId: Value(assetId),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await _db.batch((batch) {
|
|
|
|
batch.insertAll(
|
|
|
|
_db.remoteAlbumAssetEntity,
|
|
|
|
albumAssets,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return assetIds.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addUsers(String albumId, List<String> userIds) {
|
|
|
|
final albumUsers = userIds.map(
|
|
|
|
(assetId) => RemoteAlbumUserEntityCompanion(
|
|
|
|
albumId: Value(albumId),
|
|
|
|
userId: Value(assetId),
|
|
|
|
role: const Value(AlbumUserRole.editor),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return _db.batch((batch) {
|
|
|
|
batch.insertAll(
|
|
|
|
_db.remoteAlbumUserEntity,
|
|
|
|
albumUsers,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteAlbum(String albumId) async {
|
|
|
|
return _db.transaction(() async {
|
|
|
|
await _db.remoteAlbumEntity.deleteWhere(
|
|
|
|
(table) => table.id.equals(albumId),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<RemoteAlbum?> watchAlbum(String albumId) {
|
|
|
|
final query = _db.remoteAlbumEntity.select().join([
|
|
|
|
leftOuterJoin(
|
|
|
|
_db.remoteAlbumAssetEntity,
|
|
|
|
_db.remoteAlbumAssetEntity.albumId.equalsExp(_db.remoteAlbumEntity.id),
|
|
|
|
useColumns: false,
|
|
|
|
),
|
|
|
|
leftOuterJoin(
|
|
|
|
_db.remoteAssetEntity,
|
|
|
|
_db.remoteAssetEntity.id.equalsExp(_db.remoteAlbumAssetEntity.assetId),
|
|
|
|
useColumns: false,
|
|
|
|
),
|
|
|
|
leftOuterJoin(
|
|
|
|
_db.userEntity,
|
|
|
|
_db.userEntity.id.equalsExp(_db.remoteAlbumEntity.ownerId),
|
|
|
|
useColumns: false,
|
|
|
|
),
|
|
|
|
])
|
|
|
|
..where(_db.remoteAlbumEntity.id.equals(albumId))
|
|
|
|
..addColumns([_db.userEntity.name])
|
|
|
|
..groupBy([_db.remoteAlbumEntity.id]);
|
|
|
|
|
|
|
|
return query.map((row) {
|
|
|
|
final album = row.readTable(_db.remoteAlbumEntity).toDto(
|
|
|
|
ownerName: row.read(_db.userEntity.name)!,
|
|
|
|
);
|
|
|
|
return album;
|
|
|
|
}).watchSingleOrNull();
|
|
|
|
}
|
2025-06-26 19:20:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
extension on RemoteAlbumEntityData {
|
2025-07-10 10:13:46 -05:00
|
|
|
RemoteAlbum toDto({int assetCount = 0, required String ownerName}) {
|
|
|
|
return RemoteAlbum(
|
2025-06-26 19:20:39 +05:30
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
ownerId: ownerId,
|
|
|
|
createdAt: createdAt,
|
|
|
|
updatedAt: updatedAt,
|
|
|
|
description: description,
|
|
|
|
thumbnailAssetId: thumbnailAssetId,
|
|
|
|
isActivityEnabled: isActivityEnabled,
|
|
|
|
order: order,
|
2025-06-30 21:24:50 -05:00
|
|
|
assetCount: assetCount,
|
|
|
|
ownerName: ownerName,
|
2025-06-26 19:20:39 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|