diff --git a/api/src/core/notifiers/notifier.ts b/api/src/core/notifiers/notifier.ts index 26309aa04..e3678f519 100644 --- a/api/src/core/notifiers/notifier.ts +++ b/api/src/core/notifiers/notifier.ts @@ -1,10 +1,12 @@ import Mustache from 'mustache'; import { type LooseObject } from '@app/core/types'; +import { type NotificationIni } from '../types/states/notification'; export type NotifierLevel = 'info' | 'warn' | 'error'; export type NotifierOptions = Partial<{ level: NotifierLevel; + importance?: NotificationIni['importance']; helpers?: Record; template?: string; }>; diff --git a/api/src/core/notifiers/unraid-local.ts b/api/src/core/notifiers/unraid-local.ts new file mode 100644 index 000000000..e40c24145 --- /dev/null +++ b/api/src/core/notifiers/unraid-local.ts @@ -0,0 +1,39 @@ +import { logger } from '@app/core/log'; +import { Notifier, type NotifierSendOptions, type NotifierOptions } from '@app/core/notifiers/notifier'; +import { execa } from 'execa'; + +type ValidLocalLevels = 'alert' | 'warning' | 'normal'; + +export class UnraidLocalNotifier extends Notifier { + private convertNotifierLevel(level: NotifierOptions['level']): ValidLocalLevels { + switch (level) { + case 'error': + return 'alert'; + case 'warn': + return 'warning'; + case 'info': + return 'normal'; + default: + return 'normal'; + } + } + + constructor(options: NotifierOptions = {}) { + super(options); + + this.level = options.importance ?? this.convertNotifierLevel(options.level ?? 'info'); + this.template = options.template ?? '{{ message }}'; + } + + async send(options: NotifierSendOptions) { + const { title, data } = options; + const { level } = this; + + const template = this.render(data); + try { + await execa('/usr/local/emhttp/webGui/scripts/notify', ['-i', `${level}`, '-s', 'Unraid API', '-d', `${template}`, '-e', `${title}`]); + } catch (error: unknown) { + logger.warn(`Error sending unraid notification: ${error instanceof Error ? error.message : 'No Error Information'}`); + } + } +}