From 5f7538ae2bf9c6e2feea385cc299bd08df071218 Mon Sep 17 00:00:00 2001 From: TheCatLady <52870424+TheCatLady@users.noreply.github.com> Date: Mon, 10 Jan 2022 17:39:12 -0800 Subject: [PATCH] feat(discord): add 'Enable Mentions' setting (#1779) --- overseerr-api.yml | 2 + server/lib/notifications/agents/discord.ts | 54 ++++++++++--------- server/lib/settings.ts | 2 + server/routes/user/usersettings.ts | 10 ++-- .../Notifications/NotificationsDiscord.tsx | 16 ++++++ src/i18n/locale/en.json | 1 + 6 files changed, 55 insertions(+), 30 deletions(-) diff --git a/overseerr-api.yml b/overseerr-api.yml index f8be0895..213ef1d7 100644 --- a/overseerr-api.yml +++ b/overseerr-api.yml @@ -1138,6 +1138,8 @@ components: type: string webhookUrl: type: string + enableMentions: + type: boolean SlackSettings: type: object properties: diff --git a/server/lib/notifications/agents/discord.ts b/server/lib/notifications/agents/discord.ts index bd07c4de..32120035 100644 --- a/server/lib/notifications/agents/discord.ts +++ b/server/lib/notifications/agents/discord.ts @@ -258,35 +258,37 @@ class DiscordAgent const userMentions: string[] = []; try { - if (payload.notifyUser) { - if ( - payload.notifyUser.settings?.hasNotificationType( - NotificationAgentKey.DISCORD, - type - ) && - payload.notifyUser.settings.discordId - ) { - userMentions.push(`<@${payload.notifyUser.settings.discordId}>`); + if (settings.options.enableMentions) { + if (payload.notifyUser) { + if ( + payload.notifyUser.settings?.hasNotificationType( + NotificationAgentKey.DISCORD, + type + ) && + payload.notifyUser.settings.discordId + ) { + userMentions.push(`<@${payload.notifyUser.settings.discordId}>`); + } } - } - if (payload.notifyAdmin) { - const userRepository = getRepository(User); - const users = await userRepository.find(); + if (payload.notifyAdmin) { + const userRepository = getRepository(User); + const users = await userRepository.find(); - userMentions.push( - ...users - .filter( - (user) => - user.settings?.hasNotificationType( - NotificationAgentKey.DISCORD, - type - ) && - user.settings.discordId && - shouldSendAdminNotification(type, user, payload) - ) - .map((user) => `<@${user.settings?.discordId}>`) - ); + userMentions.push( + ...users + .filter( + (user) => + user.settings?.hasNotificationType( + NotificationAgentKey.DISCORD, + type + ) && + user.settings.discordId && + shouldSendAdminNotification(type, user, payload) + ) + .map((user) => `<@${user.settings?.discordId}>`) + ); + } } await axios.post(settings.options.webhookUrl, { diff --git a/server/lib/settings.ts b/server/lib/settings.ts index f7780dfc..eb50e25b 100644 --- a/server/lib/settings.ts +++ b/server/lib/settings.ts @@ -125,6 +125,7 @@ export interface NotificationAgentDiscord extends NotificationAgentConfig { botUsername?: string; botAvatarUrl?: string; webhookUrl: string; + enableMentions: boolean; }; } @@ -304,6 +305,7 @@ class Settings { types: 0, options: { webhookUrl: '', + enableMentions: true, }, }, lunasea: { diff --git a/server/routes/user/usersettings.ts b/server/routes/user/usersettings.ts index 6558115a..22a26531 100644 --- a/server/routes/user/usersettings.ts +++ b/server/routes/user/usersettings.ts @@ -252,10 +252,12 @@ userSettingsRoutes.get<{ id: string }, UserSettingsNotificationsResponse>( return res.status(200).json({ emailEnabled: settings?.email.enabled, pgpKey: user.settings?.pgpKey, - discordEnabled: settings?.discord.enabled, - discordEnabledTypes: settings?.discord.enabled - ? settings?.discord.types - : 0, + discordEnabled: + settings?.discord.enabled && settings.discord.options.enableMentions, + discordEnabledTypes: + settings?.discord.enabled && settings.discord.options.enableMentions + ? settings.discord.types + : 0, discordId: user.settings?.discordId, pushbulletAccessToken: user.settings?.pushbulletAccessToken, pushoverApplicationToken: user.settings?.pushoverApplicationToken, diff --git a/src/components/Settings/Notifications/NotificationsDiscord.tsx b/src/components/Settings/Notifications/NotificationsDiscord.tsx index 9e2a6701..8c882dd6 100644 --- a/src/components/Settings/Notifications/NotificationsDiscord.tsx +++ b/src/components/Settings/Notifications/NotificationsDiscord.tsx @@ -26,6 +26,7 @@ const messages = defineMessages({ toastDiscordTestFailed: 'Discord test notification failed to send.', validationUrl: 'You must provide a valid URL', validationTypes: 'You must select at least one notification type', + enableMentions: 'Enable Mentions', }); const NotificationsDiscord: React.FC = () => { @@ -64,6 +65,7 @@ const NotificationsDiscord: React.FC = () => { botUsername: data?.options.botUsername, botAvatarUrl: data?.options.botAvatarUrl, webhookUrl: data.options.webhookUrl, + enableMentions: data?.options.enableMentions, }} validationSchema={NotificationsDiscordSchema} onSubmit={async (values) => { @@ -75,6 +77,7 @@ const NotificationsDiscord: React.FC = () => { botUsername: values.botUsername, botAvatarUrl: values.botAvatarUrl, webhookUrl: values.webhookUrl, + enableMentions: values.enableMentions, }, }); @@ -122,6 +125,7 @@ const NotificationsDiscord: React.FC = () => { botUsername: values.botUsername, botAvatarUrl: values.botAvatarUrl, webhookUrl: values.webhookUrl, + enableMentions: values.enableMentions, }, }); @@ -227,6 +231,18 @@ const NotificationsDiscord: React.FC = () => { )} +
+ +
+ +
+
{ diff --git a/src/i18n/locale/en.json b/src/i18n/locale/en.json index bd769ba5..47f5f1c8 100644 --- a/src/i18n/locale/en.json +++ b/src/i18n/locale/en.json @@ -448,6 +448,7 @@ "components.Settings.Notifications.emailsender": "Sender Address", "components.Settings.Notifications.emailsettingsfailed": "Email notification settings failed to save.", "components.Settings.Notifications.emailsettingssaved": "Email notification settings saved successfully!", + "components.Settings.Notifications.enableMentions": "Enable Mentions", "components.Settings.Notifications.encryption": "Encryption Method", "components.Settings.Notifications.encryptionDefault": "Use STARTTLS if available", "components.Settings.Notifications.encryptionImplicitTls": "Use Implicit TLS",