-
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix contact and comment webhook delivery #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import { createClient } from '@supabase/supabase-js' | ||
| import 'server-only' | ||
|
|
||
| import { getServiceDatabase } from '@/lib/supabaseAdmin' | ||
|
|
||
| type DeliveryKind = 'contact' | 'comments' | ||
|
|
||
|
|
@@ -15,29 +17,63 @@ export async function getWebhookDelivery(kind: DeliveryKind) { | |
| const fallbackUrl = kind === 'contact' | ||
| ? process.env.CONTACT_DISCORD_WEBHOOK_URL | ||
| : process.env.COMMENTS_DISCORD_WEBHOOK_URL | ||
| const url = process.env.NEXT_PUBLIC_SUPABASE_URL | ||
| const serviceKey = process.env.SUPABASE_SERVICE_ROLE_KEY | ||
|
|
||
| if (!url || !serviceKey) return { url: fallbackUrl || '', message: '' } | ||
| const database = getServiceDatabase() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Useful? React with 👍 / 👎. |
||
| if (!database) return { url: clean(fallbackUrl), message: '' } | ||
|
|
||
| try { | ||
| const database = createClient(url, serviceKey, { auth: { persistSession: false } }) | ||
| const { data } = await database | ||
| const { data, error } = await database | ||
| .from('webhook_settings') | ||
| .select('contact_webhook_url, comments_webhook_url, contact_custom_message, comments_custom_message') | ||
| .eq('id', 1) | ||
| .single<DeliverySettings>() | ||
|
|
||
| if (error) throw error | ||
|
|
||
| return kind === 'contact' | ||
| ? { url: clean(data?.contact_webhook_url) || fallbackUrl || '', message: clean(data?.contact_custom_message) } | ||
| : { url: clean(data?.comments_webhook_url) || fallbackUrl || '', message: clean(data?.comments_custom_message) } | ||
| ? { url: clean(data?.contact_webhook_url) || clean(fallbackUrl), message: clean(data?.contact_custom_message) } | ||
| : { url: clean(data?.comments_webhook_url) || clean(fallbackUrl), message: clean(data?.comments_custom_message) } | ||
| } catch { | ||
| // The environment variables keep delivery working until the migration is applied. | ||
| return { url: fallbackUrl || '', message: '' } | ||
| return { url: clean(fallbackUrl), message: '' } | ||
| } | ||
| } | ||
|
|
||
| export function renderWebhookMessage(template: string, values: Record<string, string>, fallback: string) { | ||
| const source = template || fallback | ||
| return source.replace(/\{\{(name|email|message|comment)\}\}/g, (_, key: string) => values[key] || '') | ||
| } | ||
|
|
||
| export function isDiscordWebhookUrl(value: string) { | ||
| try { | ||
| const url = new URL(value) | ||
| const allowedHosts = new Set([ | ||
| 'discord.com', | ||
| 'discordapp.com', | ||
| 'canary.discord.com', | ||
| 'ptb.discord.com', | ||
| ]) | ||
|
|
||
| return url.protocol === 'https:' | ||
| && allowedHosts.has(url.hostname) | ||
| && /^\/api\/webhooks\/\d+\/[A-Za-z0-9_-]+\/?$/.test(url.pathname) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Discord's API also accepts webhook endpoints under versioned paths such as Useful? React with 👍 / 👎. |
||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| export async function sendDiscordWebhook(url: string, payload: Record<string, unknown>) { | ||
| if (!url) throw new Error('Discord webhook is not configured.') | ||
| if (!isDiscordWebhookUrl(url)) throw new Error('Discord webhook URL is invalid.') | ||
|
|
||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(payload), | ||
| cache: 'no-store', | ||
| signal: AbortSignal.timeout(10_000), | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Discord webhook delivery failed with status ${response.status}.`) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an existing deployment has working
GMAIL_USER/GMAIL_PASSWORDsettings but no contact-specific Discord webhook,sendDiscordWebhookrejects even though the concurrently executed email is successfully sent, and the rejection path returns 502. This regresses the previously supported email-only configuration, tells visitors to retry a message that was already emailed and persisted, and can create duplicate emails and database rows; treat the request as delivered when either configured channel succeeds, or skip an unconfigured webhook.Useful? React with 👍 / 👎.