2023-11-01 04:13:34 +01:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-03-20 21:20:38 +01:00
|
|
|
import { AccessCore, Permission } from 'src/cores/access.core';
|
2023-11-01 04:13:34 +01:00
|
|
|
import {
|
|
|
|
ActivityCreateDto,
|
|
|
|
ActivityDto,
|
|
|
|
ActivityResponseDto,
|
|
|
|
ActivitySearchDto,
|
|
|
|
ActivityStatisticsResponseDto,
|
|
|
|
MaybeDuplicate,
|
2023-11-10 03:32:31 +01:00
|
|
|
ReactionLevel,
|
2023-11-01 04:13:34 +01:00
|
|
|
ReactionType,
|
|
|
|
mapActivity,
|
2024-03-20 23:53:07 +01:00
|
|
|
} from 'src/dtos/activity.dto';
|
|
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
2024-03-20 22:02:51 +01:00
|
|
|
import { ActivityEntity } from 'src/entities/activity.entity';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAccessRepository } from 'src/interfaces/access.interface';
|
|
|
|
import { IActivityRepository } from 'src/interfaces/activity.interface';
|
2023-11-01 04:13:34 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ActivityService {
|
|
|
|
private access: AccessCore;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(IAccessRepository) accessRepository: IAccessRepository,
|
|
|
|
@Inject(IActivityRepository) private repository: IActivityRepository,
|
|
|
|
) {
|
|
|
|
this.access = AccessCore.create(accessRepository);
|
|
|
|
}
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
async getAll(auth: AuthDto, dto: ActivitySearchDto): Promise<ActivityResponseDto[]> {
|
|
|
|
await this.access.requirePermission(auth, Permission.ALBUM_READ, dto.albumId);
|
2023-11-01 04:13:34 +01:00
|
|
|
const activities = await this.repository.search({
|
2023-11-01 16:49:12 +01:00
|
|
|
userId: dto.userId,
|
2023-11-01 04:13:34 +01:00
|
|
|
albumId: dto.albumId,
|
2023-11-10 03:32:31 +01:00
|
|
|
assetId: dto.level === ReactionLevel.ALBUM ? null : dto.assetId,
|
2023-11-01 04:13:34 +01:00
|
|
|
isLiked: dto.type && dto.type === ReactionType.LIKE,
|
|
|
|
});
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
return activities.map((activity) => mapActivity(activity));
|
2023-11-01 04:13:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
async getStatistics(auth: AuthDto, dto: ActivityDto): Promise<ActivityStatisticsResponseDto> {
|
|
|
|
await this.access.requirePermission(auth, Permission.ALBUM_READ, dto.albumId);
|
2023-11-01 04:13:34 +01:00
|
|
|
return { comments: await this.repository.getStatistics(dto.assetId, dto.albumId) };
|
|
|
|
}
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
async create(auth: AuthDto, dto: ActivityCreateDto): Promise<MaybeDuplicate<ActivityResponseDto>> {
|
|
|
|
await this.access.requirePermission(auth, Permission.ACTIVITY_CREATE, dto.albumId);
|
2023-11-01 04:13:34 +01:00
|
|
|
|
|
|
|
const common = {
|
2023-12-10 05:34:12 +01:00
|
|
|
userId: auth.user.id,
|
2023-11-01 04:13:34 +01:00
|
|
|
assetId: dto.assetId,
|
|
|
|
albumId: dto.albumId,
|
|
|
|
};
|
|
|
|
|
|
|
|
let activity: ActivityEntity | null = null;
|
|
|
|
let duplicate = false;
|
|
|
|
|
2023-11-10 03:32:31 +01:00
|
|
|
if (dto.type === ReactionType.LIKE) {
|
2023-11-01 04:13:34 +01:00
|
|
|
delete dto.comment;
|
|
|
|
[activity] = await this.repository.search({
|
|
|
|
...common,
|
2023-11-10 03:32:31 +01:00
|
|
|
// `null` will search for an album like
|
|
|
|
assetId: dto.assetId ?? null,
|
2023-11-01 04:13:34 +01:00
|
|
|
isLiked: true,
|
|
|
|
});
|
|
|
|
duplicate = !!activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!activity) {
|
|
|
|
activity = await this.repository.create({
|
|
|
|
...common,
|
|
|
|
isLiked: dto.type === ReactionType.LIKE,
|
|
|
|
comment: dto.comment,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return { duplicate, value: mapActivity(activity) };
|
|
|
|
}
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
async delete(auth: AuthDto, id: string): Promise<void> {
|
|
|
|
await this.access.requirePermission(auth, Permission.ACTIVITY_DELETE, id);
|
2023-11-01 04:13:34 +01:00
|
|
|
await this.repository.delete(id);
|
|
|
|
}
|
|
|
|
}
|