feat(notif): add Gotify agent (#2196)
* feat(notifications): adds gotify notifications adds new settings screen for gotify notifications including url, token and types settings fix #2183 * feat(notif): add Gotify agent addresses PR comments, runs i18n:extract fix #2183 * reword validationTokenRequired change wording to indicate presence, not validity Co-authored-by: TheCatLady <52870424+TheCatLady@users.noreply.github.com> * feat(notifications): gotify notifications fix applies changes from #2077 in which Yup validation was failing for types fix #2183 * feat(notifications): adds gotify notifications adds new settings screen for gotify notifications including url, token and types settings fix #2183 * feat(notif): add Gotify agent addresses PR comments, runs i18n:extract fix #2183 * reword validationTokenRequired change wording to indicate presence, not validity Co-authored-by: TheCatLady <52870424+TheCatLady@users.noreply.github.com> * feat(notifications): gotify notifications fix applies changes from #2077 in which Yup validation was failing for types fix #2183 * feat(notifications): incorporate issue feature into gotify notifications * feat(notifications): adds gotify notifications adds new settings screen for gotify notifications including url, token and types settings fix #2183 * feat(notif): add Gotify agent addresses PR comments, runs i18n:extract fix #2183 * reword validationTokenRequired change wording to indicate presence, not validity Co-authored-by: TheCatLady <52870424+TheCatLady@users.noreply.github.com> * feat: add missing ts field include notifyAdmin in test notification endpoint * feat: apply formatting/line break items add addition line break before conditional, change ordering of notifyAdmin/notifyUser in test endpoint * feat: remove duplicated endpoints during rebase, notification endpoints were duplicated upon rebasing. remove duplicate routes * feat: correct linting quirks * feat: formatting improvements * feat(gotify): refactor axios post to leverage 'getNotificationPayload' Co-authored-by: TheCatLady <52870424+TheCatLady@users.noreply.github.com>pull/2082/head^2
parent
879df20022
commit
e0b6abe479
@ -0,0 +1,15 @@
|
|||||||
|
# Gotify
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
### Server URL
|
||||||
|
|
||||||
|
Set this to the URL of your Gotify server.
|
||||||
|
|
||||||
|
### Application Token
|
||||||
|
|
||||||
|
Add an application to your Gotify server, and set this field to the generated application token.
|
||||||
|
|
||||||
|
{% hint style="info" %}
|
||||||
|
Please refer to the [Gotify API documentation](https://gotify.net/docs) for more details on configuring these notifications.
|
||||||
|
{% endhint %}
|
@ -0,0 +1,148 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
import { hasNotificationType, Notification } from '..';
|
||||||
|
import { IssueStatus, IssueTypeName } from '../../../constants/issue';
|
||||||
|
import logger from '../../../logger';
|
||||||
|
import { getSettings, NotificationAgentGotify } from '../../settings';
|
||||||
|
import { BaseAgent, NotificationAgent, NotificationPayload } from './agent';
|
||||||
|
|
||||||
|
interface GotifyPayload {
|
||||||
|
title: string;
|
||||||
|
message: string;
|
||||||
|
priority: number;
|
||||||
|
extras: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
class GotifyAgent
|
||||||
|
extends BaseAgent<NotificationAgentGotify>
|
||||||
|
implements NotificationAgent
|
||||||
|
{
|
||||||
|
protected getSettings(): NotificationAgentGotify {
|
||||||
|
if (this.settings) {
|
||||||
|
return this.settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
const settings = getSettings();
|
||||||
|
|
||||||
|
return settings.notifications.agents.gotify;
|
||||||
|
}
|
||||||
|
|
||||||
|
public shouldSend(): boolean {
|
||||||
|
const settings = this.getSettings();
|
||||||
|
|
||||||
|
if (settings.enabled && settings.options.url && settings.options.token) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getNotificationPayload(
|
||||||
|
type: Notification,
|
||||||
|
payload: NotificationPayload
|
||||||
|
): GotifyPayload {
|
||||||
|
const { applicationUrl, applicationTitle } = getSettings().main;
|
||||||
|
let priority = 0;
|
||||||
|
|
||||||
|
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'
|
||||||
|
}`;
|
||||||
|
|
||||||
|
if (type == Notification.ISSUE_CREATED) {
|
||||||
|
priority = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const extra of payload.extra ?? []) {
|
||||||
|
message += `\n\n**${extra.name}**\n${extra.value}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (applicationUrl && payload.media) {
|
||||||
|
const actionUrl = `${applicationUrl}/${payload.media.mediaType}/${payload.media.tmdbId}`;
|
||||||
|
message += `\n\nOpen in ${applicationTitle}(${actionUrl})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
extras: {
|
||||||
|
'client::display': {
|
||||||
|
contentType: 'text/markdown',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
priority,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public async send(
|
||||||
|
type: Notification,
|
||||||
|
payload: NotificationPayload
|
||||||
|
): Promise<boolean> {
|
||||||
|
const settings = this.getSettings();
|
||||||
|
|
||||||
|
if (!hasNotificationType(type, settings.types ?? 0)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug('Sending Gotify notification', {
|
||||||
|
label: 'Notifications',
|
||||||
|
type: Notification[type],
|
||||||
|
subject: payload.subject,
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const endpoint = `${settings.options.url}/message?token=${settings.options.token}`;
|
||||||
|
const notificationPayload = this.getNotificationPayload(type, payload);
|
||||||
|
|
||||||
|
await axios.post(endpoint, notificationPayload);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('Error sending Gotify notification', {
|
||||||
|
label: 'Notifications',
|
||||||
|
type: Notification[type],
|
||||||
|
subject: payload.subject,
|
||||||
|
errorMessage: e.message,
|
||||||
|
response: e.response?.data,
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default GotifyAgent;
|
After Width: | Height: | Size: 4.3 KiB |
@ -0,0 +1,256 @@
|
|||||||
|
import { BeakerIcon, SaveIcon } from '@heroicons/react/solid';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { Field, Form, Formik } from 'formik';
|
||||||
|
import React, { useState } from 'react';
|
||||||
|
import { defineMessages, useIntl } from 'react-intl';
|
||||||
|
import { useToasts } from 'react-toast-notifications';
|
||||||
|
import useSWR from 'swr';
|
||||||
|
import * as Yup from 'yup';
|
||||||
|
import globalMessages from '../../../../i18n/globalMessages';
|
||||||
|
import Button from '../../../Common/Button';
|
||||||
|
import LoadingSpinner from '../../../Common/LoadingSpinner';
|
||||||
|
import NotificationTypeSelector from '../../../NotificationTypeSelector';
|
||||||
|
|
||||||
|
const messages = defineMessages({
|
||||||
|
agentenabled: 'Enable Agent',
|
||||||
|
url: 'Server URL',
|
||||||
|
token: 'Application Token',
|
||||||
|
validationUrlRequired: 'You must provide a valid URL',
|
||||||
|
validationUrlTrailingSlash: 'URL must not end in a trailing slash',
|
||||||
|
validationTokenRequired: 'You must provide an application token',
|
||||||
|
gotifysettingssaved: 'Gotify notification settings saved successfully!',
|
||||||
|
gotifysettingsfailed: 'Gotify notification settings failed to save.',
|
||||||
|
toastGotifyTestSending: 'Sending Gotify test notification…',
|
||||||
|
toastGotifyTestSuccess: 'Gotify test notification sent!',
|
||||||
|
toastGotifyTestFailed: 'Gotify test notification failed to send.',
|
||||||
|
validationTypes: 'You must select at least one notification type',
|
||||||
|
});
|
||||||
|
|
||||||
|
const NotificationsGotify: React.FC = () => {
|
||||||
|
const intl = useIntl();
|
||||||
|
const { addToast, removeToast } = useToasts();
|
||||||
|
const [isTesting, setIsTesting] = useState(false);
|
||||||
|
const { data, error, revalidate } = useSWR(
|
||||||
|
'/api/v1/settings/notifications/gotify'
|
||||||
|
);
|
||||||
|
|
||||||
|
const NotificationsGotifySchema = Yup.object().shape({
|
||||||
|
url: Yup.string()
|
||||||
|
.when('enabled', {
|
||||||
|
is: true,
|
||||||
|
then: Yup.string()
|
||||||
|
.nullable()
|
||||||
|
.required(intl.formatMessage(messages.validationUrlRequired)),
|
||||||
|
otherwise: Yup.string().nullable(),
|
||||||
|
})
|
||||||
|
.matches(
|
||||||
|
// eslint-disable-next-line no-useless-escape
|
||||||
|
/^(https?:)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*)?([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i,
|
||||||
|
intl.formatMessage(messages.validationUrlRequired)
|
||||||
|
)
|
||||||
|
.test(
|
||||||
|
'no-trailing-slash',
|
||||||
|
intl.formatMessage(messages.validationUrlTrailingSlash),
|
||||||
|
(value) => !value || !value.endsWith('/')
|
||||||
|
),
|
||||||
|
token: Yup.string().when('enabled', {
|
||||||
|
is: true,
|
||||||
|
then: Yup.string()
|
||||||
|
.nullable()
|
||||||
|
.required(intl.formatMessage(messages.validationTokenRequired)),
|
||||||
|
otherwise: Yup.string().nullable(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!data && !error) {
|
||||||
|
return <LoadingSpinner />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Formik
|
||||||
|
initialValues={{
|
||||||
|
enabled: data?.enabled,
|
||||||
|
types: data?.types,
|
||||||
|
url: data?.options.url,
|
||||||
|
token: data?.options.token,
|
||||||
|
}}
|
||||||
|
validationSchema={NotificationsGotifySchema}
|
||||||
|
onSubmit={async (values) => {
|
||||||
|
try {
|
||||||
|
await axios.post('/api/v1/settings/notifications/gotify', {
|
||||||
|
enabled: values.enabled,
|
||||||
|
types: values.types,
|
||||||
|
options: {
|
||||||
|
url: values.url,
|
||||||
|
token: values.token,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
addToast(intl.formatMessage(messages.gotifysettingssaved), {
|
||||||
|
appearance: 'success',
|
||||||
|
autoDismiss: true,
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
addToast(intl.formatMessage(messages.gotifysettingsfailed), {
|
||||||
|
appearance: 'error',
|
||||||
|
autoDismiss: true,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
revalidate();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{({
|
||||||
|
errors,
|
||||||
|
touched,
|
||||||
|
isSubmitting,
|
||||||
|
values,
|
||||||
|
isValid,
|
||||||
|
setFieldValue,
|
||||||
|
setFieldTouched,
|
||||||
|
}) => {
|
||||||
|
const testSettings = async () => {
|
||||||
|
setIsTesting(true);
|
||||||
|
let toastId: string | undefined;
|
||||||
|
try {
|
||||||
|
addToast(
|
||||||
|
intl.formatMessage(messages.toastGotifyTestSending),
|
||||||
|
{
|
||||||
|
autoDsmiss: false,
|
||||||
|
appearance: 'info',
|
||||||
|
},
|
||||||
|
(id) => {
|
||||||
|
toastId = id;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
await axios.post('/api/v1/settings/notifications/gotify/test', {
|
||||||
|
enabled: true,
|
||||||
|
types: values.types,
|
||||||
|
options: {
|
||||||
|
url: values.url,
|
||||||
|
token: values.token,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (toastId) {
|
||||||
|
removeToast(toastId);
|
||||||
|
}
|
||||||
|
addToast(intl.formatMessage(messages.toastGotifyTestSuccess), {
|
||||||
|
autoDismiss: true,
|
||||||
|
appearance: 'success',
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
if (toastId) {
|
||||||
|
removeToast(toastId);
|
||||||
|
}
|
||||||
|
addToast(intl.formatMessage(messages.toastGotifyTestFailed), {
|
||||||
|
autoDismiss: true,
|
||||||
|
appearance: 'error',
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setIsTesting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Form className="section">
|
||||||
|
<div className="form-row">
|
||||||
|
<label htmlFor="enabled" className="checkbox-label">
|
||||||
|
{intl.formatMessage(messages.agentenabled)}
|
||||||
|
<span className="label-required">*</span>
|
||||||
|
</label>
|
||||||
|
<div className="form-input">
|
||||||
|
<Field type="checkbox" id="enabled" name="enabled" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label htmlFor="url" className="text-label">
|
||||||
|
{intl.formatMessage(messages.url)}
|
||||||
|
<span className="label-required">*</span>
|
||||||
|
</label>
|
||||||
|
<div className="form-input">
|
||||||
|
<div className="form-input-field">
|
||||||
|
<Field id="url" name="url" type="text" />
|
||||||
|
</div>
|
||||||
|
{errors.url && touched.url && (
|
||||||
|
<div className="error">{errors.url}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label htmlFor="token" className="text-label">
|
||||||
|
{intl.formatMessage(messages.token)}
|
||||||
|
<span className="label-required">*</span>
|
||||||
|
</label>
|
||||||
|
<div className="form-input">
|
||||||
|
<div className="form-input-field">
|
||||||
|
<Field id="token" name="token" type="text" />
|
||||||
|
</div>
|
||||||
|
{errors.token && touched.token && (
|
||||||
|
<div className="error">{errors.token}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<NotificationTypeSelector
|
||||||
|
currentTypes={values.enabled ? values.types : 0}
|
||||||
|
onUpdate={(newTypes) => {
|
||||||
|
setFieldValue('types', newTypes);
|
||||||
|
setFieldTouched('types');
|
||||||
|
|
||||||
|
if (newTypes) {
|
||||||
|
setFieldValue('enabled', true);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
error={
|
||||||
|
values.enabled && !values.types && touched.types
|
||||||
|
? intl.formatMessage(messages.validationTypes)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<div className="actions">
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<span className="inline-flex ml-3 rounded-md shadow-sm">
|
||||||
|
<Button
|
||||||
|
buttonType="warning"
|
||||||
|
disabled={isSubmitting || !isValid || isTesting}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
testSettings();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<BeakerIcon />
|
||||||
|
<span>
|
||||||
|
{isTesting
|
||||||
|
? intl.formatMessage(globalMessages.testing)
|
||||||
|
: intl.formatMessage(globalMessages.test)}
|
||||||
|
</span>
|
||||||
|
</Button>
|
||||||
|
</span>
|
||||||
|
<span className="inline-flex ml-3 rounded-md shadow-sm">
|
||||||
|
<Button
|
||||||
|
buttonType="primary"
|
||||||
|
type="submit"
|
||||||
|
disabled={
|
||||||
|
isSubmitting ||
|
||||||
|
!isValid ||
|
||||||
|
isTesting ||
|
||||||
|
(values.enabled && !values.types)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SaveIcon />
|
||||||
|
<span>
|
||||||
|
{isSubmitting
|
||||||
|
? intl.formatMessage(globalMessages.saving)
|
||||||
|
: intl.formatMessage(globalMessages.save)}
|
||||||
|
</span>
|
||||||
|
</Button>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Form>
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
</Formik>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default NotificationsGotify;
|
@ -0,0 +1,20 @@
|
|||||||
|
import { NextPage } from 'next';
|
||||||
|
import React from 'react';
|
||||||
|
import NotificationsGotify from '../../../components/Settings/Notifications/NotificationsGotify';
|
||||||
|
import SettingsLayout from '../../../components/Settings/SettingsLayout';
|
||||||
|
import SettingsNotifications from '../../../components/Settings/SettingsNotifications';
|
||||||
|
import useRouteGuard from '../../../hooks/useRouteGuard';
|
||||||
|
import { Permission } from '../../../hooks/useUser';
|
||||||
|
|
||||||
|
const NotificationsPage: NextPage = () => {
|
||||||
|
useRouteGuard(Permission.MANAGE_SETTINGS);
|
||||||
|
return (
|
||||||
|
<SettingsLayout>
|
||||||
|
<SettingsNotifications>
|
||||||
|
<NotificationsGotify />
|
||||||
|
</SettingsNotifications>
|
||||||
|
</SettingsLayout>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default NotificationsPage;
|
Loading…
Reference in new issue