From e29c6b48427deb07e2c8ba135dcb2d3fc30f59be Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 26 Mar 2025 10:59:27 -0500 Subject: [PATCH] Refactor reminder push notifications (#2026) * Refactor reminder push notifications items should always exist and if not, we can just immediately fail imo * Use same text for reminders in /notifications --- components/notifications.js | 2 +- lib/webPush.js | 18 +++++++----------- worker/reminder.js | 16 ++++------------ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/components/notifications.js b/components/notifications.js index c03ebdf9..72dfc46e 100644 --- a/components/notifications.js +++ b/components/notifications.js @@ -700,7 +700,7 @@ function Reminder ({ n }) { return ( <> - you asked to be reminded of this {n.item.title ? 'post' : 'comment'} + you requested this reminder diff --git a/lib/webPush.js b/lib/webPush.js index 89128f09..9f0b8acc 100644 --- a/lib/webPush.js +++ b/lib/webPush.js @@ -448,15 +448,11 @@ export async function notifyStreakLost (userId, streak) { } } -export async function notifyReminder ({ userId, item, itemId }) { - try { - await sendUserNotification(userId, { - title: 'this is your requested reminder', - body: `you asked to be reminded of this ${item ? item.title ? 'post' : 'comment' : 'item'}`, - tag: `REMIND-ITEM-${item?.id ?? itemId}`, - item: item ?? { id: itemId } - }) - } catch (err) { - console.error(err) - } +export async function notifyReminder ({ userId, item }) { + await sendUserNotification(userId, { + title: 'you requested this reminder', + body: item.title ?? item.text, + tag: `REMIND-ITEM-${item.id}`, + item + }) } diff --git a/worker/reminder.js b/worker/reminder.js index 49a1c85c..bf5a60ed 100644 --- a/worker/reminder.js +++ b/worker/reminder.js @@ -1,18 +1,10 @@ import { notifyReminder } from '@/lib/webPush' + export async function remindUser ({ data: { itemId, userId }, models }) { - let item try { - item = await models.item.findUnique({ where: { id: itemId } }) + const item = await models.item.findUnique({ where: { id: itemId } }) + await notifyReminder({ userId, item }) } catch (err) { - console.error('failed to lookup item by id', err) - } - try { - if (item) { - await notifyReminder({ userId, item, itemId }) - } else { - await notifyReminder({ userId, itemId }) - } - } catch (err) { - console.error('failed to send push notification for reminder', err) + console.error('failed to send reminder:', err) } }