Merge deleteLabel in page and highlight

This commit is contained in:
Hongbo Wu 2022-06-08 17:25:33 +08:00
parent a981e13c86
commit 7c840f5b1e
2 changed files with 33 additions and 74 deletions

View file

@ -94,7 +94,7 @@ export const updateLabelsInPage = async (
}
}
export const deleteLabelInPages = async (
export const deleteLabel = async (
userId: string,
label: string,
ctx: PageContext
@ -104,8 +104,12 @@ export const deleteLabelInPages = async (
index: INDEX_ALIAS,
body: {
script: {
source:
'ctx._source.labels.removeIf(label -> label.name == params.label)',
source: `if (ctx._source.highlights != null) {
ctx._source.highlights[0].labels.removeIf(label -> label.name == params.label)
}
if (ctx._source.labels != null) {
ctx._source.labels.removeIf(label -> label.name == params.label)
}`,
lang: 'painless',
params: {
label: label,
@ -113,12 +117,12 @@ export const deleteLabelInPages = async (
},
query: {
bool: {
filter: [
{
term: {
userId,
},
must: {
term: {
userId,
},
},
should: [
{
nested: {
path: 'labels',
@ -129,11 +133,28 @@ export const deleteLabelInPages = async (
},
},
},
{
nested: {
path: 'highlights',
query: {
nested: {
path: 'highlights.labels',
query: {
term: {
'highlights.labels.name': label,
},
},
},
},
},
},
],
minimum_should_match: 1,
},
},
},
refresh: ctx.refresh,
conflicts: 'proceed', // ignore conflicts
})
body.updated > 0 &&
@ -255,61 +276,3 @@ export const setLabelsForHighlight = async (
return false
}
}
export const deleteLabelForHighlights = async (
userId: string,
label: string,
ctx: PageContext
): Promise<boolean> => {
try {
const { body } = await client.updateByQuery({
index: INDEX_ALIAS,
body: {
script: {
source:
'ctx._source.highlights[0].labels.removeIf(label -> label.name == params.label)',
lang: 'painless',
params: {
label: label,
},
},
query: {
bool: {
filter: [
{
term: {
userId,
},
},
{
nested: {
path: 'highlights',
query: {
nested: {
path: 'highlights.labels',
query: {
term: {
'highlights.labels.name': label,
},
},
},
},
},
},
],
},
},
},
refresh: ctx.refresh,
conflicts: 'proceed', // ignore conflicts
})
body.updated > 0 &&
(await ctx.pubsub.entityDeleted(EntityType.LABEL, label, ctx.uid))
return true
} catch (e) {
console.error('failed to delete a label for highlights in elastic', e)
return false
}
}

View file

@ -31,15 +31,13 @@ import { createPubSubClient } from '../../datalayer/pubsub'
import { AppDataSource } from '../../server'
import { getPageById } from '../../elastic/pages'
import {
deleteLabelForHighlights,
deleteLabelInPages,
deleteLabel,
setLabelsForHighlight,
updateLabelInPage,
updateLabelsInPage,
} from '../../elastic/labels'
import { getHighlightById } from '../../elastic/highlights'
import { getLabelsByIds } from '../../services/labels'
import { PageContext } from '../../elastic/types'
export const labelsResolver = authorized<LabelsSuccess, LabelsError>(
async (_obj, _params, { claims: { uid }, log }) => {
@ -180,13 +178,11 @@ export const deleteLabelResolver = authorized<
}
}
const ctx: PageContext = {
// delete label in elastic pages and highlights
await deleteLabel(uid, labelId, {
pubsub: createPubSubClient(),
uid,
}
// delete label in elastic pages and highlights
await deleteLabelInPages(uid, label.name, ctx)
await deleteLabelForHighlights(uid, label.name, ctx)
})
analytics.track({
userId: uid,