From 8a94bbfba322f8011017980e4362d46c4d51bb55 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 24 Jul 2024 21:24:09 -0700 Subject: feat: background notifications --- src/jobs/index.ts | 1 + src/jobs/notifications.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/jobs/index.ts create mode 100644 src/jobs/notifications.ts (limited to 'src/jobs') diff --git a/src/jobs/index.ts b/src/jobs/index.ts new file mode 100644 index 00000000..9ea5ce77 --- /dev/null +++ b/src/jobs/index.ts @@ -0,0 +1 @@ +export * from './notifications'; diff --git a/src/jobs/notifications.ts b/src/jobs/notifications.ts new file mode 100644 index 00000000..3c29ecfc --- /dev/null +++ b/src/jobs/notifications.ts @@ -0,0 +1,27 @@ +import { intervalTrigger } from '@trigger.dev/sdk'; +import { client } from '../trigger'; +import * as webpush from 'web-push'; +import { env as privateEnv } from '$env/dynamic/private'; +import { env } from '$env/dynamic/public'; +import { getUserSubscriptions } from '$lib/Database/userNotifications'; + +webpush.setVapidDetails( + privateEnv.VAPID_SUBJECT, + env.PUBLIC_VAPID_PUBLIC_KEY, + privateEnv.VAPID_PRIVATE_KEY +); + +client.defineJob({ + id: 'notifications', + name: 'Notifications', + version: '0.0.1', + trigger: intervalTrigger({ + seconds: 20 + }), + run: async () => { + for (const subscription of await getUserSubscriptions()) + await webpush.sendNotification(subscription['subscription'], '.'); + + return {}; + } +}); -- cgit v1.2.3