mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Return existing slug if page exists
This commit is contained in:
parent
86ae533b66
commit
fc662dd2d5
2 changed files with 29 additions and 32 deletions
|
|
@ -1,18 +1,17 @@
|
|||
import normalizeUrl from 'normalize-url'
|
||||
import * as privateIpLib from 'private-ip'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { enqueueParseRequest } from '../utils/createTask'
|
||||
|
||||
// TODO: switch to a proper Entity instead of using the old data models.
|
||||
import { DataModels } from '../resolvers/types'
|
||||
import { createPubSubClient, PubsubClient } from '../datalayer/pubsub'
|
||||
import { countByCreatedAt, createPage, getPageByParam } from '../elastic/pages'
|
||||
import { ArticleSavingRequestStatus, PageType } from '../elastic/types'
|
||||
import {
|
||||
ArticleSavingRequest,
|
||||
CreateArticleSavingRequestErrorCode,
|
||||
} from '../generated/graphql'
|
||||
// TODO: switch to a proper Entity instead of using the old data models.
|
||||
import { DataModels } from '../resolvers/types'
|
||||
import { enqueueParseRequest } from '../utils/createTask'
|
||||
import { generateSlug, pageToArticleSavingRequest } from '../utils/helpers'
|
||||
import * as privateIpLib from 'private-ip'
|
||||
import { countByCreatedAt, createPage, getPageByParam } from '../elastic/pages'
|
||||
import { ArticleSavingRequestStatus, PageType } from '../elastic/types'
|
||||
import { createPubSubClient, PubsubClient } from '../datalayer/pubsub'
|
||||
import normalizeUrl from 'normalize-url'
|
||||
|
||||
const SAVING_CONTENT = 'Your link is being saved...'
|
||||
|
||||
|
|
@ -92,10 +91,8 @@ export const createPageSaveRequest = async (
|
|||
userId,
|
||||
url: normalizedUrl,
|
||||
})
|
||||
if (page) {
|
||||
console.log('Page already exists', page.id, page.url)
|
||||
articleSavingRequestId = page.id
|
||||
} else {
|
||||
if (!page) {
|
||||
console.log('Page not exists', normalizedUrl)
|
||||
page = {
|
||||
id: articleSavingRequestId,
|
||||
userId,
|
||||
|
|
@ -106,7 +103,7 @@ export const createPageSaveRequest = async (
|
|||
readingProgressPercent: 0,
|
||||
slug: generateSlug(url),
|
||||
title: url,
|
||||
url,
|
||||
url: normalizedUrl,
|
||||
state: ArticleSavingRequestStatus.Processing,
|
||||
createdAt: new Date(),
|
||||
savedAt: new Date(),
|
||||
|
|
@ -123,7 +120,7 @@ export const createPageSaveRequest = async (
|
|||
}
|
||||
|
||||
// enqueue task to parse page
|
||||
await enqueueParseRequest(url, userId, articleSavingRequestId, priority)
|
||||
await enqueueParseRequest(url, userId, page.id, priority)
|
||||
|
||||
return pageToArticleSavingRequest(user, page)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
import { Readability } from '@omnivore/readability'
|
||||
import normalizeUrl from 'normalize-url'
|
||||
import { PubsubClient } from '../datalayer/pubsub'
|
||||
import { addHighlightToPage } from '../elastic/highlights'
|
||||
import { createPage, getPageByParam, updatePage } from '../elastic/pages'
|
||||
import { ArticleSavingRequestStatus, Page, PageType } from '../elastic/types'
|
||||
import { homePageURL } from '../env'
|
||||
import {
|
||||
Maybe,
|
||||
|
|
@ -15,13 +20,7 @@ import {
|
|||
wordsCount,
|
||||
} from '../utils/helpers'
|
||||
import { parsePreparedContent } from '../utils/parser'
|
||||
|
||||
import normalizeUrl from 'normalize-url'
|
||||
import { createPageSaveRequest } from './create_page_save_request'
|
||||
import { ArticleSavingRequestStatus, Page, PageType } from '../elastic/types'
|
||||
import { createPage, getPageByParam, updatePage } from '../elastic/pages'
|
||||
import { addHighlightToPage } from '../elastic/highlights'
|
||||
import { Readability } from '@omnivore/readability'
|
||||
|
||||
type SaveContext = {
|
||||
pubsub: PubsubClient
|
||||
|
|
@ -76,7 +75,6 @@ export const savePage = async (
|
|||
saver: SaverUserData,
|
||||
input: SavePageInput
|
||||
): Promise<SaveResult> => {
|
||||
const [slug, croppedPathname] = createSlug(input.url, input.title)
|
||||
const parseResult = await parsePreparedContent(
|
||||
input.url,
|
||||
{
|
||||
|
|
@ -88,12 +86,14 @@ export const savePage = async (
|
|||
},
|
||||
input.parseResult
|
||||
)
|
||||
|
||||
const [newSlug, croppedPathname] = createSlug(input.url, input.title)
|
||||
let slug = newSlug
|
||||
let pageId = input.clientRequestId
|
||||
const articleToSave = parsedContentToPage({
|
||||
url: input.url,
|
||||
title: input.title,
|
||||
userId: saver.userId,
|
||||
pageId: input.clientRequestId,
|
||||
pageId,
|
||||
slug,
|
||||
croppedPathname,
|
||||
parsedContent: parseResult.parsedContent,
|
||||
|
|
@ -102,7 +102,6 @@ export const savePage = async (
|
|||
canonicalUrl: parseResult.canonicalUrl,
|
||||
})
|
||||
|
||||
let pageId: string | undefined = undefined
|
||||
const existingPage = await getPageByParam({
|
||||
userId: saver.userId,
|
||||
url: articleToSave.url,
|
||||
|
|
@ -110,7 +109,6 @@ export const savePage = async (
|
|||
})
|
||||
|
||||
if (existingPage) {
|
||||
pageId = existingPage.id
|
||||
if (
|
||||
!(await updatePage(
|
||||
existingPage.id,
|
||||
|
|
@ -126,12 +124,13 @@ export const savePage = async (
|
|||
message: 'Failed to update existing page',
|
||||
}
|
||||
}
|
||||
input.clientRequestId = existingPage.id
|
||||
pageId = existingPage.id
|
||||
slug = existingPage.slug
|
||||
} else if (shouldParseInBackend(input)) {
|
||||
try {
|
||||
await createPageSaveRequest(
|
||||
saver.userId,
|
||||
input.url,
|
||||
articleToSave.url,
|
||||
ctx.models,
|
||||
ctx.pubsub,
|
||||
input.clientRequestId
|
||||
|
|
@ -143,16 +142,17 @@ export const savePage = async (
|
|||
}
|
||||
}
|
||||
} else {
|
||||
pageId = await createPage(articleToSave, ctx)
|
||||
if (!pageId) {
|
||||
const newPageId = await createPage(articleToSave, ctx)
|
||||
if (!newPageId) {
|
||||
return {
|
||||
errorCodes: [SaveErrorCode.Unknown],
|
||||
message: 'Failed to create new page',
|
||||
}
|
||||
}
|
||||
pageId = newPageId
|
||||
}
|
||||
|
||||
if (pageId && parseResult.highlightData) {
|
||||
if (parseResult.highlightData) {
|
||||
const highlight = {
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date(),
|
||||
|
|
@ -175,7 +175,7 @@ export const savePage = async (
|
|||
}
|
||||
|
||||
return {
|
||||
clientRequestId: input.clientRequestId,
|
||||
clientRequestId: pageId,
|
||||
url: `${homePageURL()}/${saver.username}/${slug}`,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue