Skip to content

Commit

Permalink
Add comment to push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
RemcoSimonides committed Nov 25, 2023
1 parent a5fccc2 commit 3f0c1c1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
15 changes: 11 additions & 4 deletions apps/backend-functions/src/shared/notification/notification.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)
Expand All @@ -78,7 +78,8 @@ export async function sendGoalEventNotification(
const milestonePromise = milestoneId ? getDocument<Milestone>(`Goals/${goalId}/Milestones/${milestoneId}`).then(milestone => notification.milestone = milestone) : undefined
const supportPromise = supportId ? getDocument<Support>(`Goals/${goalId}/Supports/${supportId}`).then(support => notification.support = support) : undefined
const userPromise = userId ? getDocument<User>(`Users/${userId}`).then(user => notification.user = user) : undefined
await Promise.all([goalPromise, milestonePromise, supportPromise, userPromise])
const commentPromise = commentId ? getDocument<Comment>(`Goals/${goalId}/Comments/${commentId}`).then(comment => notification.comment = comment.text) : undefined
await Promise.all([goalPromise, milestonePromise, supportPromise, userPromise, commentPromise])
}

if (options.toStakeholder?.notification) {
Expand Down Expand Up @@ -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 }
}
Expand Down
3 changes: 2 additions & 1 deletion libs/model/src/lib/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {}): NotificationBase {
Expand Down
9 changes: 6 additions & 3 deletions libs/notification/src/lib/message/push-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface PushMessage {
title: string
body: string
link: string
tag?: string // groups the notifications together
setting: keyof typeof PushNotificationSetting
}

Expand All @@ -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`)
Expand Down Expand Up @@ -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'
}

Expand Down

0 comments on commit 3f0c1c1

Please sign in to comment.