You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.5 KiB
62 lines
1.5 KiB
import logger from '../../logger';
|
|
import { getSettings } from '../settings';
|
|
import type { NotificationAgent, NotificationPayload } from './agents/agent';
|
|
|
|
export enum Notification {
|
|
MEDIA_PENDING = 2,
|
|
MEDIA_APPROVED = 4,
|
|
MEDIA_AVAILABLE = 8,
|
|
MEDIA_FAILED = 16,
|
|
TEST_NOTIFICATION = 32,
|
|
MEDIA_DECLINED = 64,
|
|
}
|
|
|
|
export const hasNotificationType = (
|
|
types: Notification | Notification[],
|
|
value: number
|
|
): boolean => {
|
|
let total = 0;
|
|
|
|
// If we are not checking any notifications, bail out and return true
|
|
if (types === 0) {
|
|
return true;
|
|
}
|
|
|
|
if (Array.isArray(types)) {
|
|
// Combine all notification values into one
|
|
total = types.reduce((a, v) => a + v, 0);
|
|
} else {
|
|
total = types;
|
|
}
|
|
|
|
return !!(value & total);
|
|
};
|
|
|
|
class NotificationManager {
|
|
private activeAgents: NotificationAgent[] = [];
|
|
|
|
public registerAgents = (agents: NotificationAgent[]): void => {
|
|
this.activeAgents = [...this.activeAgents, ...agents];
|
|
logger.info('Registered Notification Agents', { label: 'Notifications' });
|
|
};
|
|
|
|
public sendNotification(
|
|
type: Notification,
|
|
payload: NotificationPayload
|
|
): void {
|
|
const settings = getSettings().notifications;
|
|
logger.info(`Sending notification for ${Notification[type]}`, {
|
|
label: 'Notifications',
|
|
});
|
|
this.activeAgents.forEach((agent) => {
|
|
if (settings.enabled && agent.shouldSend(type, payload)) {
|
|
agent.send(type, payload);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
const notificationManager = new NotificationManager();
|
|
|
|
export default notificationManager;
|