From a1e3ac8a0791aa8ccc3f2eef893212997f8e014d Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 8 Mar 2023 15:37:24 +0800 Subject: [PATCH] Add quote to the updateHighlightInput of the updateHighlight API --- packages/api/src/elastic/highlights.ts | 7 ++- packages/api/src/generated/graphql.ts | 1 + packages/api/src/generated/schema.graphql | 1 + packages/api/src/resolvers/highlight/index.ts | 11 ++--- packages/api/src/schema.ts | 3 +- packages/api/test/resolvers/highlight.test.ts | 47 ++++++++++++++----- 6 files changed, 44 insertions(+), 26 deletions(-) diff --git a/packages/api/src/elastic/highlights.ts b/packages/api/src/elastic/highlights.ts index 19564c80e..7a93338b4 100644 --- a/packages/api/src/elastic/highlights.ts +++ b/packages/api/src/elastic/highlights.ts @@ -254,7 +254,7 @@ export const updateHighlight = async ( ctx: PageContext ): Promise => { try { - const { body } = await client.updateByQuery({ + await client.updateByQuery({ index: INDEX_ALIAS, body: { script: { @@ -263,7 +263,7 @@ export const updateHighlight = async ( ctx._source.updatedAt = params.highlight.updatedAt`, lang: 'painless', params: { - highlight: highlight, + highlight, }, }, query: { @@ -289,10 +289,9 @@ export const updateHighlight = async ( }, }, refresh: ctx.refresh, + conflicts: 'proceed', }) - if (body.updated === 0) return false - await ctx.pubsub.entityUpdated( EntityType.HIGHLIGHT, highlight, diff --git a/packages/api/src/generated/graphql.ts b/packages/api/src/generated/graphql.ts index 4f1b91032..44c16e204 100644 --- a/packages/api/src/generated/graphql.ts +++ b/packages/api/src/generated/graphql.ts @@ -2714,6 +2714,7 @@ export enum UpdateHighlightErrorCode { export type UpdateHighlightInput = { annotation?: InputMaybe; highlightId: Scalars['ID']; + quote?: InputMaybe; sharedAt?: InputMaybe; }; diff --git a/packages/api/src/generated/schema.graphql b/packages/api/src/generated/schema.graphql index 7156d96ce..7c7b36037 100644 --- a/packages/api/src/generated/schema.graphql +++ b/packages/api/src/generated/schema.graphql @@ -2075,6 +2075,7 @@ enum UpdateHighlightErrorCode { input UpdateHighlightInput { annotation: String highlightId: ID! + quote: String sharedAt: Date } diff --git a/packages/api/src/resolvers/highlight/index.ts b/packages/api/src/resolvers/highlight/index.ts index 120554c86..55c2a7879 100644 --- a/packages/api/src/resolvers/highlight/index.ts +++ b/packages/api/src/resolvers/highlight/index.ts @@ -204,8 +204,7 @@ export const updateHighlightResolver = authorized< UpdateHighlightError, MutationUpdateHighlightArgs >(async (_, { input }, { pubsub, claims, log }) => { - const { highlightId } = input - const highlight = await getHighlightById(highlightId) + const highlight = await getHighlightById(input.highlightId) if (!highlight?.id) { return { @@ -219,20 +218,16 @@ export const updateHighlightResolver = authorized< } } - if (input.annotation && input.annotation.length > 4000) { - return { - errorCodes: [UpdateHighlightErrorCode.BadData], - } - } - // unescape HTML entities const annotation = input.annotation ? unescapeHtml(input.annotation) : undefined + const quote = input.quote ? unescapeHtml(input.quote) : highlight.quote const updatedHighlight: HighlightData = { ...highlight, annotation, + quote, updatedAt: new Date(), } diff --git a/packages/api/src/schema.ts b/packages/api/src/schema.ts index 4d3403066..1a1efa40d 100755 --- a/packages/api/src/schema.ts +++ b/packages/api/src/schema.ts @@ -748,8 +748,9 @@ const schema = gql` input UpdateHighlightInput { highlightId: ID! - annotation: String @sanitize + annotation: String @sanitize(maxLength: 4000) sharedAt: Date + quote: String @sanitize(maxLength: 6000) } type UpdateHighlightSuccess { diff --git a/packages/api/test/resolvers/highlight.test.ts b/packages/api/test/resolvers/highlight.test.ts index bd7a6d1dc..3a687c12a 100644 --- a/packages/api/test/resolvers/highlight.test.ts +++ b/packages/api/test/resolvers/highlight.test.ts @@ -104,23 +104,29 @@ const mergeHighlightQuery = ( ` } -const updateHighlightQuery = ( - authToken: string, - highlightId: string, - annotation = '_annotation' -) => { +const updateHighlightQuery = ({ + highlightId, + annotation = null, + quote = null, +}: { + highlightId: string + annotation?: string | null + quote?: string | null +}) => { return ` mutation { updateHighlight( input: { annotation: "${annotation}", highlightId: "${highlightId}", + quote: "${quote}" } ) { ... on UpdateHighlightSuccess { highlight { id annotation + quote } } ... on UpdateHighlightError { @@ -271,15 +277,30 @@ describe('Highlights API', () => { ) }) - context('when the annotation has HTML reserved characters', () => { - it('unescapes the annotation and updates', async () => { - const annotation = '> This is a test' - const query = updateHighlightQuery(authToken, highlightId, annotation) - const res = await graphqlRequest(query, authToken).expect(200) - expect(res.body.data.updateHighlight.highlight.annotation).to.eql( - '> This is a test' - ) + it('updates the quote when the quote is in HTML format when the annotation has HTML reserved characters', async () => { + const quote = '> This is a test' + const query = updateHighlightQuery({ highlightId, quote }) + const res = await graphqlRequest(query, authToken).expect(200) + expect(res.body.data.updateHighlight.highlight.quote).to.eql(quote) + }) + + it('updates the quote when the quote is in plain text format', async () => { + const quote = 'This is a test' + const query = updateHighlightQuery({ highlightId, quote }) + const res = await graphqlRequest(query, authToken).expect(200) + expect(res.body.data.updateHighlight.highlight.quote).to.eql(quote) + }) + + it('unescapes the annotation and updates the annotation when the annotation has HTML reserved characters', async () => { + const annotation = '> This is a test' + const query = updateHighlightQuery({ + highlightId, + annotation, }) + const res = await graphqlRequest(query, authToken).expect(200) + expect(res.body.data.updateHighlight.highlight.annotation).to.eql( + annotation + ) }) }) })