Skip to content

High performance self-hosted photo and video management solution.

refactor(server): remove has asset (#10891)

Jason Rasmussen53d4a5268b1f
diff --git a/server/src/interfaces/album.interface.ts b/server/src/interfaces/album.interface.ts
index 2d48f4da7f..9963612fd6 100644
--- a/server/src/interfaces/album.interface.ts
+++ b/server/src/interfaces/album.interface.ts
@@ -14,21 +14,10 @@ export interface AlbumInfoOptions {
withAssets: boolean;
}
-export interface AlbumAsset {
- albumId: string;
- assetId: string;
-}
-
-export interface AlbumAssets {
- albumId: string;
- assetIds: string[];
-}
-
export interface IAlbumRepository extends IBulkAsset {
getById(id: string, options: AlbumInfoOptions): Promise<AlbumEntity | null>;
getByIds(ids: string[]): Promise<AlbumEntity[]>;
getByAssetId(ownerId: string, assetId: string): Promise<AlbumEntity[]>;
- hasAsset(asset: AlbumAsset): Promise<boolean>;
removeAsset(assetId: string): Promise<void>;
getMetadataForIds(ids: string[]): Promise<AlbumAssetCount[]>;
getInvalidThumbnail(): Promise<string[]>;
diff --git a/server/src/queries/album.repository.sql b/server/src/queries/album.repository.sql
index 44097a5a8c..729f7c7f20 100644
--- a/server/src/queries/album.repository.sql
+++ b/server/src/queries/album.repository.sql
@@ -585,37 +585,6 @@ WHERE
"albums_assets"."albumsId" = $1
AND "albums_assets"."assetsId" IN ($2)
--- AlbumRepository.hasAsset
-SELECT
- 1 AS "row_exists"
-FROM
- (
- SELECT
- 1 AS dummy_column
- ) "dummy_table"
-WHERE
- EXISTS (
- SELECT
- 1
- FROM
- "albums" "AlbumEntity"
- LEFT JOIN "albums_assets_assets" "AlbumEntity_AlbumEntity__AlbumEntity_assets" ON "AlbumEntity_AlbumEntity__AlbumEntity_assets"."albumsId" = "AlbumEntity"."id"
- LEFT JOIN "assets" "AlbumEntity__AlbumEntity_assets" ON "AlbumEntity__AlbumEntity_assets"."id" = "AlbumEntity_AlbumEntity__AlbumEntity_assets"."assetsId"
- AND (
- "AlbumEntity__AlbumEntity_assets"."deletedAt" IS NULL
- )
- WHERE
- (
- (
- ("AlbumEntity"."id" = $1)
- AND ((("AlbumEntity__AlbumEntity_assets"."id" = $2)))
- )
- )
- AND ("AlbumEntity"."deletedAt" IS NULL)
- )
-LIMIT
- 1
-
-- AlbumRepository.addAssetIds
INSERT INTO
"albums_assets_assets" ("albumsId", "assetsId")
diff --git a/server/src/repositories/album.repository.ts b/server/src/repositories/album.repository.ts
index 845694f389..a44b8dc071 100644
--- a/server/src/repositories/album.repository.ts
+++ b/server/src/repositories/album.repository.ts
@@ -3,7 +3,7 @@ import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { Chunked, ChunkedArray, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
import { AlbumEntity } from 'src/entities/album.entity';
import { AssetEntity } from 'src/entities/asset.entity';
-import { AlbumAsset, AlbumAssetCount, AlbumInfoOptions, IAlbumRepository } from 'src/interfaces/album.interface';
+import { AlbumAssetCount, AlbumInfoOptions, IAlbumRepository } from 'src/interfaces/album.interface';
import { Instrumentation } from 'src/utils/instrumentation';
import { DataSource, FindOptionsOrder, FindOptionsRelations, In, IsNull, Not, Repository } from 'typeorm';
@@ -253,21 +253,6 @@ export class AlbumRepository implements IAlbumRepository {
return new Set(results.map(({ assetId }) => assetId));
}
- @GenerateSql({ params: [{ albumId: DummyValue.UUID, assetId: DummyValue.UUID }] })
- hasAsset(asset: AlbumAsset): Promise<boolean> {
- return this.repository.exist({
- where: {
- id: asset.albumId,
- assets: {
- id: asset.assetId,
- },
- },
- relations: {
- assets: true,
- },
- });
- }
-
@GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] })
async addAssetIds(albumId: string, assetIds: string[]): Promise<void> {
await this.dataSource
diff --git a/server/src/services/album.service.spec.ts b/server/src/services/album.service.spec.ts
index d111e0c6c4..97549d89f1 100644
--- a/server/src/services/album.service.spec.ts
+++ b/server/src/services/album.service.spec.ts
@@ -270,7 +270,7 @@ describe(AlbumService.name, () => {
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-4']));
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
albumMock.update.mockResolvedValue(albumStub.oneAsset);
- albumMock.hasAsset.mockResolvedValue(false);
+ albumMock.getAssetIds.mockResolvedValue(new Set());
await expect(
sut.update(authStub.admin, albumStub.oneAsset.id, {
@@ -278,7 +278,7 @@ describe(AlbumService.name, () => {
}),
).rejects.toBeInstanceOf(BadRequestException);
- expect(albumMock.hasAsset).toHaveBeenCalledWith({ albumId: 'album-4', assetId: 'not-in-album' });
+ expect(albumMock.getAssetIds).toHaveBeenCalledWith('album-4', ['not-in-album']);
expect(albumMock.update).not.toHaveBeenCalled();
});
diff --git a/server/src/services/album.service.ts b/server/src/services/album.service.ts
index 76fc5f182c..7c86d74d45 100644
--- a/server/src/services/album.service.ts
+++ b/server/src/services/album.service.ts
@@ -146,8 +146,8 @@ export class AlbumService {
const album = await this.findOrFail(id, { withAssets: true });
if (dto.albumThumbnailAssetId) {
- const valid = await this.albumRepository.hasAsset({ albumId: id, assetId: dto.albumThumbnailAssetId });
- if (!valid) {
+ const results = await this.albumRepository.getAssetIds(id, [dto.albumThumbnailAssetId]);
+ if (results.size === 0) {
throw new BadRequestException('Invalid album thumbnail');
}
}
diff --git a/server/test/repositories/album.repository.mock.ts b/server/test/repositories/album.repository.mock.ts
index 9b852d7393..af267dd49c 100644
--- a/server/test/repositories/album.repository.mock.ts
+++ b/server/test/repositories/album.repository.mock.ts
@@ -19,7 +19,6 @@ export const newAlbumRepositoryMock = (): Mocked<IAlbumRepository> => {
removeAsset: vitest.fn(),
removeAssetIds: vitest.fn(),
getAssetIds: vitest.fn(),
- hasAsset: vitest.fn(),
create: vitest.fn(),
update: vitest.fn(),
delete: vitest.fn(),