do not publish a pubsub event if the page is imported

This commit is contained in:
Hongbo Wu
2023-08-17 18:24:57 +08:00
parent ec88a9fe8e
commit dedd76cfb7
8 changed files with 36 additions and 26 deletions

View File

@ -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<boolean> => {
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<boolean> => {
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<boolean> => {
try {
const { body } = await client.updateByQuery({

View File

@ -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<boolean> => {
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<boolean> => {
try {
@ -105,7 +105,7 @@ export const updateLabelsInPage = async (
export const deleteLabel = async (
label: string,
ctx: PageContext
ctx: Context
): Promise<boolean> => {
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<boolean> => {
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<boolean> => {
try {

View File

@ -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<string | undefined> => {
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<Page>(EntityType.PAGE, page, ctx.uid)
// only publish a pubsub event if we should
if (ctx.shouldPublish) {
await ctx.pubsub?.entityCreated<Page>(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<Page>,
ctx: PageContext
ctx: Context
): Promise<boolean> => {
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<boolean> => {
try {
const { body } = await client.delete({
@ -755,7 +759,7 @@ export const countByCreatedAt = async (
export const deletePagesByParam = async <K extends keyof ParamSet>(
param: Record<K, ParamSet[K]>,
ctx: PageContext
ctx: Context
): Promise<boolean> => {
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,

View File

@ -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[]

View File

@ -204,10 +204,11 @@ const keys = ['_id', 'url', 'slug', 'userId', 'uploadFileId', 'state'] as const
export type ParamSet = PickTuple<Page, typeof keys>
export interface PageContext {
export interface Context {
pubsub: PubsubClient
refresh?: boolean
uid: string
shouldPublish?: boolean
}
export interface PageSearchArgs {

View File

@ -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<Label[]> => {
const user = await getRepository(User).findOneBy({

View File

@ -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<string | undefined> => {
const ctx: PageContext = {
const ctx: Context = {
pubsub: createPubSubClient(),
refresh: true,
uid: userId,

View File

@ -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],