Add mark page as read and archived action in rule engine

This commit is contained in:
Hongbo Wu
2022-11-23 11:55:01 +08:00
parent a67b2867ee
commit aaa74c8ba3
2 changed files with 95 additions and 1 deletions

View File

@ -0,0 +1,82 @@
import { getAuthToken } from './index'
import axios from 'axios'
export const archivePage = async (
pageId: string,
userId: string,
apiEndpoint: string,
jwtSecret: string
) => {
const auth = await getAuthToken(userId, jwtSecret)
const data = JSON.stringify({
query: `mutation ArchivePage($input: ArchivePageInput!) {
archivePage(input: $input) {
... on ArchivePageSuccess {
page {
id
}
}
... on ArchivePageError {
errorCodes
}
}
}`,
variables: {
input: {
pageId,
},
},
})
try {
await axios.post(`${apiEndpoint}/graphql`, data, {
headers: {
Cookie: `auth=${auth};`,
'Content-Type': 'application/json',
},
})
} catch (e) {
console.error(e)
}
}
export const markPageAsRead = async (
pageId: string,
userId: string,
apiEndpoint: string,
jwtSecret: string
) => {
const auth = await getAuthToken(userId, jwtSecret)
const data = JSON.stringify({
query: `mutation MarkPageAsRead($input: MarkPageAsReadInput!) {
markPageAsRead(input: $input) {
... on MarkPageAsReadSuccess {
page {
id
}
}
... on MarkPageAsReadError {
errorCodes
}
}
}`,
variables: {
input: {
pageId,
},
},
})
try {
await axios.post(`${apiEndpoint}/graphql`, data, {
headers: {
Cookie: `auth=${auth};`,
'Content-Type': 'application/json',
},
})
} catch (e) {
console.error(e)
}
}

View File

@ -3,6 +3,7 @@ import { getAuthToken, PubSubData } from './index'
import axios from 'axios'
import { parse, SearchParserKeyWordOffset } from 'search-query-parser'
import { addLabels } from './label'
import { archivePage, markPageAsRead } from './page'
export enum RuleActionType {
AddLabel = 'ADD_LABEL',
@ -148,8 +149,19 @@ export const triggerActions = async (
)
break
case RuleActionType.Archive:
if (!data.id) {
console.log('invalid data for archive action')
continue
}
await archivePage(userId, apiEndpoint, jwtSecret, data.id)
break
case RuleActionType.MarkAsRead:
continue
if (!data.id) {
console.log('invalid data for mark as read action')
continue
}
await markPageAsRead(userId, apiEndpoint, jwtSecret, data.id)
break
case RuleActionType.SendNotification:
for (const message of action.params) {
await sendNotification(userId, apiEndpoint, jwtSecret, message)