upload highlight and label to gcs if successfully updated in elastic (#420)

This commit is contained in:
Hongbo Wu
2022-04-14 12:14:15 +08:00
committed by GitHub
parent 487713109d
commit c0966d908f
3 changed files with 53 additions and 15 deletions

View File

@ -134,7 +134,7 @@ export const deleteHighlight = async (
refresh: ctx.refresh,
})
if (body.result !== 'updated') return false
if (body.updated === 0) return false
await ctx.pubsub.entityDeleted(EntityType.HIGHLIGHT, highlightId, ctx.uid)
@ -279,7 +279,7 @@ export const updateHighlight = async (
refresh: ctx.refresh,
})
if (body.result !== 'updated') return false
if (body.updated === 0) return false
await ctx.pubsub.entityUpdated<Highlight>(
EntityType.HIGHLIGHT,

View File

@ -3,14 +3,14 @@ import { client, INDEX_ALIAS } from './index'
import { EntityType } from '../datalayer/pubsub'
export const addLabelInPage = async (
id: string,
pageId: string,
label: Label,
ctx: PageContext
): Promise<boolean> => {
try {
const { body } = await client.update({
index: INDEX_ALIAS,
id,
id: pageId,
body: {
script: {
source: `if (ctx._source.labels == null) {
@ -32,7 +32,7 @@ export const addLabelInPage = async (
await ctx.pubsub.entityCreated<Label & { pageId: string }>(
EntityType.LABEL,
{ pageId: id, ...label },
{ pageId, ...label },
ctx.uid
)
@ -43,6 +43,42 @@ export const addLabelInPage = async (
}
}
export const updateLabelsInPage = async (
pageId: string,
labels: Label[],
ctx: PageContext
): Promise<boolean> => {
try {
const { body } = await client.update({
index: INDEX_ALIAS,
id: pageId,
body: {
doc: {
labels: labels,
updatedAt: new Date(),
},
},
refresh: ctx.refresh,
retry_on_conflict: 3,
})
if (body.result !== 'updated') return false
for (const label of labels) {
await ctx.pubsub.entityCreated<Label & { pageId: string }>(
EntityType.LABEL,
{ pageId, ...label },
ctx.uid
)
}
return true
} catch (e) {
console.error('failed to update labels in elastic', e)
return false
}
}
export const deleteLabelInPages = async (
userId: string,
label: string,
@ -85,7 +121,7 @@ export const deleteLabelInPages = async (
refresh: ctx.refresh,
})
if (body.result !== 'updated') return false
if (body.updated === 0) return false
await ctx.pubsub.entityDeleted(EntityType.LABEL, label, ctx.uid)

View File

@ -28,8 +28,8 @@ import { ILike, In } from 'typeorm'
import { getRepository, setClaims } from '../../entity/utils'
import { createPubSubClient } from '../../datalayer/pubsub'
import { AppDataSource } from '../../server'
import { getPageById, updatePage } from '../../elastic/pages'
import { deleteLabelInPages } from '../../elastic/labels'
import { getPageById } from '../../elastic/pages'
import { deleteLabelInPages, updateLabelsInPage } from '../../elastic/labels'
export const labelsResolver = authorized<LabelsSuccess, LabelsError>(
async (_obj, _params, { claims: { uid }, log }) => {
@ -231,13 +231,15 @@ export const setLabelsResolver = authorized<
}
// update labels in the page
await updatePage(
pageId,
{
labels,
},
{ pubsub, uid }
)
const updated = await updateLabelsInPage(pageId, labels, {
pubsub,
uid,
})
if (!updated) {
return {
errorCodes: [SetLabelsErrorCode.NotFound],
}
}
analytics.track({
userId: uid,