diff --git a/packages/rule-handler/src/page.ts b/packages/rule-handler/src/page.ts new file mode 100644 index 000000000..0650b775a --- /dev/null +++ b/packages/rule-handler/src/page.ts @@ -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) + } +} diff --git a/packages/rule-handler/src/rule.ts b/packages/rule-handler/src/rule.ts index 7dc01c70b..50d2838e6 100644 --- a/packages/rule-handler/src/rule.ts +++ b/packages/rule-handler/src/rule.ts @@ -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)