From d73111dd3d33ce7f064d509febaeea1a35f49af6 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 16 Jun 2025 10:31:14 +0100 Subject: [PATCH] feat: send ama notifications via pushover (#1636) --- nuxt.config.ts | 5 +++++ server/api/question.ts | 8 +++++++- server/utils/pushover.ts | 31 +++++++++++++++++++++++++++++++ test/unit/bundle.spec.ts | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 server/utils/pushover.ts diff --git a/nuxt.config.ts b/nuxt.config.ts index e958b003..20671e4e 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -127,6 +127,11 @@ export default defineNuxtConfig({ sessionPassword: '', // emailing ideas resendApiKey: '', + // Pushover notifications + pushover: { + token: '', + userKey: '', + }, // Location API key for secure updates locationApiKey: '', bigdataApiKey: '', diff --git a/server/api/question.ts b/server/api/question.ts index 68655eef..0b6ccaa2 100644 --- a/server/api/question.ts +++ b/server/api/question.ts @@ -1,3 +1,5 @@ +import { sendPushoverNotification } from '../utils/pushover' + export default defineEventHandler(async event => { if (event.method === 'OPTIONS') return null const sanity = useSanity(event) @@ -17,6 +19,10 @@ export default defineEventHandler(async event => { }, }).catch(console.error)) - await sendEmail(event, 'Anonymous question', question) + await sendPushoverNotification(event, { + title: 'Anonymous question', + message: question, + priority: 0, + }) return null }) diff --git a/server/utils/pushover.ts b/server/utils/pushover.ts new file mode 100644 index 00000000..ed7ac6bb --- /dev/null +++ b/server/utils/pushover.ts @@ -0,0 +1,31 @@ +import type { H3Event } from 'h3' + +export interface PushoverOptions { + title: string + message: string + priority?: -2 | -1 | 0 | 1 | 2 + url?: string + urlTitle?: string +} + +export function sendPushoverNotification (event: H3Event, options: PushoverOptions) { + const config = useRuntimeConfig(event) + + if (!config.pushover.token || !config.pushover.userKey) { + console.warn('Pushover credentials not configured') + return Promise.resolve() + } + + return $fetch('https://api.pushover.net/1/messages.json', { + method: 'POST', + body: { + token: config.pushover.token, + user: config.pushover.userKey, + title: options.title, + message: options.message, + priority: String(options.priority ?? 0), + ...(options.url && { url: options.url }), + ...(options.urlTitle && { url_title: options.urlTitle }), + }, + }) +} diff --git a/test/unit/bundle.spec.ts b/test/unit/bundle.spec.ts index 1478844b..35b31290 100644 --- a/test/unit/bundle.spec.ts +++ b/test/unit/bundle.spec.ts @@ -79,7 +79,7 @@ describe('project sizes', () => { stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir) expect .soft(roundToKilobytes(stats.server.totalBytes)) - .toMatchInlineSnapshot(`"824k"`) + .toMatchInlineSnapshot(`"825k"`) const modules = await analyzeSizes('node_modules/**/*', serverDir) expect -- 2.51.2