Reset state to processing when creating articles

This commit is contained in:
Hongbo Wu
2023-03-21 13:05:00 +08:00
parent d733576e41
commit bc9ba1d976
2 changed files with 59 additions and 51 deletions

View File

@ -3,6 +3,26 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-floating-promises */
import { Readability } from '@omnivore/readability'
import * as httpContext from 'express-http-context'
import graphqlFields from 'graphql-fields'
import normalizeUrl from 'normalize-url'
import { searchHighlights } from '../../elastic/highlights'
import {
createPage,
getPageByParam,
searchAsYouType,
searchPages,
updatePage,
updatePagesAsync,
} from '../../elastic/pages'
import {
ArticleSavingRequestStatus,
Page,
PageType,
SearchItem as SearchItemData,
} from '../../elastic/types'
import { env } from '../../env'
import {
Article,
ArticleError,
@ -51,11 +71,13 @@ import {
UpdatesSinceErrorCode,
UpdatesSinceSuccess,
} from '../../generated/graphql'
import { createPageSaveRequest } from '../../services/create_page_save_request'
import { parsedContentToPage } from '../../services/save_page'
import { saveSearchHistory } from '../../services/search_history'
import { traceAs } from '../../tracing'
import { Merge } from '../../util'
import {
getStorageFileDetails,
makeStorageFilePublic,
} from '../../utils/uploads'
import { analytics } from '../../utils/analytics'
import { isSiteBlockedForParse } from '../../utils/blocked'
import { ContentParseError } from '../../utils/errors'
import {
authorized,
@ -67,45 +89,19 @@ import {
userDataToUser,
validatedDate,
} from '../../utils/helpers'
import { createImageProxyUrl } from '../../utils/imageproxy'
import {
getDistillerResult,
htmlToMarkdown,
ParsedContentPuppeteer,
parsePreparedContent,
} from '../../utils/parser'
import { isSiteBlockedForParse } from '../../utils/blocked'
import { Readability } from '@omnivore/readability'
import { traceAs } from '../../tracing'
import { createImageProxyUrl } from '../../utils/imageproxy'
import normalizeUrl from 'normalize-url'
import { WithDataSourcesContext } from '../types'
import { parseSearchQuery, SortBy, SortOrder } from '../../utils/search'
import { createPageSaveRequest } from '../../services/create_page_save_request'
import { analytics } from '../../utils/analytics'
import { env } from '../../env'
import graphqlFields from 'graphql-fields'
import {
ArticleSavingRequestStatus,
Page,
PageType,
SearchItem as SearchItemData,
} from '../../elastic/types'
import {
createPage,
getPageById,
getPageByParam,
searchAsYouType,
searchPages,
updatePage,
updatePagesAsync,
} from '../../elastic/pages'
import { searchHighlights } from '../../elastic/highlights'
import { saveSearchHistory } from '../../services/search_history'
import { parsedContentToPage } from '../../services/save_page'
import * as httpContext from 'express-http-context'
getStorageFileDetails,
makeStorageFilePublic,
} from '../../utils/uploads'
import { WithDataSourcesContext } from '../types'
enum ArticleFormat {
Markdown = 'markdown',
@ -649,24 +645,18 @@ export const setBookmarkArticleResolver = authorized<
{ input: { articleID, bookmark } },
{ claims: { uid }, log, pubsub }
) => {
const page = await getPageById(articleID)
const page = await getPageByParam({
userId: uid,
_id: articleID,
})
if (!page) {
return { errorCodes: [SetBookmarkArticleErrorCode.NotFound] }
}
if (!bookmark) {
const pageRemoved = await getPageByParam({
userId: uid,
_id: articleID,
})
if (!pageRemoved) {
return { errorCodes: [SetBookmarkArticleErrorCode.NotFound] }
}
// delete the page and its metadata
const deleted = await updatePage(
pageRemoved.id,
page.id,
{
state: ArticleSavingRequestStatus.Deleted,
labels: [],
@ -684,7 +674,7 @@ export const setBookmarkArticleResolver = authorized<
userId: uid,
event: 'link_removed',
properties: {
url: pageRemoved.url,
url: page.url,
env: env.server.apiEnv,
},
})
@ -704,7 +694,7 @@ export const setBookmarkArticleResolver = authorized<
// Make sure article.id instead of userArticle.id has passed. We use it for cache updates
return {
bookmarkedArticle: {
...pageRemoved,
...page,
isArchived: false,
savedByViewer: false,
postedByViewer: false,

View File

@ -2,7 +2,12 @@ import normalizeUrl from 'normalize-url'
import * as privateIpLib from 'private-ip'
import { v4 as uuidv4 } from 'uuid'
import { createPubSubClient, PubsubClient } from '../datalayer/pubsub'
import { countByCreatedAt, createPage, getPageByParam } from '../elastic/pages'
import {
countByCreatedAt,
createPage,
getPageByParam,
updatePage,
} from '../elastic/pages'
import { ArticleSavingRequestStatus, PageType } from '../elastic/types'
import {
ArticleSavingRequest,
@ -87,6 +92,10 @@ export const createPageSaveRequest = async (
stripWWW: false,
})
const ctx = {
pubsub,
uid: userId,
}
let page = await getPageByParam({
userId,
url: normalizedUrl,
@ -110,7 +119,7 @@ export const createPageSaveRequest = async (
}
// create processing page
const pageId = await createPage(page, { pubsub, uid: userId })
const pageId = await createPage(page, ctx)
if (!pageId) {
console.log('Failed to create page', page)
return Promise.reject({
@ -118,7 +127,16 @@ export const createPageSaveRequest = async (
})
}
}
// reset state to processing
if (page.state !== ArticleSavingRequestStatus.Processing) {
await updatePage(
page.id,
{
state: ArticleSavingRequestStatus.Processing,
},
ctx
)
}
// enqueue task to parse page
await enqueueParseRequest(url, userId, page.id, priority)