feat(discord): add 'Enable Mentions' setting (#1779)

pull/2415/head
TheCatLady 2 years ago committed by GitHub
parent 1b29b15d7c
commit 5f7538ae2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1138,6 +1138,8 @@ components:
type: string type: string
webhookUrl: webhookUrl:
type: string type: string
enableMentions:
type: boolean
SlackSettings: SlackSettings:
type: object type: object
properties: properties:

@ -258,35 +258,37 @@ class DiscordAgent
const userMentions: string[] = []; const userMentions: string[] = [];
try { try {
if (payload.notifyUser) { if (settings.options.enableMentions) {
if ( if (payload.notifyUser) {
payload.notifyUser.settings?.hasNotificationType( if (
NotificationAgentKey.DISCORD, payload.notifyUser.settings?.hasNotificationType(
type NotificationAgentKey.DISCORD,
) && type
payload.notifyUser.settings.discordId ) &&
) { payload.notifyUser.settings.discordId
userMentions.push(`<@${payload.notifyUser.settings.discordId}>`); ) {
userMentions.push(`<@${payload.notifyUser.settings.discordId}>`);
}
} }
}
if (payload.notifyAdmin) { if (payload.notifyAdmin) {
const userRepository = getRepository(User); const userRepository = getRepository(User);
const users = await userRepository.find(); const users = await userRepository.find();
userMentions.push( userMentions.push(
...users ...users
.filter( .filter(
(user) => (user) =>
user.settings?.hasNotificationType( user.settings?.hasNotificationType(
NotificationAgentKey.DISCORD, NotificationAgentKey.DISCORD,
type type
) && ) &&
user.settings.discordId && user.settings.discordId &&
shouldSendAdminNotification(type, user, payload) shouldSendAdminNotification(type, user, payload)
) )
.map((user) => `<@${user.settings?.discordId}>`) .map((user) => `<@${user.settings?.discordId}>`)
); );
}
} }
await axios.post(settings.options.webhookUrl, { await axios.post(settings.options.webhookUrl, {

@ -125,6 +125,7 @@ export interface NotificationAgentDiscord extends NotificationAgentConfig {
botUsername?: string; botUsername?: string;
botAvatarUrl?: string; botAvatarUrl?: string;
webhookUrl: string; webhookUrl: string;
enableMentions: boolean;
}; };
} }
@ -304,6 +305,7 @@ class Settings {
types: 0, types: 0,
options: { options: {
webhookUrl: '', webhookUrl: '',
enableMentions: true,
}, },
}, },
lunasea: { lunasea: {

@ -252,10 +252,12 @@ userSettingsRoutes.get<{ id: string }, UserSettingsNotificationsResponse>(
return res.status(200).json({ return res.status(200).json({
emailEnabled: settings?.email.enabled, emailEnabled: settings?.email.enabled,
pgpKey: user.settings?.pgpKey, pgpKey: user.settings?.pgpKey,
discordEnabled: settings?.discord.enabled, discordEnabled:
discordEnabledTypes: settings?.discord.enabled settings?.discord.enabled && settings.discord.options.enableMentions,
? settings?.discord.types discordEnabledTypes:
: 0, settings?.discord.enabled && settings.discord.options.enableMentions
? settings.discord.types
: 0,
discordId: user.settings?.discordId, discordId: user.settings?.discordId,
pushbulletAccessToken: user.settings?.pushbulletAccessToken, pushbulletAccessToken: user.settings?.pushbulletAccessToken,
pushoverApplicationToken: user.settings?.pushoverApplicationToken, pushoverApplicationToken: user.settings?.pushoverApplicationToken,

@ -26,6 +26,7 @@ const messages = defineMessages({
toastDiscordTestFailed: 'Discord test notification failed to send.', toastDiscordTestFailed: 'Discord test notification failed to send.',
validationUrl: 'You must provide a valid URL', validationUrl: 'You must provide a valid URL',
validationTypes: 'You must select at least one notification type', validationTypes: 'You must select at least one notification type',
enableMentions: 'Enable Mentions',
}); });
const NotificationsDiscord: React.FC = () => { const NotificationsDiscord: React.FC = () => {
@ -64,6 +65,7 @@ const NotificationsDiscord: React.FC = () => {
botUsername: data?.options.botUsername, botUsername: data?.options.botUsername,
botAvatarUrl: data?.options.botAvatarUrl, botAvatarUrl: data?.options.botAvatarUrl,
webhookUrl: data.options.webhookUrl, webhookUrl: data.options.webhookUrl,
enableMentions: data?.options.enableMentions,
}} }}
validationSchema={NotificationsDiscordSchema} validationSchema={NotificationsDiscordSchema}
onSubmit={async (values) => { onSubmit={async (values) => {
@ -75,6 +77,7 @@ const NotificationsDiscord: React.FC = () => {
botUsername: values.botUsername, botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl, botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl, webhookUrl: values.webhookUrl,
enableMentions: values.enableMentions,
}, },
}); });
@ -122,6 +125,7 @@ const NotificationsDiscord: React.FC = () => {
botUsername: values.botUsername, botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl, botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl, webhookUrl: values.webhookUrl,
enableMentions: values.enableMentions,
}, },
}); });
@ -227,6 +231,18 @@ const NotificationsDiscord: React.FC = () => {
)} )}
</div> </div>
</div> </div>
<div className="form-row">
<label htmlFor="enableMentions" className="checkbox-label">
{intl.formatMessage(messages.enableMentions)}
</label>
<div className="form-input">
<Field
type="checkbox"
id="enableMentions"
name="enableMentions"
/>
</div>
</div>
<NotificationTypeSelector <NotificationTypeSelector
currentTypes={values.enabled ? values.types : 0} currentTypes={values.enabled ? values.types : 0}
onUpdate={(newTypes) => { onUpdate={(newTypes) => {

@ -448,6 +448,7 @@
"components.Settings.Notifications.emailsender": "Sender Address", "components.Settings.Notifications.emailsender": "Sender Address",
"components.Settings.Notifications.emailsettingsfailed": "Email notification settings failed to save.", "components.Settings.Notifications.emailsettingsfailed": "Email notification settings failed to save.",
"components.Settings.Notifications.emailsettingssaved": "Email notification settings saved successfully!", "components.Settings.Notifications.emailsettingssaved": "Email notification settings saved successfully!",
"components.Settings.Notifications.enableMentions": "Enable Mentions",
"components.Settings.Notifications.encryption": "Encryption Method", "components.Settings.Notifications.encryption": "Encryption Method",
"components.Settings.Notifications.encryptionDefault": "Use STARTTLS if available", "components.Settings.Notifications.encryptionDefault": "Use STARTTLS if available",
"components.Settings.Notifications.encryptionImplicitTls": "Use Implicit TLS", "components.Settings.Notifications.encryptionImplicitTls": "Use Implicit TLS",

Loading…
Cancel
Save