2024-10-02 10:54:35 -04:00
|
|
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
2024-10-31 13:42:58 -04:00
|
|
|
import { OnEvent, OnJob } from 'src/decorators';
|
2025-10-14 10:15:51 -05:00
|
|
|
import { MapAlbumDto } from 'src/dtos/album.dto';
|
2025-04-30 17:02:53 -04:00
|
|
|
import { mapAsset } from 'src/dtos/asset-response.dto';
|
2025-04-28 10:36:14 -04:00
|
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
|
|
|
|
import {
|
|
|
|
|
mapNotification,
|
|
|
|
|
NotificationDeleteAllDto,
|
|
|
|
|
NotificationDto,
|
|
|
|
|
NotificationSearchDto,
|
|
|
|
|
NotificationUpdateAllDto,
|
|
|
|
|
NotificationUpdateDto,
|
|
|
|
|
} from 'src/dtos/notification.dto';
|
2024-06-07 11:34:09 -05:00
|
|
|
import { SystemConfigSmtpDto } from 'src/dtos/system-config.dto';
|
2025-04-28 10:36:14 -04:00
|
|
|
import {
|
|
|
|
|
AssetFileType,
|
|
|
|
|
JobName,
|
|
|
|
|
JobStatus,
|
|
|
|
|
NotificationLevel,
|
|
|
|
|
NotificationType,
|
|
|
|
|
Permission,
|
|
|
|
|
QueueName,
|
|
|
|
|
} from 'src/enum';
|
2025-04-21 12:53:37 -04:00
|
|
|
import { EmailTemplate } from 'src/repositories/email.repository';
|
2025-02-11 15:12:31 -05:00
|
|
|
import { ArgOf } from 'src/repositories/event.repository';
|
2024-09-30 17:31:21 -04:00
|
|
|
import { BaseService } from 'src/services/base.service';
|
2025-04-30 22:45:35 +01:00
|
|
|
import { EmailImageAttachment, JobOf } from 'src/types';
|
2024-07-21 01:00:46 +02:00
|
|
|
import { getFilenameExtension } from 'src/utils/file';
|
2024-10-16 18:13:12 -04:00
|
|
|
import { getExternalDomain } from 'src/utils/misc';
|
2024-08-29 20:10:09 +02:00
|
|
|
import { isEqualObject } from 'src/utils/object';
|
2024-06-03 16:00:20 -05:00
|
|
|
import { getPreferences } from 'src/utils/preferences';
|
2024-05-02 16:43:18 +02:00
|
|
|
|
|
|
|
|
@Injectable()
|
2024-09-30 17:31:21 -04:00
|
|
|
export class NotificationService extends BaseService {
|
2024-10-18 13:51:34 -06:00
|
|
|
private static albumUpdateEmailDelayMs = 300_000;
|
|
|
|
|
|
2025-04-28 10:36:14 -04:00
|
|
|
async search(auth: AuthDto, dto: NotificationSearchDto): Promise<NotificationDto[]> {
|
|
|
|
|
const items = await this.notificationRepository.search(auth.user.id, dto);
|
|
|
|
|
return items.map((item) => mapNotification(item));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateAll(auth: AuthDto, dto: NotificationUpdateAllDto) {
|
2025-07-15 14:50:13 -04:00
|
|
|
await this.requireAccess({ auth, ids: dto.ids, permission: Permission.NotificationUpdate });
|
2025-04-28 10:36:14 -04:00
|
|
|
await this.notificationRepository.updateAll(dto.ids, {
|
|
|
|
|
readAt: dto.readAt,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteAll(auth: AuthDto, dto: NotificationDeleteAllDto) {
|
2025-07-15 14:50:13 -04:00
|
|
|
await this.requireAccess({ auth, ids: dto.ids, permission: Permission.NotificationDelete });
|
2025-04-28 10:36:14 -04:00
|
|
|
await this.notificationRepository.deleteAll(dto.ids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async get(auth: AuthDto, id: string) {
|
2025-07-15 14:50:13 -04:00
|
|
|
await this.requireAccess({ auth, ids: [id], permission: Permission.NotificationRead });
|
2025-04-28 10:36:14 -04:00
|
|
|
const item = await this.notificationRepository.get(id);
|
|
|
|
|
if (!item) {
|
|
|
|
|
throw new BadRequestException('Notification not found');
|
|
|
|
|
}
|
|
|
|
|
return mapNotification(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(auth: AuthDto, id: string, dto: NotificationUpdateDto) {
|
2025-07-15 14:50:13 -04:00
|
|
|
await this.requireAccess({ auth, ids: [id], permission: Permission.NotificationUpdate });
|
2025-04-28 10:36:14 -04:00
|
|
|
const item = await this.notificationRepository.update(id, {
|
|
|
|
|
readAt: dto.readAt,
|
|
|
|
|
});
|
|
|
|
|
return mapNotification(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(auth: AuthDto, id: string) {
|
2025-07-15 14:50:13 -04:00
|
|
|
await this.requireAccess({ auth, ids: [id], permission: Permission.NotificationDelete });
|
2025-04-28 10:36:14 -04:00
|
|
|
await this.notificationRepository.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
@OnJob({ name: JobName.NotificationsCleanup, queue: QueueName.BackgroundTask })
|
2025-04-28 10:36:14 -04:00
|
|
|
async onNotificationsCleanup() {
|
|
|
|
|
await this.notificationRepository.cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 12:16:55 -04:00
|
|
|
@OnEvent({ name: 'JobError' })
|
|
|
|
|
async onJobError({ job, error }: ArgOf<'JobError'>) {
|
2025-04-28 10:36:14 -04:00
|
|
|
const admin = await this.userRepository.getAdmin();
|
|
|
|
|
if (!admin) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.logger.error(`Unable to run job handler (${job.name}): ${error}`, error?.stack, JSON.stringify(job.data));
|
|
|
|
|
|
|
|
|
|
switch (job.name) {
|
2025-07-15 18:39:00 -04:00
|
|
|
case JobName.DatabaseBackup: {
|
2025-04-28 10:36:14 -04:00
|
|
|
const errorMessage = error instanceof Error ? error.message : error;
|
|
|
|
|
const item = await this.notificationRepository.create({
|
|
|
|
|
userId: admin.id,
|
|
|
|
|
type: NotificationType.JobFailed,
|
|
|
|
|
level: NotificationLevel.Error,
|
|
|
|
|
title: 'Job Failed',
|
|
|
|
|
description: `Job ${[job.name]} failed with error: ${errorMessage}`,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_notification', admin.id, mapNotification(item));
|
2025-04-28 10:36:14 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'ConfigUpdate' })
|
|
|
|
|
onConfigUpdate({ oldConfig, newConfig }: ArgOf<'ConfigUpdate'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientBroadcast('on_config_update');
|
|
|
|
|
this.websocketRepository.serverSend('ConfigUpdate', { oldConfig, newConfig });
|
2024-09-30 10:35:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
@OnEvent({ name: 'AppRestart' })
|
|
|
|
|
onAppRestart(state: ArgOf<'AppRestart'>) {
|
|
|
|
|
this.websocketRepository.clientBroadcast('AppRestartV1', {
|
|
|
|
|
isMaintenanceMode: state.isMaintenanceMode,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.websocketRepository.serverSend('AppRestart', state);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'ConfigValidate', priority: -100 })
|
|
|
|
|
async onConfigValidate({ oldConfig, newConfig }: ArgOf<'ConfigValidate'>) {
|
2024-05-02 16:43:18 +02:00
|
|
|
try {
|
2024-07-10 14:37:50 +02:00
|
|
|
if (
|
|
|
|
|
newConfig.notifications.smtp.enabled &&
|
2024-08-29 20:10:09 +02:00
|
|
|
!isEqualObject(oldConfig.notifications.smtp, newConfig.notifications.smtp)
|
2024-07-10 14:37:50 +02:00
|
|
|
) {
|
2025-04-21 12:53:37 -04:00
|
|
|
await this.emailRepository.verifySmtp(newConfig.notifications.smtp.transport);
|
2024-05-02 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
} catch (error: Error | any) {
|
|
|
|
|
this.logger.error(`Failed to validate SMTP configuration: ${error}`, error?.stack);
|
|
|
|
|
throw new Error(`Invalid SMTP configuration: ${error}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AssetHide' })
|
|
|
|
|
onAssetHide({ assetId, userId }: ArgOf<'AssetHide'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_hidden', userId, assetId);
|
2024-09-10 08:51:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AssetShow' })
|
|
|
|
|
async onAssetShow({ assetId }: ArgOf<'AssetShow'>) {
|
2025-07-15 18:39:00 -04:00
|
|
|
await this.jobRepository.queue({ name: JobName.AssetGenerateThumbnails, data: { id: assetId, notify: true } });
|
2024-09-11 16:26:29 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AssetTrash' })
|
|
|
|
|
onAssetTrash({ assetId, userId }: ArgOf<'AssetTrash'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_trash', userId, [assetId]);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AssetDelete' })
|
|
|
|
|
onAssetDelete({ assetId, userId }: ArgOf<'AssetDelete'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_delete', userId, assetId);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AssetTrashAll' })
|
|
|
|
|
onAssetsTrash({ assetIds, userId }: ArgOf<'AssetTrashAll'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_trash', userId, assetIds);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AssetMetadataExtracted' })
|
|
|
|
|
async onAssetMetadataExtracted({ assetId, userId, source }: ArgOf<'AssetMetadataExtracted'>) {
|
2025-04-30 17:02:53 -04:00
|
|
|
if (source !== 'sidecar-write') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [asset] = await this.assetRepository.getByIdsWithAllRelationsButStacks([assetId]);
|
|
|
|
|
if (asset) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend(
|
2025-10-16 23:52:36 +02:00
|
|
|
'on_asset_update',
|
|
|
|
|
userId,
|
|
|
|
|
mapAsset(asset, { auth: { user: { id: userId } } as AuthDto }),
|
|
|
|
|
);
|
2025-04-30 17:02:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AssetRestoreAll' })
|
|
|
|
|
onAssetsRestore({ assetIds, userId }: ArgOf<'AssetRestoreAll'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_restore', userId, assetIds);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'StackCreate' })
|
|
|
|
|
onStackCreate({ userId }: ArgOf<'StackCreate'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_stack_update', userId);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'StackUpdate' })
|
|
|
|
|
onStackUpdate({ userId }: ArgOf<'StackUpdate'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_stack_update', userId);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'StackDelete' })
|
|
|
|
|
onStackDelete({ userId }: ArgOf<'StackDelete'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_stack_update', userId);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'StackDeleteAll' })
|
|
|
|
|
onStacksDelete({ userId }: ArgOf<'StackDeleteAll'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_asset_stack_update', userId);
|
2024-09-12 14:12:39 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'UserSignup' })
|
2025-09-10 09:11:42 -04:00
|
|
|
async onUserSignup({ notify, id, password: password }: ArgOf<'UserSignup'>) {
|
2024-07-03 22:06:20 -04:00
|
|
|
if (notify) {
|
2025-09-10 09:11:42 -04:00
|
|
|
await this.jobRepository.queue({ name: JobName.NotifyUserSignup, data: { id, password } });
|
2024-07-03 22:06:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 12:54:29 -04:00
|
|
|
@OnEvent({ name: 'UserDelete' })
|
|
|
|
|
onUserDelete({ id }: ArgOf<'UserDelete'>) {
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientBroadcast('on_user_delete', id);
|
2025-10-22 12:54:29 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AlbumUpdate' })
|
|
|
|
|
async onAlbumUpdate({ id, recipientId }: ArgOf<'AlbumUpdate'>) {
|
2025-07-15 14:50:13 -04:00
|
|
|
await this.jobRepository.removeJob(JobName.NotifyAlbumUpdate, `${id}/${recipientId}`);
|
2025-04-30 22:45:35 +01:00
|
|
|
await this.jobRepository.queue({
|
2025-07-15 14:50:13 -04:00
|
|
|
name: JobName.NotifyAlbumUpdate,
|
2025-04-30 22:45:35 +01:00
|
|
|
data: { id, recipientId, delay: NotificationService.albumUpdateEmailDelayMs },
|
|
|
|
|
});
|
2024-07-03 22:06:20 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'AlbumInvite' })
|
|
|
|
|
async onAlbumInvite({ id, userId }: ArgOf<'AlbumInvite'>) {
|
2025-07-15 14:50:13 -04:00
|
|
|
await this.jobRepository.queue({ name: JobName.NotifyAlbumInvite, data: { id, recipientId: userId } });
|
2024-07-03 22:06:20 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 13:41:19 -04:00
|
|
|
@OnEvent({ name: 'SessionDelete' })
|
|
|
|
|
onSessionDelete({ sessionId }: ArgOf<'SessionDelete'>) {
|
2024-09-07 13:21:05 -04:00
|
|
|
// after the response is sent
|
2025-10-24 16:26:27 -04:00
|
|
|
setTimeout(() => this.websocketRepository.clientSend('on_session_delete', sessionId, sessionId), 500);
|
2024-09-07 13:21:05 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-04 21:26:02 +01:00
|
|
|
async sendTestEmail(id: string, dto: SystemConfigSmtpDto, tempTemplate?: string) {
|
2024-06-07 11:34:09 -05:00
|
|
|
const user = await this.userRepository.get(id, { withDeleted: false });
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new Error('User not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-04-21 12:53:37 -04:00
|
|
|
await this.emailRepository.verifySmtp(dto.transport);
|
2024-06-07 11:34:09 -05:00
|
|
|
} catch (error) {
|
2024-09-25 12:05:03 -04:00
|
|
|
throw new BadRequestException('Failed to verify SMTP configuration', { cause: error });
|
2024-06-07 11:34:09 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-30 17:31:21 -04:00
|
|
|
const { server } = await this.getConfig({ withCache: false });
|
2025-04-21 12:53:37 -04:00
|
|
|
const { html, text } = await this.emailRepository.renderEmail({
|
2024-06-07 11:34:09 -05:00
|
|
|
template: EmailTemplate.TEST_EMAIL,
|
|
|
|
|
data: {
|
2025-03-31 13:08:41 +02:00
|
|
|
baseUrl: getExternalDomain(server),
|
2024-06-07 11:34:09 -05:00
|
|
|
displayName: user.name,
|
|
|
|
|
},
|
2024-12-04 21:26:02 +01:00
|
|
|
customTemplate: tempTemplate!,
|
2024-06-07 11:34:09 -05:00
|
|
|
});
|
2025-04-21 12:53:37 -04:00
|
|
|
const { messageId } = await this.emailRepository.sendEmail({
|
2024-06-07 11:34:09 -05:00
|
|
|
to: user.email,
|
|
|
|
|
subject: 'Test email from Immich',
|
|
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
from: dto.from,
|
|
|
|
|
replyTo: dto.replyTo || dto.from,
|
|
|
|
|
smtp: dto.transport,
|
|
|
|
|
});
|
2024-09-25 12:05:03 -04:00
|
|
|
|
|
|
|
|
return { messageId };
|
2024-06-07 11:34:09 -05:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 18:39:00 -04:00
|
|
|
@OnJob({ name: JobName.NotifyUserSignup, queue: QueueName.Notification })
|
2025-09-10 09:11:42 -04:00
|
|
|
async handleUserSignup({ id, password }: JobOf<JobName.NotifyUserSignup>) {
|
2024-05-02 16:43:18 +02:00
|
|
|
const user = await this.userRepository.get(id, { withDeleted: false });
|
|
|
|
|
if (!user) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-05-02 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-04 21:26:02 +01:00
|
|
|
const { server, templates } = await this.getConfig({ withCache: true });
|
2025-04-21 12:53:37 -04:00
|
|
|
const { html, text } = await this.emailRepository.renderEmail({
|
2024-05-02 16:43:18 +02:00
|
|
|
template: EmailTemplate.WELCOME,
|
|
|
|
|
data: {
|
2025-03-31 13:08:41 +02:00
|
|
|
baseUrl: getExternalDomain(server),
|
2024-05-02 16:43:18 +02:00
|
|
|
displayName: user.name,
|
|
|
|
|
username: user.email,
|
2025-09-10 09:11:42 -04:00
|
|
|
password,
|
2024-05-02 16:43:18 +02:00
|
|
|
},
|
2024-12-04 21:26:02 +01:00
|
|
|
customTemplate: templates.email.welcomeTemplate,
|
2024-05-02 16:43:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await this.jobRepository.queue({
|
2025-07-15 14:50:13 -04:00
|
|
|
name: JobName.SendMail,
|
2024-05-02 16:43:18 +02:00
|
|
|
data: {
|
|
|
|
|
to: user.email,
|
|
|
|
|
subject: 'Welcome to Immich',
|
|
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Success;
|
2024-05-02 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
@OnJob({ name: JobName.NotifyAlbumInvite, queue: QueueName.Notification })
|
|
|
|
|
async handleAlbumInvite({ id, recipientId }: JobOf<JobName.NotifyAlbumInvite>) {
|
2024-05-28 09:16:46 +07:00
|
|
|
const album = await this.albumRepository.getById(id, { withAssets: false });
|
|
|
|
|
if (!album) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-05-28 09:16:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const recipient = await this.userRepository.get(recipientId, { withDeleted: false });
|
|
|
|
|
if (!recipient) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-05-28 09:16:46 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-14 10:15:51 -05:00
|
|
|
await this.sendAlbumLocalNotification(album, recipientId, NotificationType.AlbumInvite, album.owner.name);
|
|
|
|
|
|
2025-04-28 09:54:51 -04:00
|
|
|
const { emailNotifications } = getPreferences(recipient.metadata);
|
2024-06-03 16:00:20 -05:00
|
|
|
|
|
|
|
|
if (!emailNotifications.enabled || !emailNotifications.albumInvite) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-06-03 16:00:20 -05:00
|
|
|
}
|
|
|
|
|
|
2024-05-28 09:16:46 +07:00
|
|
|
const attachment = await this.getAlbumThumbnailAttachment(album);
|
|
|
|
|
|
2024-12-04 21:26:02 +01:00
|
|
|
const { server, templates } = await this.getConfig({ withCache: false });
|
2025-04-21 12:53:37 -04:00
|
|
|
const { html, text } = await this.emailRepository.renderEmail({
|
2024-05-28 09:16:46 +07:00
|
|
|
template: EmailTemplate.ALBUM_INVITE,
|
|
|
|
|
data: {
|
2025-03-31 13:08:41 +02:00
|
|
|
baseUrl: getExternalDomain(server),
|
2024-05-28 09:16:46 +07:00
|
|
|
albumId: album.id,
|
|
|
|
|
albumName: album.albumName,
|
|
|
|
|
senderName: album.owner.name,
|
|
|
|
|
recipientName: recipient.name,
|
|
|
|
|
cid: attachment ? attachment.cid : undefined,
|
|
|
|
|
},
|
2024-12-04 21:26:02 +01:00
|
|
|
customTemplate: templates.email.albumInviteTemplate,
|
2024-05-28 09:16:46 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await this.jobRepository.queue({
|
2025-07-15 14:50:13 -04:00
|
|
|
name: JobName.SendMail,
|
2024-05-28 09:16:46 +07:00
|
|
|
data: {
|
|
|
|
|
to: recipient.email,
|
|
|
|
|
subject: `You have been added to a shared album - ${album.albumName}`,
|
|
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
imageAttachments: attachment ? [attachment] : undefined,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Success;
|
2024-05-28 09:16:46 +07:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
@OnJob({ name: JobName.NotifyAlbumUpdate, queue: QueueName.Notification })
|
|
|
|
|
async handleAlbumUpdate({ id, recipientId }: JobOf<JobName.NotifyAlbumUpdate>) {
|
2024-05-28 09:16:46 +07:00
|
|
|
const album = await this.albumRepository.getById(id, { withAssets: false });
|
|
|
|
|
|
|
|
|
|
if (!album) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-05-28 09:16:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const owner = await this.userRepository.get(album.ownerId, { withDeleted: false });
|
|
|
|
|
if (!owner) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-05-28 09:16:46 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-14 10:15:51 -05:00
|
|
|
await this.sendAlbumLocalNotification(album, recipientId, NotificationType.AlbumUpdate);
|
|
|
|
|
|
2024-05-28 09:16:46 +07:00
|
|
|
const attachment = await this.getAlbumThumbnailAttachment(album);
|
|
|
|
|
|
2024-12-04 21:26:02 +01:00
|
|
|
const { server, templates } = await this.getConfig({ withCache: false });
|
2024-05-28 09:16:46 +07:00
|
|
|
|
2025-04-30 22:45:35 +01:00
|
|
|
const user = await this.userRepository.get(recipientId, { withDeleted: false });
|
|
|
|
|
if (!user) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2025-04-30 22:45:35 +01:00
|
|
|
}
|
2024-06-11 03:51:58 -05:00
|
|
|
|
2025-04-30 22:45:35 +01:00
|
|
|
const { emailNotifications } = getPreferences(user.metadata);
|
2024-06-03 16:00:20 -05:00
|
|
|
|
2025-04-30 22:45:35 +01:00
|
|
|
if (!emailNotifications.enabled || !emailNotifications.albumUpdate) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-05-28 09:16:46 +07:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 22:45:35 +01:00
|
|
|
const { html, text } = await this.emailRepository.renderEmail({
|
|
|
|
|
template: EmailTemplate.ALBUM_UPDATE,
|
|
|
|
|
data: {
|
|
|
|
|
baseUrl: getExternalDomain(server),
|
|
|
|
|
albumId: album.id,
|
|
|
|
|
albumName: album.albumName,
|
|
|
|
|
recipientName: user.name,
|
|
|
|
|
cid: attachment ? attachment.cid : undefined,
|
|
|
|
|
},
|
|
|
|
|
customTemplate: templates.email.albumUpdateTemplate,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await this.jobRepository.queue({
|
2025-07-15 14:50:13 -04:00
|
|
|
name: JobName.SendMail,
|
2025-04-30 22:45:35 +01:00
|
|
|
data: {
|
|
|
|
|
to: user.email,
|
|
|
|
|
subject: `New media has been added to an album - ${album.albumName}`,
|
|
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
imageAttachments: attachment ? [attachment] : undefined,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Success;
|
2024-05-28 09:16:46 +07:00
|
|
|
}
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
@OnJob({ name: JobName.SendMail, queue: QueueName.Notification })
|
|
|
|
|
async handleSendEmail(data: JobOf<JobName.SendMail>): Promise<JobStatus> {
|
2024-09-30 17:31:21 -04:00
|
|
|
const { notifications } = await this.getConfig({ withCache: false });
|
2024-05-02 16:43:18 +02:00
|
|
|
if (!notifications.smtp.enabled) {
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Skipped;
|
2024-05-02 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { to, subject, html, text: plain } = data;
|
2025-04-21 12:53:37 -04:00
|
|
|
const response = await this.emailRepository.sendEmail({
|
2024-05-02 16:43:18 +02:00
|
|
|
to,
|
|
|
|
|
subject,
|
|
|
|
|
html,
|
|
|
|
|
text: plain,
|
|
|
|
|
from: notifications.smtp.from,
|
|
|
|
|
replyTo: notifications.smtp.replyTo || notifications.smtp.from,
|
|
|
|
|
smtp: notifications.smtp.transport,
|
2024-05-28 09:16:46 +07:00
|
|
|
imageAttachments: data.imageAttachments,
|
2024-05-02 16:43:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.logger.log(`Sent mail with id: ${response.messageId} status: ${response.response}`);
|
|
|
|
|
|
2025-07-15 14:50:13 -04:00
|
|
|
return JobStatus.Success;
|
2024-05-02 16:43:18 +02:00
|
|
|
}
|
2024-05-28 09:16:46 +07:00
|
|
|
|
2025-04-16 21:10:27 +02:00
|
|
|
private async getAlbumThumbnailAttachment(album: {
|
|
|
|
|
albumThumbnailAssetId: string | null;
|
|
|
|
|
}): Promise<EmailImageAttachment | undefined> {
|
2024-05-28 09:16:46 +07:00
|
|
|
if (!album.albumThumbnailAssetId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-16 22:10:20 +02:00
|
|
|
const albumThumbnailFiles = await this.assetJobRepository.getAlbumThumbnailFiles(
|
|
|
|
|
album.albumThumbnailAssetId,
|
2025-07-15 14:50:13 -04:00
|
|
|
AssetFileType.Thumbnail,
|
2025-04-16 22:10:20 +02:00
|
|
|
);
|
2025-04-01 18:11:46 -04:00
|
|
|
|
2025-04-16 22:10:20 +02:00
|
|
|
if (albumThumbnailFiles.length !== 1) {
|
2024-05-28 09:16:46 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2025-04-16 22:10:20 +02:00
|
|
|
filename: `album-thumbnail${getFilenameExtension(albumThumbnailFiles[0].path)}`,
|
|
|
|
|
path: albumThumbnailFiles[0].path,
|
2024-05-28 09:16:46 +07:00
|
|
|
cid: 'album-thumbnail',
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-10-14 10:15:51 -05:00
|
|
|
|
|
|
|
|
private async sendAlbumLocalNotification(
|
|
|
|
|
album: MapAlbumDto,
|
|
|
|
|
userId: string,
|
|
|
|
|
type: NotificationType.AlbumInvite | NotificationType.AlbumUpdate,
|
|
|
|
|
senderName?: string,
|
|
|
|
|
) {
|
|
|
|
|
const isInvite = type === NotificationType.AlbumInvite;
|
|
|
|
|
const item = await this.notificationRepository.create({
|
|
|
|
|
userId,
|
|
|
|
|
type,
|
|
|
|
|
level: isInvite ? NotificationLevel.Success : NotificationLevel.Info,
|
|
|
|
|
title: isInvite ? 'Shared Album Invitation' : 'Shared Album Update',
|
|
|
|
|
description: isInvite
|
|
|
|
|
? `${senderName} shared an album (${album.albumName}) with you`
|
|
|
|
|
: `New media has been added to the album (${album.albumName})`,
|
|
|
|
|
data: JSON.stringify({ albumId: album.id }),
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-24 16:26:27 -04:00
|
|
|
this.websocketRepository.clientSend('on_notification', userId, mapNotification(item));
|
2025-10-14 10:15:51 -05:00
|
|
|
}
|
2024-05-02 16:43:18 +02:00
|
|
|
}
|