mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Fix a bug to have multiple pages with the same url in lib
This commit is contained in:
parent
afe8b6e948
commit
9e3db0e053
2 changed files with 72 additions and 93 deletions
|
|
@ -246,8 +246,8 @@ export const createArticleResolver = authorized<
|
|||
|
||||
const saveTime = new Date()
|
||||
const slug = generateSlug(parsedContent?.title || croppedPathname)
|
||||
let articleToSave: Page = {
|
||||
id: '',
|
||||
const articleToSave: Page = {
|
||||
id: pageId || '',
|
||||
userId: uid,
|
||||
originalHtml: domContent,
|
||||
content: parsedContent?.content || '',
|
||||
|
|
@ -317,63 +317,47 @@ export const createArticleResolver = authorized<
|
|||
)
|
||||
}
|
||||
|
||||
const existingPage = await getPageByParam({
|
||||
userId: uid,
|
||||
url: articleToSave.url,
|
||||
state: ArticleSavingRequestStatus.Succeeded,
|
||||
})
|
||||
if (existingPage) {
|
||||
// update existing page in elastic
|
||||
existingPage.slug = slug
|
||||
existingPage.savedAt = saveTime
|
||||
existingPage.archivedAt = archive ? saveTime : undefined
|
||||
existingPage.url = uploadFileUrlOverride || articleToSave.url
|
||||
existingPage.hash = articleToSave.hash
|
||||
|
||||
await updatePage(existingPage.id, existingPage, { ...ctx, uid })
|
||||
|
||||
log.info('page updated in elastic', existingPage.id)
|
||||
articleToSave = existingPage
|
||||
} else {
|
||||
// create new page in elastic
|
||||
if (!pageId) {
|
||||
pageId = await createPage(articleToSave, { ...ctx, uid })
|
||||
if (!pageId) {
|
||||
return pageError(
|
||||
{
|
||||
errorCodes: [CreateArticleErrorCode.ElasticError],
|
||||
},
|
||||
ctx,
|
||||
pageId
|
||||
)
|
||||
}
|
||||
} else {
|
||||
const updated = await updatePage(pageId, articleToSave, {
|
||||
...ctx,
|
||||
uid,
|
||||
})
|
||||
|
||||
if (!updated) {
|
||||
return pageError(
|
||||
{
|
||||
errorCodes: [CreateArticleErrorCode.ElasticError],
|
||||
},
|
||||
ctx,
|
||||
pageId
|
||||
)
|
||||
}
|
||||
// create new page in elastic
|
||||
if (!pageId) {
|
||||
const newPageId = await createPage(articleToSave, { ...ctx, uid })
|
||||
if (!newPageId) {
|
||||
return pageError(
|
||||
{
|
||||
errorCodes: [CreateArticleErrorCode.ElasticError],
|
||||
},
|
||||
ctx,
|
||||
pageId
|
||||
)
|
||||
}
|
||||
articleToSave.id = newPageId
|
||||
} else {
|
||||
// update existing page's state from processing to succeeded
|
||||
articleToSave.archivedAt = archive ? saveTime : undefined
|
||||
articleToSave.url = uploadFileUrlOverride || articleToSave.url
|
||||
const updated = await updatePage(pageId, articleToSave, {
|
||||
...ctx,
|
||||
uid,
|
||||
})
|
||||
|
||||
log.info(
|
||||
'page created in elastic',
|
||||
pageId,
|
||||
articleToSave.url,
|
||||
articleToSave.slug,
|
||||
articleToSave.title
|
||||
)
|
||||
articleToSave.id = pageId
|
||||
if (!updated) {
|
||||
return pageError(
|
||||
{
|
||||
errorCodes: [CreateArticleErrorCode.ElasticError],
|
||||
},
|
||||
ctx,
|
||||
pageId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
log.info(
|
||||
'page created in elastic',
|
||||
articleToSave.id,
|
||||
articleToSave.url,
|
||||
articleToSave.slug,
|
||||
articleToSave.title
|
||||
)
|
||||
|
||||
const createdArticle: PartialArticle = {
|
||||
...articleToSave,
|
||||
isArchived: !!articleToSave.archivedAt,
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import {
|
|||
import { generateSlug, pageToArticleSavingRequest } from '../utils/helpers'
|
||||
import * as privateIpLib from 'private-ip'
|
||||
import { countByCreatedAt, createPage, getPageByParam } from '../elastic/pages'
|
||||
import { ArticleSavingRequestStatus, Page, PageType } from '../elastic/types'
|
||||
import { ArticleSavingRequestStatus, PageType } from '../elastic/types'
|
||||
import { createPubSubClient, PubsubClient } from '../datalayer/pubsub'
|
||||
import normalizeUrl from 'normalize-url'
|
||||
|
||||
|
|
@ -82,52 +82,47 @@ export const createPageSaveRequest = async (
|
|||
// get priority by checking rate limit if not specified
|
||||
priority = priority || (await getPriorityByRateLimit(userId))
|
||||
|
||||
// look for existing page
|
||||
url = normalizeUrl(url, {
|
||||
stripHash: true,
|
||||
stripWWW: false,
|
||||
})
|
||||
const createdTaskName = await enqueueParseRequest(
|
||||
url,
|
||||
userId,
|
||||
articleSavingRequestId,
|
||||
priority
|
||||
)
|
||||
|
||||
const existingPage = await getPageByParam({
|
||||
let page = await getPageByParam({
|
||||
userId,
|
||||
url,
|
||||
state: ArticleSavingRequestStatus.Succeeded,
|
||||
})
|
||||
if (existingPage) {
|
||||
console.log('Page already exists', url)
|
||||
existingPage.taskName = createdTaskName
|
||||
return pageToArticleSavingRequest(user, existingPage)
|
||||
if (page) {
|
||||
console.log('Page already exists', page)
|
||||
articleSavingRequestId = page.id
|
||||
} else {
|
||||
page = {
|
||||
id: articleSavingRequestId,
|
||||
userId,
|
||||
content: SAVING_CONTENT,
|
||||
hash: '',
|
||||
pageType: PageType.Unknown,
|
||||
readingProgressAnchorIndex: 0,
|
||||
readingProgressPercent: 0,
|
||||
slug: generateSlug(url),
|
||||
title: url,
|
||||
url,
|
||||
state: ArticleSavingRequestStatus.Processing,
|
||||
createdAt: new Date(),
|
||||
savedAt: new Date(),
|
||||
}
|
||||
|
||||
// create processing page
|
||||
const pageId = await createPage(page, { pubsub, uid: userId })
|
||||
if (!pageId) {
|
||||
console.log('Failed to create page', page)
|
||||
return Promise.reject({
|
||||
errorCode: CreateArticleSavingRequestErrorCode.BadData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const page: Page = {
|
||||
id: articleSavingRequestId,
|
||||
userId,
|
||||
content: SAVING_CONTENT,
|
||||
hash: '',
|
||||
pageType: PageType.Unknown,
|
||||
readingProgressAnchorIndex: 0,
|
||||
readingProgressPercent: 0,
|
||||
slug: generateSlug(url),
|
||||
title: url,
|
||||
url,
|
||||
taskName: createdTaskName,
|
||||
state: ArticleSavingRequestStatus.Processing,
|
||||
createdAt: new Date(),
|
||||
savedAt: new Date(),
|
||||
}
|
||||
|
||||
const pageId = await createPage(page, { pubsub, uid: userId })
|
||||
if (!pageId) {
|
||||
console.log('Failed to create page', page)
|
||||
return Promise.reject({
|
||||
errorCode: CreateArticleSavingRequestErrorCode.BadData,
|
||||
})
|
||||
}
|
||||
// enqueue task to parse page
|
||||
await enqueueParseRequest(url, userId, articleSavingRequestId, priority)
|
||||
|
||||
return pageToArticleSavingRequest(user, page)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue