mirror of
https://github.com/fallenbagel/jellyseerr.git
synced 2025-12-24 02:39:18 -05:00
feat(ntfy): add native ntfy notification support (#1599)
* feat(ntfy): add native ntfy notification fix #499 * feat(ntfy): update translation keys * feat(ntfy): append ntfy to cypress settings * feat(ntfy): adjust ntfy agent shouldSend * feat(ntfy): simplify ntfy post routes * feat(ntfy): refactor ntfy agent from fetch to axios * feat(ntfy): refactor ntfy frontend from fetch to axios
This commit is contained in:
@@ -10,6 +10,7 @@ import DiscordAgent from '@server/lib/notifications/agents/discord';
|
||||
import EmailAgent from '@server/lib/notifications/agents/email';
|
||||
import GotifyAgent from '@server/lib/notifications/agents/gotify';
|
||||
import LunaSeaAgent from '@server/lib/notifications/agents/lunasea';
|
||||
import NtfyAgent from '@server/lib/notifications/agents/ntfy';
|
||||
import PushbulletAgent from '@server/lib/notifications/agents/pushbullet';
|
||||
import PushoverAgent from '@server/lib/notifications/agents/pushover';
|
||||
import SlackAgent from '@server/lib/notifications/agents/slack';
|
||||
@@ -103,6 +104,7 @@ app
|
||||
new DiscordAgent(),
|
||||
new EmailAgent(),
|
||||
new GotifyAgent(),
|
||||
new NtfyAgent(),
|
||||
new LunaSeaAgent(),
|
||||
new PushbulletAgent(),
|
||||
new PushoverAgent(),
|
||||
|
||||
164
server/lib/notifications/agents/ntfy.ts
Normal file
164
server/lib/notifications/agents/ntfy.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
import { IssueStatus, IssueTypeName } from '@server/constants/issue';
|
||||
import type { NotificationAgentNtfy } from '@server/lib/settings';
|
||||
import { getSettings } from '@server/lib/settings';
|
||||
import logger from '@server/logger';
|
||||
import axios from 'axios';
|
||||
import { hasNotificationType, Notification } from '..';
|
||||
import type { NotificationAgent, NotificationPayload } from './agent';
|
||||
import { BaseAgent } from './agent';
|
||||
|
||||
class NtfyAgent
|
||||
extends BaseAgent<NotificationAgentNtfy>
|
||||
implements NotificationAgent
|
||||
{
|
||||
protected getSettings(): NotificationAgentNtfy {
|
||||
if (this.settings) {
|
||||
return this.settings;
|
||||
}
|
||||
|
||||
const settings = getSettings();
|
||||
|
||||
return settings.notifications.agents.ntfy;
|
||||
}
|
||||
|
||||
private buildPayload(type: Notification, payload: NotificationPayload) {
|
||||
const { applicationUrl } = getSettings().main;
|
||||
|
||||
const topic = this.getSettings().options.topic;
|
||||
const priority = 3;
|
||||
|
||||
const title = payload.event
|
||||
? `${payload.event} - ${payload.subject}`
|
||||
: payload.subject;
|
||||
let message = payload.message ?? '';
|
||||
|
||||
if (payload.request) {
|
||||
message += `\n\nRequested By: ${payload.request.requestedBy.displayName}`;
|
||||
|
||||
let status = '';
|
||||
switch (type) {
|
||||
case Notification.MEDIA_PENDING:
|
||||
status = 'Pending Approval';
|
||||
break;
|
||||
case Notification.MEDIA_APPROVED:
|
||||
case Notification.MEDIA_AUTO_APPROVED:
|
||||
status = 'Processing';
|
||||
break;
|
||||
case Notification.MEDIA_AVAILABLE:
|
||||
status = 'Available';
|
||||
break;
|
||||
case Notification.MEDIA_DECLINED:
|
||||
status = 'Declined';
|
||||
break;
|
||||
case Notification.MEDIA_FAILED:
|
||||
status = 'Failed';
|
||||
break;
|
||||
}
|
||||
|
||||
if (status) {
|
||||
message += `\nRequest Status: ${status}`;
|
||||
}
|
||||
} else if (payload.comment) {
|
||||
message += `\nComment from ${payload.comment.user.displayName}:\n${payload.comment.message}`;
|
||||
} else if (payload.issue) {
|
||||
message += `\n\nReported By: ${payload.issue.createdBy.displayName}`;
|
||||
message += `\nIssue Type: ${IssueTypeName[payload.issue.issueType]}`;
|
||||
message += `\nIssue Status: ${
|
||||
payload.issue.status === IssueStatus.OPEN ? 'Open' : 'Resolved'
|
||||
}`;
|
||||
}
|
||||
|
||||
for (const extra of payload.extra ?? []) {
|
||||
message += `\n\n**${extra.name}**\n${extra.value}`;
|
||||
}
|
||||
|
||||
const attach = payload.image;
|
||||
|
||||
let click;
|
||||
if (applicationUrl && payload.media) {
|
||||
click = `${applicationUrl}/${payload.media.mediaType}/${payload.media.tmdbId}`;
|
||||
}
|
||||
|
||||
return {
|
||||
topic,
|
||||
priority,
|
||||
title,
|
||||
message,
|
||||
attach,
|
||||
click,
|
||||
};
|
||||
}
|
||||
|
||||
public shouldSend(): boolean {
|
||||
const settings = this.getSettings();
|
||||
|
||||
if (settings.enabled && settings.options.url && settings.options.topic) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async send(
|
||||
type: Notification,
|
||||
payload: NotificationPayload
|
||||
): Promise<boolean> {
|
||||
const settings = this.getSettings();
|
||||
|
||||
if (
|
||||
!payload.notifySystem ||
|
||||
!hasNotificationType(type, settings.types ?? 0)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
logger.debug('Sending ntfy notification', {
|
||||
label: 'Notifications',
|
||||
type: Notification[type],
|
||||
subject: payload.subject,
|
||||
});
|
||||
|
||||
try {
|
||||
let authHeader;
|
||||
if (
|
||||
settings.options.authMethodUsernamePassword &&
|
||||
settings.options.username &&
|
||||
settings.options.password
|
||||
) {
|
||||
const encodedAuth = Buffer.from(
|
||||
`${settings.options.username}:${settings.options.password}`
|
||||
).toString('base64');
|
||||
|
||||
authHeader = `Basic ${encodedAuth}`;
|
||||
} else if (settings.options.authMethodToken) {
|
||||
authHeader = `Bearer ${settings.options.token}`;
|
||||
}
|
||||
|
||||
await axios.post(
|
||||
settings.options.url,
|
||||
this.buildPayload(type, payload),
|
||||
authHeader
|
||||
? {
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
},
|
||||
}
|
||||
: undefined
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
logger.error('Error sending ntfy notification', {
|
||||
label: 'Notifications',
|
||||
type: Notification[type],
|
||||
subject: payload.subject,
|
||||
errorMessage: e.message,
|
||||
response: e?.response?.data,
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default NtfyAgent;
|
||||
@@ -259,10 +259,23 @@ export interface NotificationAgentGotify extends NotificationAgentConfig {
|
||||
};
|
||||
}
|
||||
|
||||
export interface NotificationAgentNtfy extends NotificationAgentConfig {
|
||||
options: {
|
||||
url: string;
|
||||
topic: string;
|
||||
authMethodUsernamePassword?: boolean;
|
||||
username?: string;
|
||||
password?: string;
|
||||
authMethodToken?: boolean;
|
||||
token?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export enum NotificationAgentKey {
|
||||
DISCORD = 'discord',
|
||||
EMAIL = 'email',
|
||||
GOTIFY = 'gotify',
|
||||
NTFY = 'ntfy',
|
||||
PUSHBULLET = 'pushbullet',
|
||||
PUSHOVER = 'pushover',
|
||||
SLACK = 'slack',
|
||||
@@ -275,6 +288,7 @@ interface NotificationAgents {
|
||||
discord: NotificationAgentDiscord;
|
||||
email: NotificationAgentEmail;
|
||||
gotify: NotificationAgentGotify;
|
||||
ntfy: NotificationAgentNtfy;
|
||||
lunasea: NotificationAgentLunaSea;
|
||||
pushbullet: NotificationAgentPushbullet;
|
||||
pushover: NotificationAgentPushover;
|
||||
@@ -471,6 +485,14 @@ class Settings {
|
||||
priority: 0,
|
||||
},
|
||||
},
|
||||
ntfy: {
|
||||
enabled: false,
|
||||
types: 0,
|
||||
options: {
|
||||
url: '',
|
||||
topic: '',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
jobs: {
|
||||
|
||||
@@ -5,6 +5,7 @@ import DiscordAgent from '@server/lib/notifications/agents/discord';
|
||||
import EmailAgent from '@server/lib/notifications/agents/email';
|
||||
import GotifyAgent from '@server/lib/notifications/agents/gotify';
|
||||
import LunaSeaAgent from '@server/lib/notifications/agents/lunasea';
|
||||
import NtfyAgent from '@server/lib/notifications/agents/ntfy';
|
||||
import PushbulletAgent from '@server/lib/notifications/agents/pushbullet';
|
||||
import PushoverAgent from '@server/lib/notifications/agents/pushover';
|
||||
import SlackAgent from '@server/lib/notifications/agents/slack';
|
||||
@@ -413,4 +414,38 @@ notificationRoutes.post('/gotify/test', async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
notificationRoutes.get('/ntfy', (_req, res) => {
|
||||
const settings = getSettings();
|
||||
|
||||
res.status(200).json(settings.notifications.agents.ntfy);
|
||||
});
|
||||
|
||||
notificationRoutes.post('/ntfy', async (req, res) => {
|
||||
const settings = getSettings();
|
||||
|
||||
settings.notifications.agents.ntfy = req.body;
|
||||
await settings.save();
|
||||
|
||||
res.status(200).json(settings.notifications.agents.ntfy);
|
||||
});
|
||||
|
||||
notificationRoutes.post('/ntfy/test', async (req, res, next) => {
|
||||
if (!req.user) {
|
||||
return next({
|
||||
status: 500,
|
||||
message: 'User information is missing from the request.',
|
||||
});
|
||||
}
|
||||
|
||||
const ntfyAgent = new NtfyAgent(req.body);
|
||||
if (await sendTestNotification(ntfyAgent, req.user)) {
|
||||
return res.status(204).send();
|
||||
} else {
|
||||
return next({
|
||||
status: 500,
|
||||
message: 'Failed to send ntfy notification.',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default notificationRoutes;
|
||||
|
||||
Reference in New Issue
Block a user