2023-05-25 04:10:45 +02:00
|
|
|
import { AlbumEntity, AssetEntity, UserEntity } from '@app/infra/entities';
|
2023-06-28 15:56:24 +02:00
|
|
|
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
|
2023-08-02 03:29:14 +02:00
|
|
|
import { AccessCore, IAccessRepository, Permission } from '../access';
|
2023-08-11 18:00:51 +02:00
|
|
|
import { BulkIdErrorReason, BulkIdResponseDto, BulkIdsDto, IAssetRepository } from '../asset';
|
2023-03-26 04:46:48 +02:00
|
|
|
import { AuthUserDto } from '../auth';
|
2023-05-25 04:10:45 +02:00
|
|
|
import { IJobRepository, JobName } from '../job';
|
2023-06-07 16:37:25 +02:00
|
|
|
import { IUserRepository } from '../user';
|
2023-08-11 18:00:51 +02:00
|
|
|
import {
|
|
|
|
AlbumCountResponseDto,
|
|
|
|
AlbumResponseDto,
|
|
|
|
mapAlbum,
|
|
|
|
mapAlbumWithAssets,
|
|
|
|
mapAlbumWithoutAssets,
|
|
|
|
} from './album-response.dto';
|
2023-08-15 20:34:02 +02:00
|
|
|
import { AlbumInfoOptions, IAlbumRepository } from './album.repository';
|
2023-08-11 18:00:51 +02:00
|
|
|
import { AddUsersDto, AlbumInfoDto, CreateAlbumDto, GetAlbumsDto, UpdateAlbumDto } from './dto';
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AlbumService {
|
2023-06-28 15:56:24 +02:00
|
|
|
private access: AccessCore;
|
2023-03-26 04:46:48 +02:00
|
|
|
constructor(
|
2023-06-28 15:56:24 +02:00
|
|
|
@Inject(IAccessRepository) accessRepository: IAccessRepository,
|
2023-03-26 04:46:48 +02:00
|
|
|
@Inject(IAlbumRepository) private albumRepository: IAlbumRepository,
|
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
2023-05-25 04:10:45 +02:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-06-07 16:37:25 +02:00
|
|
|
@Inject(IUserRepository) private userRepository: IUserRepository,
|
2023-06-28 15:56:24 +02:00
|
|
|
) {
|
|
|
|
this.access = new AccessCore(accessRepository);
|
|
|
|
}
|
2023-03-26 04:46:48 +02:00
|
|
|
|
2023-06-16 17:48:48 +02:00
|
|
|
async getCount(authUser: AuthUserDto): Promise<AlbumCountResponseDto> {
|
|
|
|
const [owned, shared, notShared] = await Promise.all([
|
|
|
|
this.albumRepository.getOwned(authUser.id),
|
|
|
|
this.albumRepository.getShared(authUser.id),
|
|
|
|
this.albumRepository.getNotShared(authUser.id),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
owned: owned.length,
|
|
|
|
shared: shared.length,
|
|
|
|
notShared: notShared.length,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
async getAll({ id: ownerId }: AuthUserDto, { assetId, shared }: GetAlbumsDto): Promise<AlbumResponseDto[]> {
|
2023-08-02 03:29:14 +02:00
|
|
|
const invalidAlbumIds = await this.albumRepository.getInvalidThumbnail();
|
|
|
|
for (const albumId of invalidAlbumIds) {
|
|
|
|
const newThumbnail = await this.assetRepository.getFirstAssetForAlbumId(albumId);
|
|
|
|
await this.albumRepository.update({ id: albumId, albumThumbnailAsset: newThumbnail });
|
|
|
|
}
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
let albums: AlbumEntity[];
|
|
|
|
if (assetId) {
|
|
|
|
albums = await this.albumRepository.getByAssetId(ownerId, assetId);
|
|
|
|
} else if (shared === true) {
|
|
|
|
albums = await this.albumRepository.getShared(ownerId);
|
|
|
|
} else if (shared === false) {
|
|
|
|
albums = await this.albumRepository.getNotShared(ownerId);
|
|
|
|
} else {
|
|
|
|
albums = await this.albumRepository.getOwned(ownerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get asset count for each album. Then map the result to an object:
|
|
|
|
// { [albumId]: assetCount }
|
|
|
|
const albumsAssetCount = await this.albumRepository.getAssetCountForIds(albums.map((album) => album.id));
|
|
|
|
const albumsAssetCountObj = albumsAssetCount.reduce((obj: Record<string, number>, { albumId, assetCount }) => {
|
|
|
|
obj[albumId] = assetCount;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
2023-06-21 03:00:59 +02:00
|
|
|
return Promise.all(
|
|
|
|
albums.map(async (album) => {
|
|
|
|
const lastModifiedAsset = await this.assetRepository.getLastUpdatedAssetForAlbumId(album.id);
|
|
|
|
return {
|
2023-08-11 18:00:51 +02:00
|
|
|
...mapAlbumWithoutAssets(album),
|
|
|
|
sharedLinks: undefined,
|
2023-06-21 03:00:59 +02:00
|
|
|
assetCount: albumsAssetCountObj[album.id],
|
|
|
|
lastModifiedAssetTimestamp: lastModifiedAsset?.fileModifiedAt,
|
2023-08-11 18:00:51 +02:00
|
|
|
};
|
2023-06-21 03:00:59 +02:00
|
|
|
}),
|
|
|
|
);
|
2023-03-26 04:46:48 +02:00
|
|
|
}
|
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
async get(authUser: AuthUserDto, id: string, dto: AlbumInfoDto) {
|
2023-08-02 03:29:14 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_READ, id);
|
|
|
|
await this.albumRepository.updateThumbnails();
|
2023-08-15 20:34:02 +02:00
|
|
|
return mapAlbum(await this.findOrFail(id, { withAssets: true }), !dto.withoutAssets);
|
2023-03-26 04:46:48 +02:00
|
|
|
}
|
2023-05-25 04:10:45 +02:00
|
|
|
|
|
|
|
async create(authUser: AuthUserDto, dto: CreateAlbumDto): Promise<AlbumResponseDto> {
|
2023-06-07 16:37:25 +02:00
|
|
|
for (const userId of dto.sharedWithUserIds || []) {
|
|
|
|
const exists = await this.userRepository.get(userId);
|
|
|
|
if (!exists) {
|
|
|
|
throw new BadRequestException('User not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const album = await this.albumRepository.create({
|
|
|
|
ownerId: authUser.id,
|
|
|
|
albumName: dto.albumName,
|
2023-08-06 04:43:26 +02:00
|
|
|
description: dto.description,
|
2023-08-28 21:41:57 +02:00
|
|
|
sharedUsers: dto.sharedWithUserIds?.map((value) => ({ id: value }) as UserEntity) ?? [],
|
|
|
|
assets: (dto.assetIds || []).map((id) => ({ id }) as AssetEntity),
|
2023-05-25 04:10:45 +02:00
|
|
|
albumThumbnailAssetId: dto.assetIds?.[0] || null,
|
|
|
|
});
|
2023-06-07 16:37:25 +02:00
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ALBUM, data: { ids: [album.id] } });
|
2023-08-11 18:00:51 +02:00
|
|
|
return mapAlbumWithAssets(album);
|
2023-05-25 04:10:45 +02:00
|
|
|
}
|
2023-05-25 21:37:19 +02:00
|
|
|
|
|
|
|
async update(authUser: AuthUserDto, id: string, dto: UpdateAlbumDto): Promise<AlbumResponseDto> {
|
2023-06-28 15:56:24 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_UPDATE, id);
|
|
|
|
|
2023-08-15 20:34:02 +02:00
|
|
|
const album = await this.findOrFail(id, { withAssets: true });
|
2023-05-25 21:37:19 +02:00
|
|
|
|
|
|
|
if (dto.albumThumbnailAssetId) {
|
|
|
|
const valid = await this.albumRepository.hasAsset(id, dto.albumThumbnailAssetId);
|
|
|
|
if (!valid) {
|
|
|
|
throw new BadRequestException('Invalid album thumbnail');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedAlbum = await this.albumRepository.update({
|
|
|
|
id: album.id,
|
|
|
|
albumName: dto.albumName,
|
2023-08-06 04:43:26 +02:00
|
|
|
description: dto.description,
|
2023-05-25 21:37:19 +02:00
|
|
|
albumThumbnailAssetId: dto.albumThumbnailAssetId,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ALBUM, data: { ids: [updatedAlbum.id] } });
|
|
|
|
|
2023-08-15 20:34:02 +02:00
|
|
|
return mapAlbumWithoutAssets(updatedAlbum);
|
2023-05-25 21:37:19 +02:00
|
|
|
}
|
2023-05-26 15:04:09 +02:00
|
|
|
|
|
|
|
async delete(authUser: AuthUserDto, id: string): Promise<void> {
|
2023-06-28 15:56:24 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_DELETE, id);
|
|
|
|
|
2023-08-15 20:34:02 +02:00
|
|
|
const album = await this.findOrFail(id, { withAssets: false });
|
2023-05-26 15:04:09 +02:00
|
|
|
|
|
|
|
await this.albumRepository.delete(album);
|
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_REMOVE_ALBUM, data: { ids: [id] } });
|
|
|
|
}
|
2023-06-07 16:37:25 +02:00
|
|
|
|
2023-08-02 03:29:14 +02:00
|
|
|
async addAssets(authUser: AuthUserDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
|
2023-08-15 20:34:02 +02:00
|
|
|
const album = await this.findOrFail(id, { withAssets: true });
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_READ, id);
|
|
|
|
|
|
|
|
const results: BulkIdResponseDto[] = [];
|
|
|
|
for (const id of dto.ids) {
|
|
|
|
const hasAsset = album.assets.find((asset) => asset.id === id);
|
|
|
|
if (hasAsset) {
|
|
|
|
results.push({ id, success: false, error: BulkIdErrorReason.DUPLICATE });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasAccess = await this.access.hasPermission(authUser, Permission.ASSET_SHARE, id);
|
|
|
|
if (!hasAccess) {
|
|
|
|
results.push({ id, success: false, error: BulkIdErrorReason.NO_PERMISSION });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push({ id, success: true });
|
|
|
|
album.assets.push({ id } as AssetEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newAsset = results.find(({ success }) => success);
|
|
|
|
if (newAsset) {
|
|
|
|
await this.albumRepository.update({
|
|
|
|
id,
|
|
|
|
assets: album.assets,
|
|
|
|
updatedAt: new Date(),
|
|
|
|
albumThumbnailAssetId: album.albumThumbnailAssetId ?? newAsset.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeAssets(authUser: AuthUserDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
|
2023-08-15 20:34:02 +02:00
|
|
|
const album = await this.findOrFail(id, { withAssets: true });
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_READ, id);
|
|
|
|
|
|
|
|
const results: BulkIdResponseDto[] = [];
|
|
|
|
for (const id of dto.ids) {
|
|
|
|
const hasAsset = album.assets.find((asset) => asset.id === id);
|
|
|
|
if (!hasAsset) {
|
|
|
|
results.push({ id, success: false, error: BulkIdErrorReason.NOT_FOUND });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasAccess = await this.access.hasAny(authUser, [
|
|
|
|
{ permission: Permission.ALBUM_REMOVE_ASSET, id },
|
|
|
|
{ permission: Permission.ASSET_SHARE, id },
|
|
|
|
]);
|
|
|
|
if (!hasAccess) {
|
|
|
|
results.push({ id, success: false, error: BulkIdErrorReason.NO_PERMISSION });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push({ id, success: true });
|
|
|
|
album.assets = album.assets.filter((asset) => asset.id !== id);
|
|
|
|
if (album.albumThumbnailAssetId === id) {
|
|
|
|
album.albumThumbnailAssetId = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasSuccess = results.find(({ success }) => success);
|
|
|
|
if (hasSuccess) {
|
|
|
|
await this.albumRepository.update({
|
|
|
|
id,
|
|
|
|
assets: album.assets,
|
|
|
|
updatedAt: new Date(),
|
|
|
|
albumThumbnailAssetId: album.albumThumbnailAssetId || album.assets[0]?.id || null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
async addUsers(authUser: AuthUserDto, id: string, dto: AddUsersDto): Promise<AlbumResponseDto> {
|
2023-06-28 15:56:24 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_SHARE, id);
|
|
|
|
|
2023-08-15 20:34:02 +02:00
|
|
|
const album = await this.findOrFail(id, { withAssets: false });
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
for (const userId of dto.sharedUserIds) {
|
2023-09-11 17:56:38 +02:00
|
|
|
if (album.ownerId === userId) {
|
|
|
|
throw new BadRequestException('Cannot be shared with owner');
|
|
|
|
}
|
|
|
|
|
2023-06-07 16:37:25 +02:00
|
|
|
const exists = album.sharedUsers.find((user) => user.id === userId);
|
|
|
|
if (exists) {
|
|
|
|
throw new BadRequestException('User already added');
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await this.userRepository.get(userId);
|
|
|
|
if (!user) {
|
|
|
|
throw new BadRequestException('User not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
album.sharedUsers.push({ id: userId } as UserEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.albumRepository
|
|
|
|
.update({
|
|
|
|
id: album.id,
|
|
|
|
updatedAt: new Date(),
|
|
|
|
sharedUsers: album.sharedUsers,
|
|
|
|
})
|
2023-08-15 20:34:02 +02:00
|
|
|
.then(mapAlbumWithoutAssets);
|
2023-06-07 16:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async removeUser(authUser: AuthUserDto, id: string, userId: string | 'me'): Promise<void> {
|
|
|
|
if (userId === 'me') {
|
|
|
|
userId = authUser.id;
|
|
|
|
}
|
|
|
|
|
2023-08-15 20:34:02 +02:00
|
|
|
const album = await this.findOrFail(id, { withAssets: false });
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
if (album.ownerId === userId) {
|
|
|
|
throw new BadRequestException('Cannot remove album owner');
|
|
|
|
}
|
|
|
|
|
|
|
|
const exists = album.sharedUsers.find((user) => user.id === userId);
|
|
|
|
if (!exists) {
|
|
|
|
throw new BadRequestException('Album not shared with user');
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-admin can remove themselves
|
|
|
|
if (authUser.id !== userId) {
|
2023-06-28 15:56:24 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_SHARE, id);
|
2023-06-07 16:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.albumRepository.update({
|
|
|
|
id: album.id,
|
|
|
|
updatedAt: new Date(),
|
|
|
|
sharedUsers: album.sharedUsers.filter((user) => user.id !== userId),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-15 20:34:02 +02:00
|
|
|
private async findOrFail(id: string, options: AlbumInfoOptions) {
|
|
|
|
const album = await this.albumRepository.getById(id, options);
|
2023-06-07 16:37:25 +02:00
|
|
|
if (!album) {
|
|
|
|
throw new BadRequestException('Album not found');
|
|
|
|
}
|
|
|
|
return album;
|
|
|
|
}
|
2023-03-26 04:46:48 +02:00
|
|
|
}
|