mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Merge pull request #1459 from omnivore-app/fix/rule-engine
Fix some errors in rule engine
This commit is contained in:
commit
db3d4c7cfd
3 changed files with 48 additions and 21 deletions
|
|
@ -9,10 +9,17 @@ interface SearchResponse {
|
|||
}
|
||||
|
||||
interface Edge {
|
||||
node: Node
|
||||
node: Page
|
||||
}
|
||||
|
||||
interface Node {
|
||||
interface Page {
|
||||
id: string
|
||||
labels: Label[]
|
||||
isArchived: boolean
|
||||
readingProgressPercent: number
|
||||
}
|
||||
|
||||
interface Label {
|
||||
id: string
|
||||
}
|
||||
|
||||
|
|
@ -21,7 +28,7 @@ export const search = async (
|
|||
apiEndpoint: string,
|
||||
auth: string,
|
||||
query: string
|
||||
): Promise<Node[]> => {
|
||||
): Promise<Page[]> => {
|
||||
const requestData = JSON.stringify({
|
||||
query: `query Search($query: String) {
|
||||
search(query: $query) {
|
||||
|
|
@ -29,6 +36,11 @@ export const search = async (
|
|||
edges {
|
||||
node {
|
||||
id
|
||||
labels {
|
||||
id
|
||||
}
|
||||
isArchived
|
||||
readingProgressPercent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -55,7 +67,7 @@ export const search = async (
|
|||
)
|
||||
|
||||
const edges = response.data.data.search.edges
|
||||
if (edges.length == 0) {
|
||||
if (edges.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
|
|
@ -67,15 +79,15 @@ export const search = async (
|
|||
}
|
||||
}
|
||||
|
||||
export const isMatched = async (
|
||||
export const filterPage = async (
|
||||
userId: string,
|
||||
apiEndpoint: string,
|
||||
auth: string,
|
||||
filter: string,
|
||||
pageId: string
|
||||
): Promise<boolean> => {
|
||||
): Promise<Page | null> => {
|
||||
filter += ` includes:${pageId}`
|
||||
const nodes = await search(userId, apiEndpoint, auth, filter)
|
||||
const pages = await search(userId, apiEndpoint, auth, filter)
|
||||
|
||||
return nodes.length > 0
|
||||
return pages.length > 0 ? pages[0] : null
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import axios from 'axios'
|
||||
|
||||
export const addLabels = async (
|
||||
export const setLabels = async (
|
||||
apiEndpoint: string,
|
||||
auth: string,
|
||||
pageId: string,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { sendNotification } from './notification'
|
||||
import { getAuthToken, PubSubData } from './index'
|
||||
import axios, { AxiosResponse } from 'axios'
|
||||
import { addLabels } from './label'
|
||||
import { setLabels } from './label'
|
||||
import { archivePage, markPageAsRead } from './page'
|
||||
import { isMatched } from './filter'
|
||||
import { filterPage } from './filter'
|
||||
|
||||
export enum RuleActionType {
|
||||
AddLabel = 'ADD_LABEL',
|
||||
|
|
@ -79,26 +79,41 @@ export const triggerActions = async (
|
|||
const actionPromises: Promise<AxiosResponse<any, any> | undefined>[] = []
|
||||
|
||||
for (const rule of rules) {
|
||||
if (
|
||||
!(await isMatched(userId, apiEndpoint, authToken, rule.filter, data.id))
|
||||
) {
|
||||
const filteredPage = await filterPage(
|
||||
userId,
|
||||
apiEndpoint,
|
||||
authToken,
|
||||
rule.filter,
|
||||
data.id
|
||||
)
|
||||
if (!filteredPage) {
|
||||
continue
|
||||
}
|
||||
|
||||
rule.actions.forEach((action) => {
|
||||
switch (action.type) {
|
||||
case RuleActionType.AddLabel:
|
||||
data.id &&
|
||||
actionPromises.push(
|
||||
addLabels(apiEndpoint, authToken, data.id, action.params)
|
||||
)
|
||||
case RuleActionType.AddLabel: {
|
||||
const existingLabelIds = filteredPage.labels.map((label) => label.id)
|
||||
const newLabelIds = action.params
|
||||
if (newLabelIds.every((id) => existingLabelIds.includes(id))) {
|
||||
// All labels are already set
|
||||
return
|
||||
}
|
||||
|
||||
// combine existing labels with new labels in a set to avoid duplicates
|
||||
const labelIds = new Set([...existingLabelIds, ...newLabelIds])
|
||||
|
||||
actionPromises.push(
|
||||
setLabels(apiEndpoint, authToken, data.id, Array.from(labelIds))
|
||||
)
|
||||
break
|
||||
}
|
||||
case RuleActionType.Archive:
|
||||
data.id &&
|
||||
!filteredPage.isArchived &&
|
||||
actionPromises.push(archivePage(apiEndpoint, authToken, data.id))
|
||||
break
|
||||
case RuleActionType.MarkAsRead:
|
||||
data.id &&
|
||||
filteredPage.readingProgressPercent < 100 &&
|
||||
actionPromises.push(markPageAsRead(apiEndpoint, authToken, data.id))
|
||||
break
|
||||
case RuleActionType.SendNotification:
|
||||
|
|
|
|||
Loading…
Reference in a new issue