2023-05-25 05:52:43 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/user.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2023-11-13 17:54:41 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2023-05-25 05:52:43 +02:00
|
|
|
|
|
|
|
final partnerServiceProvider = Provider(
|
|
|
|
(ref) => PartnerService(
|
|
|
|
ref.watch(apiServiceProvider),
|
|
|
|
ref.watch(dbProvider),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
enum PartnerDirection {
|
|
|
|
sharedWith("shared-with"),
|
|
|
|
sharedBy("shared-by");
|
|
|
|
|
|
|
|
const PartnerDirection(
|
|
|
|
this._value,
|
|
|
|
);
|
|
|
|
|
|
|
|
final String _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PartnerService {
|
|
|
|
final ApiService _apiService;
|
|
|
|
final Isar _db;
|
|
|
|
final Logger _log = Logger("PartnerService");
|
|
|
|
|
|
|
|
PartnerService(this._apiService, this._db);
|
|
|
|
|
|
|
|
Future<List<User>?> getPartners(PartnerDirection direction) async {
|
|
|
|
try {
|
|
|
|
final userDtos =
|
|
|
|
await _apiService.partnerApi.getPartners(direction._value);
|
|
|
|
if (userDtos != null) {
|
2023-11-11 23:06:19 +02:00
|
|
|
return userDtos.map((u) => User.fromPartnerDto(u)).toList();
|
2023-05-25 05:52:43 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.warning("Failed to get partners for direction $direction", e);
|
2023-05-25 05:52:43 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> removePartner(User partner) async {
|
|
|
|
try {
|
|
|
|
await _apiService.partnerApi.removePartner(partner.id);
|
|
|
|
partner.isPartnerSharedBy = false;
|
|
|
|
await _db.writeTxn(() => _db.users.put(partner));
|
|
|
|
} catch (e) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.warning("Failed to remove partner ${partner.id}", e);
|
2023-05-25 05:52:43 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> addPartner(User partner) async {
|
|
|
|
try {
|
|
|
|
final dto = await _apiService.partnerApi.createPartner(partner.id);
|
|
|
|
if (dto != null) {
|
|
|
|
partner.isPartnerSharedBy = true;
|
|
|
|
await _db.writeTxn(() => _db.users.put(partner));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.warning("Failed to add partner ${partner.id}", e);
|
2023-05-25 05:52:43 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-13 17:54:41 +02:00
|
|
|
|
|
|
|
Future<bool> updatePartner(User partner, {required bool inTimeline}) async {
|
|
|
|
try {
|
|
|
|
final dto = await _apiService.partnerApi
|
|
|
|
.updatePartner(partner.id, UpdatePartnerDto(inTimeline: inTimeline));
|
|
|
|
if (dto != null) {
|
|
|
|
partner.inTimeline = dto.inTimeline ?? partner.inTimeline;
|
|
|
|
await _db.writeTxn(() => _db.users.put(partner));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.warning("Failed to update partner ${partner.id}", e);
|
2023-11-13 17:54:41 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-25 05:52:43 +02:00
|
|
|
}
|