From ff78a4b2660836dc6a09a7eadd6d33eb22965245 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 22 Mar 2024 15:55:45 +0800 Subject: [PATCH] fix readwise syncing error --- packages/api/src/jobs/trigger_rule.ts | 16 +++--- .../api/src/services/integrations/readwise.ts | 53 ++++++++++--------- packages/web/pages/settings/rules.tsx | 2 +- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/packages/api/src/jobs/trigger_rule.ts b/packages/api/src/jobs/trigger_rule.ts index e7f1040cb..1ff163980 100644 --- a/packages/api/src/jobs/trigger_rule.ts +++ b/packages/api/src/jobs/trigger_rule.ts @@ -129,13 +129,14 @@ const exportItem = async (obj: RuleActionObj) => { await Promise.all( integrations.map(async (integration) => { - try { - const logObject = { - userId, - integrationId: integration.id, - } - logger.info('exporting item...', logObject) + const logObject = { + userId, + integrationId: integration.id, + name: integration.name, + } + logger.info('exporting item...', logObject) + try { const client = getIntegrationClient( integration.name, integration.token, @@ -168,8 +169,7 @@ const exportItem = async (obj: RuleActionObj) => { }) } catch (error) { logger.error('failed to export item', { - userId, - integrationId: integration.id, + ...logObject, error, }) } diff --git a/packages/api/src/services/integrations/readwise.ts b/packages/api/src/services/integrations/readwise.ts index b70ef4260..b0e0e3459 100644 --- a/packages/api/src/services/integrations/readwise.ts +++ b/packages/api/src/services/integrations/readwise.ts @@ -1,6 +1,7 @@ import axios from 'axios' +import { HighlightType } from '../../entity/highlight' import { logger } from '../../utils/logger' -import { getHighlightUrl } from '../highlights' +import { getHighlightUrl, HighlightEvent } from '../highlights' import { ItemEvent } from '../library_item' import { IntegrationClient } from './integration' @@ -84,31 +85,33 @@ export class ReadwiseClient implements IntegrationClient { } private _itemToReadwiseHighlight = (item: ItemEvent): ReadwiseHighlight[] => { - const category = item.siteName === 'Twitter' ? 'tweets' : 'articles' - return item.highlights - ?.map((highlight) => { - // filter out highlights that are not of type highlight or have no quote - if (highlight.highlightType !== 'HIGHLIGHT' || !highlight.quote) { - return undefined - } + const isHighlight = ( + highlight: HighlightEvent + ): highlight is HighlightEvent & { quote: string } => + highlight.highlightType === HighlightType.Highlight && !!highlight.quote - return { - text: highlight.quote, - title: item.title, - author: item.author || undefined, - highlight_url: item.slug - ? getHighlightUrl(item.slug, highlight.id) - : undefined, - highlighted_at: (highlight.createdAt as Date).toISOString(), - category, - image_url: item.thumbnail || undefined, - location_type: 'order', - note: highlight.annotation || undefined, - source_type: 'omnivore', - source_url: item.originalUrl, - } - }) - .filter((highlight) => highlight !== undefined) as ReadwiseHighlight[] + const category = item.siteName === 'Twitter' ? 'tweets' : 'articles' + + return item.highlights + ? item.highlights + // filter out highlights that are not of type highlight or have no quote + .filter(isHighlight) + .map((highlight) => ({ + text: highlight.quote, + title: item.title, + author: item.author || undefined, + highlight_url: item.slug + ? getHighlightUrl(item.slug, highlight.id) + : undefined, + highlighted_at: (highlight.createdAt as Date).toISOString(), + category, + image_url: item.thumbnail || undefined, + location_type: 'order', + note: highlight.annotation || undefined, + source_type: 'omnivore', + source_url: item.originalUrl, + })) + : [] } private _syncWithReadwise = async ( diff --git a/packages/web/pages/settings/rules.tsx b/packages/web/pages/settings/rules.tsx index 9144114b6..db9c2fd69 100644 --- a/packages/web/pages/settings/rules.tsx +++ b/packages/web/pages/settings/rules.tsx @@ -104,7 +104,7 @@ const CreateRuleModal = (props: CreateRuleModalProps): JSX.Element => { const value = Object.values(RuleEventType)[index] return ( - {key} + {key === 'LABEL_CREATED' ? 'LABEL_ATTACHED' : key} ) })}