allow label and highlight events to trigger rules

This commit is contained in:
Hongbo Wu
2024-03-21 17:24:24 +08:00
parent e28dd73f9c
commit 07cee98ff9
5 changed files with 32 additions and 20 deletions

View File

@ -15,11 +15,14 @@ export enum RuleActionType {
Delete = 'DELETE',
MarkAsRead = 'MARK_AS_READ',
SendNotification = 'SEND_NOTIFICATION',
Webhook = 'WEBHOOK',
}
export enum RuleEventType {
PageCreated = 'PAGE_CREATED',
PageUpdated = 'PAGE_UPDATED',
LabelCreated = 'PAGE_CREATED',
HighlightCreated = 'HIGHLIGHT_CREATED',
}
export interface RuleAction {

View File

@ -86,7 +86,9 @@ const sendNotification = async (obj: RuleActionObj) => {
return sendPushNotifications(obj.userId, message, 'rule', data)
}
const getRuleAction = (actionType: RuleActionType): RuleActionFunc => {
const getRuleAction = (
actionType: RuleActionType
): RuleActionFunc | undefined => {
switch (actionType) {
case RuleActionType.AddLabel:
return addLabels
@ -98,6 +100,9 @@ const getRuleAction = (actionType: RuleActionType): RuleActionFunc => {
return markPageAsRead
case RuleActionType.SendNotification:
return sendNotification
default:
logger.error('Unknown rule action type', actionType)
return undefined
}
}
@ -150,6 +155,11 @@ const triggerActions = async (
for (const action of rule.actions) {
const actionFunc = getRuleAction(action.type)
if (!actionFunc) {
logger.error('No action function found for action', action.type)
continue
}
const actionObj: RuleActionObj = {
libraryItemId,
userId,

View File

@ -53,14 +53,12 @@ export const createPubSubClient = (): PubsubClient => {
libraryItemId: string
): Promise<void> => {
// queue trigger rule job
if (type === EntityType.PAGE) {
await enqueueTriggerRuleJob({
userId,
ruleEventType: RuleEventType.PageCreated,
libraryItemId,
data,
})
}
await enqueueTriggerRuleJob({
userId,
ruleEventType: `${type.toUpperCase()}_CREATED` as RuleEventType,
libraryItemId,
data,
})
// queue export item job
await enqueueExportItem({
userId,

View File

@ -9,6 +9,7 @@ import { createPubSubClient, EntityType } from '../pubsub'
import { authTrx } from '../repository'
import { highlightRepository } from '../repository/highlight'
import { enqueueUpdateHighlight } from '../utils/createTask'
import { UpdateItemEvent } from './library_item'
type HighlightEvent = { id: string; pageId: string }
type CreateHighlightEvent = DeepPartial<Highlight> & HighlightEvent
@ -56,9 +57,9 @@ export const createHighlight = async (
userId
)
await pubsub.entityCreated<CreateHighlightEvent>(
await pubsub.entityCreated<UpdateItemEvent>(
EntityType.HIGHLIGHT,
{ ...newHighlight, pageId: libraryItemId },
{ id: libraryItemId, highlights: [newHighlight] },
userId,
libraryItemId
)
@ -104,9 +105,9 @@ export const mergeHighlights = async (
})
})
await pubsub.entityCreated<CreateHighlightEvent>(
await pubsub.entityCreated<UpdateItemEvent>(
EntityType.HIGHLIGHT,
{ ...newHighlight, pageId: libraryItemId },
{ id: libraryItemId, highlights: [newHighlight] },
userId,
libraryItemId
)
@ -139,9 +140,9 @@ export const updateHighlight = async (
})
const libraryItemId = updatedHighlight.libraryItem.id
await pubsub.entityUpdated<UpdateHighlightEvent>(
await pubsub.entityUpdated<UpdateItemEvent>(
EntityType.HIGHLIGHT,
{ ...highlight, id: highlightId, pageId: libraryItemId },
{ id: libraryItemId, highlights: [highlight] },
userId,
libraryItemId
)

View File

@ -8,7 +8,7 @@ import { CreateLabelInput, labelRepository } from '../repository/label'
import { bulkEnqueueUpdateLabels } from '../utils/createTask'
import { logger } from '../utils/logger'
import { findHighlightById } from './highlights'
import { findLibraryItemIdsByLabelId } from './library_item'
import { findLibraryItemIdsByLabelId, UpdateItemEvent } from './library_item'
type AddLabelsToLibraryItemEvent = {
pageId: string
@ -144,9 +144,9 @@ export const saveLabelsInLibraryItem = async (
if (source === 'user') {
// create pubsub event
await pubsub.entityCreated<AddLabelsToLibraryItemEvent>(
await pubsub.entityCreated<UpdateItemEvent>(
EntityType.LABEL,
{ pageId: libraryItemId, labels, source },
{ id: libraryItemId, labels },
userId,
libraryItemId
)
@ -215,9 +215,9 @@ export const saveLabelsInHighlight = async (
const libraryItemId = highlight.libraryItemId
// create pubsub event
await pubsub.entityCreated<AddLabelsToHighlightEvent>(
await pubsub.entityCreated<UpdateItemEvent>(
EntityType.LABEL,
{ highlightId, labels },
{ id: libraryItemId, highlights: [{ id: highlightId, labels }] },
userId,
libraryItemId
)