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