mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
enqueue export job
This commit is contained in:
parent
8b848912c9
commit
f94267ee1a
7 changed files with 82 additions and 23 deletions
|
|
@ -1,21 +1,30 @@
|
|||
import { IntegrationType } from '../entity/integration'
|
||||
import { LibraryItem } from '../entity/library_item'
|
||||
import {
|
||||
findIntegrations,
|
||||
getIntegrationClient,
|
||||
updateIntegration,
|
||||
} from '../services/integrations'
|
||||
import { findLibraryItemById } from '../services/library_item'
|
||||
import { logger } from '../utils/logger'
|
||||
|
||||
export interface ExportItemJobData {
|
||||
userId: string
|
||||
libraryItem: LibraryItem
|
||||
libraryItemId: string
|
||||
}
|
||||
|
||||
export const EXPORT_ITEM_JOB_NAME = 'export-item'
|
||||
|
||||
export const exportItem = async (jobData: ExportItemJobData) => {
|
||||
const { libraryItem, userId } = jobData
|
||||
const { libraryItemId, userId } = jobData
|
||||
const libraryItem = await findLibraryItemById(libraryItemId, userId)
|
||||
if (!libraryItem) {
|
||||
logger.error('library item not found', {
|
||||
userId,
|
||||
libraryItemId,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const integrations = await findIntegrations(userId, {
|
||||
enabled: true,
|
||||
type: IntegrationType.Export,
|
||||
|
|
@ -29,7 +38,7 @@ export const exportItem = async (jobData: ExportItemJobData) => {
|
|||
integrations.map(async (integration) => {
|
||||
const logObject = {
|
||||
userId,
|
||||
libraryItemId: libraryItem.id,
|
||||
libraryItemId,
|
||||
integrationId: integration.id,
|
||||
}
|
||||
logger.info('exporting item...', logObject)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,12 @@ import express from 'express'
|
|||
import { RuleEventType } from './entity/rule'
|
||||
import { env } from './env'
|
||||
import { ReportType } from './generated/graphql'
|
||||
import { enqueueTriggerRuleJob, enqueueWebhookJob } from './utils/createTask'
|
||||
import { Merge } from './util'
|
||||
import {
|
||||
enqueueExportItem,
|
||||
enqueueTriggerRuleJob,
|
||||
enqueueWebhookJob,
|
||||
} from './utils/createTask'
|
||||
import { deepDelete } from './utils/helpers'
|
||||
import { buildLogger } from './utils/logger'
|
||||
|
||||
|
|
@ -11,6 +16,8 @@ const logger = buildLogger('pubsub')
|
|||
|
||||
const client = new PubSub()
|
||||
|
||||
type EntityData<T> = Merge<T, { libraryItemId: string }>
|
||||
|
||||
export const createPubSubClient = (): PubsubClient => {
|
||||
const fieldsToDelete = ['user'] as const
|
||||
|
||||
|
|
@ -45,12 +52,12 @@ export const createPubSubClient = (): PubsubClient => {
|
|||
},
|
||||
entityCreated: async <T>(
|
||||
type: EntityType,
|
||||
data: T,
|
||||
data: EntityData<T>,
|
||||
userId: string
|
||||
): Promise<void> => {
|
||||
const libraryItemId = data.libraryItemId
|
||||
// queue trigger rule job
|
||||
if (type === EntityType.PAGE) {
|
||||
const libraryItemId = (data as T & { id: string }).id
|
||||
await enqueueTriggerRuleJob({
|
||||
userId,
|
||||
ruleEventType: RuleEventType.PageCreated,
|
||||
|
|
@ -58,8 +65,13 @@ export const createPubSubClient = (): PubsubClient => {
|
|||
})
|
||||
}
|
||||
|
||||
await enqueueExportItem({
|
||||
userId,
|
||||
libraryItemId,
|
||||
})
|
||||
|
||||
const cleanData = deepDelete(
|
||||
data as T & Record<typeof fieldsToDelete[number], unknown>,
|
||||
data as EntityData<T> & Record<typeof fieldsToDelete[number], unknown>,
|
||||
[...fieldsToDelete]
|
||||
)
|
||||
|
||||
|
|
@ -77,12 +89,13 @@ export const createPubSubClient = (): PubsubClient => {
|
|||
},
|
||||
entityUpdated: async <T>(
|
||||
type: EntityType,
|
||||
data: T,
|
||||
data: EntityData<T>,
|
||||
userId: string
|
||||
): Promise<void> => {
|
||||
const libraryItemId = data.libraryItemId
|
||||
|
||||
// queue trigger rule job
|
||||
if (type === EntityType.PAGE) {
|
||||
const libraryItemId = (data as T & { id: string }).id
|
||||
await enqueueTriggerRuleJob({
|
||||
userId,
|
||||
ruleEventType: RuleEventType.PageUpdated,
|
||||
|
|
@ -90,8 +103,13 @@ export const createPubSubClient = (): PubsubClient => {
|
|||
})
|
||||
}
|
||||
|
||||
await enqueueExportItem({
|
||||
userId,
|
||||
libraryItemId,
|
||||
})
|
||||
|
||||
const cleanData = deepDelete(
|
||||
data as T & Record<typeof fieldsToDelete[number], unknown>,
|
||||
data as EntityData<T> & Record<typeof fieldsToDelete[number], unknown>,
|
||||
[...fieldsToDelete]
|
||||
)
|
||||
|
||||
|
|
@ -146,8 +164,16 @@ export interface PubsubClient {
|
|||
name: string,
|
||||
username: string
|
||||
) => Promise<void>
|
||||
entityCreated: <T>(type: EntityType, data: T, userId: string) => Promise<void>
|
||||
entityUpdated: <T>(type: EntityType, data: T, userId: string) => Promise<void>
|
||||
entityCreated: <T>(
|
||||
type: EntityType,
|
||||
data: EntityData<T>,
|
||||
userId: string
|
||||
) => Promise<void>
|
||||
entityUpdated: <T>(
|
||||
type: EntityType,
|
||||
data: EntityData<T>,
|
||||
userId: string
|
||||
) => Promise<void>
|
||||
entityDeleted: (type: EntityType, id: string, userId: string) => Promise<void>
|
||||
reportSubmitted(
|
||||
submitterId: string | undefined,
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import { appDataSource } from './data_source'
|
|||
import { env } from './env'
|
||||
import { bulkAction, BULK_ACTION_JOB_NAME } from './jobs/bulk_action'
|
||||
import { callWebhook, CALL_WEBHOOK_JOB_NAME } from './jobs/call_webhook'
|
||||
import { exportItem, EXPORT_ITEM_JOB_NAME } from './jobs/export_item'
|
||||
import { findThumbnail, THUMBNAIL_JOB } from './jobs/find_thumbnail'
|
||||
import { refreshAllFeeds } from './jobs/rss/refreshAllFeeds'
|
||||
import { refreshFeed } from './jobs/rss/refreshFeed'
|
||||
|
|
@ -103,6 +104,8 @@ export const createWorker = (connection: ConnectionOptions) =>
|
|||
return bulkAction(job.data)
|
||||
case CALL_WEBHOOK_JOB_NAME:
|
||||
return callWebhook(job.data)
|
||||
case EXPORT_ITEM_JOB_NAME:
|
||||
return exportItem(job.data)
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ export const updateHighlight = async (
|
|||
const libraryItemId = updatedHighlight.libraryItem.id
|
||||
await pubsub.entityUpdated<UpdateHighlightEvent>(
|
||||
EntityType.HIGHLIGHT,
|
||||
{ ...highlight, id: highlightId, pageId: libraryItemId },
|
||||
{ ...highlight, id: highlightId, pageId: libraryItemId, libraryItemId },
|
||||
userId
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -6,15 +6,18 @@ import { createPubSubClient, EntityType, PubsubClient } from '../pubsub'
|
|||
import { authTrx } from '../repository'
|
||||
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'
|
||||
|
||||
type AddLabelsToLibraryItemEvent = {
|
||||
libraryItemId: string
|
||||
pageId: string
|
||||
labels: DeepPartial<Label>[]
|
||||
source?: LabelSource
|
||||
}
|
||||
type AddLabelsToHighlightEvent = {
|
||||
libraryItemId: string
|
||||
highlightId: string
|
||||
labels: DeepPartial<Label>[]
|
||||
}
|
||||
|
|
@ -145,7 +148,7 @@ export const saveLabelsInLibraryItem = async (
|
|||
// create pubsub event
|
||||
await pubsub.entityCreated<AddLabelsToLibraryItemEvent>(
|
||||
EntityType.LABEL,
|
||||
{ pageId: libraryItemId, labels, source },
|
||||
{ pageId: libraryItemId, labels, source, libraryItemId },
|
||||
userId
|
||||
)
|
||||
}
|
||||
|
|
@ -205,20 +208,22 @@ export const saveLabelsInHighlight = async (
|
|||
)
|
||||
})
|
||||
|
||||
const highlight = await findHighlightById(highlightId, userId)
|
||||
if (!highlight) {
|
||||
logger.error('Highlight not found', { highlightId, userId })
|
||||
return
|
||||
}
|
||||
|
||||
const libraryItemId = highlight.libraryItemId
|
||||
// create pubsub event
|
||||
await pubsub.entityCreated<AddLabelsToHighlightEvent>(
|
||||
EntityType.LABEL,
|
||||
{ highlightId, labels },
|
||||
{ highlightId, labels, libraryItemId },
|
||||
userId
|
||||
)
|
||||
|
||||
const highlight = await findHighlightById(highlightId, userId)
|
||||
if (highlight) {
|
||||
// update labels in library item
|
||||
await bulkEnqueueUpdateLabels([
|
||||
{ libraryItemId: highlight.libraryItemId, userId },
|
||||
])
|
||||
}
|
||||
// update labels in library item
|
||||
await bulkEnqueueUpdateLabels([{ libraryItemId, userId }])
|
||||
}
|
||||
|
||||
export const findLabelsByIds = async (
|
||||
|
|
|
|||
|
|
@ -775,6 +775,7 @@ export const updateLibraryItem = async (
|
|||
{
|
||||
...libraryItem,
|
||||
id,
|
||||
libraryItemId: id,
|
||||
// don't send original content and readable content
|
||||
originalContent: undefined,
|
||||
readableContent: undefined,
|
||||
|
|
@ -935,6 +936,7 @@ export const createOrUpdateLibraryItem = async (
|
|||
EntityType.PAGE,
|
||||
{
|
||||
...newLibraryItem,
|
||||
libraryItemId: newLibraryItem.id,
|
||||
// don't send original content and readable content
|
||||
originalContent: undefined,
|
||||
readableContent: undefined,
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import {
|
|||
} from '../generated/graphql'
|
||||
import { BulkActionData, BULK_ACTION_JOB_NAME } from '../jobs/bulk_action'
|
||||
import { CallWebhookJobData, CALL_WEBHOOK_JOB_NAME } from '../jobs/call_webhook'
|
||||
import { ExportItemJobData, EXPORT_ITEM_JOB_NAME } from '../jobs/export_item'
|
||||
import { THUMBNAIL_JOB } from '../jobs/find_thumbnail'
|
||||
import {
|
||||
queueRSSRefreshFeedJob,
|
||||
|
|
@ -59,6 +60,7 @@ export const getJobPriority = (jobName: string): number => {
|
|||
return 1
|
||||
case TRIGGER_RULE_JOB_NAME:
|
||||
case CALL_WEBHOOK_JOB_NAME:
|
||||
case EXPORT_ITEM_JOB_NAME:
|
||||
return 5
|
||||
case BULK_ACTION_JOB_NAME:
|
||||
case `${REFRESH_FEED_JOB_NAME}_high`:
|
||||
|
|
@ -785,4 +787,16 @@ export const enqueueBulkAction = async (data: BulkActionData) => {
|
|||
}
|
||||
}
|
||||
|
||||
export const enqueueExportItem = async (jobData: ExportItemJobData) => {
|
||||
const queue = await getBackendQueue()
|
||||
if (!queue) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return queue.add(EXPORT_ITEM_JOB_NAME, jobData, {
|
||||
attempts: 1,
|
||||
priority: getJobPriority(EXPORT_ITEM_JOB_NAME),
|
||||
})
|
||||
}
|
||||
|
||||
export default createHttpTaskWithToken
|
||||
|
|
|
|||
Loading…
Reference in a new issue