2023-06-28 15:56:24 +02:00
|
|
|
import { BadRequestException } from '@nestjs/common';
|
2023-06-16 21:54:17 +02:00
|
|
|
import _ from 'lodash';
|
2024-03-20 23:53:07 +01:00
|
|
|
import { BulkIdErrorReason } from 'src/dtos/asset-ids.response.dto';
|
2024-04-25 06:19:49 +02:00
|
|
|
import { AlbumUserRole } from 'src/entities/album-user.entity';
|
|
|
|
import { IAlbumUserRepository } from 'src/interfaces/album-user.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAlbumRepository } from 'src/interfaces/album.interface';
|
|
|
|
import { IAssetRepository } from 'src/interfaces/asset.interface';
|
2024-05-28 04:16:46 +02:00
|
|
|
import { IJobRepository, JobName } from 'src/interfaces/job.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IUserRepository } from 'src/interfaces/user.interface';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { AlbumService } from 'src/services/album.service';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { albumStub } from 'test/fixtures/album.stub';
|
|
|
|
import { authStub } from 'test/fixtures/auth.stub';
|
|
|
|
import { userStub } from 'test/fixtures/user.stub';
|
|
|
|
import { IAccessRepositoryMock, newAccessRepositoryMock } from 'test/repositories/access.repository.mock';
|
2024-04-25 06:19:49 +02:00
|
|
|
import { newAlbumUserRepositoryMock } from 'test/repositories/album-user.repository.mock';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { newAlbumRepositoryMock } from 'test/repositories/album.repository.mock';
|
|
|
|
import { newAssetRepositoryMock } from 'test/repositories/asset.repository.mock';
|
2024-05-28 04:16:46 +02:00
|
|
|
import { newJobRepositoryMock } from 'test/repositories/job.repository.mock';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { newUserRepositoryMock } from 'test/repositories/user.repository.mock';
|
2024-04-16 16:44:45 +02:00
|
|
|
import { Mocked } from 'vitest';
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
describe(AlbumService.name, () => {
|
|
|
|
let sut: AlbumService;
|
2023-06-28 15:56:24 +02:00
|
|
|
let accessMock: IAccessRepositoryMock;
|
2024-04-16 16:44:45 +02:00
|
|
|
let albumMock: Mocked<IAlbumRepository>;
|
|
|
|
let assetMock: Mocked<IAssetRepository>;
|
|
|
|
let userMock: Mocked<IUserRepository>;
|
2024-04-25 06:19:49 +02:00
|
|
|
let albumUserMock: Mocked<IAlbumUserRepository>;
|
2024-05-28 04:16:46 +02:00
|
|
|
let jobMock: Mocked<IJobRepository>;
|
2023-03-26 04:46:48 +02:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2023-06-28 15:56:24 +02:00
|
|
|
accessMock = newAccessRepositoryMock();
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock = newAlbumRepositoryMock();
|
|
|
|
assetMock = newAssetRepositoryMock();
|
2023-06-07 16:37:25 +02:00
|
|
|
userMock = newUserRepositoryMock();
|
2024-04-25 06:19:49 +02:00
|
|
|
albumUserMock = newAlbumUserRepositoryMock();
|
2024-05-28 04:16:46 +02:00
|
|
|
jobMock = newJobRepositoryMock();
|
2023-03-26 04:46:48 +02:00
|
|
|
|
2024-05-28 04:16:46 +02:00
|
|
|
sut = new AlbumService(accessMock, albumMock, assetMock, userMock, albumUserMock, jobMock);
|
2023-03-26 04:46:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2023-06-16 17:48:48 +02:00
|
|
|
describe('getCount', () => {
|
|
|
|
it('should get the album count', async () => {
|
|
|
|
albumMock.getOwned.mockResolvedValue([]),
|
|
|
|
albumMock.getShared.mockResolvedValue([]),
|
|
|
|
albumMock.getNotShared.mockResolvedValue([]),
|
|
|
|
await expect(sut.getCount(authStub.admin)).resolves.toEqual({
|
|
|
|
owned: 0,
|
|
|
|
shared: 0,
|
|
|
|
notShared: 0,
|
|
|
|
});
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(albumMock.getOwned).toHaveBeenCalledWith(authStub.admin.user.id);
|
|
|
|
expect(albumMock.getShared).toHaveBeenCalledWith(authStub.admin.user.id);
|
|
|
|
expect(albumMock.getNotShared).toHaveBeenCalledWith(authStub.admin.user.id);
|
2023-06-16 17:48:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
describe('getAll', () => {
|
2023-03-26 04:46:48 +02:00
|
|
|
it('gets list of albums for auth user', async () => {
|
|
|
|
albumMock.getOwned.mockResolvedValue([albumStub.empty, albumStub.sharedWithUser]);
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{ albumId: albumStub.empty.id, assetCount: 0, startDate: undefined, endDate: undefined },
|
|
|
|
{ albumId: albumStub.sharedWithUser.id, assetCount: 0, startDate: undefined, endDate: undefined },
|
2023-11-21 17:07:49 +01:00
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock.getInvalidThumbnail.mockResolvedValue([]);
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const result = await sut.getAll(authStub.admin, {});
|
2023-03-26 04:46:48 +02:00
|
|
|
expect(result).toHaveLength(2);
|
|
|
|
expect(result[0].id).toEqual(albumStub.empty.id);
|
|
|
|
expect(result[1].id).toEqual(albumStub.sharedWithUser.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets list of albums that have a specific asset', async () => {
|
|
|
|
albumMock.getByAssetId.mockResolvedValue([albumStub.oneAsset]);
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
albumId: albumStub.oneAsset.id,
|
|
|
|
assetCount: 1,
|
|
|
|
startDate: new Date('1970-01-01'),
|
|
|
|
endDate: new Date('1970-01-01'),
|
|
|
|
},
|
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock.getInvalidThumbnail.mockResolvedValue([]);
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const result = await sut.getAll(authStub.admin, { assetId: albumStub.oneAsset.id });
|
2023-03-26 04:46:48 +02:00
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result[0].id).toEqual(albumStub.oneAsset.id);
|
|
|
|
expect(albumMock.getByAssetId).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets list of albums that are shared', async () => {
|
|
|
|
albumMock.getShared.mockResolvedValue([albumStub.sharedWithUser]);
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{ albumId: albumStub.sharedWithUser.id, assetCount: 0, startDate: undefined, endDate: undefined },
|
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock.getInvalidThumbnail.mockResolvedValue([]);
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const result = await sut.getAll(authStub.admin, { shared: true });
|
2023-03-26 04:46:48 +02:00
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result[0].id).toEqual(albumStub.sharedWithUser.id);
|
|
|
|
expect(albumMock.getShared).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets list of albums that are NOT shared', async () => {
|
|
|
|
albumMock.getNotShared.mockResolvedValue([albumStub.empty]);
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{ albumId: albumStub.empty.id, assetCount: 0, startDate: undefined, endDate: undefined },
|
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock.getInvalidThumbnail.mockResolvedValue([]);
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const result = await sut.getAll(authStub.admin, { shared: false });
|
2023-03-26 04:46:48 +02:00
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result[0].id).toEqual(albumStub.empty.id);
|
|
|
|
expect(albumMock.getNotShared).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('counts assets correctly', async () => {
|
|
|
|
albumMock.getOwned.mockResolvedValue([albumStub.oneAsset]);
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
albumId: albumStub.oneAsset.id,
|
|
|
|
assetCount: 1,
|
|
|
|
startDate: new Date('1970-01-01'),
|
|
|
|
endDate: new Date('1970-01-01'),
|
|
|
|
},
|
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock.getInvalidThumbnail.mockResolvedValue([]);
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const result = await sut.getAll(authStub.admin, {});
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(result[0].assetCount).toEqual(1);
|
|
|
|
expect(albumMock.getOwned).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates the album thumbnail by listing all albums', async () => {
|
|
|
|
albumMock.getOwned.mockResolvedValue([albumStub.oneAssetInvalidThumbnail]);
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
albumId: albumStub.oneAssetInvalidThumbnail.id,
|
|
|
|
assetCount: 1,
|
|
|
|
startDate: new Date('1970-01-01'),
|
|
|
|
endDate: new Date('1970-01-01'),
|
|
|
|
},
|
2023-11-21 17:07:49 +01:00
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock.getInvalidThumbnail.mockResolvedValue([albumStub.oneAssetInvalidThumbnail.id]);
|
2023-05-25 21:37:19 +02:00
|
|
|
albumMock.update.mockResolvedValue(albumStub.oneAssetValidThumbnail);
|
2023-03-26 04:46:48 +02:00
|
|
|
assetMock.getFirstAssetForAlbumId.mockResolvedValue(albumStub.oneAssetInvalidThumbnail.assets[0]);
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const result = await sut.getAll(authStub.admin, {});
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(albumMock.getInvalidThumbnail).toHaveBeenCalledTimes(1);
|
2023-05-25 21:37:19 +02:00
|
|
|
expect(albumMock.update).toHaveBeenCalledTimes(1);
|
2023-03-26 04:46:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes the thumbnail for an empty album', async () => {
|
|
|
|
albumMock.getOwned.mockResolvedValue([albumStub.emptyWithInvalidThumbnail]);
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
albumId: albumStub.emptyWithInvalidThumbnail.id,
|
|
|
|
assetCount: 1,
|
|
|
|
startDate: new Date('1970-01-01'),
|
|
|
|
endDate: new Date('1970-01-01'),
|
|
|
|
},
|
2023-11-21 17:07:49 +01:00
|
|
|
]);
|
2023-03-26 04:46:48 +02:00
|
|
|
albumMock.getInvalidThumbnail.mockResolvedValue([albumStub.emptyWithInvalidThumbnail.id]);
|
2023-05-25 21:37:19 +02:00
|
|
|
albumMock.update.mockResolvedValue(albumStub.emptyWithValidThumbnail);
|
2023-03-26 04:46:48 +02:00
|
|
|
assetMock.getFirstAssetForAlbumId.mockResolvedValue(null);
|
|
|
|
|
2023-05-25 04:10:45 +02:00
|
|
|
const result = await sut.getAll(authStub.admin, {});
|
2023-03-26 04:46:48 +02:00
|
|
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
expect(albumMock.getInvalidThumbnail).toHaveBeenCalledTimes(1);
|
2023-05-25 21:37:19 +02:00
|
|
|
expect(albumMock.update).toHaveBeenCalledTimes(1);
|
2023-03-26 04:46:48 +02:00
|
|
|
});
|
2023-05-25 04:10:45 +02:00
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
it('creates album', async () => {
|
|
|
|
albumMock.create.mockResolvedValue(albumStub.empty);
|
2023-09-11 17:56:38 +02:00
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
2024-04-10 19:41:22 +02:00
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['123']));
|
2023-05-25 04:10:45 +02:00
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
await sut.create(authStub.admin, {
|
2023-05-25 04:10:45 +02:00
|
|
|
albumName: 'Empty album',
|
2024-05-24 16:37:01 +02:00
|
|
|
albumUsers: [{ userId: 'user-id', role: AlbumUserRole.EDITOR }],
|
2023-08-06 04:43:26 +02:00
|
|
|
description: '',
|
2023-09-11 17:56:38 +02:00
|
|
|
assetIds: ['123'],
|
2023-05-25 04:10:45 +02:00
|
|
|
});
|
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
expect(albumMock.create).toHaveBeenCalledWith({
|
2023-12-10 05:34:12 +01:00
|
|
|
ownerId: authStub.admin.user.id,
|
2023-09-11 17:56:38 +02:00
|
|
|
albumName: albumStub.empty.albumName,
|
|
|
|
description: albumStub.empty.description,
|
2024-05-07 22:38:09 +02:00
|
|
|
albumUsers: [{ userId: 'user-id', role: AlbumUserRole.EDITOR }],
|
2023-09-11 17:56:38 +02:00
|
|
|
assets: [{ id: '123' }],
|
|
|
|
albumThumbnailAssetId: '123',
|
|
|
|
});
|
|
|
|
|
2023-10-31 16:01:32 +01:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith('user-id', {});
|
2024-04-10 19:41:22 +02:00
|
|
|
expect(accessMock.asset.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['123']));
|
2023-05-25 04:10:45 +02:00
|
|
|
});
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
it('should require valid userIds', async () => {
|
|
|
|
userMock.get.mockResolvedValue(null);
|
|
|
|
await expect(
|
|
|
|
sut.create(authStub.admin, {
|
|
|
|
albumName: 'Empty album',
|
2024-05-24 16:37:01 +02:00
|
|
|
albumUsers: [{ userId: 'user-3', role: AlbumUserRole.EDITOR }],
|
2023-06-07 16:37:25 +02:00
|
|
|
}),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
2023-10-31 16:01:32 +01:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith('user-3', {});
|
2023-06-07 16:37:25 +02:00
|
|
|
expect(albumMock.create).not.toHaveBeenCalled();
|
|
|
|
});
|
2024-04-10 19:41:22 +02:00
|
|
|
|
|
|
|
it('should only add assets the user is allowed to access', async () => {
|
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
|
|
|
albumMock.create.mockResolvedValue(albumStub.oneAsset);
|
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-1']));
|
|
|
|
|
|
|
|
await sut.create(authStub.admin, {
|
|
|
|
albumName: 'Test album',
|
|
|
|
description: '',
|
|
|
|
assetIds: ['asset-1', 'asset-2'],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(albumMock.create).toHaveBeenCalledWith({
|
|
|
|
ownerId: authStub.admin.user.id,
|
|
|
|
albumName: 'Test album',
|
|
|
|
description: '',
|
2024-04-25 06:19:49 +02:00
|
|
|
albumUsers: [],
|
2024-04-10 19:41:22 +02:00
|
|
|
assets: [{ id: 'asset-1' }],
|
|
|
|
albumThumbnailAssetId: 'asset-1',
|
|
|
|
});
|
|
|
|
expect(accessMock.asset.checkOwnerAccess).toHaveBeenCalledWith(
|
|
|
|
authStub.admin.user.id,
|
|
|
|
new Set(['asset-1', 'asset-2']),
|
|
|
|
);
|
|
|
|
});
|
2023-05-25 04:10:45 +02:00
|
|
|
});
|
2023-05-25 21:37:19 +02:00
|
|
|
|
|
|
|
describe('update', () => {
|
|
|
|
it('should prevent updating an album that does not exist', async () => {
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(null);
|
2023-05-25 21:37:19 +02:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.update(authStub.user1, 'invalid-id', {
|
|
|
|
albumName: 'new album name',
|
|
|
|
}),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should prevent updating a not owned album (shared with auth user)', async () => {
|
|
|
|
await expect(
|
|
|
|
sut.update(authStub.admin, albumStub.sharedWithAdmin.id, {
|
|
|
|
albumName: 'new album name',
|
|
|
|
}),
|
2023-06-28 15:56:24 +02:00
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-25 21:37:19 +02:00
|
|
|
});
|
|
|
|
|
2023-05-26 15:04:09 +02:00
|
|
|
it('should require a valid thumbnail asset id', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-4']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
2023-05-26 15:04:09 +02:00
|
|
|
albumMock.update.mockResolvedValue(albumStub.oneAsset);
|
|
|
|
albumMock.hasAsset.mockResolvedValue(false);
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.update(authStub.admin, albumStub.oneAsset.id, {
|
|
|
|
albumThumbnailAssetId: 'not-in-album',
|
|
|
|
}),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
2023-10-18 17:56:00 +02:00
|
|
|
expect(albumMock.hasAsset).toHaveBeenCalledWith({ albumId: 'album-4', assetId: 'not-in-album' });
|
2023-05-26 15:04:09 +02:00
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow the owner to update the album', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-4']));
|
2023-06-28 15:56:24 +02:00
|
|
|
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
2023-05-25 21:37:19 +02:00
|
|
|
albumMock.update.mockResolvedValue(albumStub.oneAsset);
|
|
|
|
|
|
|
|
await sut.update(authStub.admin, albumStub.oneAsset.id, {
|
|
|
|
albumName: 'new album name',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(albumMock.update).toHaveBeenCalledTimes(1);
|
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'album-4',
|
|
|
|
albumName: 'new album name',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-05-26 15:04:09 +02:00
|
|
|
|
|
|
|
describe('delete', () => {
|
|
|
|
it('should throw an error for an album not found', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set([albumStub.sharedWithAdmin.id]));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(null);
|
2023-05-26 15:04:09 +02:00
|
|
|
|
|
|
|
await expect(sut.delete(authStub.admin, albumStub.sharedWithAdmin.id)).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(albumMock.delete).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not let a shared user delete the album', async () => {
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.sharedWithAdmin);
|
2023-05-26 15:04:09 +02:00
|
|
|
|
2023-06-28 15:56:24 +02:00
|
|
|
await expect(sut.delete(authStub.admin, albumStub.sharedWithAdmin.id)).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
2023-05-26 15:04:09 +02:00
|
|
|
|
|
|
|
expect(albumMock.delete).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should let the owner delete an album', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set([albumStub.empty.id]));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.empty);
|
2023-05-26 15:04:09 +02:00
|
|
|
|
|
|
|
await sut.delete(authStub.admin, albumStub.empty.id);
|
|
|
|
|
|
|
|
expect(albumMock.delete).toHaveBeenCalledTimes(1);
|
|
|
|
expect(albumMock.delete).toHaveBeenCalledWith(albumStub.empty);
|
|
|
|
});
|
|
|
|
});
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
describe('addUsers', () => {
|
2023-06-28 15:56:24 +02:00
|
|
|
it('should throw an error if the auth user is not the owner', async () => {
|
2023-06-07 16:37:25 +02:00
|
|
|
await expect(
|
2024-04-25 06:19:49 +02:00
|
|
|
sut.addUsers(authStub.admin, albumStub.sharedWithAdmin.id, { albumUsers: [{ userId: 'user-1' }] }),
|
2023-06-28 15:56:24 +02:00
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
2023-06-07 16:37:25 +02:00
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the userId is already added', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set([albumStub.sharedWithAdmin.id]));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.sharedWithAdmin);
|
2023-06-07 16:37:25 +02:00
|
|
|
await expect(
|
2024-04-25 06:19:49 +02:00
|
|
|
sut.addUsers(authStub.user1, albumStub.sharedWithAdmin.id, {
|
|
|
|
albumUsers: [{ userId: authStub.admin.user.id }],
|
|
|
|
}),
|
2023-06-07 16:37:25 +02:00
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the userId does not exist', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set([albumStub.sharedWithAdmin.id]));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.sharedWithAdmin);
|
2023-06-07 16:37:25 +02:00
|
|
|
userMock.get.mockResolvedValue(null);
|
|
|
|
await expect(
|
2024-04-25 06:19:49 +02:00
|
|
|
sut.addUsers(authStub.user1, albumStub.sharedWithAdmin.id, { albumUsers: [{ userId: 'user-3' }] }),
|
2023-06-07 16:37:25 +02:00
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add valid shared users', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set([albumStub.sharedWithAdmin.id]));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.sharedWithAdmin));
|
2023-06-07 16:37:25 +02:00
|
|
|
albumMock.update.mockResolvedValue(albumStub.sharedWithAdmin);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.get.mockResolvedValue(userStub.user2);
|
2024-04-25 06:19:49 +02:00
|
|
|
albumUserMock.create.mockResolvedValue({
|
|
|
|
userId: userStub.user2.id,
|
|
|
|
user: userStub.user2,
|
|
|
|
albumId: albumStub.sharedWithAdmin.id,
|
|
|
|
album: albumStub.sharedWithAdmin,
|
|
|
|
role: AlbumUserRole.EDITOR,
|
|
|
|
});
|
|
|
|
await sut.addUsers(authStub.user1, albumStub.sharedWithAdmin.id, {
|
|
|
|
albumUsers: [{ userId: authStub.user2.user.id }],
|
|
|
|
});
|
|
|
|
expect(albumUserMock.create).toHaveBeenCalledWith({
|
|
|
|
userId: authStub.user2.user.id,
|
|
|
|
albumId: albumStub.sharedWithAdmin.id,
|
2023-06-07 16:37:25 +02:00
|
|
|
});
|
2024-05-28 04:16:46 +02:00
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: JobName.NOTIFY_ALBUM_INVITE,
|
|
|
|
data: { id: albumStub.sharedWithAdmin.id, recipientId: authStub.user2.user.id },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
2023-06-07 16:37:25 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeUser', () => {
|
|
|
|
it('should require a valid album id', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-1']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(null);
|
2023-06-07 16:37:25 +02:00
|
|
|
await expect(sut.removeUser(authStub.admin, 'album-1', 'user-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove a shared user from an owned album', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set([albumStub.sharedWithUser.id]));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.sharedWithUser);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
await expect(
|
2023-08-01 03:28:07 +02:00
|
|
|
sut.removeUser(authStub.admin, albumStub.sharedWithUser.id, userStub.user1.id),
|
2023-06-07 16:37:25 +02:00
|
|
|
).resolves.toBeUndefined();
|
|
|
|
|
2024-04-25 06:19:49 +02:00
|
|
|
expect(albumUserMock.delete).toHaveBeenCalledTimes(1);
|
|
|
|
expect(albumUserMock.delete).toHaveBeenCalledWith({
|
|
|
|
albumId: albumStub.sharedWithUser.id,
|
|
|
|
userId: userStub.user1.id,
|
2023-06-07 16:37:25 +02:00
|
|
|
});
|
2023-08-15 20:34:02 +02:00
|
|
|
expect(albumMock.getById).toHaveBeenCalledWith(albumStub.sharedWithUser.id, { withAssets: false });
|
2023-06-07 16:37:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should prevent removing a shared user from a not-owned album (shared with auth user)', async () => {
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.sharedWithMultiple);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
await expect(
|
2023-12-10 05:34:12 +01:00
|
|
|
sut.removeUser(authStub.user1, albumStub.sharedWithMultiple.id, authStub.user2.user.id),
|
2023-06-28 15:56:24 +02:00
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
2024-04-25 06:19:49 +02:00
|
|
|
expect(albumUserMock.delete).not.toHaveBeenCalled();
|
2023-11-25 23:56:23 +01:00
|
|
|
expect(accessMock.album.checkOwnerAccess).toHaveBeenCalledWith(
|
2023-12-10 05:34:12 +01:00
|
|
|
authStub.user1.user.id,
|
2023-11-25 23:56:23 +01:00
|
|
|
new Set([albumStub.sharedWithMultiple.id]),
|
|
|
|
);
|
2023-06-07 16:37:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow a shared user to remove themselves', async () => {
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.sharedWithUser);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await sut.removeUser(authStub.user1, albumStub.sharedWithUser.id, authStub.user1.user.id);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
2024-04-25 06:19:49 +02:00
|
|
|
expect(albumUserMock.delete).toHaveBeenCalledTimes(1);
|
|
|
|
expect(albumUserMock.delete).toHaveBeenCalledWith({
|
|
|
|
albumId: albumStub.sharedWithUser.id,
|
|
|
|
userId: authStub.user1.user.id,
|
2023-06-07 16:37:25 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow a shared user to remove themselves using "me"', async () => {
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.sharedWithUser);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
await sut.removeUser(authStub.user1, albumStub.sharedWithUser.id, 'me');
|
|
|
|
|
2024-04-25 06:19:49 +02:00
|
|
|
expect(albumUserMock.delete).toHaveBeenCalledTimes(1);
|
|
|
|
expect(albumUserMock.delete).toHaveBeenCalledWith({
|
|
|
|
albumId: albumStub.sharedWithUser.id,
|
|
|
|
userId: authStub.user1.user.id,
|
2023-06-07 16:37:25 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow the owner to be removed', async () => {
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.empty);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.removeUser(authStub.admin, albumStub.empty.id, authStub.admin.user.id)).rejects.toBeInstanceOf(
|
2023-06-07 16:37:25 +02:00
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error for a user not in the album', async () => {
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.empty);
|
2023-06-07 16:37:25 +02:00
|
|
|
|
|
|
|
await expect(sut.removeUser(authStub.admin, albumStub.empty.id, 'user-3')).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
describe('getAlbumInfo', () => {
|
|
|
|
it('should get a shared album', async () => {
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set([albumStub.oneAsset.id]));
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
albumId: albumStub.oneAsset.id,
|
|
|
|
assetCount: 1,
|
|
|
|
startDate: new Date('1970-01-01'),
|
|
|
|
endDate: new Date('1970-01-01'),
|
|
|
|
},
|
|
|
|
]);
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
await sut.get(authStub.admin, albumStub.oneAsset.id, {});
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-11-27 04:21:04 +01:00
|
|
|
expect(albumMock.getById).toHaveBeenCalledWith(albumStub.oneAsset.id, { withAssets: true });
|
2023-11-25 23:56:23 +01:00
|
|
|
expect(accessMock.album.checkOwnerAccess).toHaveBeenCalledWith(
|
2023-12-10 05:34:12 +01:00
|
|
|
authStub.admin.user.id,
|
2023-11-25 23:56:23 +01:00
|
|
|
new Set([albumStub.oneAsset.id]),
|
|
|
|
);
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should get a shared album via a shared link', async () => {
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkSharedLinkAccess.mockResolvedValue(new Set(['album-123']));
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
albumId: albumStub.oneAsset.id,
|
|
|
|
assetCount: 1,
|
|
|
|
startDate: new Date('1970-01-01'),
|
|
|
|
endDate: new Date('1970-01-01'),
|
|
|
|
},
|
|
|
|
]);
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
await sut.get(authStub.adminSharedLink, 'album-123', {});
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-11-27 04:21:04 +01:00
|
|
|
expect(albumMock.getById).toHaveBeenCalledWith('album-123', { withAssets: true });
|
2023-11-25 23:56:23 +01:00
|
|
|
expect(accessMock.album.checkSharedLinkAccess).toHaveBeenCalledWith(
|
2023-12-10 05:34:12 +01:00
|
|
|
authStub.adminSharedLink.sharedLink?.id,
|
2023-11-25 23:56:23 +01:00
|
|
|
new Set(['album-123']),
|
2023-08-02 03:29:14 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get a shared album via shared with user', async () => {
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkSharedAlbumAccess.mockResolvedValue(new Set(['album-123']));
|
2023-11-26 16:23:43 +01:00
|
|
|
albumMock.getMetadataForIds.mockResolvedValue([
|
|
|
|
{
|
|
|
|
albumId: albumStub.oneAsset.id,
|
|
|
|
assetCount: 1,
|
|
|
|
startDate: new Date('1970-01-01'),
|
|
|
|
endDate: new Date('1970-01-01'),
|
|
|
|
},
|
|
|
|
]);
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
await sut.get(authStub.user1, 'album-123', {});
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-11-27 04:21:04 +01:00
|
|
|
expect(albumMock.getById).toHaveBeenCalledWith('album-123', { withAssets: true });
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.album.checkSharedAlbumAccess).toHaveBeenCalledWith(
|
|
|
|
authStub.user1.user.id,
|
|
|
|
new Set(['album-123']),
|
2024-04-25 06:19:49 +02:00
|
|
|
AlbumUserRole.VIEWER,
|
2023-12-10 05:34:12 +01:00
|
|
|
);
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error for no access', async () => {
|
2023-08-11 18:00:51 +02:00
|
|
|
await expect(sut.get(authStub.admin, 'album-123', {})).rejects.toBeInstanceOf(BadRequestException);
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.album.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['album-123']));
|
|
|
|
expect(accessMock.album.checkSharedAlbumAccess).toHaveBeenCalledWith(
|
|
|
|
authStub.admin.user.id,
|
|
|
|
new Set(['album-123']),
|
2024-04-25 06:19:49 +02:00
|
|
|
AlbumUserRole.VIEWER,
|
2023-12-10 05:34:12 +01:00
|
|
|
);
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addAssets', () => {
|
|
|
|
it('should allow the owner to add assets', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
chore(server): Check asset permissions in bulk (#5329)
Modify Access repository, to evaluate `asset` permissions in bulk.
Queries have been validated to match what they currently generate for single ids.
Queries:
* `asset` album access:
```sql
-- Before
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
LEFT JOIN "albums_shared_users_users" "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."albumsId"="AlbumEntity"."id"
LEFT JOIN "users" "AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity__AlbumEntity_sharedUsers"."id"="AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."usersId"
AND "AlbumEntity__AlbumEntity_sharedUsers"."deletedAt" IS NULL
WHERE
(
("AlbumEntity"."ownerId" = $1 AND "AlbumEntity__AlbumEntity_assets"."id" = $2)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $3 AND "AlbumEntity__AlbumEntity_assets"."id" = $4)
OR ("AlbumEntity"."ownerId" = $5 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $6)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $7 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $8)
)
AND "AlbumEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId",
"asset"."livePhotoVideoId" AS "livePhotoVideoId"
FROM "albums" "album"
INNER JOIN "albums_assets_assets" "album_asset"
ON "album_asset"."albumsId"="album"."id"
INNER JOIN "assets" "asset"
ON "asset"."id"="album_asset"."assetsId"
AND "asset"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "album_sharedUsers"
ON "album_sharedUsers"."albumsId"="album"."id"
LEFT JOIN "users" "sharedUsers"
ON "sharedUsers"."id"="album_sharedUsers"."usersId"
AND "sharedUsers"."deletedAt" IS NULL
WHERE
(
"album"."ownerId" = $1
OR "sharedUsers"."id" = $2
)
AND (
"asset"."id" IN ($3, $4)
OR "asset"."livePhotoVideoId" IN ($5, $6)
)
AND "album"."deletedAt" IS NULL
```
* `asset` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" = $1
AND "AssetEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT
"AssetEntity"."id" AS "AssetEntity_id"
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" IN ($1, $2)
AND "AssetEntity"."ownerId" = $3
```
* `asset` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedWith"
ON "PartnerEntity__PartnerEntity_sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__PartnerEntity_sharedWith"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedBy"
ON "PartnerEntity__PartnerEntity_sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__PartnerEntity_sharedBy"."deletedAt" IS NULL
LEFT JOIN "assets" "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"
ON "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."ownerId"="PartnerEntity__PartnerEntity_sharedBy"."id"
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity__PartnerEntity_sharedWith"."id" = $1
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."id" = $2
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId"
FROM "partners" "partner"
INNER JOIN "users" "sharedBy"
ON "sharedBy"."id"="partner"."sharedById"
AND "sharedBy"."deletedAt" IS NULL
INNER JOIN "assets" "asset"
ON "asset"."ownerId"="sharedBy"."id"
AND "asset"."deletedAt" IS NULL
WHERE
"partner"."sharedWithId" = $1
AND "asset"."id" IN ($2, $3)
```
* `asset` shared link access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "shared_links" "SharedLinkEntity"
LEFT JOIN "albums" "SharedLinkEntity__SharedLinkEntity_album"
ON "SharedLinkEntity__SharedLinkEntity_album"."id"="SharedLinkEntity"."albumId"
AND "SharedLinkEntity__SharedLinkEntity_album"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "760f12c00d97bdcec1ce224d1e3bf449859942b6"
ON "760f12c00d97bdcec1ce224d1e3bf449859942b6"."albumsId"="SharedLinkEntity__SharedLinkEntity_album"."id"
LEFT JOIN "assets" "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"
ON "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id"="760f12c00d97bdcec1ce224d1e3bf449859942b6"."assetsId"
AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"
ON "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."sharedLinksId"="SharedLinkEntity"."id"
LEFT JOIN "assets" "SharedLinkEntity__SharedLinkEntity_assets"
ON "SharedLinkEntity__SharedLinkEntity_assets"."id"="SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."assetsId"
AND "SharedLinkEntity__SharedLinkEntity_assets"."deletedAt" IS NULL
WHERE (
("SharedLinkEntity"."id" = $1 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id" = $2)
OR ("SharedLinkEntity"."id" = $3 AND "SharedLinkEntity__SharedLinkEntity_assets"."id" = $4)
OR ("SharedLinkEntity"."id" = $5 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."livePhotoVideoId" = $6)
OR ("SharedLinkEntity"."id" = $7 AND "SharedLinkEntity__SharedLinkEntity_assets"."livePhotoVideoId" = $8)
)
)
LIMIT 1
-- After
SELECT
"assets"."id" AS "assetId",
"assets"."livePhotoVideoId" AS "assetLivePhotoVideoId",
"albumAssets"."id" AS "albumAssetId",
"albumAssets"."livePhotoVideoId" AS "albumAssetLivePhotoVideoId"
FROM "shared_links" "sharedLink"
LEFT JOIN "albums" "album"
ON "album"."id"="sharedLink"."albumId"
AND "album"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "assets_sharedLink"
ON "assets_sharedLink"."sharedLinksId"="sharedLink"."id"
LEFT JOIN "assets" "assets"
ON "assets"."id"="assets_sharedLink"."assetsId"
AND "assets"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "album_albumAssets"
ON "album_albumAssets"."albumsId"="album"."id"
LEFT JOIN "assets" "albumAssets"
ON "albumAssets"."id"="album_albumAssets"."assetsId"
AND "albumAssets"."deletedAt" IS NULL
WHERE
"sharedLink"."id" = $1
AND (
"assets"."id" IN ($2, $3)
OR "albumAssets"."id" IN ($4, $5)
OR "assets"."livePhotoVideoId" IN ($6, $7)
OR "albumAssets"."livePhotoVideoId" IN ($8, $9)
)
```
2023-12-02 03:56:41 +01:00
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-1', 'asset-2', 'asset-3']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.oneAsset));
|
fix(server): Check album asset membership in bulk (#4603)
Add `AlbumRepository` method to retrieve an album's asset ids, with an
optional parameter to only filter by the provided asset ids. With this,
we can now check asset membership using a single query.
When adding or removing assets to an album, checking whether each asset
is already present in the album now requires a single query, instead of
one query per asset.
Related to #4539 performance improvements.
Before:
```
// Asset membership and permissions check (2 queries per asset)
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","b666ae6c-afa8-4d6f-a1ad-7091a0659320"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","c656ab1c-7775-4ff7-b56f-01308c072a76"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
After:
```
// Asset membership check (1 query for all assets)
immich_server | query: SELECT "albums_assets"."assetsId" AS "assetId" FROM "albums_assets_assets" "albums_assets" WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2, $3, $4) -- PARAMETERS: ["ca870d76-6311-4e89-bf9a-f5b51ea2452c","b666ae6c-afa8-4d6f-a1ad-7091a0659320","c656ab1c-7775-4ff7-b56f-01308c072a76","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
// Permissions check (1 query per asset)
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
2023-10-23 15:02:27 +02:00
|
|
|
albumMock.getAssetIds.mockResolvedValueOnce(new Set());
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.addAssets(authStub.admin, 'album-123', { ids: ['asset-1', 'asset-2', 'asset-3'] }),
|
|
|
|
).resolves.toEqual([
|
|
|
|
{ success: true, id: 'asset-1' },
|
|
|
|
{ success: true, id: 'asset-2' },
|
|
|
|
{ success: true, id: 'asset-3' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'album-123',
|
|
|
|
updatedAt: expect.any(Date),
|
|
|
|
albumThumbnailAssetId: 'asset-1',
|
|
|
|
});
|
2024-03-29 12:56:16 +01:00
|
|
|
expect(albumMock.addAssetIds).toHaveBeenCalledWith('album-123', ['asset-1', 'asset-2', 'asset-3']);
|
2024-05-28 04:16:46 +02:00
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: JobName.NOTIFY_ALBUM_UPDATE,
|
|
|
|
data: { id: 'album-123', senderId: authStub.admin.user.id },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not set the thumbnail if the album has one already', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
chore(server): Check asset permissions in bulk (#5329)
Modify Access repository, to evaluate `asset` permissions in bulk.
Queries have been validated to match what they currently generate for single ids.
Queries:
* `asset` album access:
```sql
-- Before
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
LEFT JOIN "albums_shared_users_users" "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."albumsId"="AlbumEntity"."id"
LEFT JOIN "users" "AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity__AlbumEntity_sharedUsers"."id"="AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."usersId"
AND "AlbumEntity__AlbumEntity_sharedUsers"."deletedAt" IS NULL
WHERE
(
("AlbumEntity"."ownerId" = $1 AND "AlbumEntity__AlbumEntity_assets"."id" = $2)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $3 AND "AlbumEntity__AlbumEntity_assets"."id" = $4)
OR ("AlbumEntity"."ownerId" = $5 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $6)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $7 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $8)
)
AND "AlbumEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId",
"asset"."livePhotoVideoId" AS "livePhotoVideoId"
FROM "albums" "album"
INNER JOIN "albums_assets_assets" "album_asset"
ON "album_asset"."albumsId"="album"."id"
INNER JOIN "assets" "asset"
ON "asset"."id"="album_asset"."assetsId"
AND "asset"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "album_sharedUsers"
ON "album_sharedUsers"."albumsId"="album"."id"
LEFT JOIN "users" "sharedUsers"
ON "sharedUsers"."id"="album_sharedUsers"."usersId"
AND "sharedUsers"."deletedAt" IS NULL
WHERE
(
"album"."ownerId" = $1
OR "sharedUsers"."id" = $2
)
AND (
"asset"."id" IN ($3, $4)
OR "asset"."livePhotoVideoId" IN ($5, $6)
)
AND "album"."deletedAt" IS NULL
```
* `asset` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" = $1
AND "AssetEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT
"AssetEntity"."id" AS "AssetEntity_id"
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" IN ($1, $2)
AND "AssetEntity"."ownerId" = $3
```
* `asset` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedWith"
ON "PartnerEntity__PartnerEntity_sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__PartnerEntity_sharedWith"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedBy"
ON "PartnerEntity__PartnerEntity_sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__PartnerEntity_sharedBy"."deletedAt" IS NULL
LEFT JOIN "assets" "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"
ON "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."ownerId"="PartnerEntity__PartnerEntity_sharedBy"."id"
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity__PartnerEntity_sharedWith"."id" = $1
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."id" = $2
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId"
FROM "partners" "partner"
INNER JOIN "users" "sharedBy"
ON "sharedBy"."id"="partner"."sharedById"
AND "sharedBy"."deletedAt" IS NULL
INNER JOIN "assets" "asset"
ON "asset"."ownerId"="sharedBy"."id"
AND "asset"."deletedAt" IS NULL
WHERE
"partner"."sharedWithId" = $1
AND "asset"."id" IN ($2, $3)
```
* `asset` shared link access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "shared_links" "SharedLinkEntity"
LEFT JOIN "albums" "SharedLinkEntity__SharedLinkEntity_album"
ON "SharedLinkEntity__SharedLinkEntity_album"."id"="SharedLinkEntity"."albumId"
AND "SharedLinkEntity__SharedLinkEntity_album"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "760f12c00d97bdcec1ce224d1e3bf449859942b6"
ON "760f12c00d97bdcec1ce224d1e3bf449859942b6"."albumsId"="SharedLinkEntity__SharedLinkEntity_album"."id"
LEFT JOIN "assets" "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"
ON "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id"="760f12c00d97bdcec1ce224d1e3bf449859942b6"."assetsId"
AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"
ON "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."sharedLinksId"="SharedLinkEntity"."id"
LEFT JOIN "assets" "SharedLinkEntity__SharedLinkEntity_assets"
ON "SharedLinkEntity__SharedLinkEntity_assets"."id"="SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."assetsId"
AND "SharedLinkEntity__SharedLinkEntity_assets"."deletedAt" IS NULL
WHERE (
("SharedLinkEntity"."id" = $1 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id" = $2)
OR ("SharedLinkEntity"."id" = $3 AND "SharedLinkEntity__SharedLinkEntity_assets"."id" = $4)
OR ("SharedLinkEntity"."id" = $5 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."livePhotoVideoId" = $6)
OR ("SharedLinkEntity"."id" = $7 AND "SharedLinkEntity__SharedLinkEntity_assets"."livePhotoVideoId" = $8)
)
)
LIMIT 1
-- After
SELECT
"assets"."id" AS "assetId",
"assets"."livePhotoVideoId" AS "assetLivePhotoVideoId",
"albumAssets"."id" AS "albumAssetId",
"albumAssets"."livePhotoVideoId" AS "albumAssetLivePhotoVideoId"
FROM "shared_links" "sharedLink"
LEFT JOIN "albums" "album"
ON "album"."id"="sharedLink"."albumId"
AND "album"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "assets_sharedLink"
ON "assets_sharedLink"."sharedLinksId"="sharedLink"."id"
LEFT JOIN "assets" "assets"
ON "assets"."id"="assets_sharedLink"."assetsId"
AND "assets"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "album_albumAssets"
ON "album_albumAssets"."albumsId"="album"."id"
LEFT JOIN "assets" "albumAssets"
ON "albumAssets"."id"="album_albumAssets"."assetsId"
AND "albumAssets"."deletedAt" IS NULL
WHERE
"sharedLink"."id" = $1
AND (
"assets"."id" IN ($2, $3)
OR "albumAssets"."id" IN ($4, $5)
OR "assets"."livePhotoVideoId" IN ($6, $7)
OR "albumAssets"."livePhotoVideoId" IN ($8, $9)
)
```
2023-12-02 03:56:41 +01:00
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-1']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep({ ...albumStub.empty, albumThumbnailAssetId: 'asset-id' }));
|
fix(server): Check album asset membership in bulk (#4603)
Add `AlbumRepository` method to retrieve an album's asset ids, with an
optional parameter to only filter by the provided asset ids. With this,
we can now check asset membership using a single query.
When adding or removing assets to an album, checking whether each asset
is already present in the album now requires a single query, instead of
one query per asset.
Related to #4539 performance improvements.
Before:
```
// Asset membership and permissions check (2 queries per asset)
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","b666ae6c-afa8-4d6f-a1ad-7091a0659320"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","c656ab1c-7775-4ff7-b56f-01308c072a76"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
After:
```
// Asset membership check (1 query for all assets)
immich_server | query: SELECT "albums_assets"."assetsId" AS "assetId" FROM "albums_assets_assets" "albums_assets" WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2, $3, $4) -- PARAMETERS: ["ca870d76-6311-4e89-bf9a-f5b51ea2452c","b666ae6c-afa8-4d6f-a1ad-7091a0659320","c656ab1c-7775-4ff7-b56f-01308c072a76","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
// Permissions check (1 query per asset)
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
2023-10-23 15:02:27 +02:00
|
|
|
albumMock.getAssetIds.mockResolvedValueOnce(new Set());
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.addAssets(authStub.admin, 'album-123', { ids: ['asset-1'] })).resolves.toEqual([
|
|
|
|
{ success: true, id: 'asset-1' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'album-123',
|
|
|
|
updatedAt: expect.any(Date),
|
|
|
|
albumThumbnailAssetId: 'asset-id',
|
|
|
|
});
|
2024-03-29 12:56:16 +01:00
|
|
|
expect(albumMock.addAssetIds).toHaveBeenCalled();
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow a shared user to add assets', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkSharedAlbumAccess.mockResolvedValue(new Set(['album-123']));
|
chore(server): Check asset permissions in bulk (#5329)
Modify Access repository, to evaluate `asset` permissions in bulk.
Queries have been validated to match what they currently generate for single ids.
Queries:
* `asset` album access:
```sql
-- Before
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
LEFT JOIN "albums_shared_users_users" "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."albumsId"="AlbumEntity"."id"
LEFT JOIN "users" "AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity__AlbumEntity_sharedUsers"."id"="AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."usersId"
AND "AlbumEntity__AlbumEntity_sharedUsers"."deletedAt" IS NULL
WHERE
(
("AlbumEntity"."ownerId" = $1 AND "AlbumEntity__AlbumEntity_assets"."id" = $2)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $3 AND "AlbumEntity__AlbumEntity_assets"."id" = $4)
OR ("AlbumEntity"."ownerId" = $5 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $6)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $7 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $8)
)
AND "AlbumEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId",
"asset"."livePhotoVideoId" AS "livePhotoVideoId"
FROM "albums" "album"
INNER JOIN "albums_assets_assets" "album_asset"
ON "album_asset"."albumsId"="album"."id"
INNER JOIN "assets" "asset"
ON "asset"."id"="album_asset"."assetsId"
AND "asset"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "album_sharedUsers"
ON "album_sharedUsers"."albumsId"="album"."id"
LEFT JOIN "users" "sharedUsers"
ON "sharedUsers"."id"="album_sharedUsers"."usersId"
AND "sharedUsers"."deletedAt" IS NULL
WHERE
(
"album"."ownerId" = $1
OR "sharedUsers"."id" = $2
)
AND (
"asset"."id" IN ($3, $4)
OR "asset"."livePhotoVideoId" IN ($5, $6)
)
AND "album"."deletedAt" IS NULL
```
* `asset` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" = $1
AND "AssetEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT
"AssetEntity"."id" AS "AssetEntity_id"
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" IN ($1, $2)
AND "AssetEntity"."ownerId" = $3
```
* `asset` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedWith"
ON "PartnerEntity__PartnerEntity_sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__PartnerEntity_sharedWith"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedBy"
ON "PartnerEntity__PartnerEntity_sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__PartnerEntity_sharedBy"."deletedAt" IS NULL
LEFT JOIN "assets" "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"
ON "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."ownerId"="PartnerEntity__PartnerEntity_sharedBy"."id"
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity__PartnerEntity_sharedWith"."id" = $1
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."id" = $2
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId"
FROM "partners" "partner"
INNER JOIN "users" "sharedBy"
ON "sharedBy"."id"="partner"."sharedById"
AND "sharedBy"."deletedAt" IS NULL
INNER JOIN "assets" "asset"
ON "asset"."ownerId"="sharedBy"."id"
AND "asset"."deletedAt" IS NULL
WHERE
"partner"."sharedWithId" = $1
AND "asset"."id" IN ($2, $3)
```
* `asset` shared link access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "shared_links" "SharedLinkEntity"
LEFT JOIN "albums" "SharedLinkEntity__SharedLinkEntity_album"
ON "SharedLinkEntity__SharedLinkEntity_album"."id"="SharedLinkEntity"."albumId"
AND "SharedLinkEntity__SharedLinkEntity_album"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "760f12c00d97bdcec1ce224d1e3bf449859942b6"
ON "760f12c00d97bdcec1ce224d1e3bf449859942b6"."albumsId"="SharedLinkEntity__SharedLinkEntity_album"."id"
LEFT JOIN "assets" "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"
ON "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id"="760f12c00d97bdcec1ce224d1e3bf449859942b6"."assetsId"
AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"
ON "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."sharedLinksId"="SharedLinkEntity"."id"
LEFT JOIN "assets" "SharedLinkEntity__SharedLinkEntity_assets"
ON "SharedLinkEntity__SharedLinkEntity_assets"."id"="SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."assetsId"
AND "SharedLinkEntity__SharedLinkEntity_assets"."deletedAt" IS NULL
WHERE (
("SharedLinkEntity"."id" = $1 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id" = $2)
OR ("SharedLinkEntity"."id" = $3 AND "SharedLinkEntity__SharedLinkEntity_assets"."id" = $4)
OR ("SharedLinkEntity"."id" = $5 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."livePhotoVideoId" = $6)
OR ("SharedLinkEntity"."id" = $7 AND "SharedLinkEntity__SharedLinkEntity_assets"."livePhotoVideoId" = $8)
)
)
LIMIT 1
-- After
SELECT
"assets"."id" AS "assetId",
"assets"."livePhotoVideoId" AS "assetLivePhotoVideoId",
"albumAssets"."id" AS "albumAssetId",
"albumAssets"."livePhotoVideoId" AS "albumAssetLivePhotoVideoId"
FROM "shared_links" "sharedLink"
LEFT JOIN "albums" "album"
ON "album"."id"="sharedLink"."albumId"
AND "album"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "assets_sharedLink"
ON "assets_sharedLink"."sharedLinksId"="sharedLink"."id"
LEFT JOIN "assets" "assets"
ON "assets"."id"="assets_sharedLink"."assetsId"
AND "assets"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "album_albumAssets"
ON "album_albumAssets"."albumsId"="album"."id"
LEFT JOIN "assets" "albumAssets"
ON "albumAssets"."id"="album_albumAssets"."assetsId"
AND "albumAssets"."deletedAt" IS NULL
WHERE
"sharedLink"."id" = $1
AND (
"assets"."id" IN ($2, $3)
OR "albumAssets"."id" IN ($4, $5)
OR "assets"."livePhotoVideoId" IN ($6, $7)
OR "albumAssets"."livePhotoVideoId" IN ($8, $9)
)
```
2023-12-02 03:56:41 +01:00
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-1', 'asset-2', 'asset-3']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.sharedWithUser));
|
fix(server): Check album asset membership in bulk (#4603)
Add `AlbumRepository` method to retrieve an album's asset ids, with an
optional parameter to only filter by the provided asset ids. With this,
we can now check asset membership using a single query.
When adding or removing assets to an album, checking whether each asset
is already present in the album now requires a single query, instead of
one query per asset.
Related to #4539 performance improvements.
Before:
```
// Asset membership and permissions check (2 queries per asset)
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","b666ae6c-afa8-4d6f-a1ad-7091a0659320"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","c656ab1c-7775-4ff7-b56f-01308c072a76"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
After:
```
// Asset membership check (1 query for all assets)
immich_server | query: SELECT "albums_assets"."assetsId" AS "assetId" FROM "albums_assets_assets" "albums_assets" WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2, $3, $4) -- PARAMETERS: ["ca870d76-6311-4e89-bf9a-f5b51ea2452c","b666ae6c-afa8-4d6f-a1ad-7091a0659320","c656ab1c-7775-4ff7-b56f-01308c072a76","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
// Permissions check (1 query per asset)
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
2023-10-23 15:02:27 +02:00
|
|
|
albumMock.getAssetIds.mockResolvedValueOnce(new Set());
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.addAssets(authStub.user1, 'album-123', { ids: ['asset-1', 'asset-2', 'asset-3'] }),
|
|
|
|
).resolves.toEqual([
|
|
|
|
{ success: true, id: 'asset-1' },
|
|
|
|
{ success: true, id: 'asset-2' },
|
|
|
|
{ success: true, id: 'asset-3' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'album-123',
|
|
|
|
updatedAt: expect.any(Date),
|
|
|
|
albumThumbnailAssetId: 'asset-1',
|
|
|
|
});
|
2024-03-29 12:56:16 +01:00
|
|
|
expect(albumMock.addAssetIds).toHaveBeenCalledWith('album-123', ['asset-1', 'asset-2', 'asset-3']);
|
2024-05-28 04:16:46 +02:00
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: JobName.NOTIFY_ALBUM_UPDATE,
|
|
|
|
data: { id: 'album-123', senderId: authStub.user1.user.id },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
2024-04-25 06:19:49 +02:00
|
|
|
it('should not allow a shared user with viewer access to add assets', async () => {
|
|
|
|
accessMock.album.checkSharedAlbumAccess.mockResolvedValue(new Set([]));
|
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.sharedWithUser));
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.addAssets(authStub.user2, 'album-123', { ids: ['asset-1', 'asset-2', 'asset-3'] }),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-08-02 03:29:14 +02:00
|
|
|
it('should allow a shared link user to add assets', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkSharedLinkAccess.mockResolvedValue(new Set(['album-123']));
|
chore(server): Check asset permissions in bulk (#5329)
Modify Access repository, to evaluate `asset` permissions in bulk.
Queries have been validated to match what they currently generate for single ids.
Queries:
* `asset` album access:
```sql
-- Before
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
LEFT JOIN "albums_shared_users_users" "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."albumsId"="AlbumEntity"."id"
LEFT JOIN "users" "AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity__AlbumEntity_sharedUsers"."id"="AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."usersId"
AND "AlbumEntity__AlbumEntity_sharedUsers"."deletedAt" IS NULL
WHERE
(
("AlbumEntity"."ownerId" = $1 AND "AlbumEntity__AlbumEntity_assets"."id" = $2)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $3 AND "AlbumEntity__AlbumEntity_assets"."id" = $4)
OR ("AlbumEntity"."ownerId" = $5 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $6)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $7 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $8)
)
AND "AlbumEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId",
"asset"."livePhotoVideoId" AS "livePhotoVideoId"
FROM "albums" "album"
INNER JOIN "albums_assets_assets" "album_asset"
ON "album_asset"."albumsId"="album"."id"
INNER JOIN "assets" "asset"
ON "asset"."id"="album_asset"."assetsId"
AND "asset"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "album_sharedUsers"
ON "album_sharedUsers"."albumsId"="album"."id"
LEFT JOIN "users" "sharedUsers"
ON "sharedUsers"."id"="album_sharedUsers"."usersId"
AND "sharedUsers"."deletedAt" IS NULL
WHERE
(
"album"."ownerId" = $1
OR "sharedUsers"."id" = $2
)
AND (
"asset"."id" IN ($3, $4)
OR "asset"."livePhotoVideoId" IN ($5, $6)
)
AND "album"."deletedAt" IS NULL
```
* `asset` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" = $1
AND "AssetEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT
"AssetEntity"."id" AS "AssetEntity_id"
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" IN ($1, $2)
AND "AssetEntity"."ownerId" = $3
```
* `asset` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedWith"
ON "PartnerEntity__PartnerEntity_sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__PartnerEntity_sharedWith"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedBy"
ON "PartnerEntity__PartnerEntity_sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__PartnerEntity_sharedBy"."deletedAt" IS NULL
LEFT JOIN "assets" "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"
ON "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."ownerId"="PartnerEntity__PartnerEntity_sharedBy"."id"
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity__PartnerEntity_sharedWith"."id" = $1
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."id" = $2
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId"
FROM "partners" "partner"
INNER JOIN "users" "sharedBy"
ON "sharedBy"."id"="partner"."sharedById"
AND "sharedBy"."deletedAt" IS NULL
INNER JOIN "assets" "asset"
ON "asset"."ownerId"="sharedBy"."id"
AND "asset"."deletedAt" IS NULL
WHERE
"partner"."sharedWithId" = $1
AND "asset"."id" IN ($2, $3)
```
* `asset` shared link access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "shared_links" "SharedLinkEntity"
LEFT JOIN "albums" "SharedLinkEntity__SharedLinkEntity_album"
ON "SharedLinkEntity__SharedLinkEntity_album"."id"="SharedLinkEntity"."albumId"
AND "SharedLinkEntity__SharedLinkEntity_album"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "760f12c00d97bdcec1ce224d1e3bf449859942b6"
ON "760f12c00d97bdcec1ce224d1e3bf449859942b6"."albumsId"="SharedLinkEntity__SharedLinkEntity_album"."id"
LEFT JOIN "assets" "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"
ON "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id"="760f12c00d97bdcec1ce224d1e3bf449859942b6"."assetsId"
AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"
ON "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."sharedLinksId"="SharedLinkEntity"."id"
LEFT JOIN "assets" "SharedLinkEntity__SharedLinkEntity_assets"
ON "SharedLinkEntity__SharedLinkEntity_assets"."id"="SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."assetsId"
AND "SharedLinkEntity__SharedLinkEntity_assets"."deletedAt" IS NULL
WHERE (
("SharedLinkEntity"."id" = $1 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id" = $2)
OR ("SharedLinkEntity"."id" = $3 AND "SharedLinkEntity__SharedLinkEntity_assets"."id" = $4)
OR ("SharedLinkEntity"."id" = $5 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."livePhotoVideoId" = $6)
OR ("SharedLinkEntity"."id" = $7 AND "SharedLinkEntity__SharedLinkEntity_assets"."livePhotoVideoId" = $8)
)
)
LIMIT 1
-- After
SELECT
"assets"."id" AS "assetId",
"assets"."livePhotoVideoId" AS "assetLivePhotoVideoId",
"albumAssets"."id" AS "albumAssetId",
"albumAssets"."livePhotoVideoId" AS "albumAssetLivePhotoVideoId"
FROM "shared_links" "sharedLink"
LEFT JOIN "albums" "album"
ON "album"."id"="sharedLink"."albumId"
AND "album"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "assets_sharedLink"
ON "assets_sharedLink"."sharedLinksId"="sharedLink"."id"
LEFT JOIN "assets" "assets"
ON "assets"."id"="assets_sharedLink"."assetsId"
AND "assets"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "album_albumAssets"
ON "album_albumAssets"."albumsId"="album"."id"
LEFT JOIN "assets" "albumAssets"
ON "albumAssets"."id"="album_albumAssets"."assetsId"
AND "albumAssets"."deletedAt" IS NULL
WHERE
"sharedLink"."id" = $1
AND (
"assets"."id" IN ($2, $3)
OR "albumAssets"."id" IN ($4, $5)
OR "assets"."livePhotoVideoId" IN ($6, $7)
OR "albumAssets"."livePhotoVideoId" IN ($8, $9)
)
```
2023-12-02 03:56:41 +01:00
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-1', 'asset-2', 'asset-3']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.oneAsset));
|
fix(server): Check album asset membership in bulk (#4603)
Add `AlbumRepository` method to retrieve an album's asset ids, with an
optional parameter to only filter by the provided asset ids. With this,
we can now check asset membership using a single query.
When adding or removing assets to an album, checking whether each asset
is already present in the album now requires a single query, instead of
one query per asset.
Related to #4539 performance improvements.
Before:
```
// Asset membership and permissions check (2 queries per asset)
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","b666ae6c-afa8-4d6f-a1ad-7091a0659320"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","c656ab1c-7775-4ff7-b56f-01308c072a76"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
After:
```
// Asset membership check (1 query for all assets)
immich_server | query: SELECT "albums_assets"."assetsId" AS "assetId" FROM "albums_assets_assets" "albums_assets" WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2, $3, $4) -- PARAMETERS: ["ca870d76-6311-4e89-bf9a-f5b51ea2452c","b666ae6c-afa8-4d6f-a1ad-7091a0659320","c656ab1c-7775-4ff7-b56f-01308c072a76","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
// Permissions check (1 query per asset)
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
2023-10-23 15:02:27 +02:00
|
|
|
albumMock.getAssetIds.mockResolvedValueOnce(new Set());
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.addAssets(authStub.adminSharedLink, 'album-123', { ids: ['asset-1', 'asset-2', 'asset-3'] }),
|
|
|
|
).resolves.toEqual([
|
|
|
|
{ success: true, id: 'asset-1' },
|
|
|
|
{ success: true, id: 'asset-2' },
|
|
|
|
{ success: true, id: 'asset-3' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'album-123',
|
|
|
|
updatedAt: expect.any(Date),
|
|
|
|
albumThumbnailAssetId: 'asset-1',
|
|
|
|
});
|
2024-03-29 12:56:16 +01:00
|
|
|
expect(albumMock.addAssetIds).toHaveBeenCalledWith('album-123', ['asset-1', 'asset-2', 'asset-3']);
|
2023-08-02 03:29:14 +02:00
|
|
|
|
2023-11-25 23:56:23 +01:00
|
|
|
expect(accessMock.album.checkSharedLinkAccess).toHaveBeenCalledWith(
|
2023-12-10 05:34:12 +01:00
|
|
|
authStub.adminSharedLink.sharedLink?.id,
|
2023-11-25 23:56:23 +01:00
|
|
|
new Set(['album-123']),
|
2023-08-02 03:29:14 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow adding assets shared via partner sharing', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
chore(server): Check asset permissions in bulk (#5329)
Modify Access repository, to evaluate `asset` permissions in bulk.
Queries have been validated to match what they currently generate for single ids.
Queries:
* `asset` album access:
```sql
-- Before
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
LEFT JOIN "albums_shared_users_users" "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."albumsId"="AlbumEntity"."id"
LEFT JOIN "users" "AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity__AlbumEntity_sharedUsers"."id"="AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."usersId"
AND "AlbumEntity__AlbumEntity_sharedUsers"."deletedAt" IS NULL
WHERE
(
("AlbumEntity"."ownerId" = $1 AND "AlbumEntity__AlbumEntity_assets"."id" = $2)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $3 AND "AlbumEntity__AlbumEntity_assets"."id" = $4)
OR ("AlbumEntity"."ownerId" = $5 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $6)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $7 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $8)
)
AND "AlbumEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId",
"asset"."livePhotoVideoId" AS "livePhotoVideoId"
FROM "albums" "album"
INNER JOIN "albums_assets_assets" "album_asset"
ON "album_asset"."albumsId"="album"."id"
INNER JOIN "assets" "asset"
ON "asset"."id"="album_asset"."assetsId"
AND "asset"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "album_sharedUsers"
ON "album_sharedUsers"."albumsId"="album"."id"
LEFT JOIN "users" "sharedUsers"
ON "sharedUsers"."id"="album_sharedUsers"."usersId"
AND "sharedUsers"."deletedAt" IS NULL
WHERE
(
"album"."ownerId" = $1
OR "sharedUsers"."id" = $2
)
AND (
"asset"."id" IN ($3, $4)
OR "asset"."livePhotoVideoId" IN ($5, $6)
)
AND "album"."deletedAt" IS NULL
```
* `asset` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" = $1
AND "AssetEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT
"AssetEntity"."id" AS "AssetEntity_id"
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" IN ($1, $2)
AND "AssetEntity"."ownerId" = $3
```
* `asset` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedWith"
ON "PartnerEntity__PartnerEntity_sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__PartnerEntity_sharedWith"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedBy"
ON "PartnerEntity__PartnerEntity_sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__PartnerEntity_sharedBy"."deletedAt" IS NULL
LEFT JOIN "assets" "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"
ON "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."ownerId"="PartnerEntity__PartnerEntity_sharedBy"."id"
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity__PartnerEntity_sharedWith"."id" = $1
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."id" = $2
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId"
FROM "partners" "partner"
INNER JOIN "users" "sharedBy"
ON "sharedBy"."id"="partner"."sharedById"
AND "sharedBy"."deletedAt" IS NULL
INNER JOIN "assets" "asset"
ON "asset"."ownerId"="sharedBy"."id"
AND "asset"."deletedAt" IS NULL
WHERE
"partner"."sharedWithId" = $1
AND "asset"."id" IN ($2, $3)
```
* `asset` shared link access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "shared_links" "SharedLinkEntity"
LEFT JOIN "albums" "SharedLinkEntity__SharedLinkEntity_album"
ON "SharedLinkEntity__SharedLinkEntity_album"."id"="SharedLinkEntity"."albumId"
AND "SharedLinkEntity__SharedLinkEntity_album"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "760f12c00d97bdcec1ce224d1e3bf449859942b6"
ON "760f12c00d97bdcec1ce224d1e3bf449859942b6"."albumsId"="SharedLinkEntity__SharedLinkEntity_album"."id"
LEFT JOIN "assets" "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"
ON "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id"="760f12c00d97bdcec1ce224d1e3bf449859942b6"."assetsId"
AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"
ON "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."sharedLinksId"="SharedLinkEntity"."id"
LEFT JOIN "assets" "SharedLinkEntity__SharedLinkEntity_assets"
ON "SharedLinkEntity__SharedLinkEntity_assets"."id"="SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."assetsId"
AND "SharedLinkEntity__SharedLinkEntity_assets"."deletedAt" IS NULL
WHERE (
("SharedLinkEntity"."id" = $1 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id" = $2)
OR ("SharedLinkEntity"."id" = $3 AND "SharedLinkEntity__SharedLinkEntity_assets"."id" = $4)
OR ("SharedLinkEntity"."id" = $5 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."livePhotoVideoId" = $6)
OR ("SharedLinkEntity"."id" = $7 AND "SharedLinkEntity__SharedLinkEntity_assets"."livePhotoVideoId" = $8)
)
)
LIMIT 1
-- After
SELECT
"assets"."id" AS "assetId",
"assets"."livePhotoVideoId" AS "assetLivePhotoVideoId",
"albumAssets"."id" AS "albumAssetId",
"albumAssets"."livePhotoVideoId" AS "albumAssetLivePhotoVideoId"
FROM "shared_links" "sharedLink"
LEFT JOIN "albums" "album"
ON "album"."id"="sharedLink"."albumId"
AND "album"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "assets_sharedLink"
ON "assets_sharedLink"."sharedLinksId"="sharedLink"."id"
LEFT JOIN "assets" "assets"
ON "assets"."id"="assets_sharedLink"."assetsId"
AND "assets"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "album_albumAssets"
ON "album_albumAssets"."albumsId"="album"."id"
LEFT JOIN "assets" "albumAssets"
ON "albumAssets"."id"="album_albumAssets"."assetsId"
AND "albumAssets"."deletedAt" IS NULL
WHERE
"sharedLink"."id" = $1
AND (
"assets"."id" IN ($2, $3)
OR "albumAssets"."id" IN ($4, $5)
OR "assets"."livePhotoVideoId" IN ($6, $7)
OR "albumAssets"."livePhotoVideoId" IN ($8, $9)
)
```
2023-12-02 03:56:41 +01:00
|
|
|
accessMock.asset.checkPartnerAccess.mockResolvedValue(new Set(['asset-1']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.oneAsset));
|
fix(server): Check album asset membership in bulk (#4603)
Add `AlbumRepository` method to retrieve an album's asset ids, with an
optional parameter to only filter by the provided asset ids. With this,
we can now check asset membership using a single query.
When adding or removing assets to an album, checking whether each asset
is already present in the album now requires a single query, instead of
one query per asset.
Related to #4539 performance improvements.
Before:
```
// Asset membership and permissions check (2 queries per asset)
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","b666ae6c-afa8-4d6f-a1ad-7091a0659320"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","c656ab1c-7775-4ff7-b56f-01308c072a76"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
After:
```
// Asset membership check (1 query for all assets)
immich_server | query: SELECT "albums_assets"."assetsId" AS "assetId" FROM "albums_assets_assets" "albums_assets" WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2, $3, $4) -- PARAMETERS: ["ca870d76-6311-4e89-bf9a-f5b51ea2452c","b666ae6c-afa8-4d6f-a1ad-7091a0659320","c656ab1c-7775-4ff7-b56f-01308c072a76","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
// Permissions check (1 query per asset)
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
2023-10-23 15:02:27 +02:00
|
|
|
albumMock.getAssetIds.mockResolvedValueOnce(new Set());
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.addAssets(authStub.admin, 'album-123', { ids: ['asset-1'] })).resolves.toEqual([
|
|
|
|
{ success: true, id: 'asset-1' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'album-123',
|
|
|
|
updatedAt: expect.any(Date),
|
|
|
|
albumThumbnailAssetId: 'asset-1',
|
|
|
|
});
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.asset.checkPartnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['asset-1']));
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip duplicate assets', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
chore(server): Check asset permissions in bulk (#5329)
Modify Access repository, to evaluate `asset` permissions in bulk.
Queries have been validated to match what they currently generate for single ids.
Queries:
* `asset` album access:
```sql
-- Before
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
LEFT JOIN "albums_shared_users_users" "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."albumsId"="AlbumEntity"."id"
LEFT JOIN "users" "AlbumEntity__AlbumEntity_sharedUsers"
ON "AlbumEntity__AlbumEntity_sharedUsers"."id"="AlbumEntity_AlbumEntity__AlbumEntity_sharedUsers"."usersId"
AND "AlbumEntity__AlbumEntity_sharedUsers"."deletedAt" IS NULL
WHERE
(
("AlbumEntity"."ownerId" = $1 AND "AlbumEntity__AlbumEntity_assets"."id" = $2)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $3 AND "AlbumEntity__AlbumEntity_assets"."id" = $4)
OR ("AlbumEntity"."ownerId" = $5 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $6)
OR ("AlbumEntity__AlbumEntity_sharedUsers"."id" = $7 AND "AlbumEntity__AlbumEntity_assets"."livePhotoVideoId" = $8)
)
AND "AlbumEntity"."deletedAt" IS NULL
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId",
"asset"."livePhotoVideoId" AS "livePhotoVideoId"
FROM "albums" "album"
INNER JOIN "albums_assets_assets" "album_asset"
ON "album_asset"."albumsId"="album"."id"
INNER JOIN "assets" "asset"
ON "asset"."id"="album_asset"."assetsId"
AND "asset"."deletedAt" IS NULL
LEFT JOIN "albums_shared_users_users" "album_sharedUsers"
ON "album_sharedUsers"."albumsId"="album"."id"
LEFT JOIN "users" "sharedUsers"
ON "sharedUsers"."id"="album_sharedUsers"."usersId"
AND "sharedUsers"."deletedAt" IS NULL
WHERE
(
"album"."ownerId" = $1
OR "sharedUsers"."id" = $2
)
AND (
"asset"."id" IN ($3, $4)
OR "asset"."livePhotoVideoId" IN ($5, $6)
)
AND "album"."deletedAt" IS NULL
```
* `asset` owner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" = $1
AND "AssetEntity"."ownerId" = $2
)
LIMIT 1
-- After
SELECT
"AssetEntity"."id" AS "AssetEntity_id"
FROM "assets" "AssetEntity"
WHERE
"AssetEntity"."id" IN ($1, $2)
AND "AssetEntity"."ownerId" = $3
```
* `asset` partner access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "partners" "PartnerEntity"
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedWith"
ON "PartnerEntity__PartnerEntity_sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__PartnerEntity_sharedWith"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__PartnerEntity_sharedBy"
ON "PartnerEntity__PartnerEntity_sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__PartnerEntity_sharedBy"."deletedAt" IS NULL
LEFT JOIN "assets" "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"
ON "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."ownerId"="PartnerEntity__PartnerEntity_sharedBy"."id"
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedBy"
ON "PartnerEntity__sharedBy"."id"="PartnerEntity"."sharedById"
AND "PartnerEntity__sharedBy"."deletedAt" IS NULL
LEFT JOIN "users" "PartnerEntity__sharedWith"
ON "PartnerEntity__sharedWith"."id"="PartnerEntity"."sharedWithId"
AND "PartnerEntity__sharedWith"."deletedAt" IS NULL
WHERE
"PartnerEntity__PartnerEntity_sharedWith"."id" = $1
AND "0aabe9f4a62b794e2c24a074297e534f51a4ac6c"."id" = $2
)
LIMIT 1
-- After
SELECT
"asset"."id" AS "assetId"
FROM "partners" "partner"
INNER JOIN "users" "sharedBy"
ON "sharedBy"."id"="partner"."sharedById"
AND "sharedBy"."deletedAt" IS NULL
INNER JOIN "assets" "asset"
ON "asset"."ownerId"="sharedBy"."id"
AND "asset"."deletedAt" IS NULL
WHERE
"partner"."sharedWithId" = $1
AND "asset"."id" IN ($2, $3)
```
* `asset` shared link access:
```sql
-- Before
SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (
SELECT 1
FROM "shared_links" "SharedLinkEntity"
LEFT JOIN "albums" "SharedLinkEntity__SharedLinkEntity_album"
ON "SharedLinkEntity__SharedLinkEntity_album"."id"="SharedLinkEntity"."albumId"
AND "SharedLinkEntity__SharedLinkEntity_album"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "760f12c00d97bdcec1ce224d1e3bf449859942b6"
ON "760f12c00d97bdcec1ce224d1e3bf449859942b6"."albumsId"="SharedLinkEntity__SharedLinkEntity_album"."id"
LEFT JOIN "assets" "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"
ON "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id"="760f12c00d97bdcec1ce224d1e3bf449859942b6"."assetsId"
AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"
ON "SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."sharedLinksId"="SharedLinkEntity"."id"
LEFT JOIN "assets" "SharedLinkEntity__SharedLinkEntity_assets"
ON "SharedLinkEntity__SharedLinkEntity_assets"."id"="SharedLinkEntity__SharedLinkEntity_assets_SharedLinkEntity"."assetsId"
AND "SharedLinkEntity__SharedLinkEntity_assets"."deletedAt" IS NULL
WHERE (
("SharedLinkEntity"."id" = $1 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."id" = $2)
OR ("SharedLinkEntity"."id" = $3 AND "SharedLinkEntity__SharedLinkEntity_assets"."id" = $4)
OR ("SharedLinkEntity"."id" = $5 AND "4a35f463ae8c5544ede95c4b6d9ce8c686b6bfe6"."livePhotoVideoId" = $6)
OR ("SharedLinkEntity"."id" = $7 AND "SharedLinkEntity__SharedLinkEntity_assets"."livePhotoVideoId" = $8)
)
)
LIMIT 1
-- After
SELECT
"assets"."id" AS "assetId",
"assets"."livePhotoVideoId" AS "assetLivePhotoVideoId",
"albumAssets"."id" AS "albumAssetId",
"albumAssets"."livePhotoVideoId" AS "albumAssetLivePhotoVideoId"
FROM "shared_links" "sharedLink"
LEFT JOIN "albums" "album"
ON "album"."id"="sharedLink"."albumId"
AND "album"."deletedAt" IS NULL
LEFT JOIN "shared_link__asset" "assets_sharedLink"
ON "assets_sharedLink"."sharedLinksId"="sharedLink"."id"
LEFT JOIN "assets" "assets"
ON "assets"."id"="assets_sharedLink"."assetsId"
AND "assets"."deletedAt" IS NULL
LEFT JOIN "albums_assets_assets" "album_albumAssets"
ON "album_albumAssets"."albumsId"="album"."id"
LEFT JOIN "assets" "albumAssets"
ON "albumAssets"."id"="album_albumAssets"."assetsId"
AND "albumAssets"."deletedAt" IS NULL
WHERE
"sharedLink"."id" = $1
AND (
"assets"."id" IN ($2, $3)
OR "albumAssets"."id" IN ($4, $5)
OR "assets"."livePhotoVideoId" IN ($6, $7)
OR "albumAssets"."livePhotoVideoId" IN ($8, $9)
)
```
2023-12-02 03:56:41 +01:00
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-id']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.oneAsset));
|
fix(server): Check album asset membership in bulk (#4603)
Add `AlbumRepository` method to retrieve an album's asset ids, with an
optional parameter to only filter by the provided asset ids. With this,
we can now check asset membership using a single query.
When adding or removing assets to an album, checking whether each asset
is already present in the album now requires a single query, instead of
one query per asset.
Related to #4539 performance improvements.
Before:
```
// Asset membership and permissions check (2 queries per asset)
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","b666ae6c-afa8-4d6f-a1ad-7091a0659320"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","c656ab1c-7775-4ff7-b56f-01308c072a76"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
After:
```
// Asset membership check (1 query for all assets)
immich_server | query: SELECT "albums_assets"."assetsId" AS "assetId" FROM "albums_assets_assets" "albums_assets" WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2, $3, $4) -- PARAMETERS: ["ca870d76-6311-4e89-bf9a-f5b51ea2452c","b666ae6c-afa8-4d6f-a1ad-7091a0659320","c656ab1c-7775-4ff7-b56f-01308c072a76","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
// Permissions check (1 query per asset)
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
2023-10-23 15:02:27 +02:00
|
|
|
albumMock.getAssetIds.mockResolvedValueOnce(new Set(['asset-id']));
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.addAssets(authStub.admin, 'album-123', { ids: ['asset-id'] })).resolves.toEqual([
|
|
|
|
{ success: false, id: 'asset-id', error: BulkIdErrorReason.DUPLICATE },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip assets not shared with user', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
fix(server): Check album asset membership in bulk (#4603)
Add `AlbumRepository` method to retrieve an album's asset ids, with an
optional parameter to only filter by the provided asset ids. With this,
we can now check asset membership using a single query.
When adding or removing assets to an album, checking whether each asset
is already present in the album now requires a single query, instead of
one query per asset.
Related to #4539 performance improvements.
Before:
```
// Asset membership and permissions check (2 queries per asset)
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","b666ae6c-afa8-4d6f-a1ad-7091a0659320"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","c656ab1c-7775-4ff7-b56f-01308c072a76"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: 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 -- PARAMETERS: ["3fdf0e58-a1c7-4efe-8288-06e4c3f38df9","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
After:
```
// Asset membership check (1 query for all assets)
immich_server | query: SELECT "albums_assets"."assetsId" AS "assetId" FROM "albums_assets_assets" "albums_assets" WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2, $3, $4) -- PARAMETERS: ["ca870d76-6311-4e89-bf9a-f5b51ea2452c","b666ae6c-afa8-4d6f-a1ad-7091a0659320","c656ab1c-7775-4ff7-b56f-01308c072a76","cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9"]
// Permissions check (1 query per asset)
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["b666ae6c-afa8-4d6f-a1ad-7091a0659320","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["c656ab1c-7775-4ff7-b56f-01308c072a76","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
immich_server | query: SELECT 1 AS "row_exists" FROM (SELECT 1 AS dummy_column) "dummy_table" WHERE EXISTS (SELECT 1 FROM "assets" "AssetEntity" WHERE ("AssetEntity"."id" = $1 AND "AssetEntity"."ownerId" = $2)) LIMIT 1 -- PARAMETERS: ["cf82adb2-1fcc-4f9e-9013-8fc03cc8d3a9","6bc60cf1-bd18-4501-a1c2-120b51276fda"]
```
2023-10-23 15:02:27 +02:00
|
|
|
albumMock.getAssetIds.mockResolvedValueOnce(new Set());
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.addAssets(authStub.admin, 'album-123', { ids: ['asset-1'] })).resolves.toEqual([
|
|
|
|
{ success: false, id: 'asset-1', error: BulkIdErrorReason.NO_PERMISSION },
|
|
|
|
]);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(accessMock.asset.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['asset-1']));
|
|
|
|
expect(accessMock.asset.checkPartnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['asset-1']));
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow unauthorized access to the album', async () => {
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.addAssets(authStub.admin, 'album-123', { ids: ['asset-1', 'asset-2', 'asset-3'] }),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
2023-11-25 23:56:23 +01:00
|
|
|
expect(accessMock.album.checkOwnerAccess).toHaveBeenCalled();
|
|
|
|
expect(accessMock.album.checkSharedAlbumAccess).toHaveBeenCalled();
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow unauthorized shared link access to the album', async () => {
|
|
|
|
albumMock.getById.mockResolvedValue(albumStub.oneAsset);
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.addAssets(authStub.adminSharedLink, 'album-123', { ids: ['asset-1', 'asset-2', 'asset-3'] }),
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
2023-11-25 23:56:23 +01:00
|
|
|
expect(accessMock.album.checkSharedLinkAccess).toHaveBeenCalled();
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeAssets', () => {
|
|
|
|
it('should allow the owner to remove assets', async () => {
|
2024-03-29 12:56:16 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-id']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.oneAsset));
|
2024-03-29 12:56:16 +01:00
|
|
|
albumMock.getAssetIds.mockResolvedValue(new Set(['asset-id']));
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.removeAssets(authStub.admin, 'album-123', { ids: ['asset-id'] })).resolves.toEqual([
|
|
|
|
{ success: true, id: 'asset-id' },
|
|
|
|
]);
|
|
|
|
|
2023-10-18 17:56:00 +02:00
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({ id: 'album-123', updatedAt: expect.any(Date) });
|
2024-03-29 12:56:16 +01:00
|
|
|
expect(albumMock.removeAssetIds).toHaveBeenCalledWith('album-123', ['asset-id']);
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip assets not in the album', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.empty));
|
2024-03-29 12:56:16 +01:00
|
|
|
albumMock.getAssetIds.mockResolvedValue(new Set());
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.removeAssets(authStub.admin, 'album-123', { ids: ['asset-id'] })).resolves.toEqual([
|
|
|
|
{ success: false, id: 'asset-id', error: BulkIdErrorReason.NOT_FOUND },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-04-25 06:19:49 +02:00
|
|
|
it('should skip assets when user has remove permission on album but not on asset', async () => {
|
2023-11-25 23:56:23 +01:00
|
|
|
accessMock.album.checkSharedAlbumAccess.mockResolvedValue(new Set(['album-123']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.oneAsset));
|
2024-03-29 12:56:16 +01:00
|
|
|
albumMock.getAssetIds.mockResolvedValue(new Set(['asset-id']));
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.removeAssets(authStub.admin, 'album-123', { ids: ['asset-id'] })).resolves.toEqual([
|
2023-10-18 17:56:00 +02:00
|
|
|
{
|
|
|
|
success: false,
|
|
|
|
id: 'asset-id',
|
|
|
|
error: BulkIdErrorReason.NO_PERMISSION,
|
|
|
|
},
|
2023-08-02 03:29:14 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should reset the thumbnail if it is removed', async () => {
|
2024-03-29 12:56:16 +01:00
|
|
|
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-123']));
|
|
|
|
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset-id']));
|
2023-08-02 03:29:14 +02:00
|
|
|
albumMock.getById.mockResolvedValue(_.cloneDeep(albumStub.twoAssets));
|
2024-03-29 12:56:16 +01:00
|
|
|
albumMock.getAssetIds.mockResolvedValue(new Set(['asset-id']));
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
await expect(sut.removeAssets(authStub.admin, 'album-123', { ids: ['asset-id'] })).resolves.toEqual([
|
|
|
|
{ success: true, id: 'asset-id' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(albumMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'album-123',
|
|
|
|
updatedAt: expect.any(Date),
|
|
|
|
});
|
2023-10-18 17:56:00 +02:00
|
|
|
expect(albumMock.updateThumbnails).toHaveBeenCalled();
|
2023-08-02 03:29:14 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// // it('removes assets from shared album (shared with auth user)', async () => {
|
|
|
|
// // const albumEntity = _getOwnedSharedAlbum();
|
|
|
|
// // albumRepositoryMock.get.mockImplementation(() => Promise.resolve<AlbumEntity>(albumEntity));
|
|
|
|
// // albumRepositoryMock.removeAssets.mockImplementation(() => Promise.resolve<AlbumEntity>(albumEntity));
|
|
|
|
|
|
|
|
// // await expect(
|
|
|
|
// // sut.removeAssetsFromAlbum(
|
2023-12-10 05:34:12 +01:00
|
|
|
// // auth,
|
2023-08-02 03:29:14 +02:00
|
|
|
// // {
|
|
|
|
// // ids: ['1'],
|
|
|
|
// // },
|
|
|
|
// // albumEntity.id,
|
|
|
|
// // ),
|
|
|
|
// // ).resolves.toBeUndefined();
|
|
|
|
// // expect(albumRepositoryMock.removeAssets).toHaveBeenCalledTimes(1);
|
|
|
|
// // expect(albumRepositoryMock.removeAssets).toHaveBeenCalledWith(albumEntity, {
|
|
|
|
// // ids: ['1'],
|
|
|
|
// // });
|
|
|
|
// // });
|
|
|
|
|
|
|
|
// it('prevents removing assets from a not owned / shared album', async () => {
|
|
|
|
// const albumEntity = _getNotOwnedNotSharedAlbum();
|
|
|
|
|
|
|
|
// const albumResponse: AddAssetsResponseDto = {
|
|
|
|
// alreadyInAlbum: [],
|
|
|
|
// successfullyAdded: 1,
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const albumId = albumEntity.id;
|
|
|
|
|
|
|
|
// albumRepositoryMock.get.mockImplementation(() => Promise.resolve<AlbumEntity>(albumEntity));
|
|
|
|
// albumRepositoryMock.addAssets.mockImplementation(() => Promise.resolve<AddAssetsResponseDto>(albumResponse));
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
// await expect(sut.removeAssets(auth, albumId, { ids: ['1'] })).rejects.toBeInstanceOf(ForbiddenException);
|
2023-08-02 03:29:14 +02:00
|
|
|
// });
|
2023-03-26 04:46:48 +02:00
|
|
|
});
|