mirror of
https://github.com/databasus/databasus.git
synced 2026-04-06 00:32:03 +02:00
FEATURE (notifiers): Add Discord notifier
This commit is contained in:
@@ -7,4 +7,5 @@ const (
|
||||
NotifierTypeTelegram NotifierType = "TELEGRAM"
|
||||
NotifierTypeWebhook NotifierType = "WEBHOOK"
|
||||
NotifierTypeSlack NotifierType = "SLACK"
|
||||
NotifierTypeDiscord NotifierType = "DISCORD"
|
||||
)
|
||||
|
||||
@@ -3,6 +3,7 @@ package notifiers
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
discord_notifier "postgresus-backend/internal/features/notifiers/models/discord"
|
||||
"postgresus-backend/internal/features/notifiers/models/email_notifier"
|
||||
slack_notifier "postgresus-backend/internal/features/notifiers/models/slack"
|
||||
telegram_notifier "postgresus-backend/internal/features/notifiers/models/telegram"
|
||||
@@ -23,6 +24,7 @@ type Notifier struct {
|
||||
EmailNotifier *email_notifier.EmailNotifier `json:"emailNotifier" gorm:"foreignKey:NotifierID"`
|
||||
WebhookNotifier *webhook_notifier.WebhookNotifier `json:"webhookNotifier" gorm:"foreignKey:NotifierID"`
|
||||
SlackNotifier *slack_notifier.SlackNotifier `json:"slackNotifier" gorm:"foreignKey:NotifierID"`
|
||||
DiscordNotifier *discord_notifier.DiscordNotifier `json:"discordNotifier" gorm:"foreignKey:NotifierID"`
|
||||
}
|
||||
|
||||
func (n *Notifier) TableName() string {
|
||||
@@ -60,6 +62,8 @@ func (n *Notifier) getSpecificNotifier() NotificationSender {
|
||||
return n.WebhookNotifier
|
||||
case NotifierTypeSlack:
|
||||
return n.SlackNotifier
|
||||
case NotifierTypeDiscord:
|
||||
return n.DiscordNotifier
|
||||
default:
|
||||
panic("unknown notifier type: " + string(n.NotifierType))
|
||||
}
|
||||
|
||||
73
backend/internal/features/notifiers/models/discord/model.go
Normal file
73
backend/internal/features/notifiers/models/discord/model.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package discord_notifier
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type DiscordNotifier struct {
|
||||
NotifierID uuid.UUID `json:"notifierId" gorm:"primaryKey;column:notifier_id"`
|
||||
ChannelWebhookURL string `json:"channelWebhookUrl" gorm:"not null;column:channel_webhook_url"`
|
||||
}
|
||||
|
||||
func (d *DiscordNotifier) TableName() string {
|
||||
return "discord_notifiers"
|
||||
}
|
||||
|
||||
func (d *DiscordNotifier) Validate() error {
|
||||
if d.ChannelWebhookURL == "" {
|
||||
return errors.New("webhook URL is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiscordNotifier) Send(logger *slog.Logger, heading string, message string) error {
|
||||
fullMessage := heading
|
||||
if message != "" {
|
||||
fullMessage = fmt.Sprintf("%s\n\n%s", heading, message)
|
||||
}
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"content": fullMessage,
|
||||
}
|
||||
|
||||
jsonPayload, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal Discord payload: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", d.ChannelWebhookURL, bytes.NewReader(jsonPayload))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send Discord message: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf(
|
||||
"discord API returned non-OK status: %s. Error: %s",
|
||||
resp.Status,
|
||||
string(bodyBytes),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (e *EmailNotifier) Send(logger *slog.Logger, heading string, message string
|
||||
timeout := DefaultTimeout
|
||||
|
||||
// Determine if authentication is required
|
||||
authRequired := e.SMTPUser != "" && e.SMTPPassword != ""
|
||||
isAuthRequired := e.SMTPUser != "" && e.SMTPPassword != ""
|
||||
|
||||
// Handle different port scenarios
|
||||
if e.SMTPPort == ImplicitTLSPort {
|
||||
@@ -110,7 +110,7 @@ func (e *EmailNotifier) Send(logger *slog.Logger, heading string, message string
|
||||
}()
|
||||
|
||||
// Set up authentication only if credentials are provided
|
||||
if authRequired {
|
||||
if isAuthRequired {
|
||||
auth := smtp.PlainAuth("", e.SMTPUser, e.SMTPPassword, e.SMTPHost)
|
||||
if err := client.Auth(auth); err != nil {
|
||||
return fmt.Errorf("SMTP authentication failed: %w", err)
|
||||
@@ -173,7 +173,7 @@ func (e *EmailNotifier) Send(logger *slog.Logger, heading string, message string
|
||||
}
|
||||
|
||||
// Authenticate only if credentials are provided
|
||||
if authRequired {
|
||||
if isAuthRequired {
|
||||
auth := smtp.PlainAuth("", e.SMTPUser, e.SMTPPassword, e.SMTPHost)
|
||||
if err := client.Auth(auth); err != nil {
|
||||
return fmt.Errorf("SMTP authentication failed: %w", err)
|
||||
|
||||
@@ -30,17 +30,33 @@ func (r *NotifierRepository) Save(notifier *Notifier) (*Notifier, error) {
|
||||
if notifier.SlackNotifier != nil {
|
||||
notifier.SlackNotifier.NotifierID = notifier.ID
|
||||
}
|
||||
case NotifierTypeDiscord:
|
||||
if notifier.DiscordNotifier != nil {
|
||||
notifier.DiscordNotifier.NotifierID = notifier.ID
|
||||
}
|
||||
}
|
||||
|
||||
if notifier.ID == uuid.Nil {
|
||||
if err := tx.Create(notifier).
|
||||
Omit("TelegramNotifier", "EmailNotifier", "WebhookNotifier", "SlackNotifier").
|
||||
Omit(
|
||||
"TelegramNotifier",
|
||||
"EmailNotifier",
|
||||
"WebhookNotifier",
|
||||
"SlackNotifier",
|
||||
"DiscordNotifier",
|
||||
).
|
||||
Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := tx.Save(notifier).
|
||||
Omit("TelegramNotifier", "EmailNotifier", "WebhookNotifier", "SlackNotifier").
|
||||
Omit(
|
||||
"TelegramNotifier",
|
||||
"EmailNotifier",
|
||||
"WebhookNotifier",
|
||||
"SlackNotifier",
|
||||
"DiscordNotifier",
|
||||
).
|
||||
Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -75,6 +91,13 @@ func (r *NotifierRepository) Save(notifier *Notifier) (*Notifier, error) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case NotifierTypeDiscord:
|
||||
if notifier.DiscordNotifier != nil {
|
||||
notifier.DiscordNotifier.NotifierID = notifier.ID // Ensure ID is set
|
||||
if err := tx.Save(notifier.DiscordNotifier).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -96,6 +119,7 @@ func (r *NotifierRepository) FindByID(id uuid.UUID) (*Notifier, error) {
|
||||
Preload("EmailNotifier").
|
||||
Preload("WebhookNotifier").
|
||||
Preload("SlackNotifier").
|
||||
Preload("DiscordNotifier").
|
||||
Where("id = ?", id).
|
||||
First(¬ifier).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -113,7 +137,9 @@ func (r *NotifierRepository) FindByUserID(userID uuid.UUID) ([]*Notifier, error)
|
||||
Preload("EmailNotifier").
|
||||
Preload("WebhookNotifier").
|
||||
Preload("SlackNotifier").
|
||||
Preload("DiscordNotifier").
|
||||
Where("user_id = ?", userID).
|
||||
Order("name ASC").
|
||||
Find(¬ifiers).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -149,6 +175,12 @@ func (r *NotifierRepository) Delete(notifier *Notifier) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case NotifierTypeDiscord:
|
||||
if notifier.DiscordNotifier != nil {
|
||||
if err := tx.Delete(notifier.DiscordNotifier).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the main notifier
|
||||
|
||||
23
backend/migrations/20250716072247_add_discord_notifier.sql
Normal file
23
backend/migrations/20250716072247_add_discord_notifier.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
-- Create discord notifiers table
|
||||
CREATE TABLE discord_notifiers (
|
||||
notifier_id UUID PRIMARY KEY,
|
||||
channel_webhook_url TEXT NOT NULL
|
||||
);
|
||||
|
||||
ALTER TABLE discord_notifiers
|
||||
ADD CONSTRAINT fk_discord_notifiers_notifier
|
||||
FOREIGN KEY (notifier_id)
|
||||
REFERENCES notifiers (id)
|
||||
ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
|
||||
DROP TABLE IF EXISTS discord_notifiers;
|
||||
|
||||
-- +goose StatementEnd
|
||||
8
frontend/public/icons/notifiers/discord.svg
Normal file
8
frontend/public/icons/notifiers/discord.svg
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<svg width="800px" height="800px" viewBox="0 -28.5 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
|
||||
<g>
|
||||
<path d="M216.856339,16.5966031 C200.285002,8.84328665 182.566144,3.2084988 164.041564,0 C161.766523,4.11318106 159.108624,9.64549908 157.276099,14.0464379 C137.583995,11.0849896 118.072967,11.0849896 98.7430163,14.0464379 C96.9108417,9.64549908 94.1925838,4.11318106 91.8971895,0 C73.3526068,3.2084988 55.6133949,8.86399117 39.0420583,16.6376612 C5.61752293,67.146514 -3.4433191,116.400813 1.08711069,164.955721 C23.2560196,181.510915 44.7403634,191.567697 65.8621325,198.148576 C71.0772151,190.971126 75.7283628,183.341335 79.7352139,175.300261 C72.104019,172.400575 64.7949724,168.822202 57.8887866,164.667963 C59.7209612,163.310589 61.5131304,161.891452 63.2445898,160.431257 C105.36741,180.133187 151.134928,180.133187 192.754523,160.431257 C194.506336,161.891452 196.298154,163.310589 198.110326,164.667963 C191.183787,168.842556 183.854737,172.420929 176.223542,175.320965 C180.230393,183.341335 184.861538,190.991831 190.096624,198.16893 C211.238746,191.588051 232.743023,181.531619 254.911949,164.955721 C260.227747,108.668201 245.831087,59.8662432 216.856339,16.5966031 Z M85.4738752,135.09489 C72.8290281,135.09489 62.4592217,123.290155 62.4592217,108.914901 C62.4592217,94.5396472 72.607595,82.7145587 85.4738752,82.7145587 C98.3405064,82.7145587 108.709962,94.5189427 108.488529,108.914901 C108.508531,123.290155 98.3405064,135.09489 85.4738752,135.09489 Z M170.525237,135.09489 C157.88039,135.09489 147.510584,123.290155 147.510584,108.914901 C147.510584,94.5396472 157.658606,82.7145587 170.525237,82.7145587 C183.391518,82.7145587 193.761324,94.5189427 193.539891,108.914901 C193.539891,123.290155 183.391518,135.09489 170.525237,135.09489 Z" fill="#5865F2" fill-rule="nonzero">
|
||||
|
||||
</path>
|
||||
</g>
|
||||
|
After Width: | Height: | Size: 2.0 KiB |
@@ -14,3 +14,6 @@ export { WebhookMethod } from './models/webhook/WebhookMethod';
|
||||
|
||||
export type { SlackNotifier } from './models/slack/SlackNotifier';
|
||||
export { validateSlackNotifier } from './models/slack/validateSlackNotifier';
|
||||
|
||||
export type { DiscordNotifier } from './models/discord/DiscordNotifier';
|
||||
export { validateDiscordNotifier } from './models/discord/validateDiscordNotifier';
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { NotifierType } from './NotifierType';
|
||||
import type { DiscordNotifier } from './discord/DiscordNotifier';
|
||||
import type { EmailNotifier } from './email/EmailNotifier';
|
||||
import type { SlackNotifier } from './slack/SlackNotifier';
|
||||
import type { TelegramNotifier } from './telegram/TelegramNotifier';
|
||||
@@ -15,4 +16,5 @@ export interface Notifier {
|
||||
emailNotifier?: EmailNotifier;
|
||||
webhookNotifier?: WebhookNotifier;
|
||||
slackNotifier?: SlackNotifier;
|
||||
discordNotifier?: DiscordNotifier;
|
||||
}
|
||||
|
||||
@@ -3,4 +3,5 @@ export enum NotifierType {
|
||||
TELEGRAM = 'TELEGRAM',
|
||||
WEBHOOK = 'WEBHOOK',
|
||||
SLACK = 'SLACK',
|
||||
DISCORD = 'DISCORD',
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export interface DiscordNotifier {
|
||||
channelWebhookUrl: string;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { DiscordNotifier } from './DiscordNotifier';
|
||||
|
||||
export const validateDiscordNotifier = (notifier: DiscordNotifier): boolean => {
|
||||
if (!notifier.channelWebhookUrl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
@@ -10,6 +10,8 @@ export const getNotifierLogoFromType = (type: NotifierType) => {
|
||||
return '/icons/notifiers/webhook.svg';
|
||||
case NotifierType.SLACK:
|
||||
return '/icons/notifiers/slack.svg';
|
||||
case NotifierType.DISCORD:
|
||||
return '/icons/notifiers/discord.svg';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
NotifierType,
|
||||
WebhookMethod,
|
||||
notifierApi,
|
||||
validateDiscordNotifier,
|
||||
validateEmailNotifier,
|
||||
validateSlackNotifier,
|
||||
validateTelegramNotifier,
|
||||
@@ -13,6 +14,7 @@ import {
|
||||
} from '../../../../entity/notifiers';
|
||||
import { getNotifierLogoFromType } from '../../../../entity/notifiers/models/getNotifierLogoFromType';
|
||||
import { ToastHelper } from '../../../../shared/toast';
|
||||
import { EditDiscordNotifierComponent } from './notifiers/EditDiscordNotifierComponent';
|
||||
import { EditEmailNotifierComponent } from './notifiers/EditEmailNotifierComponent';
|
||||
import { EditSlackNotifierComponent } from './notifiers/EditSlackNotifierComponent';
|
||||
import { EditTelegramNotifierComponent } from './notifiers/EditTelegramNotifierComponent';
|
||||
@@ -120,6 +122,12 @@ export function EditNotifierComponent({
|
||||
};
|
||||
}
|
||||
|
||||
if (type === NotifierType.DISCORD) {
|
||||
notifier.discordNotifier = {
|
||||
channelWebhookUrl: '',
|
||||
};
|
||||
}
|
||||
|
||||
setNotifier(
|
||||
JSON.parse(
|
||||
JSON.stringify({
|
||||
@@ -171,6 +179,10 @@ export function EditNotifierComponent({
|
||||
return validateSlackNotifier(notifier.slackNotifier);
|
||||
}
|
||||
|
||||
if (notifier.notifierType === NotifierType.DISCORD && notifier.discordNotifier) {
|
||||
return validateDiscordNotifier(notifier.discordNotifier);
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -205,6 +217,7 @@ export function EditNotifierComponent({
|
||||
{ label: 'Email', value: NotifierType.EMAIL },
|
||||
{ label: 'Webhook', value: NotifierType.WEBHOOK },
|
||||
{ label: 'Slack', value: NotifierType.SLACK },
|
||||
{ label: 'Discord', value: NotifierType.DISCORD },
|
||||
]}
|
||||
onChange={(value) => {
|
||||
setNotifierType(value);
|
||||
@@ -251,6 +264,14 @@ export function EditNotifierComponent({
|
||||
setIsUnsaved={setIsUnsaved}
|
||||
/>
|
||||
)}
|
||||
|
||||
{notifier?.notifierType === NotifierType.DISCORD && (
|
||||
<EditDiscordNotifierComponent
|
||||
notifier={notifier}
|
||||
setNotifier={setNotifier}
|
||||
setIsUnsaved={setIsUnsaved}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mt-3 flex">
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { Input } from 'antd';
|
||||
|
||||
import type { Notifier } from '../../../../../entity/notifiers';
|
||||
|
||||
interface Props {
|
||||
notifier: Notifier;
|
||||
setNotifier: (notifier: Notifier) => void;
|
||||
setIsUnsaved: (isUnsaved: boolean) => void;
|
||||
}
|
||||
|
||||
export function EditDiscordNotifierComponent({ notifier, setNotifier, setIsUnsaved }: Props) {
|
||||
return (
|
||||
<>
|
||||
<div className="flex">
|
||||
<div className="min-w-[110px] max-w-[110px] pr-3">Channel webhook URL</div>
|
||||
|
||||
<div className="w-[250px]">
|
||||
<Input
|
||||
value={notifier?.discordNotifier?.channelWebhookUrl || ''}
|
||||
onChange={(e) => {
|
||||
if (!notifier?.discordNotifier) return;
|
||||
setNotifier({
|
||||
...notifier,
|
||||
discordNotifier: {
|
||||
...notifier.discordNotifier,
|
||||
channelWebhookUrl: e.target.value.trim(),
|
||||
},
|
||||
});
|
||||
setIsUnsaved(true);
|
||||
}}
|
||||
size="small"
|
||||
className="w-full"
|
||||
placeholder="1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="ml-[110px] max-w-[250px]">
|
||||
<div className="mt-1 text-xs text-gray-500">
|
||||
<strong>How to get Discord webhook URL:</strong>
|
||||
<br />
|
||||
<br />
|
||||
1. Create or select a Discord channel
|
||||
<br />
|
||||
2. Go to channel settings (gear icon)
|
||||
<br />
|
||||
3. Navigate to Integrations
|
||||
<br />
|
||||
4. Create a new webhook
|
||||
<br />
|
||||
5. Copy the webhook URL
|
||||
<br />
|
||||
<br />
|
||||
<em>Note: make sure make channel private if needed</em>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -34,13 +34,6 @@ export function EditWebhookNotifierComponent({ notifier, setNotifier, setIsUnsav
|
||||
placeholder="https://example.com/webhook"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Tooltip
|
||||
className="cursor-pointer"
|
||||
title="The URL that will be called when a notification is triggered"
|
||||
>
|
||||
<InfoCircleOutlined className="ml-2" style={{ color: 'gray' }} />
|
||||
</Tooltip>
|
||||
</div>
|
||||
|
||||
<div className="mt-1 flex items-center">
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { type Notifier, NotifierType } from '../../../../entity/notifiers';
|
||||
import { getNotifierLogoFromType } from '../../../../entity/notifiers/models/getNotifierLogoFromType';
|
||||
import { getNotifierNameFromType } from '../../../../entity/notifiers/models/getNotifierNameFromType';
|
||||
import { ShowDiscordNotifierComponent } from './notifier/ShowDiscordNotifierComponent';
|
||||
import { ShowEmailNotifierComponent } from './notifier/ShowEmailNotifierComponent';
|
||||
import { ShowSlackNotifierComponent } from './notifier/ShowSlackNotifierComponent';
|
||||
import { ShowTelegramNotifierComponent } from './notifier/ShowTelegramNotifierComponent';
|
||||
@@ -36,6 +37,10 @@ export function ShowNotifierComponent({ notifier }: Props) {
|
||||
{notifier?.notifierType === NotifierType.SLACK && (
|
||||
<ShowSlackNotifierComponent notifier={notifier} />
|
||||
)}
|
||||
|
||||
{notifier?.notifierType === NotifierType.DISCORD && (
|
||||
<ShowDiscordNotifierComponent notifier={notifier} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { Notifier } from '../../../../../entity/notifiers';
|
||||
|
||||
interface Props {
|
||||
notifier: Notifier;
|
||||
}
|
||||
|
||||
export function ShowDiscordNotifierComponent({ notifier }: Props) {
|
||||
return (
|
||||
<>
|
||||
<div className="flex">
|
||||
<div className="max-w-[110px] min-w-[110px] pr-3">Channel webhook URL</div>
|
||||
|
||||
<div className="w-[250px]">{notifier.webhookNotifier?.webhookUrl.slice(0, 10)}*******</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user