mirror of
https://github.com/immich-app/immich.git
synced 2026-03-04 09:57:33 +03:00
feat: sync stacks (#19629)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ExpressionBuilder, Kysely, Updateable } from 'kysely';
|
||||
import { ExpressionBuilder, Insertable, Kysely, Updateable } from 'kysely';
|
||||
import { jsonArrayFrom } from 'kysely/helpers/postgres';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { columns } from 'src/database';
|
||||
@@ -55,12 +55,12 @@ export class StackRepository {
|
||||
.execute();
|
||||
}
|
||||
|
||||
async create(entity: { ownerId: string; assetIds: string[] }) {
|
||||
async create(entity: Omit<Insertable<StackTable>, 'primaryAssetId'>, assetIds: string[]) {
|
||||
return this.db.transaction().execute(async (tx) => {
|
||||
const stacks = await tx
|
||||
.selectFrom('asset_stack')
|
||||
.where('asset_stack.ownerId', '=', entity.ownerId)
|
||||
.where('asset_stack.primaryAssetId', 'in', entity.assetIds)
|
||||
.where('asset_stack.primaryAssetId', 'in', assetIds)
|
||||
.select('asset_stack.id')
|
||||
.select((eb) =>
|
||||
jsonArrayFrom(
|
||||
@@ -73,13 +73,13 @@ export class StackRepository {
|
||||
)
|
||||
.execute();
|
||||
|
||||
const assetIds = new Set<string>(entity.assetIds);
|
||||
const uniqueIds = new Set<string>(assetIds);
|
||||
|
||||
// children
|
||||
for (const stack of stacks) {
|
||||
if (stack.assets && stack.assets.length > 0) {
|
||||
for (const asset of stack.assets) {
|
||||
assetIds.add(asset.id);
|
||||
uniqueIds.add(asset.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,10 +97,7 @@ export class StackRepository {
|
||||
|
||||
const newRecord = await tx
|
||||
.insertInto('asset_stack')
|
||||
.values({
|
||||
ownerId: entity.ownerId,
|
||||
primaryAssetId: entity.assetIds[0],
|
||||
})
|
||||
.values({ ...entity, primaryAssetId: assetIds[0] })
|
||||
.returning('id')
|
||||
.executeTakeFirstOrThrow();
|
||||
|
||||
@@ -110,7 +107,7 @@ export class StackRepository {
|
||||
stackId: newRecord.id,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where('id', 'in', [...assetIds])
|
||||
.where('id', 'in', [...uniqueIds])
|
||||
.execute();
|
||||
|
||||
return tx
|
||||
|
||||
@@ -14,7 +14,8 @@ type AuditTables =
|
||||
| 'album_users_audit'
|
||||
| 'album_assets_audit'
|
||||
| 'memories_audit'
|
||||
| 'memory_assets_audit';
|
||||
| 'memory_assets_audit'
|
||||
| 'stacks_audit';
|
||||
type UpsertTables =
|
||||
| 'users'
|
||||
| 'partners'
|
||||
@@ -23,7 +24,8 @@ type UpsertTables =
|
||||
| 'albums'
|
||||
| 'albums_shared_users_users'
|
||||
| 'memories'
|
||||
| 'memories_assets_assets';
|
||||
| 'memories_assets_assets'
|
||||
| 'asset_stack';
|
||||
|
||||
@Injectable()
|
||||
export class SyncRepository {
|
||||
@@ -39,6 +41,7 @@ export class SyncRepository {
|
||||
partner: PartnerSync;
|
||||
partnerAsset: PartnerAssetsSync;
|
||||
partnerAssetExif: PartnerAssetExifsSync;
|
||||
stack: StackSync;
|
||||
user: UserSync;
|
||||
|
||||
constructor(@InjectKysely() private db: Kysely<DB>) {
|
||||
@@ -54,6 +57,7 @@ export class SyncRepository {
|
||||
this.partner = new PartnerSync(this.db);
|
||||
this.partnerAsset = new PartnerAssetsSync(this.db);
|
||||
this.partnerAssetExif = new PartnerAssetExifsSync(this.db);
|
||||
this.stack = new StackSync(this.db);
|
||||
this.user = new UserSync(this.db);
|
||||
}
|
||||
}
|
||||
@@ -533,6 +537,28 @@ class PartnerAssetExifsSync extends BaseSync {
|
||||
}
|
||||
}
|
||||
|
||||
class StackSync extends BaseSync {
|
||||
@GenerateSql({ params: [DummyValue.UUID], stream: true })
|
||||
getDeletes(userId: string, ack?: SyncAck) {
|
||||
return this.db
|
||||
.selectFrom('stacks_audit')
|
||||
.select(['id', 'stackId'])
|
||||
.where('userId', '=', userId)
|
||||
.$call((qb) => this.auditTableFilters(qb, ack))
|
||||
.stream();
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID], stream: true })
|
||||
getUpserts(userId: string, ack?: SyncAck) {
|
||||
return this.db
|
||||
.selectFrom('asset_stack')
|
||||
.select(['id', 'createdAt', 'updatedAt', 'primaryAssetId', 'ownerId', 'updateId'])
|
||||
.where('ownerId', '=', userId)
|
||||
.$call((qb) => this.upsertTableFilters(qb, ack))
|
||||
.stream();
|
||||
}
|
||||
}
|
||||
|
||||
class UserSync extends BaseSync {
|
||||
@GenerateSql({ params: [], stream: true })
|
||||
getDeletes(ack?: SyncAck) {
|
||||
|
||||
Reference in New Issue
Block a user