Skip to content

Commit

Permalink
feat(discord): add 'Enable Mentions' setting (#1779)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCatLady committed Jan 11, 2022
1 parent 1b29b15 commit 5f7538a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 30 deletions.
2 changes: 2 additions & 0 deletions overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,8 @@ components:
type: string
webhookUrl:
type: string
enableMentions:
type: boolean
SlackSettings:
type: object
properties:
Expand Down
54 changes: 28 additions & 26 deletions server/lib/notifications/agents/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
2 changes: 2 additions & 0 deletions server/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export interface NotificationAgentDiscord extends NotificationAgentConfig {
botUsername?: string;
botAvatarUrl?: string;
webhookUrl: string;
enableMentions: boolean;
};
}

Expand Down Expand Up @@ -304,6 +305,7 @@ class Settings {
types: 0,
options: {
webhookUrl: '',
enableMentions: true,
},
},
lunasea: {
Expand Down
10 changes: 6 additions & 4 deletions server/routes/user/usersettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/components/Settings/Notifications/NotificationsDiscord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -75,6 +77,7 @@ const NotificationsDiscord: React.FC = () => {
botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl,
enableMentions: values.enableMentions,
},
});

Expand Down Expand Up @@ -122,6 +125,7 @@ const NotificationsDiscord: React.FC = () => {
botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl,
enableMentions: values.enableMentions,
},
});

Expand Down Expand Up @@ -227,6 +231,18 @@ const NotificationsDiscord: React.FC = () => {
)}
</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
currentTypes={values.enabled ? values.types : 0}
onUpdate={(newTypes) => {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5f7538a

Please sign in to comment.