diff --git a/packages/api/src/routers/svc/reminders.ts b/packages/api/src/routers/svc/reminders.ts index 5da5dd4da..85f9ce6ea 100644 --- a/packages/api/src/routers/svc/reminders.ts +++ b/packages/api/src/routers/svc/reminders.ts @@ -1,17 +1,19 @@ import express from 'express' -import { analytics } from '../../utils/analytics' -import { initModels } from '../../server' -import { kx } from '../../datalayer/knex_config' -import { setClaims } from '../../datalayer/helpers' -import { sendEmail } from '../../utils/sendEmail' -import { env, homePageURL } from '../../env' import { MulticastMessage } from 'firebase-admin/messaging' +import { setClaims } from '../../datalayer/helpers' +import { kx } from '../../datalayer/knex_config' +import { createPubSubClient } from '../../datalayer/pubsub' +import { updatePage } from '../../elastic/pages' import { UserDeviceToken } from '../../entity/user_device_tokens' +import { env, homePageURL } from '../../env' import { ContentReader } from '../../generated/graphql' import { DataModels } from '../../resolvers/types' -import { updatePage } from '../../elastic/pages' -import { createPubSubClient } from '../../datalayer/pubsub' +import { initModels } from '../../server' import { getPagesWithReminder, PageReminder } from '../../services/reminders' +import { getDeviceTokensByUserId } from '../../services/user_device_tokens' +import { analytics } from '../../utils/analytics' +import { sendEmail } from '../../utils/sendEmail' +import { sendMulticastPushNotifications } from '../../utils/sendNotification' interface PageToNotify { title: string @@ -106,19 +108,19 @@ export function remindersServiceRouter() { to: user.email, }) - // // send push notifications - // const deviceTokens = await getDeviceTokensByUserId(userId) - // if (deviceTokens && deviceTokens.length > 0) { - // const message = messageForPages(pageReminders, deviceTokens) - // await sendMulticastPushNotifications(userId, message, 'reminder') - // } - // - // if (!deviceTokens) { - // console.log('Device tokens not set:', userId) - // - // res.status(400).send('Device token Not Found') - // return - // } + // send push notifications + const deviceTokens = await getDeviceTokensByUserId(userId) + if (deviceTokens && deviceTokens.length > 0) { + const message = messageForPages(pageReminders, deviceTokens) + await sendMulticastPushNotifications(userId, message, 'reminder') + } + + if (!deviceTokens) { + console.log('Device tokens not set:', userId) + + res.status(400).send('Device token Not Found') + return + } } await updateRemindersStatus(models, userId, pagesToUnarchive, remindAt) diff --git a/packages/api/src/services/save_newsletter_email.ts b/packages/api/src/services/save_newsletter_email.ts index b8e6ee8bf..4f3b3f603 100644 --- a/packages/api/src/services/save_newsletter_email.ts +++ b/packages/api/src/services/save_newsletter_email.ts @@ -9,9 +9,11 @@ import { ContentReader } from '../generated/graphql' import { analytics } from '../utils/analytics' import { isBase64Image } from '../utils/helpers' import { fetchFavicon } from '../utils/parser' +import { sendMulticastPushNotifications } from '../utils/sendNotification' import { addLabelToPage } from './labels' import { SaveContext, saveEmail, SaveEmailInput } from './save_email' import { saveSubscription } from './subscriptions' +import { getDeviceTokensByUserId } from './user_device_tokens' export interface NewsletterMessage { email: string @@ -93,19 +95,19 @@ export const saveNewsletterEmail = async ( }) console.log('newsletter label added:', result) - // // sends push notification - // const deviceTokens = await getDeviceTokensByUserId(newsletterEmail.user.id) - // if (!deviceTokens) { - // console.log('Device tokens not set:', newsletterEmail.user.id) - // return true - // } - // - // const multicastMessage = messageForLink(page, deviceTokens) - // await sendMulticastPushNotifications( - // newsletterEmail.user.id, - // multicastMessage, - // 'newsletter' - // ) + // sends push notification + const deviceTokens = await getDeviceTokensByUserId(newsletterEmail.user.id) + if (!deviceTokens) { + console.log('Device tokens not set:', newsletterEmail.user.id) + return true + } + + const multicastMessage = messageForLink(page, deviceTokens) + await sendMulticastPushNotifications( + newsletterEmail.user.id, + multicastMessage, + 'newsletter' + ) return true } diff --git a/packages/rule-handler/src/rule.ts b/packages/rule-handler/src/rule.ts index f7a9b799c..867340286 100644 --- a/packages/rule-handler/src/rule.ts +++ b/packages/rule-handler/src/rule.ts @@ -2,6 +2,7 @@ import axios, { AxiosResponse } from 'axios' import { filterPage } from './filter' import { getAuthToken, PubSubData } from './index' import { setLabels } from './label' +import { NotificationData, sendNotification } from './notification' import { archivePage, markPageAsRead } from './page' export enum RuleActionType { @@ -140,25 +141,25 @@ export const triggerActions = async ( filteredPage.readingProgressPercent < 100 && actionPromises.push(markPageAsRead(apiEndpoint, authToken, data.id)) ) - // case RuleActionType.SendNotification: { - // const data: NotificationData = { - // title: 'New page added to your library', - // body: filteredPage.title, - // image: filteredPage.image || undefined, - // } + case RuleActionType.SendNotification: { + const data: NotificationData = { + title: 'New page added to your library', + body: filteredPage.title, + image: filteredPage.image || undefined, + } - // const params = action.params - // if (params.length > 0) { - // const param = JSON.parse(params[0]) as NotificationData - // data.body = param.body || data.body - // data.title = param.title || data.title - // data.image = param.image || data.image - // } + const params = action.params + if (params.length > 0) { + const param = JSON.parse(params[0]) as NotificationData + data.body = param.body || data.body + data.title = param.title || data.title + data.image = param.image || data.image + } - // return actionPromises.push( - // sendNotification(apiEndpoint, authToken, data) - // ) - // } + return actionPromises.push( + sendNotification(apiEndpoint, authToken, data) + ) + } } }) }