From 0c3fa2cdd4ef50ffe0caa709014b9d98a0a7c47e Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 6 Jun 2023 11:56:31 +0800 Subject: [PATCH] feat: start a cloud task to set thumbnail and pre-cache all the images in the content --- packages/api/src/services/save_page.ts | 20 ++++++++++++++++++-- packages/api/src/util.ts | 3 +++ packages/api/src/utils/createTask.ts | 21 +++++++++++++-------- packages/api/src/utils/helpers.ts | 2 +- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/packages/api/src/services/save_page.ts b/packages/api/src/services/save_page.ts index 0d92b63c0..ca332a89f 100644 --- a/packages/api/src/services/save_page.ts +++ b/packages/api/src/services/save_page.ts @@ -4,7 +4,7 @@ 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 { env, homePageURL } from '../env' import { HighlightType, Maybe, @@ -14,6 +14,8 @@ import { SaveResult, } from '../generated/graphql' import { DataModels } from '../resolvers/types' +import { generateVerificationToken } from '../utils/auth' +import createHttpTaskWithToken from '../utils/createTask' import { generateSlug, stringToHash, @@ -164,7 +166,21 @@ export const savePage = async ( pageId = newPageId } - // TODO: update thumbnail and pre-cache images + // create a task to update thumbnail and pre-cache all images + try { + await createHttpTaskWithToken({ + payload: { + userId: saver.userId, + slug, + }, + taskHandlerUrl: env.queue.thumbnailTaskHandlerUrl, + requestHeaders: { + Authorization: generateVerificationToken(saver.userId), + }, + }) + } catch (e) { + console.log('Failed to create thumbnail task', e) + } if (parseResult.highlightData) { const highlight = { diff --git a/packages/api/src/util.ts b/packages/api/src/util.ts index f8c6f9d4c..2f2c9a5ef 100755 --- a/packages/api/src/util.ts +++ b/packages/api/src/util.ts @@ -66,6 +66,7 @@ interface BackendEnv { integrationTaskHandlerUrl: string textToSpeechTaskHandlerUrl: string recommendationTaskHandlerUrl: string + thumbnailTaskHandlerUrl: string } fileUpload: { gcsUploadBucket: string @@ -159,6 +160,7 @@ const nullableEnvVars = [ 'GCP_LOCATION', 'RECOMMENDATION_TASK_HANDLER_URL', 'POCKET_CONSUMER_KEY', + 'THUMBNAIL_TASK_HANDLER_URL', ] // Allow some vars to be null/empty /* If not in GAE and Prod/QA/Demo env (f.e. on localhost/dev env), allow following env vars to be null */ @@ -245,6 +247,7 @@ export function getEnv(): BackendEnv { integrationTaskHandlerUrl: parse('INTEGRATION_TASK_HANDLER_URL'), textToSpeechTaskHandlerUrl: parse('TEXT_TO_SPEECH_TASK_HANDLER_URL'), recommendationTaskHandlerUrl: parse('RECOMMENDATION_TASK_HANDLER_URL'), + thumbnailTaskHandlerUrl: parse('THUMBNAIL_TASK_HANDLER_URL'), } const imageProxy = { url: parse('IMAGE_PROXY_URL'), diff --git a/packages/api/src/utils/createTask.ts b/packages/api/src/utils/createTask.ts index 5d48e2a54..331a771e8 100644 --- a/packages/api/src/utils/createTask.ts +++ b/packages/api/src/utils/createTask.ts @@ -22,7 +22,7 @@ const logger = buildLogger('app.dispatch') const client = new CloudTasksClient() const createHttpTaskWithToken = async ({ - project, + project = process.env.GOOGLE_CLOUD_PROJECT, queue = env.queue.name, location = env.queue.location, taskHandlerUrl = env.queue.contentFetchUrl, @@ -32,7 +32,7 @@ const createHttpTaskWithToken = async ({ scheduleTime, requestHeaders, }: { - project: string + project?: string queue?: string location?: string taskHandlerUrl?: string @@ -42,12 +42,18 @@ const createHttpTaskWithToken = async ({ scheduleTime?: number requestHeaders?: Record }): Promise< - [ - protos.google.cloud.tasks.v2.ITask, - protos.google.cloud.tasks.v2.ICreateTaskRequest | undefined, - unknown | undefined - ] + | [ + protos.google.cloud.tasks.v2.ITask, + protos.google.cloud.tasks.v2.ICreateTaskRequest | undefined, + unknown | undefined + ] + | null > => { + // If there is no Google Cloud Project Id exposed, it means that we are in local environment + if (env.dev.isLocal || !project) { + return null + } + // Construct the fully qualified queue name. priority === 'low' && (queue = `${queue}-low`) @@ -458,7 +464,6 @@ export const enqueueRecommendation = async ( } export const enqueueImportFromIntegration = async ( - userId: string, integrationId: string, authToken: string ): Promise => { diff --git a/packages/api/src/utils/helpers.ts b/packages/api/src/utils/helpers.ts index f65994d21..315a46804 100644 --- a/packages/api/src/utils/helpers.ts +++ b/packages/api/src/utils/helpers.ts @@ -83,7 +83,7 @@ export function authorized< return (parent, args, ctx, info) => { const { claims } = ctx if (claims?.uid) { - return resolver(parent, args, { ...ctx, claims }, info) + return resolver(parent, args, { ...ctx, claims, uid: claims.uid }, info) } return { errorCodes: ['UNAUTHORIZED'] } as TError }