From c240f1d2c81619e17ec8f49b6147411eae04d292 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 12 Jan 2023 10:50:06 +0800 Subject: [PATCH] Make SavePage API call if we have content provided --- packages/import-handler/src/index.ts | 65 +++++++++++++++++++++------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/packages/import-handler/src/index.ts b/packages/import-handler/src/index.ts index 3411f07d1..262dbf138 100644 --- a/packages/import-handler/src/index.ts +++ b/packages/import-handler/src/index.ts @@ -6,6 +6,7 @@ import { Stream } from 'node:stream' import { v4 as uuid } from 'uuid' import { CONTENT_FETCH_URL, createCloudTask, emailUserUrl } from './task' +import axios from 'axios' import { promisify } from 'util' import * as jwt from 'jsonwebtoken' import { Readability } from '@omnivore/readability' @@ -136,6 +137,44 @@ const urlHandler = async (ctx: ImportContext, url: URL): Promise => { } } +const sendSavePageMutation = async (userId: string, input: unknown) => { + const JWT_SECRET = process.env.JWT_SECRET + const REST_BACKEND_ENDPOINT = process.env.REST_BACKEND_ENDPOINT + + if (!JWT_SECRET || !REST_BACKEND_ENDPOINT) { + throw 'Environment not configured correctly' + } + + const data = JSON.stringify({ + query: `mutation SavePage ($input: SavePageInput!){ + savePage(input:$input){ + ... on SaveSuccess{ + url + clientRequestId + } + ... on SaveError{ + errorCodes + } + } + }`, + variables: { + input: Object.assign({}, input, { source: 'puppeteer-parse' }), + }, + }) + + const auth = (await signToken({ uid: userId }, JWT_SECRET)) as string + const response = await axios.post(`${REST_BACKEND_ENDPOINT}/graphql`, data, { + headers: { + Cookie: `auth=${auth};`, + 'Content-Type': 'application/json', + }, + }) + console.log('save page response: ', response) + + /* eslint-disable @typescript-eslint/no-unsafe-member-access */ + return !!response.data.data.savePage +} + const contentHandler = async ( ctx: ImportContext, url: URL, @@ -143,14 +182,17 @@ const contentHandler = async ( originalContent: string, parseResult: Readability.ParseResult ): Promise => { - // const apiResponse = await sendSavePageMutation(userId, { - // url: finalUrl, - // clientRequestId: articleSavingRequestId, - // title, - // originalContent: content, - // parseResult: readabilityResult, - // }) - console.log('content handler: ', url, title) + const requestId = uuid() + const apiResponse = await sendSavePageMutation(ctx.userId, { + url, + clientRequestId: requestId, + title, + originalContent, + parseResult, + }) + if (!apiResponse) { + return Promise.reject() + } return Promise.resolve() } @@ -199,13 +241,6 @@ const handleEvent = async (data: StorageEvent) => { } } -function isPubsubMessage(event: any): event is StorageEvent { - if ('name' in event && 'bucket' in event && 'contentType' in event) { - return true - } - return false -} - export const importHandler = Sentry.GCPFunction.wrapHttpFunction( async (req, res) => { /* eslint-disable @typescript-eslint/no-unsafe-member-access */