mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
feat: asset face sync (#20048)
* chore: remove thumbnailPath from person sync dto * feat: asset face sync
This commit is contained in:
parent
826eaedae6
commit
df318ac641
26 changed files with 699 additions and 20 deletions
|
|
@ -272,6 +272,8 @@ export type AssetFace = {
|
|||
personId: string | null;
|
||||
sourceType: SourceType;
|
||||
person?: Person | null;
|
||||
updatedAt: Date;
|
||||
updateId: string;
|
||||
};
|
||||
|
||||
const userColumns = ['id', 'name', 'email', 'avatarColor', 'profileImagePath', 'profileChangedAt'] as const;
|
||||
|
|
|
|||
|
|
@ -245,7 +245,6 @@ export class SyncPersonV1 {
|
|||
ownerId!: string;
|
||||
name!: string;
|
||||
birthDate!: Date | null;
|
||||
thumbnailPath!: string;
|
||||
isHidden!: boolean;
|
||||
isFavorite!: boolean;
|
||||
color!: string | null;
|
||||
|
|
@ -257,6 +256,25 @@ export class SyncPersonDeleteV1 {
|
|||
personId!: string;
|
||||
}
|
||||
|
||||
@ExtraModel()
|
||||
export class SyncAssetFaceV1 {
|
||||
id!: string;
|
||||
assetId!: string;
|
||||
personId!: string | null;
|
||||
imageWidth!: number;
|
||||
imageHeight!: number;
|
||||
boundingBoxX1!: number;
|
||||
boundingBoxY1!: number;
|
||||
boundingBoxX2!: number;
|
||||
boundingBoxY2!: number;
|
||||
sourceType!: string;
|
||||
}
|
||||
|
||||
@ExtraModel()
|
||||
export class SyncAssetFaceDeleteV1 {
|
||||
assetFaceId!: string;
|
||||
}
|
||||
|
||||
@ExtraModel()
|
||||
export class SyncUserMetadataV1 {
|
||||
userId!: string;
|
||||
|
|
@ -312,6 +330,8 @@ export type SyncItem = {
|
|||
[SyncEntityType.PartnerStackV1]: SyncStackV1;
|
||||
[SyncEntityType.PersonV1]: SyncPersonV1;
|
||||
[SyncEntityType.PersonDeleteV1]: SyncPersonDeleteV1;
|
||||
[SyncEntityType.AssetFaceV1]: SyncAssetFaceV1;
|
||||
[SyncEntityType.AssetFaceDeleteV1]: SyncAssetFaceDeleteV1;
|
||||
[SyncEntityType.UserMetadataV1]: SyncUserMetadataV1;
|
||||
[SyncEntityType.UserMetadataDeleteV1]: SyncUserMetadataDeleteV1;
|
||||
[SyncEntityType.SyncAckV1]: SyncAckV1;
|
||||
|
|
|
|||
|
|
@ -568,6 +568,7 @@ export enum SyncRequestType {
|
|||
StacksV1 = 'StacksV1',
|
||||
UsersV1 = 'UsersV1',
|
||||
PeopleV1 = 'PeopleV1',
|
||||
AssetFacesV1 = 'AssetFacesV1',
|
||||
UserMetadataV1 = 'UserMetadataV1',
|
||||
}
|
||||
|
||||
|
|
@ -619,6 +620,9 @@ export enum SyncEntityType {
|
|||
PersonV1 = 'PersonV1',
|
||||
PersonDeleteV1 = 'PersonDeleteV1',
|
||||
|
||||
AssetFaceV1 = 'AssetFaceV1',
|
||||
AssetFaceDeleteV1 = 'AssetFaceDeleteV1',
|
||||
|
||||
UserMetadataV1 = 'UserMetadataV1',
|
||||
UserMetadataDeleteV1 = 'UserMetadataDeleteV1',
|
||||
|
||||
|
|
|
|||
|
|
@ -409,6 +409,41 @@ where
|
|||
order by
|
||||
"updateId" asc
|
||||
|
||||
-- SyncRepository.assetFace.getDeletes
|
||||
select
|
||||
"asset_face_audit"."id",
|
||||
"assetFaceId"
|
||||
from
|
||||
"asset_face_audit"
|
||||
left join "asset" on "asset"."id" = "asset_face_audit"."assetId"
|
||||
where
|
||||
"asset"."ownerId" = $1
|
||||
and "asset_face_audit"."deletedAt" < now() - interval '1 millisecond'
|
||||
order by
|
||||
"asset_face_audit"."id" asc
|
||||
|
||||
-- SyncRepository.assetFace.getUpserts
|
||||
select
|
||||
"asset_face"."id",
|
||||
"assetId",
|
||||
"personId",
|
||||
"imageWidth",
|
||||
"imageHeight",
|
||||
"boundingBoxX1",
|
||||
"boundingBoxY1",
|
||||
"boundingBoxX2",
|
||||
"boundingBoxY2",
|
||||
"sourceType",
|
||||
"asset_face"."updateId"
|
||||
from
|
||||
"asset_face"
|
||||
left join "asset" on "asset"."id" = "asset_face"."assetId"
|
||||
where
|
||||
"asset_face"."updatedAt" < now() - interval '1 millisecond'
|
||||
and "asset"."ownerId" = $1
|
||||
order by
|
||||
"asset_face"."updateId" asc
|
||||
|
||||
-- SyncRepository.memory.getDeletes
|
||||
select
|
||||
"id",
|
||||
|
|
@ -779,7 +814,6 @@ select
|
|||
"ownerId",
|
||||
"name",
|
||||
"birthDate",
|
||||
"thumbnailPath",
|
||||
"isHidden",
|
||||
"isFavorite",
|
||||
"color",
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ type AuditTables =
|
|||
| 'memory_asset_audit'
|
||||
| 'stack_audit'
|
||||
| 'person_audit'
|
||||
| 'user_metadata_audit';
|
||||
| 'user_metadata_audit'
|
||||
| 'asset_face_audit';
|
||||
type UpsertTables =
|
||||
| 'user'
|
||||
| 'partner'
|
||||
|
|
@ -29,7 +30,8 @@ type UpsertTables =
|
|||
| 'memory_asset'
|
||||
| 'stack'
|
||||
| 'person'
|
||||
| 'user_metadata';
|
||||
| 'user_metadata'
|
||||
| 'asset_face';
|
||||
|
||||
@Injectable()
|
||||
export class SyncRepository {
|
||||
|
|
@ -40,6 +42,7 @@ export class SyncRepository {
|
|||
albumUser: AlbumUserSync;
|
||||
asset: AssetSync;
|
||||
assetExif: AssetExifSync;
|
||||
assetFace: AssetFaceSync;
|
||||
memory: MemorySync;
|
||||
memoryToAsset: MemoryToAssetSync;
|
||||
partner: PartnerSync;
|
||||
|
|
@ -59,6 +62,7 @@ export class SyncRepository {
|
|||
this.albumUser = new AlbumUserSync(this.db);
|
||||
this.asset = new AssetSync(this.db);
|
||||
this.assetExif = new AssetExifSync(this.db);
|
||||
this.assetFace = new AssetFaceSync(this.db);
|
||||
this.memory = new MemorySync(this.db);
|
||||
this.memoryToAsset = new MemoryToAssetSync(this.db);
|
||||
this.partner = new PartnerSync(this.db);
|
||||
|
|
@ -385,7 +389,6 @@ class PersonSync extends BaseSync {
|
|||
'ownerId',
|
||||
'name',
|
||||
'birthDate',
|
||||
'thumbnailPath',
|
||||
'isHidden',
|
||||
'isFavorite',
|
||||
'color',
|
||||
|
|
@ -398,6 +401,46 @@ class PersonSync extends BaseSync {
|
|||
}
|
||||
}
|
||||
|
||||
class AssetFaceSync extends BaseSync {
|
||||
@GenerateSql({ params: [DummyValue.UUID], stream: true })
|
||||
getDeletes(userId: string, ack?: SyncAck) {
|
||||
return this.db
|
||||
.selectFrom('asset_face_audit')
|
||||
.select(['asset_face_audit.id', 'assetFaceId'])
|
||||
.orderBy('asset_face_audit.id', 'asc')
|
||||
.leftJoin('asset', 'asset.id', 'asset_face_audit.assetId')
|
||||
.where('asset.ownerId', '=', userId)
|
||||
.where('asset_face_audit.deletedAt', '<', sql.raw<Date>("now() - interval '1 millisecond'"))
|
||||
.$if(!!ack, (qb) => qb.where('asset_face_audit.id', '>', ack!.updateId))
|
||||
.stream();
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID], stream: true })
|
||||
getUpserts(userId: string, ack?: SyncAck) {
|
||||
return this.db
|
||||
.selectFrom('asset_face')
|
||||
.select([
|
||||
'asset_face.id',
|
||||
'assetId',
|
||||
'personId',
|
||||
'imageWidth',
|
||||
'imageHeight',
|
||||
'boundingBoxX1',
|
||||
'boundingBoxY1',
|
||||
'boundingBoxX2',
|
||||
'boundingBoxY2',
|
||||
'sourceType',
|
||||
'asset_face.updateId',
|
||||
])
|
||||
.where('asset_face.updatedAt', '<', sql.raw<Date>("now() - interval '1 millisecond'"))
|
||||
.$if(!!ack, (qb) => qb.where('asset_face.updateId', '>', ack!.updateId))
|
||||
.orderBy('asset_face.updateId', 'asc')
|
||||
.leftJoin('asset', 'asset.id', 'asset_face.assetId')
|
||||
.where('asset.ownerId', '=', userId)
|
||||
.stream();
|
||||
}
|
||||
}
|
||||
|
||||
class AssetExifSync extends BaseSync {
|
||||
@GenerateSql({ params: [DummyValue.UUID], stream: true })
|
||||
getUpserts(userId: string, ack?: SyncAck) {
|
||||
|
|
|
|||
|
|
@ -229,3 +229,16 @@ export const user_metadata_audit = registerFunction({
|
|||
RETURN NULL;
|
||||
END`,
|
||||
});
|
||||
|
||||
export const asset_face_audit = registerFunction({
|
||||
name: 'asset_face_audit',
|
||||
returnType: 'TRIGGER',
|
||||
language: 'PLPGSQL',
|
||||
body: `
|
||||
BEGIN
|
||||
INSERT INTO asset_face_audit ("assetFaceId", "assetId")
|
||||
SELECT "id", "assetId"
|
||||
FROM OLD;
|
||||
RETURN NULL;
|
||||
END`,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import {
|
|||
album_user_after_insert,
|
||||
album_user_delete_audit,
|
||||
asset_delete_audit,
|
||||
asset_face_audit,
|
||||
f_concat_ws,
|
||||
f_unaccent,
|
||||
immich_uuid_v7,
|
||||
|
|
@ -27,6 +28,7 @@ import { AlbumTable } from 'src/schema/tables/album.table';
|
|||
import { ApiKeyTable } from 'src/schema/tables/api-key.table';
|
||||
import { AssetAuditTable } from 'src/schema/tables/asset-audit.table';
|
||||
import { AssetExifTable } from 'src/schema/tables/asset-exif.table';
|
||||
import { AssetFaceAuditTable } from 'src/schema/tables/asset-face-audit.table';
|
||||
import { AssetFaceTable } from 'src/schema/tables/asset-face.table';
|
||||
import { AssetFileTable } from 'src/schema/tables/asset-file.table';
|
||||
import { AssetJobStatusTable } from 'src/schema/tables/asset-job-status.table';
|
||||
|
|
@ -78,6 +80,7 @@ export class ImmichDatabase {
|
|||
ApiKeyTable,
|
||||
AssetAuditTable,
|
||||
AssetFaceTable,
|
||||
AssetFaceAuditTable,
|
||||
AssetJobStatusTable,
|
||||
AssetTable,
|
||||
AssetFileTable,
|
||||
|
|
@ -132,6 +135,7 @@ export class ImmichDatabase {
|
|||
stack_delete_audit,
|
||||
person_delete_audit,
|
||||
user_metadata_audit,
|
||||
asset_face_audit,
|
||||
];
|
||||
|
||||
enum = [assets_status_enum, asset_face_source_type, asset_visibility_enum];
|
||||
|
|
@ -158,6 +162,7 @@ export interface DB {
|
|||
asset: AssetTable;
|
||||
asset_exif: AssetExifTable;
|
||||
asset_face: AssetFaceTable;
|
||||
asset_face_audit: AssetFaceAuditTable;
|
||||
asset_file: AssetFileTable;
|
||||
asset_job_status: AssetJobStatusTable;
|
||||
asset_audit: AssetAuditTable;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
import { Kysely, sql } from 'kysely';
|
||||
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await sql`CREATE OR REPLACE FUNCTION asset_face_audit()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE PLPGSQL
|
||||
AS $$
|
||||
BEGIN
|
||||
INSERT INTO asset_face_audit ("assetFaceId", "assetId")
|
||||
SELECT "id", "assetId"
|
||||
FROM OLD;
|
||||
RETURN NULL;
|
||||
END
|
||||
$$;`.execute(db);
|
||||
await sql`CREATE TABLE "asset_face_audit" (
|
||||
"id" uuid NOT NULL DEFAULT immich_uuid_v7(),
|
||||
"assetFaceId" uuid NOT NULL,
|
||||
"assetId" uuid NOT NULL,
|
||||
"deletedAt" timestamp with time zone NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT "asset_face_audit_pkey" PRIMARY KEY ("id")
|
||||
);`.execute(db);
|
||||
await sql`CREATE INDEX "asset_face_audit_assetFaceId_idx" ON "asset_face_audit" ("assetFaceId");`.execute(db);
|
||||
await sql`CREATE INDEX "asset_face_audit_assetId_idx" ON "asset_face_audit" ("assetId");`.execute(db);
|
||||
await sql`CREATE INDEX "asset_face_audit_deletedAt_idx" ON "asset_face_audit" ("deletedAt");`.execute(db);
|
||||
await sql`ALTER TABLE "asset_face" ADD "updatedAt" timestamp with time zone NOT NULL DEFAULT now();`.execute(db);
|
||||
await sql`ALTER TABLE "asset_face" ADD "updateId" uuid NOT NULL DEFAULT immich_uuid_v7();`.execute(db);
|
||||
await sql`CREATE OR REPLACE TRIGGER "asset_face_audit"
|
||||
AFTER DELETE ON "asset_face"
|
||||
REFERENCING OLD TABLE AS "old"
|
||||
FOR EACH STATEMENT
|
||||
WHEN (pg_trigger_depth() = 0)
|
||||
EXECUTE FUNCTION asset_face_audit();`.execute(db);
|
||||
await sql`CREATE OR REPLACE TRIGGER "asset_face_updatedAt"
|
||||
BEFORE UPDATE ON "asset_face"
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION updated_at();`.execute(db);
|
||||
await sql`INSERT INTO "migration_overrides" ("name", "value") VALUES ('function_asset_face_audit', '{"type":"function","name":"asset_face_audit","sql":"CREATE OR REPLACE FUNCTION asset_face_audit()\\n RETURNS TRIGGER\\n LANGUAGE PLPGSQL\\n AS $$\\n BEGIN\\n INSERT INTO asset_face_audit (\\"assetFaceId\\", \\"assetId\\")\\n SELECT \\"id\\", \\"assetId\\"\\n FROM OLD;\\n RETURN NULL;\\n END\\n $$;"}'::jsonb);`.execute(db);
|
||||
await sql`INSERT INTO "migration_overrides" ("name", "value") VALUES ('trigger_asset_face_audit', '{"type":"trigger","name":"asset_face_audit","sql":"CREATE OR REPLACE TRIGGER \\"asset_face_audit\\"\\n AFTER DELETE ON \\"asset_face\\"\\n REFERENCING OLD TABLE AS \\"old\\"\\n FOR EACH STATEMENT\\n WHEN (pg_trigger_depth() = 0)\\n EXECUTE FUNCTION asset_face_audit();"}'::jsonb);`.execute(db);
|
||||
await sql`INSERT INTO "migration_overrides" ("name", "value") VALUES ('trigger_asset_face_updatedAt', '{"type":"trigger","name":"asset_face_updatedAt","sql":"CREATE OR REPLACE TRIGGER \\"asset_face_updatedAt\\"\\n BEFORE UPDATE ON \\"asset_face\\"\\n FOR EACH ROW\\n EXECUTE FUNCTION updated_at();"}'::jsonb);`.execute(db);
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await sql`DROP TRIGGER "asset_face_audit" ON "asset_face";`.execute(db);
|
||||
await sql`DROP TRIGGER "asset_face_updatedAt" ON "asset_face";`.execute(db);
|
||||
await sql`ALTER TABLE "asset_face" DROP COLUMN "updatedAt";`.execute(db);
|
||||
await sql`ALTER TABLE "asset_face" DROP COLUMN "updateId";`.execute(db);
|
||||
await sql`DROP TABLE "asset_face_audit";`.execute(db);
|
||||
await sql`DROP FUNCTION asset_face_audit;`.execute(db);
|
||||
await sql`DELETE FROM "migration_overrides" WHERE "name" = 'function_asset_face_audit';`.execute(db);
|
||||
await sql`DELETE FROM "migration_overrides" WHERE "name" = 'trigger_asset_face_audit';`.execute(db);
|
||||
await sql`DELETE FROM "migration_overrides" WHERE "name" = 'trigger_asset_face_updatedAt';`.execute(db);
|
||||
}
|
||||
17
server/src/schema/tables/asset-face-audit.table.ts
Normal file
17
server/src/schema/tables/asset-face-audit.table.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { PrimaryGeneratedUuidV7Column } from 'src/decorators';
|
||||
import { Column, CreateDateColumn, Generated, Table, Timestamp } from 'src/sql-tools';
|
||||
|
||||
@Table('asset_face_audit')
|
||||
export class AssetFaceAuditTable {
|
||||
@PrimaryGeneratedUuidV7Column()
|
||||
id!: Generated<string>;
|
||||
|
||||
@Column({ type: 'uuid', index: true })
|
||||
assetFaceId!: string;
|
||||
|
||||
@Column({ type: 'uuid', index: true })
|
||||
assetId!: string;
|
||||
|
||||
@CreateDateColumn({ default: () => 'clock_timestamp()', index: true })
|
||||
deletedAt!: Generated<Timestamp>;
|
||||
}
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
import { UpdatedAtTrigger, UpdateIdColumn } from 'src/decorators';
|
||||
import { SourceType } from 'src/enum';
|
||||
import { asset_face_source_type } from 'src/schema/enums';
|
||||
import { asset_face_audit } from 'src/schema/functions';
|
||||
import { AssetTable } from 'src/schema/tables/asset.table';
|
||||
import { PersonTable } from 'src/schema/tables/person.table';
|
||||
import {
|
||||
AfterDeleteTrigger,
|
||||
Column,
|
||||
DeleteDateColumn,
|
||||
ForeignKeyColumn,
|
||||
|
|
@ -11,9 +14,17 @@ import {
|
|||
PrimaryGeneratedColumn,
|
||||
Table,
|
||||
Timestamp,
|
||||
UpdateDateColumn,
|
||||
} from 'src/sql-tools';
|
||||
|
||||
@Table({ name: 'asset_face' })
|
||||
@UpdatedAtTrigger('asset_face_updatedAt')
|
||||
@AfterDeleteTrigger({
|
||||
scope: 'statement',
|
||||
function: asset_face_audit,
|
||||
referencingOldTableAs: 'old',
|
||||
when: 'pg_trigger_depth() = 0',
|
||||
})
|
||||
// schemaFromDatabase does not preserve column order
|
||||
@Index({ name: 'asset_face_assetId_personId_idx', columns: ['assetId', 'personId'] })
|
||||
@Index({ columns: ['personId', 'assetId'] })
|
||||
|
|
@ -61,4 +72,10 @@ export class AssetFaceTable {
|
|||
|
||||
@DeleteDateColumn()
|
||||
deletedAt!: Timestamp | null;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt!: Generated<Timestamp>;
|
||||
|
||||
@UpdateIdColumn()
|
||||
updateId!: Generated<string>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ export const SYNC_TYPES_ORDER = [
|
|||
SyncRequestType.MemoriesV1,
|
||||
SyncRequestType.MemoryToAssetsV1,
|
||||
SyncRequestType.PeopleV1,
|
||||
SyncRequestType.AssetFacesV1,
|
||||
SyncRequestType.UserMetadataV1,
|
||||
];
|
||||
|
||||
|
|
@ -156,6 +157,7 @@ export class SyncService extends BaseService {
|
|||
[SyncRequestType.StacksV1]: () => this.syncStackV1(response, checkpointMap, auth),
|
||||
[SyncRequestType.PartnerStacksV1]: () => this.syncPartnerStackV1(response, checkpointMap, auth, session.id),
|
||||
[SyncRequestType.PeopleV1]: () => this.syncPeopleV1(response, checkpointMap, auth),
|
||||
[SyncRequestType.AssetFacesV1]: async () => this.syncAssetFacesV1(response, checkpointMap, auth),
|
||||
[SyncRequestType.UserMetadataV1]: () => this.syncUserMetadataV1(response, checkpointMap, auth),
|
||||
};
|
||||
|
||||
|
|
@ -606,6 +608,20 @@ export class SyncService extends BaseService {
|
|||
}
|
||||
}
|
||||
|
||||
private async syncAssetFacesV1(response: Writable, checkpointMap: CheckpointMap, auth: AuthDto) {
|
||||
const deleteType = SyncEntityType.AssetFaceDeleteV1;
|
||||
const deletes = this.syncRepository.assetFace.getDeletes(auth.user.id, checkpointMap[deleteType]);
|
||||
for await (const { id, ...data } of deletes) {
|
||||
send(response, { type: deleteType, ids: [id], data });
|
||||
}
|
||||
|
||||
const upsertType = SyncEntityType.AssetFaceV1;
|
||||
const upserts = this.syncRepository.assetFace.getUpserts(auth.user.id, checkpointMap[upsertType]);
|
||||
for await (const { updateId, ...data } of upserts) {
|
||||
send(response, { type: upsertType, ids: [updateId], data });
|
||||
}
|
||||
}
|
||||
|
||||
private async syncUserMetadataV1(response: Writable, checkpointMap: CheckpointMap, auth: AuthDto) {
|
||||
const deleteType = SyncEntityType.UserMetadataDeleteV1;
|
||||
const deletes = this.syncRepository.userMetadata.getDeletes(auth.user.id, checkpointMap[deleteType]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue