2023-05-25 05:52:43 +02:00
|
|
|
import 'package:collection/collection.dart';
|
2022-06-26 03:46:51 +09:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-05-28 22:35:45 -05:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
2025-03-12 19:26:56 +05:30
|
|
|
import 'package:immich_mobile/domain/interfaces/user.interface.dart';
|
|
|
|
|
import 'package:immich_mobile/domain/models/user.model.dart';
|
2024-09-24 14:50:21 +02:00
|
|
|
import 'package:immich_mobile/interfaces/partner_api.interface.dart';
|
|
|
|
|
import 'package:immich_mobile/interfaces/user_api.interface.dart';
|
2025-03-12 19:26:56 +05:30
|
|
|
import 'package:immich_mobile/providers/infrastructure/user.provider.dart';
|
2024-09-24 14:50:21 +02:00
|
|
|
import 'package:immich_mobile/repositories/partner_api.repository.dart';
|
|
|
|
|
import 'package:immich_mobile/repositories/user_api.repository.dart';
|
2023-05-25 05:52:43 +02:00
|
|
|
import 'package:immich_mobile/utils/diff.dart';
|
|
|
|
|
import 'package:logging/logging.dart';
|
2022-04-23 21:08:45 -05:00
|
|
|
|
2022-07-13 07:23:48 -05:00
|
|
|
final userServiceProvider = Provider(
|
|
|
|
|
(ref) => UserService(
|
2024-09-24 14:50:21 +02:00
|
|
|
ref.watch(partnerApiRepositoryProvider),
|
|
|
|
|
ref.watch(userApiRepositoryProvider),
|
|
|
|
|
ref.watch(userRepositoryProvider),
|
2022-07-13 07:23:48 -05:00
|
|
|
),
|
|
|
|
|
);
|
2022-06-26 03:46:51 +09:00
|
|
|
|
2022-04-23 21:08:45 -05:00
|
|
|
class UserService {
|
2024-09-24 14:50:21 +02:00
|
|
|
final IPartnerApiRepository _partnerApiRepository;
|
|
|
|
|
final IUserApiRepository _userApiRepository;
|
|
|
|
|
final IUserRepository _userRepository;
|
2023-05-25 05:52:43 +02:00
|
|
|
final Logger _log = Logger("UserService");
|
2022-04-23 21:08:45 -05:00
|
|
|
|
2023-05-25 05:52:43 +02:00
|
|
|
UserService(
|
2024-09-24 14:50:21 +02:00
|
|
|
this._partnerApiRepository,
|
|
|
|
|
this._userApiRepository,
|
|
|
|
|
this._userRepository,
|
2023-05-25 05:52:43 +02:00
|
|
|
);
|
2022-04-23 21:08:45 -05:00
|
|
|
|
2024-09-24 14:50:21 +02:00
|
|
|
Future<({String profileImagePath})?> uploadProfileImage(XFile image) async {
|
2022-05-28 22:35:45 -05:00
|
|
|
try {
|
2024-09-24 14:50:21 +02:00
|
|
|
return await _userApiRepository.createProfileImage(
|
|
|
|
|
name: image.name,
|
|
|
|
|
data: await image.readAsBytes(),
|
2022-05-28 22:35:45 -05:00
|
|
|
);
|
|
|
|
|
} catch (e) {
|
2024-02-24 04:38:57 +01:00
|
|
|
_log.warning("Failed to upload profile image", e);
|
2022-05-28 22:35:45 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-03 23:38:30 +01:00
|
|
|
|
2025-03-12 19:26:56 +05:30
|
|
|
Future<List<UserDto>> getAll() async {
|
|
|
|
|
return await _userRepository.getAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<List<UserDto>?> getUsersFromServer() async {
|
|
|
|
|
List<UserDto>? users;
|
2024-09-24 14:50:21 +02:00
|
|
|
try {
|
|
|
|
|
users = await _userApiRepository.getAll();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
_log.warning("Failed to fetch users", e);
|
|
|
|
|
users = null;
|
|
|
|
|
}
|
2025-03-12 19:26:56 +05:30
|
|
|
final List<UserDto> sharedBy =
|
2024-09-24 14:50:21 +02:00
|
|
|
await _partnerApiRepository.getAll(Direction.sharedByMe);
|
2025-03-12 19:26:56 +05:30
|
|
|
final List<UserDto> sharedWith =
|
2024-09-24 14:50:21 +02:00
|
|
|
await _partnerApiRepository.getAll(Direction.sharedWithMe);
|
2023-05-25 05:52:43 +02:00
|
|
|
|
2024-09-24 14:50:21 +02:00
|
|
|
if (users == null) {
|
2023-05-25 05:52:43 +02:00
|
|
|
_log.warning("Failed to refresh users");
|
2024-05-14 17:35:37 +02:00
|
|
|
return null;
|
2023-03-03 23:38:30 +01:00
|
|
|
}
|
2023-05-25 05:52:43 +02:00
|
|
|
|
2025-03-12 19:26:56 +05:30
|
|
|
users.sortBy((u) => u.uid);
|
|
|
|
|
sharedBy.sortBy((u) => u.uid);
|
|
|
|
|
sharedWith.sortBy((u) => u.uid);
|
|
|
|
|
|
|
|
|
|
final updatedSharedBy = <UserDto>[];
|
2023-05-25 05:52:43 +02:00
|
|
|
|
|
|
|
|
diffSortedListsSync(
|
|
|
|
|
users,
|
|
|
|
|
sharedBy,
|
2025-03-12 19:26:56 +05:30
|
|
|
compare: (UserDto a, UserDto b) => a.uid.compareTo(b.uid),
|
|
|
|
|
both: (UserDto a, UserDto b) {
|
|
|
|
|
updatedSharedBy.add(a.copyWith(isPartnerSharedBy: true));
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
onlyFirst: (UserDto a) => updatedSharedBy.add(a),
|
|
|
|
|
onlySecond: (UserDto b) => updatedSharedBy.add(b),
|
2023-05-25 05:52:43 +02:00
|
|
|
);
|
|
|
|
|
|
2025-03-12 19:26:56 +05:30
|
|
|
final updatedSharedWith = <UserDto>[];
|
|
|
|
|
|
2023-05-25 05:52:43 +02:00
|
|
|
diffSortedListsSync(
|
2025-03-12 19:26:56 +05:30
|
|
|
updatedSharedBy,
|
2023-05-25 05:52:43 +02:00
|
|
|
sharedWith,
|
2025-03-12 19:26:56 +05:30
|
|
|
compare: (UserDto a, UserDto b) => a.uid.compareTo(b.uid),
|
|
|
|
|
both: (UserDto a, UserDto b) {
|
|
|
|
|
updatedSharedWith.add(
|
|
|
|
|
a.copyWith(inTimeline: b.inTimeline, isPartnerSharedWith: true),
|
|
|
|
|
);
|
2023-11-13 16:54:41 +01:00
|
|
|
return true;
|
|
|
|
|
},
|
2025-03-12 19:26:56 +05:30
|
|
|
onlyFirst: (UserDto a) => updatedSharedWith.add(a),
|
|
|
|
|
onlySecond: (UserDto b) => updatedSharedWith.add(b),
|
2023-05-25 05:52:43 +02:00
|
|
|
);
|
|
|
|
|
|
2025-03-12 19:26:56 +05:30
|
|
|
return updatedSharedWith;
|
2024-05-14 17:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 13:10:55 -06:00
|
|
|
Future<void> clearTable() {
|
2025-03-12 19:26:56 +05:30
|
|
|
return _userRepository.deleteAll();
|
2025-02-18 13:10:55 -06:00
|
|
|
}
|
2022-04-23 21:08:45 -05:00
|
|
|
}
|