refactor(api): directly accept importance level in UnraidLocalNotifier

This commit is contained in:
Pujit Mehrotra
2024-10-09 10:20:45 -04:00
parent 01441961c3
commit d5a424ebe1
2 changed files with 41 additions and 0 deletions

View File

@@ -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<string, unknown>;
template?: string;
}>;

View File

@@ -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'}`);
}
}
}