Get notification data from params

This commit is contained in:
Hongbo Wu
2022-12-09 10:41:23 +08:00
parent 53bd6a96f2
commit e85ec51e6c
3 changed files with 31 additions and 11 deletions

View File

@ -17,6 +17,8 @@ interface Page {
labels?: Label[] // labels is optional in the API response
isArchived: boolean
readingProgressPercent: number
title: string
image?: string
}
interface Label {
@ -41,6 +43,8 @@ export const search = async (
}
isArchived
readingProgressPercent
title
image
}
}
}

View File

@ -8,13 +8,17 @@ interface RequestData {
notificationType?: string
}
export interface NotificationData {
body: string
title?: string
image?: string
data?: Record<string, string>
}
export const sendNotification = async (
apiEndpoint: string,
auth: string,
body: string,
title?: string,
image?: string,
data?: Record<string, string>
{ body, title, image, data }: NotificationData
) => {
const requestData: RequestData = {
body,

View File

@ -1,4 +1,4 @@
import { sendNotification } from './notification'
import { NotificationData, sendNotification } from './notification'
import { getAuthToken, PubSubData } from './index'
import axios, { AxiosResponse } from 'axios'
import { setLabels } from './label'
@ -122,14 +122,26 @@ export const triggerActions = async (
filteredPage.readingProgressPercent < 100 &&
actionPromises.push(markPageAsRead(apiEndpoint, authToken, data.id))
)
case RuleActionType.SendNotification:
case RuleActionType.SendNotification: {
const data: NotificationData = {
title: 'New page added to your feed',
body: filteredPage.title,
image: filteredPage.image,
}
const params = action.params
if (params.length > 0) {
const param = JSON.parse(params[0]) as NotificationData
data.body = param.body
data.title = param.title
data.image = param.image
data.data = param.data
}
return actionPromises.push(
sendNotification(
apiEndpoint,
authToken,
'New page added to your feed'
)
sendNotification(apiEndpoint, authToken, data)
)
}
}
})
}