2023-05-25 03:52:43 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2022-06-25 18:46:51 +00:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-05-29 03:35:45 +00:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
2024-09-24 12:50:21 +00:00
|
|
|
import 'package:immich_mobile/interfaces/partner_api.interface.dart';
|
|
|
|
import 'package:immich_mobile/interfaces/user.interface.dart';
|
|
|
|
import 'package:immich_mobile/interfaces/user_api.interface.dart';
|
|
|
|
import 'package:immich_mobile/repositories/partner_api.repository.dart';
|
|
|
|
import 'package:immich_mobile/repositories/user.repository.dart';
|
|
|
|
import 'package:immich_mobile/repositories/user_api.repository.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/entities/user.entity.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/services/sync.service.dart';
|
2023-05-25 03:52:43 +00:00
|
|
|
import 'package:immich_mobile/utils/diff.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2022-04-24 02:08:45 +00:00
|
|
|
|
2022-07-13 12:23:48 +00:00
|
|
|
final userServiceProvider = Provider(
|
|
|
|
(ref) => UserService(
|
2024-09-24 12:50:21 +00:00
|
|
|
ref.watch(partnerApiRepositoryProvider),
|
|
|
|
ref.watch(userApiRepositoryProvider),
|
|
|
|
ref.watch(userRepositoryProvider),
|
2023-03-03 22:38:30 +00:00
|
|
|
ref.watch(syncServiceProvider),
|
2022-07-13 12:23:48 +00:00
|
|
|
),
|
|
|
|
);
|
2022-06-25 18:46:51 +00:00
|
|
|
|
2022-04-24 02:08:45 +00:00
|
|
|
class UserService {
|
2024-09-24 12:50:21 +00:00
|
|
|
final IPartnerApiRepository _partnerApiRepository;
|
|
|
|
final IUserApiRepository _userApiRepository;
|
|
|
|
final IUserRepository _userRepository;
|
2023-03-03 22:38:30 +00:00
|
|
|
final SyncService _syncService;
|
2023-05-25 03:52:43 +00:00
|
|
|
final Logger _log = Logger("UserService");
|
2022-04-24 02:08:45 +00:00
|
|
|
|
2023-05-25 03:52:43 +00:00
|
|
|
UserService(
|
2024-09-24 12:50:21 +00:00
|
|
|
this._partnerApiRepository,
|
|
|
|
this._userApiRepository,
|
|
|
|
this._userRepository,
|
2023-05-25 03:52:43 +00:00
|
|
|
this._syncService,
|
|
|
|
);
|
2022-04-24 02:08:45 +00:00
|
|
|
|
2024-11-26 18:43:44 +00:00
|
|
|
Future<List<User>> getUsers({bool self = false}) {
|
|
|
|
return _userRepository.getAll(self: self);
|
|
|
|
}
|
2022-05-29 03:35:45 +00:00
|
|
|
|
2024-09-24 12:50:21 +00:00
|
|
|
Future<({String profileImagePath})?> uploadProfileImage(XFile image) async {
|
2022-05-29 03:35:45 +00:00
|
|
|
try {
|
2024-09-24 12:50:21 +00:00
|
|
|
return await _userApiRepository.createProfileImage(
|
|
|
|
name: image.name,
|
|
|
|
data: await image.readAsBytes(),
|
2022-05-29 03:35:45 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2024-02-24 03:38:57 +00:00
|
|
|
_log.warning("Failed to upload profile image", e);
|
2022-05-29 03:35:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-03-03 22:38:30 +00:00
|
|
|
|
2024-05-14 15:35:37 +00:00
|
|
|
Future<List<User>?> getUsersFromServer() async {
|
2024-09-24 12:50:21 +00:00
|
|
|
List<User>? users;
|
|
|
|
try {
|
|
|
|
users = await _userApiRepository.getAll();
|
|
|
|
} catch (e) {
|
|
|
|
_log.warning("Failed to fetch users", e);
|
|
|
|
users = null;
|
|
|
|
}
|
|
|
|
final List<User> sharedBy =
|
|
|
|
await _partnerApiRepository.getAll(Direction.sharedByMe);
|
|
|
|
final List<User> sharedWith =
|
|
|
|
await _partnerApiRepository.getAll(Direction.sharedWithMe);
|
2023-05-25 03:52:43 +00:00
|
|
|
|
2024-09-24 12:50:21 +00:00
|
|
|
if (users == null) {
|
2023-05-25 03:52:43 +00:00
|
|
|
_log.warning("Failed to refresh users");
|
2024-05-14 15:35:37 +00:00
|
|
|
return null;
|
2023-03-03 22:38:30 +00:00
|
|
|
}
|
2023-05-25 03:52:43 +00:00
|
|
|
|
|
|
|
users.sortBy((u) => u.id);
|
|
|
|
sharedBy.sortBy((u) => u.id);
|
|
|
|
sharedWith.sortBy((u) => u.id);
|
|
|
|
|
|
|
|
diffSortedListsSync(
|
|
|
|
users,
|
|
|
|
sharedBy,
|
|
|
|
compare: (User a, User b) => a.id.compareTo(b.id),
|
|
|
|
both: (User a, User b) => a.isPartnerSharedBy = true,
|
|
|
|
onlyFirst: (_) {},
|
|
|
|
onlySecond: (_) {},
|
|
|
|
);
|
|
|
|
|
|
|
|
diffSortedListsSync(
|
|
|
|
users,
|
|
|
|
sharedWith,
|
|
|
|
compare: (User a, User b) => a.id.compareTo(b.id),
|
2023-11-13 15:54:41 +00:00
|
|
|
both: (User a, User b) {
|
|
|
|
a.isPartnerSharedWith = true;
|
|
|
|
a.inTimeline = b.inTimeline;
|
|
|
|
return true;
|
|
|
|
},
|
2023-05-25 03:52:43 +00:00
|
|
|
onlyFirst: (_) {},
|
|
|
|
onlySecond: (_) {},
|
|
|
|
);
|
|
|
|
|
2024-05-14 15:35:37 +00:00
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> refreshUsers() async {
|
|
|
|
final users = await getUsersFromServer();
|
|
|
|
if (users == null) return false;
|
2023-03-03 22:38:30 +00:00
|
|
|
return _syncService.syncUsersFromServer(users);
|
|
|
|
}
|
2022-04-24 02:08:45 +00:00
|
|
|
}
|