Only publish labels to add to the pubsub queue

This commit is contained in:
Hongbo Wu 2023-03-09 21:46:33 +08:00
parent 315c54466e
commit 590a1cb8ad
2 changed files with 25 additions and 12 deletions

View file

@ -57,7 +57,8 @@ export const addLabelInPage = async (
export const updateLabelsInPage = async (
pageId: string,
labels: Label[],
ctx: PageContext
ctx: PageContext,
labelsToAdd?: Label[]
): Promise<boolean> => {
try {
const { body } = await client.update({
@ -74,12 +75,16 @@ export const updateLabelsInPage = async (
})
if (body.result !== 'updated') return false
for (const label of labels) {
await ctx.pubsub.entityCreated<Label & { pageId: string }>(
EntityType.LABEL,
{ pageId, ...label },
ctx.uid
if (labelsToAdd) {
// publish labels to be added
await Promise.all(
labelsToAdd.map((label) =>
ctx.pubsub.entityCreated<Label & { pageId: string }>(
EntityType.LABEL,
{ pageId, ...label },
ctx.uid
)
)
)
}

View file

@ -243,12 +243,20 @@ export const setLabelsResolver = authorized<
errorCodes: [SetLabelsErrorCode.NotFound],
}
}
// filter out labels that are already set
const labelsToAdd = labels.filter(
(label) => !page.labels?.some((pageLabel) => pageLabel.id === label.id)
)
// update labels in the page
const updated = await updateLabelsInPage(pageId, labels, {
pubsub,
uid,
})
const updated = await updateLabelsInPage(
pageId,
labels,
{
pubsub,
uid,
},
labelsToAdd
)
if (!updated) {
return {
errorCodes: [SetLabelsErrorCode.NotFound],