2023-05-25 03:52:43 +00:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/entities/user.entity.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/repositories/partner_api.repository.dart';
|
|
|
|
import 'package:immich_mobile/repositories/user.repository.dart';
|
2023-05-25 03:52:43 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
final partnerServiceProvider = Provider(
|
|
|
|
(ref) => PartnerService(
|
2024-09-24 12:50:21 +00:00
|
|
|
ref.watch(partnerApiRepositoryProvider),
|
|
|
|
ref.watch(userRepositoryProvider),
|
2023-05-25 03:52:43 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
class PartnerService {
|
2024-09-24 12:50:21 +00:00
|
|
|
final IPartnerApiRepository _partnerApiRepository;
|
|
|
|
final IUserRepository _userRepository;
|
2023-05-25 03:52:43 +00:00
|
|
|
final Logger _log = Logger("PartnerService");
|
|
|
|
|
2024-09-24 12:50:21 +00:00
|
|
|
PartnerService(
|
|
|
|
this._partnerApiRepository,
|
|
|
|
this._userRepository,
|
|
|
|
);
|
2023-05-25 03:52:43 +00:00
|
|
|
|
|
|
|
Future<bool> removePartner(User partner) async {
|
|
|
|
try {
|
2024-09-24 12:50:21 +00:00
|
|
|
await _partnerApiRepository.delete(partner.id);
|
2023-05-25 03:52:43 +00:00
|
|
|
partner.isPartnerSharedBy = false;
|
2024-09-24 12:50:21 +00:00
|
|
|
await _userRepository.update(partner);
|
2023-05-25 03:52:43 +00:00
|
|
|
} catch (e) {
|
2024-02-24 03:38:57 +00:00
|
|
|
_log.warning("Failed to remove partner ${partner.id}", e);
|
2023-05-25 03:52:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> addPartner(User partner) async {
|
|
|
|
try {
|
2024-09-24 12:50:21 +00:00
|
|
|
await _partnerApiRepository.create(partner.id);
|
|
|
|
partner.isPartnerSharedBy = true;
|
|
|
|
await _userRepository.update(partner);
|
|
|
|
return true;
|
2023-05-25 03:52:43 +00:00
|
|
|
} catch (e) {
|
2024-02-24 03:38:57 +00:00
|
|
|
_log.warning("Failed to add partner ${partner.id}", e);
|
2023-05-25 03:52:43 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-13 15:54:41 +00:00
|
|
|
|
|
|
|
Future<bool> updatePartner(User partner, {required bool inTimeline}) async {
|
|
|
|
try {
|
2024-09-24 12:50:21 +00:00
|
|
|
final dto = await _partnerApiRepository.update(
|
|
|
|
partner.id,
|
|
|
|
inTimeline: inTimeline,
|
|
|
|
);
|
|
|
|
partner.inTimeline = dto.inTimeline;
|
|
|
|
await _userRepository.update(partner);
|
|
|
|
return true;
|
2023-11-13 15:54:41 +00:00
|
|
|
} catch (e) {
|
2024-02-24 03:38:57 +00:00
|
|
|
_log.warning("Failed to update partner ${partner.id}", e);
|
2023-11-13 15:54:41 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-25 03:52:43 +00:00
|
|
|
}
|