2024-01-04 22:20:55 -07:00
|
|
|
import 'package:immich_mobile/constants/errors.dart';
|
|
|
|
import 'package:immich_mobile/mixins/error_logger.mixin.dart';
|
2024-04-30 19:36:40 -07:00
|
|
|
import 'package:immich_mobile/models/activities/activity.model.dart';
|
2024-05-02 13:59:14 -07:00
|
|
|
import 'package:immich_mobile/services/api.service.dart';
|
2023-11-06 08:46:26 -07:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
|
2024-01-04 22:20:55 -07:00
|
|
|
class ActivityService with ErrorLoggerMixin {
|
2023-11-06 08:46:26 -07:00
|
|
|
final ApiService _apiService;
|
2024-01-04 22:20:55 -07:00
|
|
|
|
|
|
|
@override
|
|
|
|
final Logger logger = Logger("ActivityService");
|
2023-11-06 08:46:26 -07:00
|
|
|
|
|
|
|
ActivityService(this._apiService);
|
|
|
|
|
|
|
|
Future<List<Activity>> getAllActivities(
|
2024-01-04 22:20:55 -07:00
|
|
|
String albumId, {
|
2023-11-06 08:46:26 -07:00
|
|
|
String? assetId,
|
2024-01-04 22:20:55 -07:00
|
|
|
}) async {
|
|
|
|
return logError(
|
|
|
|
() async {
|
2024-05-29 15:26:57 -07:00
|
|
|
final list = await _apiService.activitiesApi
|
2024-01-04 22:20:55 -07:00
|
|
|
.getActivities(albumId, assetId: assetId);
|
|
|
|
return list != null ? list.map(Activity.fromDto).toList() : [];
|
|
|
|
},
|
|
|
|
defaultValue: [],
|
2024-02-23 20:38:57 -07:00
|
|
|
errorMessage: "Failed to get all activities for album $albumId",
|
2024-01-04 22:20:55 -07:00
|
|
|
);
|
2023-11-06 08:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> getStatistics(String albumId, {String? assetId}) async {
|
2024-01-04 22:20:55 -07:00
|
|
|
return logError(
|
|
|
|
() async {
|
2024-05-29 15:26:57 -07:00
|
|
|
final dto = await _apiService.activitiesApi
|
2024-01-04 22:20:55 -07:00
|
|
|
.getActivityStatistics(albumId, assetId: assetId);
|
|
|
|
return dto?.comments ?? 0;
|
|
|
|
},
|
|
|
|
defaultValue: 0,
|
2024-02-23 20:38:57 -07:00
|
|
|
errorMessage: "Failed to statistics for album $albumId",
|
2024-01-04 22:20:55 -07:00
|
|
|
);
|
2023-11-06 08:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> removeActivity(String id) async {
|
2024-01-04 22:20:55 -07:00
|
|
|
return logError(
|
|
|
|
() async {
|
2024-05-29 15:26:57 -07:00
|
|
|
await _apiService.activitiesApi.deleteActivity(id);
|
2024-01-04 22:20:55 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
defaultValue: false,
|
2024-02-23 20:38:57 -07:00
|
|
|
errorMessage: "Failed to delete activity",
|
2024-01-04 22:20:55 -07:00
|
|
|
);
|
2023-11-06 08:46:26 -07:00
|
|
|
}
|
|
|
|
|
2024-01-04 22:20:55 -07:00
|
|
|
AsyncFuture<Activity> addActivity(
|
2023-11-06 08:46:26 -07:00
|
|
|
String albumId,
|
|
|
|
ActivityType type, {
|
|
|
|
String? assetId,
|
|
|
|
String? comment,
|
|
|
|
}) async {
|
2024-02-23 20:38:57 -07:00
|
|
|
return guardError(
|
|
|
|
() async {
|
2024-05-29 15:26:57 -07:00
|
|
|
final dto = await _apiService.activitiesApi.createActivity(
|
2024-02-23 20:38:57 -07:00
|
|
|
ActivityCreateDto(
|
|
|
|
albumId: albumId,
|
|
|
|
type: type == ActivityType.comment
|
|
|
|
? ReactionType.comment
|
|
|
|
: ReactionType.like,
|
|
|
|
assetId: assetId,
|
|
|
|
comment: comment,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (dto != null) {
|
|
|
|
return Activity.fromDto(dto);
|
|
|
|
}
|
|
|
|
throw NoResponseDtoError();
|
|
|
|
},
|
|
|
|
errorMessage: "Failed to create $type for album $albumId",
|
|
|
|
);
|
2023-11-06 08:46:26 -07:00
|
|
|
}
|
|
|
|
}
|