mirror of
https://github.com/immich-app/immich.git
synced 2025-12-30 01:11:52 +03:00
feat: sync memories (#19579)
This commit is contained in:
@@ -4,9 +4,9 @@ import { DateTime } from 'luxon';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { Writable } from 'node:stream';
|
||||
import { AssetFace } from 'src/database';
|
||||
import { Albums, AssetJobStatus, Assets, DB, Exif, FaceSearch, Person, Sessions } from 'src/db';
|
||||
import { Albums, AssetJobStatus, Assets, DB, Exif, FaceSearch, Memories, Person, Sessions } from 'src/db';
|
||||
import { AuthDto } from 'src/dtos/auth.dto';
|
||||
import { AlbumUserRole, AssetType, AssetVisibility, SourceType, SyncRequestType } from 'src/enum';
|
||||
import { AlbumUserRole, AssetType, AssetVisibility, MemoryType, SourceType, SyncRequestType } from 'src/enum';
|
||||
import { AccessRepository } from 'src/repositories/access.repository';
|
||||
import { ActivityRepository } from 'src/repositories/activity.repository';
|
||||
import { AlbumUserRepository } from 'src/repositories/album-user.repository';
|
||||
@@ -129,6 +129,17 @@ export class MediumTestContext<S extends BaseService = BaseService> {
|
||||
return { asset, result };
|
||||
}
|
||||
|
||||
async newMemory(dto: Partial<Insertable<Memories>> = {}) {
|
||||
const memory = mediumFactory.memoryInsert(dto);
|
||||
const result = await this.get(MemoryRepository).create(memory, new Set<string>());
|
||||
return { memory, result };
|
||||
}
|
||||
|
||||
async newMemoryAsset(dto: { memoryId: string; assetId: string }) {
|
||||
const result = await this.get(MemoryRepository).addAssetIds(dto.memoryId, [dto.assetId]);
|
||||
return { memoryAsset: dto, result };
|
||||
}
|
||||
|
||||
async newExif(dto: Insertable<Exif>) {
|
||||
const result = await this.get(AssetRepository).upsertExif(dto);
|
||||
return { result };
|
||||
@@ -452,6 +463,28 @@ const userInsert = (user: Partial<Insertable<UserTable>> = {}) => {
|
||||
return { ...defaults, ...user, id };
|
||||
};
|
||||
|
||||
const memoryInsert = (memory: Partial<Insertable<Memories>> = {}) => {
|
||||
const id = memory.id || newUuid();
|
||||
const date = newDate();
|
||||
|
||||
const defaults: Insertable<Memories> = {
|
||||
id,
|
||||
createdAt: date,
|
||||
updatedAt: date,
|
||||
deletedAt: null,
|
||||
type: MemoryType.ON_THIS_DAY,
|
||||
data: { year: 2025 },
|
||||
showAt: null,
|
||||
hideAt: null,
|
||||
seenAt: null,
|
||||
isSaved: false,
|
||||
memoryAt: date,
|
||||
ownerId: memory.ownerId || newUuid(),
|
||||
};
|
||||
|
||||
return { ...defaults, ...memory, id };
|
||||
};
|
||||
|
||||
class CustomWritable extends Writable {
|
||||
private data = '';
|
||||
|
||||
@@ -483,4 +516,5 @@ export const mediumFactory = {
|
||||
sessionInsert,
|
||||
syncStream,
|
||||
userInsert,
|
||||
memoryInsert,
|
||||
};
|
||||
|
||||
84
server/test/medium/specs/sync/sync-memory-asset.spec.ts
Normal file
84
server/test/medium/specs/sync/sync-memory-asset.spec.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { DB } from 'src/db';
|
||||
import { SyncEntityType, SyncRequestType } from 'src/enum';
|
||||
import { MemoryRepository } from 'src/repositories/memory.repository';
|
||||
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.MemoryToAssetV1, () => {
|
||||
it('should detect and sync a memory to asset relation', async () => {
|
||||
const { auth, user, ctx } = await setup();
|
||||
const { asset } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { memory } = await ctx.newMemory({ ownerId: user.id });
|
||||
await ctx.newMemoryAsset({ memoryId: memory.id, assetId: asset.id });
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.MemoryToAssetsV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: {
|
||||
memoryId: memory.id,
|
||||
assetId: asset.id,
|
||||
},
|
||||
type: 'MemoryToAssetV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, response);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.MemoryToAssetsV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should detect and sync a deleted memory to asset relation', async () => {
|
||||
const { auth, user, ctx } = await setup();
|
||||
const memoryRepo = ctx.get(MemoryRepository);
|
||||
const { asset } = await ctx.newAsset({ ownerId: user.id });
|
||||
const { memory } = await ctx.newMemory({ ownerId: user.id });
|
||||
await ctx.newMemoryAsset({ memoryId: memory.id, assetId: asset.id });
|
||||
await memoryRepo.removeAssetIds(memory.id, [asset.id]);
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.MemoryToAssetsV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: {
|
||||
assetId: asset.id,
|
||||
memoryId: memory.id,
|
||||
},
|
||||
type: 'MemoryToAssetDeleteV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, response);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.MemoryToAssetsV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should not sync a memory to asset relation or delete for an unrelated user', async () => {
|
||||
const { auth, ctx } = await setup();
|
||||
const memoryRepo = ctx.get(MemoryRepository);
|
||||
const { auth: auth2, user: user2 } = await ctx.newSyncAuthUser();
|
||||
const { asset } = await ctx.newAsset({ ownerId: user2.id });
|
||||
const { memory } = await ctx.newMemory({ ownerId: user2.id });
|
||||
await ctx.newMemoryAsset({ memoryId: memory.id, assetId: asset.id });
|
||||
|
||||
expect(await ctx.syncStream(auth, [SyncRequestType.MemoryToAssetsV1])).toHaveLength(0);
|
||||
expect(await ctx.syncStream(auth2, [SyncRequestType.MemoryToAssetsV1])).toHaveLength(1);
|
||||
|
||||
await memoryRepo.removeAssetIds(memory.id, [asset.id]);
|
||||
expect(await ctx.syncStream(auth, [SyncRequestType.MemoryToAssetsV1])).toHaveLength(0);
|
||||
expect(await ctx.syncStream(auth2, [SyncRequestType.MemoryToAssetsV1])).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
115
server/test/medium/specs/sync/sync-memory.spec.ts
Normal file
115
server/test/medium/specs/sync/sync-memory.spec.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { DB } from 'src/db';
|
||||
import { SyncEntityType, SyncRequestType } from 'src/enum';
|
||||
import { MemoryRepository } from 'src/repositories/memory.repository';
|
||||
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.MemoryV1, () => {
|
||||
it('should detect and sync the first memory with the right properties', async () => {
|
||||
const { auth, user: user1, ctx } = await setup();
|
||||
const { memory } = await ctx.newMemory({ ownerId: user1.id });
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.MemoriesV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: {
|
||||
id: memory.id,
|
||||
createdAt: expect.any(String),
|
||||
updatedAt: expect.any(String),
|
||||
deletedAt: memory.deletedAt,
|
||||
type: memory.type,
|
||||
data: memory.data,
|
||||
hideAt: memory.hideAt,
|
||||
showAt: memory.showAt,
|
||||
seenAt: memory.seenAt,
|
||||
memoryAt: expect.any(String),
|
||||
isSaved: memory.isSaved,
|
||||
ownerId: memory.ownerId,
|
||||
},
|
||||
type: 'MemoryV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, response);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.MemoriesV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should detect and sync a deleted memory', async () => {
|
||||
const { auth, user, ctx } = await setup();
|
||||
const memoryRepo = ctx.get(MemoryRepository);
|
||||
const { memory } = await ctx.newMemory({ ownerId: user.id });
|
||||
await memoryRepo.delete(memory.id);
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.MemoriesV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: {
|
||||
memoryId: memory.id,
|
||||
},
|
||||
type: 'MemoryDeleteV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, response);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.MemoriesV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should sync a memory and then an update to that same memory', async () => {
|
||||
const { auth, user, ctx } = await setup();
|
||||
const memoryRepo = ctx.get(MemoryRepository);
|
||||
const { memory } = await ctx.newMemory({ ownerId: user.id });
|
||||
|
||||
const response = await ctx.syncStream(auth, [SyncRequestType.MemoriesV1]);
|
||||
expect(response).toHaveLength(1);
|
||||
expect(response).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: expect.objectContaining({ id: memory.id }),
|
||||
type: 'MemoryV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, response);
|
||||
await memoryRepo.update(memory.id, { seenAt: new Date() });
|
||||
const newResponse = await ctx.syncStream(auth, [SyncRequestType.MemoriesV1]);
|
||||
expect(newResponse).toHaveLength(1);
|
||||
expect(newResponse).toEqual([
|
||||
{
|
||||
ack: expect.any(String),
|
||||
data: expect.objectContaining({ id: memory.id }),
|
||||
type: 'MemoryV1',
|
||||
},
|
||||
]);
|
||||
|
||||
await ctx.syncAckAll(auth, newResponse);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.MemoriesV1])).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it('should not sync a memory or a memory delete for an unrelated user', async () => {
|
||||
const { auth, ctx } = await setup();
|
||||
const memoryRepo = ctx.get(MemoryRepository);
|
||||
const { user: user2 } = await ctx.newUser();
|
||||
const { memory } = await ctx.newMemory({ ownerId: user2.id });
|
||||
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.MemoriesV1])).resolves.toEqual([]);
|
||||
await memoryRepo.delete(memory.id);
|
||||
await expect(ctx.syncStream(auth, [SyncRequestType.MemoriesV1])).resolves.toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user