Merge pull request #1446 from omnivore-app/add-label-rule
Add label actions in rule engine
This commit is contained in:
@ -19,6 +19,7 @@ interface PubSubRequestBody {
|
||||
}
|
||||
|
||||
export interface PubSubData {
|
||||
id: string
|
||||
userId: string
|
||||
type: EntityType
|
||||
subscription?: string
|
||||
|
||||
44
packages/rule-handler/src/label.ts
Normal file
44
packages/rule-handler/src/label.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import axios from 'axios'
|
||||
import { getAuthToken } from './index'
|
||||
|
||||
export const addLabels = async (
|
||||
userId: string,
|
||||
apiEndpoint: string,
|
||||
jwtSecret: string,
|
||||
pageId: string,
|
||||
labelIds: string[]
|
||||
) => {
|
||||
const auth = await getAuthToken(userId, jwtSecret)
|
||||
|
||||
const data = JSON.stringify({
|
||||
query: `mutation SetLabels($input: SetLabelsInput!) {
|
||||
setLabels(input: $input) {
|
||||
... on SetLabelsSuccess {
|
||||
labels {
|
||||
id
|
||||
}
|
||||
}
|
||||
... on SetLabelsError {
|
||||
errorCodes
|
||||
}
|
||||
}
|
||||
}`,
|
||||
variables: {
|
||||
input: {
|
||||
pageId,
|
||||
labelIds,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await axios.post(`${apiEndpoint}/graphql`, data, {
|
||||
headers: {
|
||||
Cookie: `auth=${auth};`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ import { sendNotification } from './notification'
|
||||
import { getAuthToken, PubSubData } from './index'
|
||||
import axios from 'axios'
|
||||
import { parse, SearchParserKeyWordOffset } from 'search-query-parser'
|
||||
import { addLabels } from './label'
|
||||
|
||||
export enum RuleActionType {
|
||||
AddLabel = 'ADD_LABEL',
|
||||
@ -134,6 +135,18 @@ export const triggerActions = async (
|
||||
for (const action of rule.actions) {
|
||||
switch (action.type) {
|
||||
case RuleActionType.AddLabel:
|
||||
if (!data.id || action.params.length === 0) {
|
||||
console.log('invalid data for add label action')
|
||||
continue
|
||||
}
|
||||
await addLabels(
|
||||
userId,
|
||||
apiEndpoint,
|
||||
jwtSecret,
|
||||
data.id,
|
||||
action.params
|
||||
)
|
||||
break
|
||||
case RuleActionType.Archive:
|
||||
case RuleActionType.MarkAsRead:
|
||||
continue
|
||||
@ -141,6 +154,7 @@ export const triggerActions = async (
|
||||
for (const message of action.params) {
|
||||
await sendNotification(userId, apiEndpoint, jwtSecret, message)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user