mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
feat: favorite albums
This commit is contained in:
@@ -76,6 +76,7 @@ export const getDehydrated = <T extends Record<string, unknown>>(entity: T) => {
|
||||
|
||||
export const getForAlbum = (album: ReturnType<AlbumFactory['build']>) => ({
|
||||
...album,
|
||||
isFavorite: false,
|
||||
assets: album.assets.map((asset) =>
|
||||
getDehydrated({ ...getForAsset(asset), exifInfo: getDehydrated(asset.exifInfo) }),
|
||||
),
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
} from 'src/enum';
|
||||
import { AccessRepository } from 'src/repositories/access.repository';
|
||||
import { ActivityRepository } from 'src/repositories/activity.repository';
|
||||
import { AlbumUserMetadataRepository } from 'src/repositories/album-user-metadata.repository';
|
||||
import { AlbumUserRepository } from 'src/repositories/album-user.repository';
|
||||
import { AlbumRepository } from 'src/repositories/album.repository';
|
||||
import { AssetEditRepository } from 'src/repositories/asset-edit.repository';
|
||||
@@ -401,6 +402,7 @@ const newRealRepository = <T>(key: ClassConstructor<T>, db: Kysely<DB>): T => {
|
||||
switch (key) {
|
||||
case AccessRepository:
|
||||
case AlbumRepository:
|
||||
case AlbumUserMetadataRepository:
|
||||
case AlbumUserRepository:
|
||||
case ActivityRepository:
|
||||
case AssetRepository:
|
||||
@@ -465,6 +467,7 @@ const newMockRepository = <T>(key: ClassConstructor<T>) => {
|
||||
switch (key) {
|
||||
case ActivityRepository:
|
||||
case AlbumRepository:
|
||||
case AlbumUserMetadataRepository:
|
||||
case AssetRepository:
|
||||
case AssetJobRepository:
|
||||
case ConfigRepository:
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { AlbumUserMetadataRepository } from 'src/repositories/album-user-metadata.repository';
|
||||
import { AlbumUserRepository } from 'src/repositories/album-user.repository';
|
||||
import { LoggingRepository } from 'src/repositories/logging.repository';
|
||||
import { DB } from 'src/schema';
|
||||
import { BaseService } from 'src/services/base.service';
|
||||
import { newMediumService } from 'test/medium.factory';
|
||||
import { getKyselyDB } from 'test/utils';
|
||||
|
||||
let defaultDatabase: Kysely<DB>;
|
||||
|
||||
const setup = (db?: Kysely<DB>) => {
|
||||
const { ctx } = newMediumService(BaseService, {
|
||||
database: db || defaultDatabase,
|
||||
real: [],
|
||||
mock: [LoggingRepository],
|
||||
});
|
||||
|
||||
return {
|
||||
ctx,
|
||||
sut: ctx.get(AlbumUserMetadataRepository),
|
||||
albumUserRepo: ctx.get(AlbumUserRepository),
|
||||
};
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
defaultDatabase = await getKyselyDB();
|
||||
});
|
||||
|
||||
describe(AlbumUserMetadataRepository.name, () => {
|
||||
it('should create an owner metadata row when an album is created', async () => {
|
||||
const { ctx } = setup();
|
||||
const { user } = await ctx.newUser();
|
||||
const { album } = await ctx.newAlbum({ ownerId: user.id });
|
||||
|
||||
await expect(
|
||||
ctx.database
|
||||
.selectFrom('album_user_metadata')
|
||||
.select(['albumId', 'userId', 'isFavorite'])
|
||||
.where('albumId', '=', album.id)
|
||||
.where('userId', '=', user.id)
|
||||
.executeTakeFirstOrThrow(),
|
||||
).resolves.toEqual({
|
||||
albumId: album.id,
|
||||
userId: user.id,
|
||||
isFavorite: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should create a shared-user metadata row when an album user is added', async () => {
|
||||
const { ctx, albumUserRepo } = setup();
|
||||
const { user: owner } = await ctx.newUser();
|
||||
const { user: sharedUser } = await ctx.newUser();
|
||||
const { album } = await ctx.newAlbum({ ownerId: owner.id });
|
||||
|
||||
await albumUserRepo.create({ albumId: album.id, userId: sharedUser.id });
|
||||
|
||||
await expect(
|
||||
ctx.database
|
||||
.selectFrom('album_user_metadata')
|
||||
.select(['albumId', 'userId', 'isFavorite'])
|
||||
.where('albumId', '=', album.id)
|
||||
.where('userId', '=', sharedUser.id)
|
||||
.executeTakeFirstOrThrow(),
|
||||
).resolves.toEqual({
|
||||
albumId: album.id,
|
||||
userId: sharedUser.id,
|
||||
isFavorite: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should delete metadata and write an audit row when album access is removed', async () => {
|
||||
const { ctx, albumUserRepo, sut } = setup();
|
||||
const { user: owner } = await ctx.newUser();
|
||||
const { user: sharedUser } = await ctx.newUser();
|
||||
const { album } = await ctx.newAlbum({ ownerId: owner.id });
|
||||
|
||||
await albumUserRepo.create({ albumId: album.id, userId: sharedUser.id });
|
||||
await sut.upsert({ albumId: album.id, userId: sharedUser.id, isFavorite: true });
|
||||
|
||||
await albumUserRepo.delete({ albumId: album.id, userId: sharedUser.id });
|
||||
|
||||
await expect(
|
||||
ctx.database
|
||||
.selectFrom('album_user_metadata')
|
||||
.select('albumId')
|
||||
.where('albumId', '=', album.id)
|
||||
.where('userId', '=', sharedUser.id)
|
||||
.executeTakeFirst(),
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
await expect(
|
||||
ctx.database
|
||||
.selectFrom('album_user_metadata_audit')
|
||||
.select(['albumId', 'userId'])
|
||||
.where('albumId', '=', album.id)
|
||||
.where('userId', '=', sharedUser.id)
|
||||
.executeTakeFirstOrThrow(),
|
||||
).resolves.toEqual({
|
||||
albumId: album.id,
|
||||
userId: sharedUser.id,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { SyncEntityType, SyncRequestType } from 'src/enum';
|
||||
import { AlbumUserMetadataRepository } from 'src/repositories/album-user-metadata.repository';
|
||||
import { AlbumUserRepository } from 'src/repositories/album-user.repository';
|
||||
import { DB } from 'src/schema';
|
||||
import { SyncTestContext } from 'test/medium.factory';
|
||||
import { getKyselyDB } from 'test/utils';
|
||||
|
||||
let defaultDatabase: Kysely<DB>;
|
||||
|
||||
const setup = async (db?: Kysely<DB>) => {
|
||||
const ctx = new SyncTestContext(db || defaultDatabase);
|
||||
const { auth, user, session } = await ctx.newSyncAuthUser();
|
||||
return { auth, user, session, ctx };
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
defaultDatabase = await getKyselyDB();
|
||||
});
|
||||
|
||||
describe(SyncEntityType.AlbumUserMetadataV1, () => {
|
||||
it('should sync owner album metadata rows', async () => {
|
||||
const { auth, ctx } = await setup();
|
||||
const { album } = await ctx.newAlbum({ ownerId: auth.user.id });
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.AlbumUserMetadataV1]);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: {
|
||||
albumId: album.id,
|
||||
userId: auth.user.id,
|
||||
isFavorite: false,
|
||||
},
|
||||
type: SyncEntityType.AlbumUserMetadataV1,
|
||||
},
|
||||
expect.objectContaining({ type: SyncEntityType.SyncCompleteV1 }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('should sync favorite updates', async () => {
|
||||
const { auth, ctx } = await setup();
|
||||
const repo = ctx.get(AlbumUserMetadataRepository);
|
||||
const { album } = await ctx.newAlbum({ ownerId: auth.user.id });
|
||||
|
||||
const initial = await ctx.syncStream(auth, [SyncRequestType.AlbumUserMetadataV1]);
|
||||
await ctx.syncAckAll(auth, initial);
|
||||
await ctx.assertSyncIsComplete(auth, [SyncRequestType.AlbumUserMetadataV1]);
|
||||
|
||||
await repo.upsert({ albumId: album.id, userId: auth.user.id, isFavorite: true });
|
||||
|
||||
const updated = await ctx.syncStream(auth, [SyncRequestType.AlbumUserMetadataV1]);
|
||||
expect(updated).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: {
|
||||
albumId: album.id,
|
||||
userId: auth.user.id,
|
||||
isFavorite: true,
|
||||
},
|
||||
type: SyncEntityType.AlbumUserMetadataV1,
|
||||
},
|
||||
expect.objectContaining({ type: SyncEntityType.SyncCompleteV1 }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe(SyncEntityType.AlbumUserMetadataDeleteV1, () => {
|
||||
it('should sync metadata deletes when shared album access is removed', async () => {
|
||||
const { auth, ctx } = await setup();
|
||||
const albumUserRepo = ctx.get(AlbumUserRepository);
|
||||
const { user: owner } = await ctx.newUser();
|
||||
const { album } = await ctx.newAlbum({ ownerId: owner.id });
|
||||
await albumUserRepo.create({ albumId: album.id, userId: auth.user.id });
|
||||
|
||||
const initial = await ctx.syncStream(auth, [SyncRequestType.AlbumUserMetadataV1]);
|
||||
await ctx.syncAckAll(auth, initial);
|
||||
await ctx.assertSyncIsComplete(auth, [SyncRequestType.AlbumUserMetadataV1]);
|
||||
|
||||
await albumUserRepo.delete({ albumId: album.id, userId: auth.user.id });
|
||||
|
||||
const deleted = await ctx.syncStream(auth, [SyncRequestType.AlbumUserMetadataV1]);
|
||||
expect(deleted).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: {
|
||||
albumId: album.id,
|
||||
userId: auth.user.id,
|
||||
},
|
||||
type: SyncEntityType.AlbumUserMetadataDeleteV1,
|
||||
},
|
||||
expect.objectContaining({ type: SyncEntityType.SyncCompleteV1 }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -16,6 +16,7 @@ import { AuthGuard } from 'src/middleware/auth.guard';
|
||||
import { FileUploadInterceptor } from 'src/middleware/file-upload.interceptor';
|
||||
import { AccessRepository } from 'src/repositories/access.repository';
|
||||
import { ActivityRepository } from 'src/repositories/activity.repository';
|
||||
import { AlbumUserMetadataRepository } from 'src/repositories/album-user-metadata.repository';
|
||||
import { AlbumUserRepository } from 'src/repositories/album-user.repository';
|
||||
import { AlbumRepository } from 'src/repositories/album.repository';
|
||||
import { ApiKeyRepository } from 'src/repositories/api-key.repository';
|
||||
@@ -211,6 +212,7 @@ export type ServiceOverrides = {
|
||||
access: AccessRepository;
|
||||
activity: ActivityRepository;
|
||||
album: AlbumRepository;
|
||||
albumUserMetadata: AlbumUserMetadataRepository;
|
||||
albumUser: AlbumUserRepository;
|
||||
apiKey: ApiKeyRepository;
|
||||
app: AppRepository;
|
||||
@@ -296,6 +298,7 @@ export const getMocks = () => {
|
||||
activity: automock(ActivityRepository),
|
||||
audit: automock(AuditRepository),
|
||||
album: automock(AlbumRepository, { strict: false }),
|
||||
albumUserMetadata: automock(AlbumUserMetadataRepository),
|
||||
albumUser: automock(AlbumUserRepository),
|
||||
asset: newAssetRepositoryMock(),
|
||||
assetEdit: automock(AssetEditRepository),
|
||||
@@ -362,6 +365,7 @@ export const newTestService = <T extends BaseService>(
|
||||
overrides.access || (mocks.access as IAccessRepository as AccessRepository),
|
||||
overrides.activity || (mocks.activity as As<ActivityRepository>),
|
||||
overrides.album || (mocks.album as As<AlbumRepository>),
|
||||
overrides.albumUserMetadata || (mocks.albumUserMetadata as As<AlbumUserMetadataRepository>),
|
||||
overrides.albumUser || (mocks.albumUser as As<AlbumUserRepository>),
|
||||
overrides.apiKey || (mocks.apiKey as As<ApiKeyRepository>),
|
||||
overrides.app || (mocks.app as As<AppRepository>),
|
||||
|
||||
Reference in New Issue
Block a user