mirror of
https://github.com/immich-app/immich.git
synced 2026-01-19 00:05:50 -08:00
feat(server,web): add websocket events for album updates
and person face changes
This commit is contained in:
@@ -48,10 +48,15 @@ type EventMap = {
|
||||
'config.validate': [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
|
||||
|
||||
// album events
|
||||
'album.update': [{ id: string; recipientId: string }];
|
||||
'album.update': [
|
||||
{ id: string; recipientId: string[]; assetId: string[]; userId: string; status: 'added' | 'removed' },
|
||||
];
|
||||
'album.invite': [{ id: string; userId: string }];
|
||||
|
||||
// asset events
|
||||
'asset.person': [
|
||||
{ assetId: string; userId: string; personId: string | undefined; status: 'created' | 'removed' | 'removed_soft' },
|
||||
];
|
||||
'asset.tag': [{ assetId: string }];
|
||||
'asset.untag': [{ assetId: string }];
|
||||
'asset.hide': [{ assetId: string; userId: string }];
|
||||
@@ -97,6 +102,8 @@ export type ArgsOf<T extends EmitEvent> = EventMap[T];
|
||||
export interface ClientEventMap {
|
||||
on_upload_success: [AssetResponseDto];
|
||||
on_user_delete: [string];
|
||||
on_album_update: [{ albumId: string; assetId: string[]; status: 'added' | 'removed' }];
|
||||
on_asset_person: [{ assetId: string; personId: string | undefined; status: 'created' | 'removed' | 'removed_soft' }];
|
||||
on_asset_delete: [string];
|
||||
on_asset_trash: [string[]];
|
||||
on_asset_update: [AssetResponseDto];
|
||||
|
||||
@@ -484,6 +484,15 @@ export class PersonRepository {
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID] })
|
||||
async getAssetPersonByFaceId(id: string) {
|
||||
return this.db
|
||||
.selectFrom('asset_faces')
|
||||
.select(['asset_faces.assetId', 'asset_faces.personId'])
|
||||
.where('asset_faces.id', '=', id)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
@GenerateSql()
|
||||
async getLatestFaceDate(): Promise<string | undefined> {
|
||||
const result = (await this.db
|
||||
|
||||
@@ -178,9 +178,13 @@ export class AlbumService extends BaseService {
|
||||
(userId) => userId !== auth.user.id,
|
||||
);
|
||||
|
||||
for (const recipientId of allUsersExceptUs) {
|
||||
await this.eventRepository.emit('album.update', { id, recipientId });
|
||||
}
|
||||
await this.eventRepository.emit('album.update', {
|
||||
id,
|
||||
userId: auth.user.id,
|
||||
assetId: dto.ids,
|
||||
recipientId: allUsersExceptUs,
|
||||
status: 'added',
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
@@ -200,7 +204,16 @@ export class AlbumService extends BaseService {
|
||||
if (removedIds.length > 0 && album.albumThumbnailAssetId && removedIds.includes(album.albumThumbnailAssetId)) {
|
||||
await this.albumRepository.updateThumbnails();
|
||||
}
|
||||
|
||||
const allUsersExceptUs = [...album.albumUsers.map(({ user }) => user.id), album.owner.id].filter(
|
||||
(userId) => userId !== auth.user.id,
|
||||
);
|
||||
await this.eventRepository.emit('album.update', {
|
||||
id,
|
||||
userId: auth.user.id,
|
||||
assetId: dto.ids,
|
||||
recipientId: allUsersExceptUs,
|
||||
status: 'removed',
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
@@ -128,6 +128,11 @@ export class NotificationService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
@OnEvent({ name: 'asset.person' })
|
||||
onAssetPerson({ assetId, userId, personId, status }: ArgOf<'asset.person'>) {
|
||||
this.eventRepository.clientSend('on_asset_person', userId, { assetId, personId, status });
|
||||
}
|
||||
|
||||
@OnEvent({ name: 'asset.hide' })
|
||||
onAssetHide({ assetId, userId }: ArgOf<'asset.hide'>) {
|
||||
this.eventRepository.clientSend('on_asset_hidden', userId, assetId);
|
||||
@@ -198,12 +203,18 @@ export class NotificationService extends BaseService {
|
||||
}
|
||||
|
||||
@OnEvent({ name: 'album.update' })
|
||||
async onAlbumUpdate({ id, recipientId }: ArgOf<'album.update'>) {
|
||||
await this.jobRepository.removeJob(JobName.NOTIFY_ALBUM_UPDATE, `${id}/${recipientId}`);
|
||||
await this.jobRepository.queue({
|
||||
name: JobName.NOTIFY_ALBUM_UPDATE,
|
||||
data: { id, recipientId, delay: NotificationService.albumUpdateEmailDelayMs },
|
||||
});
|
||||
async onAlbumUpdate({ id, recipientId, userId, assetId, status }: ArgOf<'album.update'>) {
|
||||
if (status === 'added') {
|
||||
for (const recipient of recipientId) {
|
||||
await this.jobRepository.removeJob(JobName.NOTIFY_ALBUM_UPDATE, `${id}/${recipientId}`);
|
||||
await this.jobRepository.queue({
|
||||
name: JobName.NOTIFY_ALBUM_UPDATE,
|
||||
data: { id, recipientId: recipient, delay: NotificationService.albumUpdateEmailDelayMs },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
this.eventRepository.clientSend('on_album_update', userId, { albumId: id, assetId, status });
|
||||
}
|
||||
|
||||
@OnEvent({ name: 'album.invite' })
|
||||
|
||||
@@ -627,11 +627,28 @@ export class PersonService extends BaseService {
|
||||
boundingBoxY2: dto.y + dto.height,
|
||||
sourceType: SourceType.MANUAL,
|
||||
});
|
||||
|
||||
await this.eventRepository.emit('asset.person', {
|
||||
assetId: dto.assetId,
|
||||
userId: auth.user.id,
|
||||
personId: dto.personId,
|
||||
status: 'created',
|
||||
});
|
||||
}
|
||||
|
||||
async deleteFace(auth: AuthDto, id: string, dto: AssetFaceDeleteDto): Promise<void> {
|
||||
await this.requireAccess({ auth, permission: Permission.FACE_DELETE, ids: [id] });
|
||||
const assetPerson = await this.personRepository.getAssetPersonByFaceId(id);
|
||||
if (!assetPerson) {
|
||||
throw new NotFoundException('Asset face not found');
|
||||
}
|
||||
|
||||
return dto.force ? this.personRepository.deleteAssetFace(id) : this.personRepository.softDeleteAssetFaces(id);
|
||||
await (dto.force ? this.personRepository.deleteAssetFace(id) : this.personRepository.softDeleteAssetFaces(id));
|
||||
await this.eventRepository.emit('asset.person', {
|
||||
userId: auth.user.id,
|
||||
assetId: assetPerson.assetId,
|
||||
personId: assetPerson.personId ?? undefined,
|
||||
status: dto.force ? 'removed' : 'removed_soft',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user