From 71826148fd6799f7b8fc4aebf450e9eb9fa47146 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 13 Mar 2023 15:15:47 +0800 Subject: [PATCH 1/5] Add type to the highlight in the elasticsearch and three types are HIGHLIGHTS, REDACTION and NOTE --- packages/api/src/elastic/types.ts | 7 ++++ .../db/elastic_migrations/index_settings.json | 4 +++ packages/db/migrate.ts | 33 ------------------- 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/packages/api/src/elastic/types.ts b/packages/api/src/elastic/types.ts index 8191115ac..513b425c7 100644 --- a/packages/api/src/elastic/types.ts +++ b/packages/api/src/elastic/types.ts @@ -71,6 +71,12 @@ export enum ArticleSavingRequestStatus { Deleted = 'DELETED', } +export enum HighlightType { + Highlight = 'HIGHLIGHT', + Redaction = 'REDACTION', // allowing people to remove text from the page + Note = 'NOTE', // allowing people to add a note at the document level +} + export interface Label { id: string name: string @@ -94,6 +100,7 @@ export interface Highlight { labels?: Label[] highlightPositionPercent?: number | null highlightPositionAnchorIndex?: number | null + type: HighlightType html?: string | null } diff --git a/packages/db/elastic_migrations/index_settings.json b/packages/db/elastic_migrations/index_settings.json index 1d9c977e1..6b14fd5ce 100644 --- a/packages/db/elastic_migrations/index_settings.json +++ b/packages/db/elastic_migrations/index_settings.json @@ -84,6 +84,10 @@ "userId": { "type": "keyword" }, + "type": { + "type": "keyword", + "null_value": "HIGHLIGHT" + }, "quote": { "type": "text", "analyzer": "strip_html_analyzer" diff --git a/packages/db/migrate.ts b/packages/db/migrate.ts index 8a4386940..ef17fe997 100755 --- a/packages/db/migrate.ts +++ b/packages/db/migrate.ts @@ -148,41 +148,8 @@ const elasticMigration = esClient.indices log('Elastic index mappings updated.') }) }) - .then(() => { - log('Adding default state to pages in elastic...') - return esClient - .update_by_query({ - index: INDEX_ALIAS, - requests_per_second: 250, - scroll_size: 500, - timeout: '30m', - body: { - script: { - source: 'ctx._source.state = params.state', - lang: 'painless', - params: { - state: 'SUCCEEDED', - }, - }, - query: { - bool: { - must_not: [ - { - exists: { - field: 'state', - }, - }, - ], - }, - }, - }, - }) - .then(() => log('Default state added.')) - }) .catch((error) => { log(`${chalk.red('Elastic migration failed: ')}${error.message}`, chalk.red) - const { appliedMigrations } = error - logAppliedMigrations(appliedMigrations) process.exit(1) }) From b0e91cbbbb03b7271dbc1f9fec7f4d2399f8d6e4 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 13 Mar 2023 17:14:36 +0800 Subject: [PATCH 2/5] Change GraphQL APIs --- packages/api/src/elastic/highlights.ts | 6 ++- packages/api/src/elastic/labels.ts | 2 +- packages/api/src/elastic/types.ts | 4 +- packages/api/src/generated/graphql.ts | 22 ++++++--- packages/api/src/generated/schema.graphql | 16 +++++-- packages/api/src/resolvers/highlight/index.ts | 45 ++++++++++++++----- packages/api/src/schema.ts | 16 +++++-- packages/api/src/services/integrations.ts | 11 +++-- packages/api/src/services/save_page.ts | 2 + 9 files changed, 90 insertions(+), 34 deletions(-) diff --git a/packages/api/src/elastic/highlights.ts b/packages/api/src/elastic/highlights.ts index 7a93338b4..a3d130153 100644 --- a/packages/api/src/elastic/highlights.ts +++ b/packages/api/src/elastic/highlights.ts @@ -29,7 +29,7 @@ export const addHighlightToPage = async ( ctx._source.updatedAt = params.highlight.updatedAt`, lang: 'painless', params: { - highlight: highlight, + highlight, }, }, }, @@ -254,7 +254,7 @@ export const updateHighlight = async ( ctx: PageContext ): Promise => { try { - await client.updateByQuery({ + const { body } = await client.updateByQuery({ index: INDEX_ALIAS, body: { script: { @@ -292,6 +292,8 @@ export const updateHighlight = async ( conflicts: 'proceed', }) + if (body.updated === 0) return false + await ctx.pubsub.entityUpdated( EntityType.HIGHLIGHT, highlight, diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index 79c2b6d97..5fcb0ba49 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -282,7 +282,7 @@ export const setLabelsForHighlight = async ( lang: 'painless', params: { highlightId, - labels: labels, + labels, updatedAt: new Date(), }, }, diff --git a/packages/api/src/elastic/types.ts b/packages/api/src/elastic/types.ts index 513b425c7..6ea001506 100644 --- a/packages/api/src/elastic/types.ts +++ b/packages/api/src/elastic/types.ts @@ -88,8 +88,8 @@ export interface Label { export interface Highlight { id: string shortId: string - patch: string - quote: string + patch?: string | null + quote?: string | null userId: string createdAt: Date prefix?: string | null diff --git a/packages/api/src/generated/graphql.ts b/packages/api/src/generated/graphql.ts index 2a14ee746..d1c96d1cf 100644 --- a/packages/api/src/generated/graphql.ts +++ b/packages/api/src/generated/graphql.ts @@ -348,12 +348,13 @@ export type CreateHighlightInput = { highlightPositionPercent?: InputMaybe; html?: InputMaybe; id: Scalars['ID']; - patch: Scalars['String']; + patch?: InputMaybe; prefix?: InputMaybe; - quote: Scalars['String']; + quote?: InputMaybe; sharedAt?: InputMaybe; shortId: Scalars['String']; suffix?: InputMaybe; + type?: InputMaybe; }; export type CreateHighlightReplyError = { @@ -905,14 +906,15 @@ export type Highlight = { html?: Maybe; id: Scalars['ID']; labels?: Maybe>; - patch: Scalars['String']; + patch?: Maybe; prefix?: Maybe; - quote: Scalars['String']; + quote?: Maybe; reactions: Array; replies: Array; sharedAt?: Maybe; shortId: Scalars['String']; suffix?: Maybe; + type: HighlightType; updatedAt: Scalars['Date']; user: User; }; @@ -932,6 +934,12 @@ export type HighlightStats = { highlightCount: Scalars['Int']; }; +export enum HighlightType { + Highlight = 'HIGHLIGHT', + Note = 'NOTE', + Redaction = 'REDACTION' +} + export type Integration = { __typename?: 'Integration'; createdAt: Scalars['Date']; @@ -3384,6 +3392,7 @@ export type ResolversTypes = { Highlight: ResolverTypeWrapper; HighlightReply: ResolverTypeWrapper; HighlightStats: ResolverTypeWrapper; + HighlightType: HighlightType; ID: ResolverTypeWrapper; Int: ResolverTypeWrapper; Integration: ResolverTypeWrapper; @@ -4722,14 +4731,15 @@ export type HighlightResolvers, ParentType, ContextType>; id?: Resolver; labels?: Resolver>, ParentType, ContextType>; - patch?: Resolver; + patch?: Resolver, ParentType, ContextType>; prefix?: Resolver, ParentType, ContextType>; - quote?: Resolver; + quote?: Resolver, ParentType, ContextType>; reactions?: Resolver, ParentType, ContextType>; replies?: Resolver, ParentType, ContextType>; sharedAt?: Resolver, ParentType, ContextType>; shortId?: Resolver; suffix?: Resolver, ParentType, ContextType>; + type?: Resolver; updatedAt?: Resolver; user?: Resolver; __isTypeOf?: IsTypeOfResolverFn; diff --git a/packages/api/src/generated/schema.graphql b/packages/api/src/generated/schema.graphql index 989f65764..7490fb535 100644 --- a/packages/api/src/generated/schema.graphql +++ b/packages/api/src/generated/schema.graphql @@ -300,12 +300,13 @@ input CreateHighlightInput { highlightPositionPercent: Float html: String id: ID! - patch: String! + patch: String prefix: String - quote: String! + quote: String sharedAt: Date shortId: String! suffix: String + type: HighlightType } type CreateHighlightReplyError { @@ -802,14 +803,15 @@ type Highlight { html: String id: ID! labels: [Label!] - patch: String! + patch: String prefix: String - quote: String! + quote: String reactions: [Reaction!]! replies: [HighlightReply!]! sharedAt: Date shortId: String! suffix: String + type: HighlightType! updatedAt: Date! user: User! } @@ -827,6 +829,12 @@ type HighlightStats { highlightCount: Int! } +enum HighlightType { + HIGHLIGHT + NOTE + REDACTION +} + type Integration { createdAt: Date! enabled: Boolean! diff --git a/packages/api/src/resolvers/highlight/index.ts b/packages/api/src/resolvers/highlight/index.ts index ec5c4f787..35e7b4ca4 100644 --- a/packages/api/src/resolvers/highlight/index.ts +++ b/packages/api/src/resolvers/highlight/index.ts @@ -8,7 +8,11 @@ import { updateHighlight, } from '../../elastic/highlights' import { getPageById, updatePage } from '../../elastic/pages' -import { Highlight as HighlightData } from '../../elastic/types' +import { + Highlight as HighlightData, + HighlightType, + Label, +} from '../../elastic/types' import { env } from '../../env' import { CreateHighlightError, @@ -81,6 +85,7 @@ export const createHighlightResolver = authorized< createdAt: new Date(), userId: claims.uid, annotation, + type: input.type || HighlightType.Highlight, } if ( @@ -142,20 +147,35 @@ export const mergeHighlightResolver = authorized< const articleHighlights = page.highlights /* Compute merged annotation form the order of highlights appearing on page */ - const overlapAnnotations: { [id: string]: string } = {} + const overlappings: { annotation?: string | null; labels?: Label[] }[] = [] articleHighlights.forEach((highlight, index) => { - if (overlapHighlightIdList.includes(highlight.id)) { + // only consider highlights that are in the overlap list + // and are of type highlight (not annotation or note) + if ( + overlapHighlightIdList.includes(highlight.id) && + highlight.type === HighlightType.Highlight + ) { articleHighlights.splice(index, 1) - - if (highlight.annotation) { - overlapAnnotations[highlight.id] = highlight.annotation - } + overlappings.push({ + annotation: highlight.annotation, + labels: highlight.labels, + }) } }) + console.log(overlapHighlightIdList) const mergedAnnotation: string[] = [] - overlapHighlightIdList.forEach((highlightId) => { - if (overlapAnnotations[highlightId]) { - mergedAnnotation.push(overlapAnnotations[highlightId]) + const mergedLabels: Label[] = [] + overlappings.forEach((highlight) => { + if (highlight.annotation) { + mergedAnnotation.push(highlight.annotation) + } + if (highlight.labels) { + // remove duplicates from labels by checking id + highlight.labels.forEach((label) => { + if (!mergedLabels.find((mergedLabel) => mergedLabel.id === label.id)) { + mergedLabels.push(label) + } + }) } }) @@ -165,7 +185,10 @@ export const mergeHighlightResolver = authorized< updatedAt: new Date(), createdAt: new Date(), userId: claims.uid, - annotation: mergedAnnotation ? mergedAnnotation.join('\n') : null, + annotation: + mergedAnnotation.length > 0 ? mergedAnnotation.join('\n') : null, + type: HighlightType.Highlight, + labels: mergedLabels, } const merged = await updatePage( diff --git a/packages/api/src/schema.ts b/packages/api/src/schema.ts index 5fbf077dc..cab9f41a2 100755 --- a/packages/api/src/schema.ts +++ b/packages/api/src/schema.ts @@ -660,18 +660,24 @@ const schema = gql` reactions: [Reaction!]! } + enum HighlightType { + HIGHLIGHT + REDACTION + NOTE + } + # Highlight type Highlight { id: ID! # used for simplified url format shortId: String! user: User! - quote: String! + quote: String # piece of content before the quote prefix: String # piece of content after the quote suffix: String - patch: String! + patch: String annotation: String replies: [HighlightReply!]! sharedAt: Date @@ -682,6 +688,7 @@ const schema = gql` highlightPositionPercent: Float highlightPositionAnchorIndex: Int labels: [Label!] + type: HighlightType! html: String } @@ -689,14 +696,15 @@ const schema = gql` id: ID! shortId: String! articleId: ID! - patch: String! - quote: String! @sanitize(maxLength: 6000, minLength: 1) + patch: String + quote: String @sanitize(maxLength: 6000, minLength: 1) prefix: String @sanitize suffix: String @sanitize annotation: String @sanitize(maxLength: 4000) sharedAt: Date highlightPositionPercent: Float highlightPositionAnchorIndex: Int + type: HighlightType html: String } diff --git a/packages/api/src/services/integrations.ts b/packages/api/src/services/integrations.ts index acde28e20..91d0df654 100644 --- a/packages/api/src/services/integrations.ts +++ b/packages/api/src/services/integrations.ts @@ -2,7 +2,7 @@ import { IntegrationType } from '../generated/graphql' import { env } from '../env' import axios from 'axios' import { wait } from '../utils/helpers' -import { Page } from '../elastic/types' +import { HighlightType, Page } from '../elastic/types' import { getHighlightUrl } from './highlights' import { Integration } from '../entity/integration' import { getRepository } from '../entity/utils' @@ -68,11 +68,14 @@ const pageToReadwiseHighlight = (page: Page): ReadwiseHighlight[] => { const category = page.siteName === 'Twitter' ? 'tweets' : 'articles' return ( page.highlights - // filter out highlights with no quote - .filter((highlight) => highlight.quote.length > 0) + // filter out highlights with no quote and are not of type Highlight + .filter( + (highlight) => + highlight.type === HighlightType.Highlight && highlight.quote + ) .map((highlight) => { return { - text: highlight.quote, + text: highlight.quote!, title: page.title, author: page.author || undefined, highlight_url: getHighlightUrl(page.slug, highlight.id), diff --git a/packages/api/src/services/save_page.ts b/packages/api/src/services/save_page.ts index 1acb6ea17..2158d1ace 100644 --- a/packages/api/src/services/save_page.ts +++ b/packages/api/src/services/save_page.ts @@ -6,6 +6,7 @@ import { createPage, getPageByParam, updatePage } from '../elastic/pages' import { ArticleSavingRequestStatus, Page, PageType } from '../elastic/types' import { homePageURL } from '../env' import { + HighlightType, Maybe, PreparedDocumentInput, SaveErrorCode, @@ -161,6 +162,7 @@ export const savePage = async ( userId: ctx.uid, elasticPageId: pageId, ...parseResult.highlightData, + type: HighlightType.Highlight, } if ( From 9df1546323839d86962afff64a4e558aee912635 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 13 Mar 2023 22:09:53 +0800 Subject: [PATCH 3/5] Fix tests --- packages/api/src/elastic/highlights.ts | 2 - packages/api/src/resolvers/highlight/index.ts | 48 ++++++++----------- packages/api/test/elastic/highlights.test.ts | 8 +++- packages/api/test/elastic/labels.test.ts | 2 + packages/api/test/resolvers/article.test.ts | 3 ++ packages/api/test/resolvers/highlight.test.ts | 17 +++---- packages/api/test/resolvers/labels.test.ts | 10 +++- .../api/test/routers/integrations.test.ts | 8 +++- 8 files changed, 58 insertions(+), 40 deletions(-) diff --git a/packages/api/src/elastic/highlights.ts b/packages/api/src/elastic/highlights.ts index a3d130153..f06ebcd42 100644 --- a/packages/api/src/elastic/highlights.ts +++ b/packages/api/src/elastic/highlights.ts @@ -292,8 +292,6 @@ export const updateHighlight = async ( conflicts: 'proceed', }) - if (body.updated === 0) return false - await ctx.pubsub.entityUpdated( EntityType.HIGHLIGHT, highlight, diff --git a/packages/api/src/resolvers/highlight/index.ts b/packages/api/src/resolvers/highlight/index.ts index 35e7b4ca4..eea9e1238 100644 --- a/packages/api/src/resolvers/highlight/index.ts +++ b/packages/api/src/resolvers/highlight/index.ts @@ -147,38 +147,32 @@ export const mergeHighlightResolver = authorized< const articleHighlights = page.highlights /* Compute merged annotation form the order of highlights appearing on page */ - const overlappings: { annotation?: string | null; labels?: Label[] }[] = [] - articleHighlights.forEach((highlight, index) => { - // only consider highlights that are in the overlap list + const mergedAnnotations: string[] = [] + const mergedLabels: Label[] = [] + const pageHighlights = page.highlights.filter((highlight) => { + // filter out highlights that are in the overlap list // and are of type highlight (not annotation or note) if ( overlapHighlightIdList.includes(highlight.id) && highlight.type === HighlightType.Highlight ) { - articleHighlights.splice(index, 1) - overlappings.push({ - annotation: highlight.annotation, - labels: highlight.labels, - }) + if (highlight.annotation) { + mergedAnnotations.push(highlight.annotation) + } + if (highlight.labels) { + // remove duplicates from labels by checking id + highlight.labels.forEach((label) => { + if ( + !mergedLabels.find((mergedLabel) => mergedLabel.id === label.id) + ) { + mergedLabels.push(label) + } + }) + } + return false } + return true }) - console.log(overlapHighlightIdList) - const mergedAnnotation: string[] = [] - const mergedLabels: Label[] = [] - overlappings.forEach((highlight) => { - if (highlight.annotation) { - mergedAnnotation.push(highlight.annotation) - } - if (highlight.labels) { - // remove duplicates from labels by checking id - highlight.labels.forEach((label) => { - if (!mergedLabels.find((mergedLabel) => mergedLabel.id === label.id)) { - mergedLabels.push(label) - } - }) - } - }) - try { const highlight: HighlightData = { ...newHighlightInput, @@ -186,14 +180,14 @@ export const mergeHighlightResolver = authorized< createdAt: new Date(), userId: claims.uid, annotation: - mergedAnnotation.length > 0 ? mergedAnnotation.join('\n') : null, + mergedAnnotations.length > 0 ? mergedAnnotations.join('\n') : null, type: HighlightType.Highlight, labels: mergedLabels, } const merged = await updatePage( pageId, - { highlights: articleHighlights.concat(highlight) }, + { highlights: pageHighlights.concat(highlight) }, { pubsub, uid: claims.uid } ) if (!merged) { diff --git a/packages/api/test/elastic/highlights.test.ts b/packages/api/test/elastic/highlights.test.ts index 4950e5f62..cbc497df5 100644 --- a/packages/api/test/elastic/highlights.test.ts +++ b/packages/api/test/elastic/highlights.test.ts @@ -1,6 +1,11 @@ import 'mocha' import { expect } from 'chai' -import { Highlight, Page, PageContext } from '../../src/elastic/types' +import { + Highlight, + HighlightType, + Page, + PageContext, +} from '../../src/elastic/types' import { createPubSubClient } from '../../src/datalayer/pubsub' import { deletePage } from '../../src/elastic/pages' import { @@ -32,6 +37,7 @@ describe('highlights in elastic', () => { userId: page.userId, createdAt: new Date(), updatedAt: new Date(), + type: HighlightType.Highlight, } await addHighlightToPage(page.id, highlightData, ctx) diff --git a/packages/api/test/elastic/labels.test.ts b/packages/api/test/elastic/labels.test.ts index 685e0a773..3a1ce37b5 100644 --- a/packages/api/test/elastic/labels.test.ts +++ b/packages/api/test/elastic/labels.test.ts @@ -3,6 +3,7 @@ import { expect } from 'chai' import { ArticleSavingRequestStatus, Highlight, + HighlightType, Label, Page, PageContext, @@ -123,6 +124,7 @@ describe('labels in elastic', () => { userId: page.userId, createdAt: new Date(), updatedAt: new Date(), + type: HighlightType.Highlight, } await addHighlightToPage(page.id, highlightData, ctx) diff --git a/packages/api/test/resolvers/article.test.ts b/packages/api/test/resolvers/article.test.ts index 3ab9829cd..4e0440a8d 100644 --- a/packages/api/test/resolvers/article.test.ts +++ b/packages/api/test/resolvers/article.test.ts @@ -15,6 +15,7 @@ import { import { ArticleSavingRequestStatus, Highlight, + HighlightType, Page, PageContext, PageType, @@ -476,6 +477,7 @@ describe('Article API', () => { quote: 'test quote', updatedAt: new Date(), userId: user.id, + type: HighlightType.Highlight, }, ], } @@ -861,6 +863,7 @@ describe('Article API', () => { quote: '

search highlight

', createdAt: new Date(), updatedAt: new Date(), + type: HighlightType.Highlight, } await addHighlightToPage(page.id, highlight, ctx) highlights.push(highlight) diff --git a/packages/api/test/resolvers/highlight.test.ts b/packages/api/test/resolvers/highlight.test.ts index 5ed90759f..d4b2a9537 100644 --- a/packages/api/test/resolvers/highlight.test.ts +++ b/packages/api/test/resolvers/highlight.test.ts @@ -1,11 +1,3 @@ -import * as chai from 'chai' -import { expect } from 'chai' -import chaiString from 'chai-string' -import 'mocha' -import { createPubSubClient } from '../../src/datalayer/pubsub' -import { deletePage, updatePage } from '../../src/elastic/pages' -import { PageContext } from '../../src/elastic/types' -import { User } from '../../src/entity/user' import { createTestUser, deleteTestUser } from '../db' import { createTestElasticPage, @@ -13,6 +5,14 @@ import { graphqlRequest, request, } from '../util' +import * as chai from 'chai' +import { expect } from 'chai' +import 'mocha' +import { User } from '../../src/entity/user' +import chaiString from 'chai-string' +import { createPubSubClient } from '../../src/datalayer/pubsub' +import { HighlightType, PageContext } from '../../src/elastic/types' +import { deletePage, updatePage } from '../../src/elastic/pages' chai.use(chaiString) @@ -269,6 +269,7 @@ describe('Highlights API', () => { quote: '', createdAt: new Date(), updatedAt: new Date(), + type: HighlightType.Highlight, }, ], }, diff --git a/packages/api/test/resolvers/labels.test.ts b/packages/api/test/resolvers/labels.test.ts index 9469db3ad..4717c820e 100644 --- a/packages/api/test/resolvers/labels.test.ts +++ b/packages/api/test/resolvers/labels.test.ts @@ -14,7 +14,12 @@ import { Label } from '../../src/entity/label' import { expect } from 'chai' import 'mocha' import { User } from '../../src/entity/user' -import { Highlight, Page, PageContext } from '../../src/elastic/types' +import { + Highlight, + HighlightType, + Page, + PageContext, +} from '../../src/elastic/types' import { getRepository } from '../../src/entity/utils' import { deletePage, getPageById } from '../../src/elastic/pages' import { createPubSubClient } from '../../src/datalayer/pubsub' @@ -287,6 +292,7 @@ describe('Labels API', () => { createdAt: new Date(), labels: [toDeleteLabel], updatedAt: new Date(), + type: HighlightType.Highlight, } await addHighlightToPage(page.id, highlight, ctx) }) @@ -596,6 +602,7 @@ describe('Labels API', () => { shortId: 'test shortId', userId: user.id, updatedAt: new Date(), + type: HighlightType.Highlight, } await addHighlightToPage(page.id, highlight, ctx) labelIds = [labels[0].id, labels[1].id] @@ -620,6 +627,7 @@ describe('Labels API', () => { shortId: 'test shortId', userId: user.id, updatedAt: new Date(), + type: HighlightType.Highlight, } await addHighlightToPage(page.id, highlight, ctx) labelIds = [generateFakeUuid(), generateFakeUuid()] diff --git a/packages/api/test/routers/integrations.test.ts b/packages/api/test/routers/integrations.test.ts index 0c197a11a..eb3ef2360 100644 --- a/packages/api/test/routers/integrations.test.ts +++ b/packages/api/test/routers/integrations.test.ts @@ -10,7 +10,12 @@ import { User } from '../../src/entity/user' import { createTestUser, deleteTestIntegrations, deleteTestUser } from '../db' import { Integration, IntegrationType } from '../../src/entity/integration' import { getRepository } from '../../src/entity/utils' -import { Highlight, Page, PageContext } from '../../src/elastic/types' +import { + Highlight, + HighlightType, + Page, + PageContext, +} from '../../src/elastic/types' import nock from 'nock' import { READWISE_API_URL } from '../../src/services/integrations' import { addHighlightToPage } from '../../src/elastic/highlights' @@ -142,6 +147,7 @@ describe('Integrations routers', () => { updatedAt: new Date(), userId: user.id, highlightPositionPercent, + type: HighlightType.Highlight, } await addHighlightToPage(page.id, highlight, ctx) // create highlights data for integration request From ee151637e3d52cba19553b867829796119afa6d7 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 13 Mar 2023 22:14:15 +0800 Subject: [PATCH 4/5] Only send event if highlight updated --- packages/api/src/elastic/highlights.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/api/src/elastic/highlights.ts b/packages/api/src/elastic/highlights.ts index f06ebcd42..207db8cc5 100644 --- a/packages/api/src/elastic/highlights.ts +++ b/packages/api/src/elastic/highlights.ts @@ -136,9 +136,12 @@ export const deleteHighlight = async ( refresh: ctx.refresh, }) - if (body.updated === 0) return false - - await ctx.pubsub.entityDeleted(EntityType.HIGHLIGHT, highlightId, ctx.uid) + body.updated > 0 && + (await ctx.pubsub.entityDeleted( + EntityType.HIGHLIGHT, + highlightId, + ctx.uid + )) return true } catch (e) { @@ -292,11 +295,12 @@ export const updateHighlight = async ( conflicts: 'proceed', }) - await ctx.pubsub.entityUpdated( - EntityType.HIGHLIGHT, - highlight, - ctx.uid - ) + body.updated > 0 && + (await ctx.pubsub.entityUpdated( + EntityType.HIGHLIGHT, + highlight, + ctx.uid + )) return true } catch (e) { From b3a965781c8c0b353851fee7e010c09474099c48 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 22 Mar 2023 10:40:03 +0800 Subject: [PATCH 5/5] resolve conflicts --- packages/api/src/resolvers/highlight/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/api/src/resolvers/highlight/index.ts b/packages/api/src/resolvers/highlight/index.ts index eea9e1238..ce16fc7e6 100644 --- a/packages/api/src/resolvers/highlight/index.ts +++ b/packages/api/src/resolvers/highlight/index.ts @@ -144,8 +144,6 @@ export const mergeHighlightResolver = authorized< errorCodes: [MergeHighlightErrorCode.Unauthorized], } } - const articleHighlights = page.highlights - /* Compute merged annotation form the order of highlights appearing on page */ const mergedAnnotations: string[] = [] const mergedLabels: Label[] = []