mirror of
https://github.com/immich-app/immich.git
synced 2026-05-29 19:12:32 -04:00
1fcc2b704b
* feat(server)!: add owned filter to albums API BREAKING CHANGE: GET /albums with no parameters now returns all accessible albums (owned + shared-with-me) instead of only owned albums. * document tri-state matrix * web impl * collapse to single method and handover branching to sql * dedupe * verify that owned, shared, and notShared counts are mapped independently from their respective queries * refactor(server): add select:['id'] overload to albumRepository.getAll Avoid fetching full album rows (with albumUsers/sharedLinks subqueries) in map.service where only album IDs are needed. * focus relevant test filters * fmt * Revert "verify that owned, shared, and notShared counts are mapped independently from their respective queries" This reverts commit 47aab458192c766de4662aada5a6841b091d2a80. * sync sql * Revert "document tri-state matrix" This reverts commit a5b2355d0c6a084f2c021d517bf5ee80123d108c. * address review comments * inline shared condition and return as ternary * sync sql * use [...albums].sort Array.toSorted() is not supported in Chrome 109 * use isShared and isOwned nomenclature * fix e2e tests * add params to sql query
356 lines
13 KiB
TypeScript
356 lines
13 KiB
TypeScript
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import {
|
|
AddUsersDto,
|
|
AlbumResponseDto,
|
|
AlbumsAddAssetsDto,
|
|
AlbumsAddAssetsResponseDto,
|
|
AlbumStatisticsResponseDto,
|
|
CreateAlbumDto,
|
|
GetAlbumsDto,
|
|
mapAlbum,
|
|
UpdateAlbumDto,
|
|
UpdateAlbumUserDto,
|
|
} from 'src/dtos/album.dto';
|
|
import { BulkIdErrorReason, BulkIdResponseDto, BulkIdsDto } from 'src/dtos/asset-ids.response.dto';
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
|
import { MapMarkerResponseDto } from 'src/dtos/map.dto';
|
|
import { AlbumUserRole, Permission } from 'src/enum';
|
|
import { AlbumAssetCount, AlbumInfoOptions } from 'src/repositories/album.repository';
|
|
import { BaseService } from 'src/services/base.service';
|
|
import { addAssets, removeAssets } from 'src/utils/asset.util';
|
|
import { asDateString } from 'src/utils/date';
|
|
import { getPreferences } from 'src/utils/preferences';
|
|
|
|
@Injectable()
|
|
export class AlbumService extends BaseService {
|
|
async getStatistics(auth: AuthDto): Promise<AlbumStatisticsResponseDto> {
|
|
const [owned, shared, notShared] = await Promise.all([
|
|
this.albumRepository.getAll(auth.user.id, { isOwned: true }),
|
|
this.albumRepository.getAll(auth.user.id, { isShared: true }),
|
|
this.albumRepository.getAll(auth.user.id, { isOwned: true, isShared: false }),
|
|
]);
|
|
|
|
return {
|
|
owned: owned.length,
|
|
shared: shared.length,
|
|
notShared: notShared.length,
|
|
};
|
|
}
|
|
|
|
async getAll(
|
|
{ user: { id: ownerId } }: AuthDto,
|
|
{ assetId, isOwned, isShared }: GetAlbumsDto,
|
|
): Promise<AlbumResponseDto[]> {
|
|
await this.albumRepository.updateThumbnails();
|
|
|
|
const albums = assetId
|
|
? await this.albumRepository.getByAssetId(ownerId, assetId)
|
|
: await this.albumRepository.getAll(ownerId, { isOwned, isShared });
|
|
|
|
if (albums.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
// Get asset count for each album. Then map the result to an object:
|
|
// { [albumId]: assetCount }
|
|
const results = await this.albumRepository.getMetadataForIds(albums.map((album) => album.id));
|
|
const albumMetadata: Record<string, AlbumAssetCount> = {};
|
|
for (const metadata of results) {
|
|
albumMetadata[metadata.albumId] = metadata;
|
|
}
|
|
|
|
return albums.map((album) => ({
|
|
...mapAlbum(album),
|
|
sharedLinks: undefined,
|
|
startDate: asDateString(albumMetadata[album.id]?.startDate ?? undefined),
|
|
endDate: asDateString(albumMetadata[album.id]?.endDate ?? undefined),
|
|
assetCount: albumMetadata[album.id]?.assetCount ?? 0,
|
|
// lastModifiedAssetTimestamp is only used in mobile app, please remove if not need
|
|
lastModifiedAssetTimestamp: asDateString(albumMetadata[album.id]?.lastModifiedAssetTimestamp ?? undefined),
|
|
}));
|
|
}
|
|
|
|
async get(auth: AuthDto, id: string): Promise<AlbumResponseDto> {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumRead, ids: [id] });
|
|
await this.albumRepository.updateThumbnails();
|
|
const album = await this.findOrFail(id, auth.user.id, { withAssets: false });
|
|
const [albumMetadataForIds] = await this.albumRepository.getMetadataForIds([album.id]);
|
|
|
|
const hasSharedUsers = album.albumUsers && album.albumUsers.length > 1;
|
|
const hasSharedLink = album.sharedLinks && album.sharedLinks.length > 0;
|
|
const isShared = hasSharedUsers || hasSharedLink;
|
|
|
|
return {
|
|
...mapAlbum(album),
|
|
startDate: asDateString(albumMetadataForIds?.startDate ?? undefined),
|
|
endDate: asDateString(albumMetadataForIds?.endDate ?? undefined),
|
|
assetCount: albumMetadataForIds?.assetCount ?? 0,
|
|
lastModifiedAssetTimestamp: asDateString(albumMetadataForIds?.lastModifiedAssetTimestamp ?? undefined),
|
|
contributorCounts: isShared ? await this.albumRepository.getContributorCounts(album.id) : undefined,
|
|
};
|
|
}
|
|
|
|
async getMapMarkers(auth: AuthDto, id: string): Promise<MapMarkerResponseDto[]> {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumRead, ids: [id] });
|
|
|
|
if (auth.sharedLink && !auth.sharedLink.showExif) {
|
|
return [];
|
|
}
|
|
|
|
return this.mapRepository.getAlbumMapMarkers(id);
|
|
}
|
|
|
|
async create(auth: AuthDto, dto: CreateAlbumDto): Promise<AlbumResponseDto> {
|
|
const albumUsers = dto.albumUsers || [];
|
|
|
|
for (const { userId } of albumUsers) {
|
|
const exists = await this.userRepository.get(userId, {});
|
|
if (!exists) {
|
|
this.logger.debug('Album creation failed: user not found');
|
|
throw new BadRequestException('Invalid user');
|
|
}
|
|
|
|
if (userId == auth.user.id) {
|
|
throw new BadRequestException('Cannot share album with owner');
|
|
}
|
|
}
|
|
|
|
const allowedAssetIdsSet = await this.checkAccess({
|
|
auth,
|
|
permission: Permission.AssetShare,
|
|
ids: dto.assetIds || [],
|
|
});
|
|
const assetIds = [...allowedAssetIdsSet].map((id) => id);
|
|
|
|
const userMetadata = await this.userRepository.getMetadata(auth.user.id);
|
|
|
|
const album = await this.albumRepository.create(
|
|
{
|
|
albumName: dto.albumName,
|
|
description: dto.description,
|
|
albumThumbnailAssetId: assetIds[0] || null,
|
|
order: getPreferences(userMetadata).albums.defaultAssetOrder,
|
|
},
|
|
assetIds,
|
|
[{ userId: auth.user.id, role: AlbumUserRole.Owner }, ...albumUsers],
|
|
auth.user.id,
|
|
);
|
|
|
|
for (const { userId } of albumUsers) {
|
|
await this.eventRepository.emit('AlbumInvite', { id: album.id, userId, senderName: auth.user.name });
|
|
}
|
|
|
|
return mapAlbum(album);
|
|
}
|
|
|
|
async update(auth: AuthDto, id: string, dto: UpdateAlbumDto): Promise<AlbumResponseDto> {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumUpdate, ids: [id] });
|
|
|
|
const album = await this.findOrFail(id, auth.user.id, { withAssets: true });
|
|
|
|
if (dto.albumThumbnailAssetId) {
|
|
const results = await this.albumRepository.getAssetIds(id, [dto.albumThumbnailAssetId]);
|
|
if (results.size === 0) {
|
|
throw new BadRequestException('Invalid album thumbnail');
|
|
}
|
|
}
|
|
const updatedAlbum = await this.albumRepository.update(
|
|
album.id,
|
|
{
|
|
id: album.id,
|
|
albumName: dto.albumName,
|
|
description: dto.description,
|
|
albumThumbnailAssetId: dto.albumThumbnailAssetId,
|
|
isActivityEnabled: dto.isActivityEnabled,
|
|
order: dto.order,
|
|
},
|
|
auth.user.id,
|
|
);
|
|
|
|
return mapAlbum({ ...updatedAlbum, assets: album.assets });
|
|
}
|
|
|
|
async delete(auth: AuthDto, id: string): Promise<void> {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumDelete, ids: [id] });
|
|
await this.albumRepository.delete(id);
|
|
}
|
|
|
|
async addAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
|
|
const album = await this.findOrFail(id, auth.user.id, { withAssets: false });
|
|
await this.requireAccess({ auth, permission: Permission.AlbumAssetCreate, ids: [id] });
|
|
|
|
const results = await addAssets(
|
|
auth,
|
|
{ access: this.accessRepository, bulk: this.albumRepository },
|
|
{ parentId: id, assetIds: dto.ids },
|
|
);
|
|
|
|
const { id: firstNewAssetId } = results.find(({ success }) => success) || {};
|
|
if (firstNewAssetId) {
|
|
await this.albumRepository.update(
|
|
id,
|
|
{
|
|
id,
|
|
updatedAt: new Date(),
|
|
albumThumbnailAssetId: album.albumThumbnailAssetId ?? firstNewAssetId,
|
|
},
|
|
auth.user.id,
|
|
);
|
|
|
|
const allUsersExceptUs = album.albumUsers.map(({ user }) => user.id).filter((userId) => userId !== auth.user.id);
|
|
|
|
for (const recipientId of allUsersExceptUs) {
|
|
await this.eventRepository.emit('AlbumUpdate', { id, recipientId });
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
async addAssetsToAlbums(auth: AuthDto, dto: AlbumsAddAssetsDto): Promise<AlbumsAddAssetsResponseDto> {
|
|
const results: AlbumsAddAssetsResponseDto = {
|
|
success: false,
|
|
error: BulkIdErrorReason.DUPLICATE,
|
|
};
|
|
|
|
const allowedAlbumIds = await this.checkAccess({
|
|
auth,
|
|
permission: Permission.AlbumAssetCreate,
|
|
ids: dto.albumIds,
|
|
});
|
|
if (allowedAlbumIds.size === 0) {
|
|
results.error = BulkIdErrorReason.NO_PERMISSION;
|
|
return results;
|
|
}
|
|
|
|
const allowedAssetIds = await this.checkAccess({ auth, permission: Permission.AssetShare, ids: dto.assetIds });
|
|
if (allowedAssetIds.size === 0) {
|
|
results.error = BulkIdErrorReason.NO_PERMISSION;
|
|
return results;
|
|
}
|
|
|
|
const albumAssetValues: { albumId: string; assetId: string }[] = [];
|
|
const events: { id: string; recipients: string[] }[] = [];
|
|
for (const albumId of allowedAlbumIds) {
|
|
const existingAssetIds = await this.albumRepository.getAssetIds(albumId, [...allowedAssetIds]);
|
|
const notPresentAssetIds = [...allowedAssetIds].filter((id) => !existingAssetIds.has(id));
|
|
if (notPresentAssetIds.length === 0) {
|
|
continue;
|
|
}
|
|
const album = await this.findOrFail(albumId, auth.user.id, { withAssets: false });
|
|
results.error = undefined;
|
|
results.success = true;
|
|
|
|
for (const assetId of notPresentAssetIds) {
|
|
albumAssetValues.push({ albumId, assetId });
|
|
}
|
|
await this.albumRepository.update(
|
|
albumId,
|
|
{
|
|
id: albumId,
|
|
updatedAt: new Date(),
|
|
albumThumbnailAssetId: album.albumThumbnailAssetId ?? notPresentAssetIds[0],
|
|
},
|
|
auth.user.id,
|
|
);
|
|
const allUsersExceptUs = album.albumUsers.map(({ user }) => user.id).filter((userId) => userId !== auth.user.id);
|
|
events.push({ id: albumId, recipients: allUsersExceptUs });
|
|
}
|
|
|
|
await this.albumRepository.addAssetIdsToAlbums(albumAssetValues);
|
|
for (const event of events) {
|
|
for (const recipientId of event.recipients) {
|
|
await this.eventRepository.emit('AlbumUpdate', { id: event.id, recipientId });
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
async removeAssets(auth: AuthDto, id: string, dto: BulkIdsDto): Promise<BulkIdResponseDto[]> {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumAssetDelete, ids: [id] });
|
|
|
|
const album = await this.findOrFail(id, auth.user.id, { withAssets: false });
|
|
const results = await removeAssets(
|
|
auth,
|
|
{ access: this.accessRepository, bulk: this.albumRepository },
|
|
{ parentId: id, assetIds: dto.ids, canAlwaysRemove: Permission.AlbumDelete },
|
|
);
|
|
|
|
const removedIds = results.filter(({ success }) => success).map(({ id }) => id);
|
|
if (removedIds.length > 0 && album.albumThumbnailAssetId && removedIds.includes(album.albumThumbnailAssetId)) {
|
|
await this.albumRepository.updateThumbnails();
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
async addUsers(auth: AuthDto, id: string, { albumUsers }: AddUsersDto): Promise<AlbumResponseDto> {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumShare, ids: [id] });
|
|
|
|
const album = await this.findOrFail(id, auth.user.id, { withAssets: false });
|
|
|
|
for (const { userId, role } of albumUsers) {
|
|
if (role === AlbumUserRole.Owner) {
|
|
throw new BadRequestException('Cannot add another owner');
|
|
}
|
|
|
|
const exists = album.albumUsers.find(({ user: { id } }) => id === userId);
|
|
if (exists) {
|
|
throw new BadRequestException('User already added');
|
|
}
|
|
|
|
const user = await this.userRepository.get(userId, {});
|
|
if (!user) {
|
|
this.logger.debug('Adding user to album failed: user not found');
|
|
throw new BadRequestException('Invalid user');
|
|
}
|
|
|
|
await this.albumUserRepository.create({ userId, albumId: id, role });
|
|
await this.eventRepository.emit('AlbumInvite', { id, userId, senderName: auth.user.name });
|
|
}
|
|
|
|
return this.findOrFail(id, auth.user.id, { withAssets: true }).then(mapAlbum);
|
|
}
|
|
|
|
async removeUser(auth: AuthDto, id: string, userId: string | 'me'): Promise<void> {
|
|
if (userId === 'me') {
|
|
userId = auth.user.id;
|
|
}
|
|
|
|
const album = await this.findOrFail(id, auth.user.id, { withAssets: false });
|
|
|
|
const exists = album.albumUsers.find(({ user: { id } }) => id === userId);
|
|
if (!exists) {
|
|
throw new BadRequestException('Album not shared with user');
|
|
}
|
|
|
|
if (
|
|
exists.role === AlbumUserRole.Owner &&
|
|
album.albumUsers.filter(({ role }) => role === AlbumUserRole.Owner).length === 1
|
|
) {
|
|
throw new BadRequestException('Cannot remove the last album owner');
|
|
}
|
|
|
|
// non-admin can remove themselves
|
|
if (auth.user.id !== userId) {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumShare, ids: [id] });
|
|
}
|
|
|
|
await this.albumUserRepository.delete({ albumId: id, userId });
|
|
}
|
|
|
|
async updateUser(auth: AuthDto, id: string, userId: string, dto: UpdateAlbumUserDto): Promise<void> {
|
|
await this.requireAccess({ auth, permission: Permission.AlbumShare, ids: [id] });
|
|
await this.albumUserRepository.update({ albumId: id, userId }, { role: dto.role });
|
|
}
|
|
|
|
private async findOrFail(id: string, authUserId: string, options: AlbumInfoOptions) {
|
|
const album = await this.albumRepository.getById(id, options, authUserId);
|
|
if (!album) {
|
|
throw new BadRequestException('Album not found');
|
|
}
|
|
return album;
|
|
}
|
|
}
|