fix: add rss label and feed url to the pdf in rss feed item

This commit is contained in:
Hongbo Wu
2024-01-26 22:34:55 +08:00
parent 2e1c99780a
commit ac5acf7f21
5 changed files with 44 additions and 89 deletions

View File

@ -2283,8 +2283,11 @@ export type SaveFileInput = {
clientRequestId: Scalars['ID'];
folder?: InputMaybe<Scalars['String']>;
labels?: InputMaybe<Array<CreateLabelInput>>;
publishedAt?: InputMaybe<Scalars['Date']>;
savedAt?: InputMaybe<Scalars['Date']>;
source: Scalars['String'];
state?: InputMaybe<ArticleSavingRequestStatus>;
subscription?: InputMaybe<Scalars['String']>;
uploadFileId: Scalars['ID'];
url: Scalars['String'];
};

View File

@ -1718,8 +1718,11 @@ input SaveFileInput {
clientRequestId: ID!
folder: String
labels: [CreateLabelInput!]
publishedAt: Date
savedAt: Date
source: String!
state: ArticleSavingRequestStatus
subscription: String
uploadFileId: ID!
url: String!
}

View File

@ -8,6 +8,7 @@ import {
} from '../generated/graphql'
import { redisDataSource } from '../redis_data_source'
import { userRepository } from '../repository/user'
import { saveFile } from '../services/save_file'
import { savePage } from '../services/save_page'
import { logger } from '../utils/logger'
@ -46,17 +47,6 @@ interface UploadFileResponse {
}
}
interface CreateArticleResponse {
data: {
createArticle: {
createdArticle: {
id: string
}
errorCodes: string[]
}
}
}
interface FetchResult {
finalUrl: string
title?: string
@ -174,57 +164,6 @@ const uploadPdf = async (
return uploadResult.id
}
const sendCreateArticleMutation = async (userId: string, input: unknown) => {
const data = JSON.stringify({
query: `mutation CreateArticle ($input: CreateArticleInput!){
createArticle(input:$input){
... on CreateArticleSuccess{
createdArticle{
id
}
}
... on CreateArticleError{
errorCodes
}
}
}`,
variables: {
input,
},
})
const auth = await signToken({ uid: userId }, JWT_SECRET)
try {
const response = await axios.post<CreateArticleResponse>(
`${REST_BACKEND_ENDPOINT}/graphql`,
data,
{
headers: {
Cookie: `auth=${auth as string};`,
'Content-Type': 'application/json',
},
timeout: REQUEST_TIMEOUT,
}
)
if (
response.data.data.createArticle.errorCodes &&
response.data.data.createArticle.errorCodes.length > 0
) {
console.error(
'error while creating article',
response.data.data.createArticle.errorCodes[0]
)
return null
}
return response.data.data.createArticle
} catch (error) {
console.error('error creating article', error)
return null
}
}
const sendImportStatusUpdate = async (
userId: string,
taskId: string,
@ -298,25 +237,37 @@ export const savePageJob = async (data: Data, attemptsMade: number) => {
const { title, contentType } = fetchedResult
let content = fetchedResult.content
const user = await userRepository.findById(userId)
if (!user) {
logger.error('Unable to save job, user can not be found.', {
userId,
url,
})
// if the user is not found, we do not retry
return false
}
// for pdf content, we need to upload the pdf
if (contentType === 'application/pdf') {
const encodedUrl = encodeURI(url)
const uploadFileId = await uploadPdf(url, userId, articleSavingRequestId)
const uploadedPdf = await sendCreateArticleMutation(userId, {
url: encodedUrl,
articleSavingRequestId,
uploadFileId,
state,
labels,
source,
folder,
rssFeedUrl,
savedAt,
publishedAt,
})
if (!uploadedPdf) {
throw new Error('error while saving uploaded pdf')
const result = await saveFile(
{
url,
uploadFileId,
state: state ? (state as ArticleSavingRequestStatus) : undefined,
labels,
source,
folder,
subscription: rssFeedUrl,
savedAt,
publishedAt,
clientRequestId: articleSavingRequestId,
},
user
)
if (result.__typename == 'SaveError') {
throw new Error(result.message || result.errorCodes[0])
}
isSaved = true
@ -331,16 +282,6 @@ export const savePageJob = async (data: Data, attemptsMade: number) => {
state = ArticleSavingRequestStatus.Failed
}
const user = await userRepository.findById(userId)
if (!user) {
logger.error('Unable to save job, user can not be found.', {
userId,
url,
})
// if the user is not found, we do not retry
return false
}
// for non-pdf content, we need to save the page
const result = await savePage(
{

View File

@ -545,6 +545,9 @@ const schema = gql`
state: ArticleSavingRequestStatus
labels: [CreateLabelInput!]
folder: String
savedAt: Date
publishedAt: Date
subscription: String
}
input ParseResult {

View File

@ -14,6 +14,7 @@ export const saveFile = async (
const uploadFile = await findUploadFileById(input.uploadFileId)
if (!uploadFile) {
return {
__typename: 'SaveError',
errorCodes: [SaveErrorCode.Unauthorized],
}
}
@ -24,6 +25,7 @@ export const saveFile = async (
if (!uploadFileData) {
return {
__typename: 'SaveError',
errorCodes: [SaveErrorCode.Unknown],
}
}
@ -34,6 +36,8 @@ export const saveFile = async (
{
state: (input.state as unknown as LibraryItemState) || undefined,
folder: input.folder || undefined,
savedAt: input.savedAt || undefined,
publishedAt: input.publishedAt || undefined,
},
user.id
)
@ -43,7 +47,8 @@ export const saveFile = async (
await createAndSaveLabelsInLibraryItem(
input.clientRequestId,
user.id,
input.labels
input.labels,
input.subscription
)
return {