Add quote to the updateHighlightInput of the updateHighlight API

This commit is contained in:
Hongbo Wu 2023-03-08 15:37:24 +08:00
parent ee9bf05f37
commit a1e3ac8a07
6 changed files with 44 additions and 26 deletions

View file

@ -254,7 +254,7 @@ export const updateHighlight = async (
ctx: PageContext
): Promise<boolean> => {
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<Highlight>(
EntityType.HIGHLIGHT,
highlight,

View file

@ -2714,6 +2714,7 @@ export enum UpdateHighlightErrorCode {
export type UpdateHighlightInput = {
annotation?: InputMaybe<Scalars['String']>;
highlightId: Scalars['ID'];
quote?: InputMaybe<Scalars['String']>;
sharedAt?: InputMaybe<Scalars['Date']>;
};

View file

@ -2075,6 +2075,7 @@ enum UpdateHighlightErrorCode {
input UpdateHighlightInput {
annotation: String
highlightId: ID!
quote: String
sharedAt: Date
}

View file

@ -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(),
}

View file

@ -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 {

View file

@ -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
)
})
})
})