From 3f0c1c17d766c36f7002aa175055fdc555b5d5ee Mon Sep 17 00:00:00 2001 From: Bamco Date: Sat, 25 Nov 2023 18:20:16 +0100 Subject: [PATCH] Add comment to push notification --- .../src/shared/goal-event/goal.events.ts | 1 + .../src/shared/notification/notification.ts | 15 +++++++++++---- libs/model/src/lib/notification.ts | 3 ++- .../src/lib/message/push-notification.ts | 9 ++++++--- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/backend-functions/src/shared/goal-event/goal.events.ts b/apps/backend-functions/src/shared/goal-event/goal.events.ts index a8e0d0d61..1a02838a5 100644 --- a/apps/backend-functions/src/shared/goal-event/goal.events.ts +++ b/apps/backend-functions/src/shared/goal-event/goal.events.ts @@ -9,6 +9,7 @@ export function addGoalEvent(name: EventType, source: GoalSource, id?: string) { milestoneId: source.milestoneId, supportId: source.supportId, postId: source.postId, + commentId: source.commentId, createdAt: serverTimestamp() as any, updatedAt: serverTimestamp() as any }) diff --git a/apps/backend-functions/src/shared/notification/notification.ts b/apps/backend-functions/src/shared/notification/notification.ts index 1ca2ab63a..e1698729b 100644 --- a/apps/backend-functions/src/shared/notification/notification.ts +++ b/apps/backend-functions/src/shared/notification/notification.ts @@ -1,7 +1,7 @@ import { admin, logger, db, serverTimestamp } from '@strive/api/firebase' import type { Message } from 'firebase-admin/messaging' // Interfaces -import { createNotificationBase, NotificationBase, GoalEvent, createGoalStakeholder, createPersonal, Goal, Milestone, Support, User, Notification, SupportBase, GoalStakeholder, Roles, PushNotificationSettingKey } from '@strive/model' +import { createNotificationBase, NotificationBase, GoalEvent, createGoalStakeholder, createPersonal, Goal, Milestone, Support, User, Notification, SupportBase, GoalStakeholder, Roles, PushNotificationSettingKey, Comment } from '@strive/model' import { getPushMessage, PushMessage, PushNotificationSetting, PushNotificationTarget } from '@strive/notification/message/push-notification' import { getDocument, toDate, unique } from '../utils' @@ -52,7 +52,7 @@ export async function sendGoalEventNotification( options: SendOptions, excludeTriggerer: boolean ) { - const { goalId, userId, milestoneId, supportId } = event + const { goalId, userId, milestoneId, supportId, commentId } = event const notificationBase = createNotificationBase({ ...event, event: event.name }) const notification: Notification = createNotificationBase(notificationBase) @@ -78,7 +78,8 @@ export async function sendGoalEventNotification( const milestonePromise = milestoneId ? getDocument(`Goals/${goalId}/Milestones/${milestoneId}`).then(milestone => notification.milestone = milestone) : undefined const supportPromise = supportId ? getDocument(`Goals/${goalId}/Supports/${supportId}`).then(support => notification.support = support) : undefined const userPromise = userId ? getDocument(`Users/${userId}`).then(user => notification.user = user) : undefined - await Promise.all([goalPromise, milestonePromise, supportPromise, userPromise]) + const commentPromise = commentId ? getDocument(`Goals/${goalId}/Comments/${commentId}`).then(comment => notification.comment = comment.text) : undefined + await Promise.all([goalPromise, milestonePromise, supportPromise, userPromise, commentPromise]) } if (options.toStakeholder?.notification) { @@ -170,9 +171,15 @@ function createPushMessage(message: PushMessage, token: string): Message { body: message.body }, data: { link }, + android: { + notification: { + tag: message.tag + } + }, webpush: { notification: { - icon: 'https://firebasestorage.googleapis.com/v0/b/strive-journal.appspot.com/o/FCMImages%2Ficon-72x72.png?alt=media&token=19250b44-1aef-4ea6-bbaf-d888150fe4a9' + icon: 'https://firebasestorage.googleapis.com/v0/b/strive-journal.appspot.com/o/FCMImages%2Ficon-72x72.png?alt=media&token=19250b44-1aef-4ea6-bbaf-d888150fe4a9', + tag: message.tag }, fcmOptions: { link } } diff --git a/libs/model/src/lib/notification.ts b/libs/model/src/lib/notification.ts index 8d567bcec..74c03d7c0 100644 --- a/libs/model/src/lib/notification.ts +++ b/libs/model/src/lib/notification.ts @@ -104,7 +104,8 @@ export interface Notification extends NotificationBase { user?: User, goal?: Goal, milestone?: Milestone, - support?: SupportBase + support?: SupportBase, + comment?: string } export function createNotificationBase(params: Partial = {}): NotificationBase { diff --git a/libs/notification/src/lib/message/push-notification.ts b/libs/notification/src/lib/message/push-notification.ts index 42c34afff..95a6da0b1 100644 --- a/libs/notification/src/lib/message/push-notification.ts +++ b/libs/notification/src/lib/message/push-notification.ts @@ -22,6 +22,7 @@ export interface PushMessage { title: string body: string link: string + tag?: string // groups the notifications together setting: keyof typeof PushNotificationSetting } @@ -31,7 +32,7 @@ export function getPushMessage(notification: Notification, target: PushNotificat if (target === 'spectator') return getSpectatorPushMessage(notification) } -function getStakeholderPushMessage({ event, goal, milestone, user }: Notification): PushMessage | void { +function getStakeholderPushMessage({ event, goal, milestone, user, comment }: Notification): PushMessage | void { switch (event) { case 'goalDeadlinePassed': if (!goal) throw new Error(`${event} push message needs goal defined`) @@ -155,11 +156,13 @@ function getStakeholderPushMessage({ event, goal, milestone, user }: Notificatio case 'goalChatMessageCreated': if (!goal) throw new Error(`${event} push message needs goal defined`) if (!user) throw new Error(`${event} push message needs user defined`) + if (!comment) throw new Error(`${event} push message needs comment defined`) return { - title: goal.title, - body: `${user.username} sent a message in chat`, + title: `${user.username} in ${goal.title}`, + body: comment, link: `/goal/${goal.id}`, + tag: `goal/${goal.id}/chat`, setting: 'goalChat' }