From dedd76cfb755f7cbbde61b034643179d69c2d801 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 17 Aug 2023 18:24:57 +0800 Subject: [PATCH] do not publish a pubsub event if the page is imported --- packages/api/src/elastic/highlights.ts | 8 ++++---- packages/api/src/elastic/labels.ts | 12 ++++++------ packages/api/src/elastic/pages.ts | 18 +++++++++++------- packages/api/src/elastic/recommendation.ts | 4 ++-- packages/api/src/elastic/types.ts | 3 ++- packages/api/src/services/labels.ts | 6 +++--- packages/api/src/services/popular_reads.ts | 4 ++-- packages/api/src/services/save_page.ts | 7 ++++++- 8 files changed, 36 insertions(+), 26 deletions(-) diff --git a/packages/api/src/elastic/highlights.ts b/packages/api/src/elastic/highlights.ts index 3d0808c04..c5b516118 100644 --- a/packages/api/src/elastic/highlights.ts +++ b/packages/api/src/elastic/highlights.ts @@ -3,9 +3,9 @@ import { EntityType } from '../datalayer/pubsub' import { SortBy, SortOrder, SortParams } from '../utils/search' import { client, INDEX_ALIAS, logger } from './index' import { + Context, Highlight, Page, - PageContext, PageType, SearchItem, SearchResponse, @@ -14,7 +14,7 @@ import { export const addHighlightToPage = async ( id: string, highlight: Highlight, - ctx: PageContext + ctx: Context ): Promise => { try { const { body } = await client.update({ @@ -97,7 +97,7 @@ export const getHighlightById = async ( export const deleteHighlight = async ( highlightId: string, - ctx: PageContext + ctx: Context ): Promise => { try { const { body } = await client.updateByQuery({ @@ -256,7 +256,7 @@ export const searchHighlights = async ( export const updateHighlight = async ( highlight: Highlight, - ctx: PageContext + ctx: Context ): Promise => { try { const { body } = await client.updateByQuery({ diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index 5864a0dff..a6364ce6d 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -1,12 +1,12 @@ import { errors } from '@elastic/elasticsearch' import { EntityType } from '../datalayer/pubsub' import { client, INDEX_ALIAS, logger } from './index' -import { Label, PageContext } from './types' +import { Context, Label } from './types' export const addLabelInPage = async ( pageId: string, label: Label, - ctx: PageContext + ctx: Context ): Promise => { try { const { body } = await client.update({ @@ -57,7 +57,7 @@ export const addLabelInPage = async ( export const updateLabelsInPage = async ( pageId: string, labels: Label[], - ctx: PageContext, + ctx: Context, labelsToAdd?: Label[] ): Promise => { try { @@ -105,7 +105,7 @@ export const updateLabelsInPage = async ( export const deleteLabel = async ( label: string, - ctx: PageContext + ctx: Context ): Promise => { try { const { body } = await client.updateByQuery({ @@ -181,7 +181,7 @@ export const deleteLabel = async ( export const updateLabel = async ( label: Label, - ctx: PageContext + ctx: Context ): Promise => { try { const { body } = await client.updateByQuery({ @@ -273,7 +273,7 @@ export const updateLabel = async ( export const setLabelsForHighlight = async ( highlightId: string, labels: Label[], - ctx: PageContext, + ctx: Context, labelsToAdd?: Label[] ): Promise => { try { diff --git a/packages/api/src/elastic/pages.ts b/packages/api/src/elastic/pages.ts index 0545b1093..d1fba284a 100644 --- a/packages/api/src/elastic/pages.ts +++ b/packages/api/src/elastic/pages.ts @@ -18,9 +18,9 @@ import { import { client, INDEX_ALIAS, logger } from './index' import { ArticleSavingRequestStatus, + Context, Label, Page, - PageContext, PageSearchArgs, PageType, ParamSet, @@ -404,7 +404,7 @@ const appendSiteNameFilter = ( export const createPage = async ( page: Page, - ctx: PageContext + ctx: Context ): Promise => { try { if (page.content.length > MAX_CONTENT_LENGTH) { @@ -429,7 +429,11 @@ export const createPage = async ( }) page.id = body._id as string - await ctx.pubsub.entityCreated(EntityType.PAGE, page, ctx.uid) + + // only publish a pubsub event if we should + if (ctx.shouldPublish) { + await ctx.pubsub?.entityCreated(EntityType.PAGE, page, ctx.uid) + } return page.id } catch (e) { @@ -441,7 +445,7 @@ export const createPage = async ( export const updatePage = async ( id: string, page: Partial, - ctx: PageContext + ctx: Context ): Promise => { try { if (page.content && page.content.length > MAX_CONTENT_LENGTH) { @@ -492,7 +496,7 @@ export const updatePage = async ( export const deletePage = async ( id: string, - ctx: PageContext + ctx: Context ): Promise => { try { const { body } = await client.delete({ @@ -755,7 +759,7 @@ export const countByCreatedAt = async ( export const deletePagesByParam = async ( param: Record, - ctx: PageContext + ctx: Context ): Promise => { try { const params = { @@ -852,7 +856,7 @@ export const searchAsYouType = async ( } export const updatePages = async ( - ctx: PageContext, + ctx: Context, action: BulkActionType, args: PageSearchArgs, maxDocs: number, diff --git a/packages/api/src/elastic/recommendation.ts b/packages/api/src/elastic/recommendation.ts index 3caa544e6..bb8a6c4c4 100644 --- a/packages/api/src/elastic/recommendation.ts +++ b/packages/api/src/elastic/recommendation.ts @@ -2,13 +2,13 @@ import { logger } from '.' import { createPage, getPageByParam, updatePage } from './pages' import { ArticleSavingRequestStatus, + Context, Page, - PageContext, Recommendation, } from './types' export const addRecommendation = async ( - ctx: PageContext, + ctx: Context, page: Page, recommendation: Recommendation, highlightIds?: string[] diff --git a/packages/api/src/elastic/types.ts b/packages/api/src/elastic/types.ts index b6e5bf41b..f3c32e87e 100644 --- a/packages/api/src/elastic/types.ts +++ b/packages/api/src/elastic/types.ts @@ -204,10 +204,11 @@ const keys = ['_id', 'url', 'slug', 'userId', 'uploadFileId', 'state'] as const export type ParamSet = PickTuple -export interface PageContext { +export interface Context { pubsub: PubsubClient refresh?: boolean uid: string + shouldPublish?: boolean } export interface PageSearchArgs { diff --git a/packages/api/src/services/labels.ts b/packages/api/src/services/labels.ts index 5837ddf72..5ed059029 100644 --- a/packages/api/src/services/labels.ts +++ b/packages/api/src/services/labels.ts @@ -1,7 +1,7 @@ import DataLoader from 'dataloader' import { In } from 'typeorm' import { addLabelInPage } from '../elastic/labels' -import { PageContext } from '../elastic/types' +import { Context } from '../elastic/types' import { Label } from '../entity/label' import { Link } from '../entity/link' import { User } from '../entity/user' @@ -37,7 +37,7 @@ const batchGetLabelsFromLinkIds = async ( export const labelsLoader = new DataLoader(batchGetLabelsFromLinkIds) export const addLabelToPage = async ( - ctx: PageContext, + ctx: Context, pageId: string, label: { name: string @@ -114,7 +114,7 @@ export const createLabel = async ( } export const createLabels = async ( - ctx: PageContext, + ctx: Context, labels: CreateLabelInput[] ): Promise => { const user = await getRepository(User).findOneBy({ diff --git a/packages/api/src/services/popular_reads.ts b/packages/api/src/services/popular_reads.ts index 7f79406d5..a536a6848 100644 --- a/packages/api/src/services/popular_reads.ts +++ b/packages/api/src/services/popular_reads.ts @@ -3,7 +3,7 @@ import { readFileSync } from 'fs' import path from 'path' import { createPubSubClient } from '../datalayer/pubsub' import { createPage } from '../elastic/pages' -import { ArticleSavingRequestStatus, Page, PageContext } from '../elastic/types' +import { ArticleSavingRequestStatus, Context, Page } from '../elastic/types' import { PageType } from '../generated/graphql' import { generateSlug, stringToHash } from '../utils/helpers' import { logger } from '../utils/logger' @@ -61,7 +61,7 @@ export const addPopularRead = async ( userId: string, name: string ): Promise => { - const ctx: PageContext = { + const ctx: Context = { pubsub: createPubSubClient(), refresh: true, uid: userId, diff --git a/packages/api/src/services/save_page.ts b/packages/api/src/services/save_page.ts index 2790ae311..fcac553fc 100644 --- a/packages/api/src/services/save_page.ts +++ b/packages/api/src/services/save_page.ts @@ -174,7 +174,12 @@ export const savePage = async ( } } } else { - const newPageId = await createPage(articleToSave, ctx) + // do not publish a pubsub event if the page is imported + const shouldPublish = input.source !== 'csv-importer' + const newPageId = await createPage(articleToSave, { + ...ctx, + shouldPublish, + }) if (!newPageId) { return { errorCodes: [SaveErrorCode.Unknown],