2024-09-16 20:26:14 +00:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-09-24 12:50:21 +00:00
|
|
|
import 'package:immich_mobile/entities/store.entity.dart';
|
2024-09-16 20:26:14 +00:00
|
|
|
import 'package:immich_mobile/entities/user.entity.dart';
|
|
|
|
import 'package:immich_mobile/interfaces/user.interface.dart';
|
|
|
|
import 'package:immich_mobile/providers/db.provider.dart';
|
2024-09-30 14:37:30 +00:00
|
|
|
import 'package:immich_mobile/repositories/database.repository.dart';
|
2024-09-16 20:26:14 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
|
|
|
|
final userRepositoryProvider =
|
|
|
|
Provider((ref) => UserRepository(ref.watch(dbProvider)));
|
|
|
|
|
2024-09-30 14:37:30 +00:00
|
|
|
class UserRepository extends DatabaseRepository implements IUserRepository {
|
|
|
|
UserRepository(super.db);
|
2024-09-16 20:26:14 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<User>> getByIds(List<String> ids) async =>
|
2024-09-30 14:37:30 +00:00
|
|
|
(await db.users.getAllById(ids)).nonNulls.toList();
|
2024-09-20 13:32:37 +00:00
|
|
|
|
|
|
|
@override
|
2024-09-30 14:37:30 +00:00
|
|
|
Future<User?> get(String id) => db.users.getById(id);
|
2024-09-24 12:50:21 +00:00
|
|
|
|
|
|
|
@override
|
2024-09-30 14:37:30 +00:00
|
|
|
Future<List<User>> getAll({bool self = true, UserSort? sortBy}) {
|
|
|
|
final baseQuery = db.users.where();
|
2024-09-24 12:50:21 +00:00
|
|
|
final int userId = Store.get(StoreKey.currentUser).isarId;
|
2024-09-30 14:37:30 +00:00
|
|
|
final QueryBuilder<User, User, QAfterWhereClause> afterWhere =
|
|
|
|
self ? baseQuery.noOp() : baseQuery.isarIdNotEqualTo(userId);
|
|
|
|
final QueryBuilder<User, User, QAfterSortBy> query;
|
|
|
|
switch (sortBy) {
|
|
|
|
case null:
|
|
|
|
query = afterWhere.noOp();
|
|
|
|
case UserSort.id:
|
|
|
|
query = afterWhere.sortById();
|
|
|
|
}
|
|
|
|
return query.findAll();
|
2024-09-24 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<User> update(User user) async {
|
2024-09-30 14:37:30 +00:00
|
|
|
await txn(() => db.users.put(user));
|
2024-09-24 12:50:21 +00:00
|
|
|
return user;
|
|
|
|
}
|
2024-09-30 14:37:30 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<User> me() => Future.value(Store.get(StoreKey.currentUser));
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> deleteById(List<int> ids) => txn(() => db.users.deleteAll(ids));
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<User>> upsertAll(List<User> users) async {
|
|
|
|
await txn(() => db.users.putAll(users));
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<User>> getAllAccessible() => db.users
|
|
|
|
.filter()
|
|
|
|
.isPartnerSharedWithEqualTo(true)
|
|
|
|
.or()
|
|
|
|
.isarIdEqualTo(Store.get(StoreKey.currentUser).isarId)
|
|
|
|
.findAll();
|
2024-09-16 20:26:14 +00:00
|
|
|
}
|