Updated recent albums updates

This commit is contained in:
Pawel Wojtaszko 2025-08-13 15:23:04 +00:00
parent 7215e59809
commit e3d49bfa86
7 changed files with 110 additions and 35 deletions

View file

@ -49,7 +49,7 @@ type EventMap = {
ConfigValidate: [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
// album events
AlbumUpdate: [{ id: string; recipientId: string }];
AlbumUpdate: [{ id: string; userId: string; notifyRecipients?: boolean }];
AlbumInvite: [{ id: string; userId: string }];
AlbumDelete: [{ id: string; userId: string }];
AlbumCreate: [{ id: string; userId: string }];
@ -114,6 +114,7 @@ export interface ClientEventMap {
on_session_delete: [string];
on_album_delete: [string];
on_album_create: [string];
on_album_update: [string];
AssetUploadReadyV1: [{ asset: SyncAssetV1; exif: SyncAssetExifV1 }];
}

View file

@ -150,6 +150,12 @@ export class AlbumService extends BaseService {
order: dto.order,
});
// Emit AlbumUpdate event with notifyRecipients flag for notification service to handle recipient lookup
await this.eventRepository.emit('AlbumUpdate', {
id: album.id,
userId: auth.user.id,
});
return mapAlbumWithoutAssets({ ...updatedAlbum, assets: album.assets });
}
@ -177,13 +183,12 @@ export class AlbumService extends BaseService {
albumThumbnailAssetId: album.albumThumbnailAssetId ?? firstNewAssetId,
});
const allUsersExceptUs = [...album.albumUsers.map(({ user }) => user.id), album.owner.id].filter(
(userId) => userId !== auth.user.id,
);
for (const recipientId of allUsersExceptUs) {
await this.eventRepository.emit('AlbumUpdate', { id, recipientId });
}
// Emit AlbumUpdate event with notifyRecipients flag for notification service to handle recipient lookup
await this.eventRepository.emit('AlbumUpdate', {
id,
userId: auth.user.id,
notifyRecipients: true,
});
}
return results;
@ -204,6 +209,15 @@ export class AlbumService extends BaseService {
await this.albumRepository.updateThumbnails();
}
// Emit AlbumUpdate event if any assets were successfully removed
if (removedIds.length > 0) {
await this.eventRepository.emit('AlbumUpdate', {
id,
userId: auth.user.id,
notifyRecipients: false,
});
}
return results;
}
@ -231,6 +245,12 @@ export class AlbumService extends BaseService {
await this.eventRepository.emit('AlbumInvite', { id, userId });
}
// Emit AlbumUpdate event to notify all album members about new users being added
await this.eventRepository.emit('AlbumUpdate', {
id,
userId: auth.user.id,
});
return this.findOrFail(id, { withAssets: true }).then(mapAlbumWithoutAssets);
}
@ -256,11 +276,23 @@ export class AlbumService extends BaseService {
}
await this.albumUserRepository.delete({ albumsId: id, usersId: userId });
// Emit AlbumUpdate event to notify remaining album members about user removal
await this.eventRepository.emit('AlbumUpdate', {
id,
userId: auth.user.id,
});
}
async updateUser(auth: AuthDto, id: string, userId: string, dto: UpdateAlbumUserDto): Promise<void> {
await this.requireAccess({ auth, permission: Permission.AlbumShare, ids: [id] });
await this.albumUserRepository.update({ albumsId: id, usersId: userId }, { role: dto.role });
// Emit AlbumUpdate event to notify all album members about role changes
await this.eventRepository.emit('AlbumUpdate', {
id,
userId: auth.user.id,
});
}
private async findOrFail(id: string, options: AlbumInfoOptions) {

View file

@ -198,12 +198,35 @@ export class NotificationService extends BaseService {
}
@OnEvent({ name: 'AlbumUpdate' })
async onAlbumUpdate({ id, recipientId }: ArgOf<'AlbumUpdate'>) {
await this.jobRepository.removeJob(JobName.NotifyAlbumUpdate, `${id}/${recipientId}`);
await this.jobRepository.queue({
name: JobName.NotifyAlbumUpdate,
data: { id, recipientId, delay: NotificationService.albumUpdateEmailDelayMs },
});
async onAlbumUpdate({ id, userId, notifyRecipients }: ArgOf<'AlbumUpdate'>) {
if (notifyRecipients) {
// Fetch album with users to get recipient list
const album = await this.albumRepository.getById(id, { withAssets: false });
if (!album) {
this.logger.warn(`Album ${id} not found for update notification`);
return;
}
// Get all users except the one who made the update
const allRecipients = [...album.albumUsers.map(({ user }) => user.id), album.owner.id].filter(
(recipientUserId) => recipientUserId !== userId,
);
// Send notifications and websocket events to all recipients
for (const recipient of allRecipients) {
await this.jobRepository.removeJob(JobName.NotifyAlbumUpdate, `${id}/${recipient}`);
await this.jobRepository.queue({
name: JobName.NotifyAlbumUpdate,
data: { id, recipientId: recipient, delay: NotificationService.albumUpdateEmailDelayMs },
});
// Send websocket event to the recipient
this.eventRepository.clientSend('on_album_update', recipient, id);
}
}
// Always send websocket event to the user who made the update
this.eventRepository.clientSend('on_album_update', userId, id);
}
@OnEvent({ name: 'AlbumInvite' })