add a job for updating highlight annotations in library_item

This commit is contained in:
Hongbo Wu
2024-01-31 14:15:33 +08:00
parent 43855247b8
commit 5646413b59
4 changed files with 95 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import { authTrx } from '../repository'
export const UPDATE_LABELS_JOB = 'update-labels'
export const UPDATE_HIGHLIGHT_JOB = 'update-highlight'
export interface UpdateLabelsData {
libraryItemId: string
@ -20,7 +21,31 @@ export const updateLabels = async (data: UpdateLabelsData) => {
)
UPDATE omnivore.library_item li
SET label_names = COALESCE((SELECT names_agg FROM labels_agg), ARRAY[]::TEXT[])
WHERE li.id = $1;`,
WHERE li.id = $1`,
[data.libraryItemId]
),
undefined,
data.userId
)
}
export interface UpdateHighlightData {
libraryItemId: string
userId: string
}
export const updateHighlight = async (data: UpdateHighlightData) => {
return authTrx(
async (tx) =>
tx.query(
`WITH highlight_agg AS (
SELECT array_agg(COALESCE(annotation, '')) AS annotation_agg
FROM omnivore.highlight
WHERE library_item_id = $1
)
UPDATE omnivore.library_item
SET highlight_annotations = COALESCE((SELECT annotation_agg FROM highlight_agg), ARRAY[]::TEXT[])
WHERE id = $1`,
[data.libraryItemId]
),
undefined,

View File

@ -12,7 +12,12 @@ import { refreshAllFeeds } from './jobs/rss/refreshAllFeeds'
import { refreshFeed } from './jobs/rss/refreshFeed'
import { savePageJob } from './jobs/save_page'
import { triggerRule, TRIGGER_RULE_JOB_NAME } from './jobs/trigger_rule'
import { updateLabels, UPDATE_LABELS_JOB } from './jobs/update_db'
import {
updateHighlight,
updateLabels,
UPDATE_HIGHLIGHT_JOB,
UPDATE_LABELS_JOB,
} from './jobs/update_db'
import { updatePDFContentJob } from './jobs/update_pdf_content'
import { redisDataSource } from './redis_data_source'
import { CustomTypeOrmLogger } from './utils/logger'
@ -128,6 +133,8 @@ const main = async () => {
return triggerRule(job.data)
case UPDATE_LABELS_JOB:
return updateLabels(job.data)
case UPDATE_HIGHLIGHT_JOB:
return updateHighlight(job.data)
}
},
{

View File

@ -8,6 +8,8 @@ import { homePageURL } from '../env'
import { createPubSubClient, EntityType } from '../pubsub'
import { authTrx } from '../repository'
import { highlightRepository } from '../repository/highlight'
import { enqueueUpdateHighlight } from '../utils/createTask'
import { logger } from '../utils/logger'
type HighlightEvent = { id: string; pageId: string }
type CreateHighlightEvent = DeepPartial<Highlight> & HighlightEvent
@ -61,6 +63,14 @@ export const createHighlight = async (
userId
)
if (newHighlight.annotation) {
const job = await enqueueUpdateHighlight({
libraryItemId,
userId,
})
logger.info('update highlight job enqueued', job)
}
return newHighlight
}
@ -103,6 +113,14 @@ export const mergeHighlights = async (
userId
)
if (newHighlight.annotation) {
const job = await enqueueUpdateHighlight({
libraryItemId,
userId,
})
logger.info('update highlight job enqueued', job)
}
return newHighlight
}
@ -125,28 +143,48 @@ export const updateHighlight = async (
})
})
const libraryItemId = updatedHighlight.libraryItem.id
await pubsub.entityUpdated<UpdateHighlightEvent>(
EntityType.HIGHLIGHT,
{ ...highlight, id: highlightId, pageId: updatedHighlight.libraryItem.id },
{ ...highlight, id: highlightId, pageId: libraryItemId },
userId
)
if (highlight.annotation) {
const job = await enqueueUpdateHighlight({
libraryItemId,
userId,
})
logger.info('update highlight job enqueued', job)
}
return updatedHighlight
}
export const deleteHighlightById = async (highlightId: string) => {
return authTrx(async (tx) => {
const deletedHighlight = await authTrx(async (tx) => {
const highlightRepo = tx.withRepository(highlightRepository)
const highlight = await highlightRepo.findOneOrFail({
where: { id: highlightId },
relations: {
user: true,
libraryItem: true,
},
})
await highlightRepo.delete(highlightId)
return highlight
})
if (deletedHighlight.annotation) {
const job = await enqueueUpdateHighlight({
libraryItemId: deletedHighlight.libraryItem.id,
userId: deletedHighlight.user.id,
})
logger.info('update highlight job enqueued', job)
}
return deletedHighlight
}
export const findHighlightById = async (

View File

@ -17,7 +17,12 @@ import {
import { THUMBNAIL_JOB } from '../jobs/find_thumbnail'
import { queueRSSRefreshFeedJob } from '../jobs/rss/refreshAllFeeds'
import { TriggerRuleJobData, TRIGGER_RULE_JOB_NAME } from '../jobs/trigger_rule'
import { UpdateLabelsData, UPDATE_LABELS_JOB } from '../jobs/update_db'
import {
UpdateHighlightData,
UpdateLabelsData,
UPDATE_HIGHLIGHT_JOB,
UPDATE_LABELS_JOB,
} from '../jobs/update_db'
import { getBackendQueue } from '../queue-processor'
import { redisDataSource } from '../redis_data_source'
import { signFeatureToken } from '../services/features'
@ -682,4 +687,19 @@ export const bulkEnqueueUpdateLabels = async (data: UpdateLabelsData[]) => {
}
}
export const enqueueUpdateHighlight = async (data: UpdateHighlightData) => {
const queue = await getBackendQueue()
if (!queue) {
return undefined
}
try {
return queue.add(UPDATE_HIGHLIGHT_JOB, data, {
priority: 1,
})
} catch (error) {
logger.error('error enqueuing update highlight job', error)
}
}
export default createHttpTaskWithToken