2023-05-25 04:10:45 +02:00
|
|
|
import { AlbumEntity, AssetEntity, UserEntity } from '@app/infra/entities';
|
2023-03-26 04:46:48 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { IAssetRepository } from '../asset';
|
|
|
|
import { AuthUserDto } from '../auth';
|
2023-05-25 04:10:45 +02:00
|
|
|
import { IJobRepository, JobName } from '../job';
|
2023-03-26 04:46:48 +02:00
|
|
|
import { IAlbumRepository } from './album.repository';
|
2023-05-25 04:10:45 +02:00
|
|
|
import { CreateAlbumDto } from './dto/album-create.dto';
|
2023-03-26 04:46:48 +02:00
|
|
|
import { GetAlbumsDto } from './dto/get-albums.dto';
|
2023-05-25 04:10:45 +02:00
|
|
|
import { AlbumResponseDto, mapAlbum } from './response-dto';
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AlbumService {
|
|
|
|
constructor(
|
|
|
|
@Inject(IAlbumRepository) private albumRepository: IAlbumRepository,
|
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
2023-05-25 04:10:45 +02:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-03-26 04:46:48 +02:00
|
|
|
) {}
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
async getAll({ id: ownerId }: AuthUserDto, { assetId, shared }: GetAlbumsDto): Promise<AlbumResponseDto[]> {
|
2023-03-26 04:46:48 +02:00
|
|
|
await this.updateInvalidThumbnails();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return albums.map((album) => {
|
|
|
|
return {
|
|
|
|
...album,
|
|
|
|
sharedLinks: undefined, // Don't return shared links
|
|
|
|
shared: album.sharedLinks?.length > 0 || album.sharedUsers?.length > 0,
|
|
|
|
assetCount: albumsAssetCountObj[album.id],
|
|
|
|
} as AlbumResponseDto;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateInvalidThumbnails(): Promise<number> {
|
|
|
|
const invalidAlbumIds = await this.albumRepository.getInvalidThumbnail();
|
|
|
|
|
|
|
|
for (const albumId of invalidAlbumIds) {
|
|
|
|
const newThumbnail = await this.assetRepository.getFirstAssetForAlbumId(albumId);
|
|
|
|
await this.albumRepository.save({ id: albumId, albumThumbnailAsset: newThumbnail });
|
|
|
|
}
|
|
|
|
|
|
|
|
return invalidAlbumIds.length;
|
|
|
|
}
|
2023-05-25 04:10:45 +02:00
|
|
|
|
|
|
|
async create(authUser: AuthUserDto, dto: CreateAlbumDto): Promise<AlbumResponseDto> {
|
|
|
|
// TODO: Handle nonexistent sharedWithUserIds and assetIds.
|
|
|
|
const album = await this.albumRepository.create({
|
|
|
|
ownerId: authUser.id,
|
|
|
|
albumName: dto.albumName,
|
|
|
|
sharedUsers: dto.sharedWithUserIds?.map((value) => ({ id: value } as UserEntity)) ?? [],
|
|
|
|
assets: (dto.assetIds || []).map((id) => ({ id } as AssetEntity)),
|
|
|
|
albumThumbnailAssetId: dto.assetIds?.[0] || null,
|
|
|
|
});
|
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ALBUM, data: { ids: [album.id] } });
|
|
|
|
return mapAlbum(album);
|
|
|
|
}
|
2023-03-26 04:46:48 +02:00
|
|
|
}
|