From ef13cbf39a36f2d12f5821f45bffe3227c2f64a5 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 8 Dec 2022 12:33:05 +0800 Subject: [PATCH] Recommend highlights --- packages/api/src/elastic/recommendation.ts | 36 +++++++++++++++++----- packages/api/src/routers/page_router.ts | 6 ++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/api/src/elastic/recommendation.ts b/packages/api/src/elastic/recommendation.ts index 924f444fe..19a0a3cd1 100644 --- a/packages/api/src/elastic/recommendation.ts +++ b/packages/api/src/elastic/recommendation.ts @@ -4,28 +4,50 @@ import { createPage, getPageByParam, updatePage } from './pages' export const addRecommendation = async ( ctx: PageContext, page: Page, - recommendation: Recommendation + recommendation: Recommendation, + highlightIds?: string[] ): Promise => { try { + const highlights = page.highlights?.filter((highlight) => + highlightIds?.includes(highlight.id) + ) + // check if the page is already recommended to the group const existingPage = await getPageByParam({ userId: ctx.uid, url: page.url, }) if (existingPage) { - if (existingPage.recommendations?.includes(recommendation)) { + const existingHighlights = existingPage.highlights || [] + + // remove duplicates + const newHighlights = + highlights?.filter( + (highlight) => + !existingHighlights.find( + (existingHighlight) => existingHighlight.quote === highlight.quote + ) + ) || [] + + const existingRecommendations = existingPage.recommendations || [] + const isRecommended = existingRecommendations.some( + (existingRecommendation) => + existingRecommendation.id === recommendation.id + ) + if (isRecommended && newHighlights.length === 0) { return existingPage._id } - // update recommendedBy in the existing page - const recommendations = (existingPage.recommendations || []).concat( - recommendation - ) + // update recommendations in the existing page + const recommendations = isRecommended + ? undefined + : existingRecommendations.concat(recommendation) await updatePage( existingPage.id, { recommendations, + highlights: existingHighlights.concat(newHighlights), }, ctx ) @@ -41,7 +63,7 @@ export const addRecommendation = async ( readingProgressPercent: 0, readingProgressAnchorIndex: 0, sharedAt: new Date(), - highlights: [], + highlights, readAt: undefined, labels: [], } diff --git a/packages/api/src/routers/page_router.ts b/packages/api/src/routers/page_router.ts index da74925fc..50bca8865 100644 --- a/packages/api/src/routers/page_router.ts +++ b/packages/api/src/routers/page_router.ts @@ -163,10 +163,11 @@ export function pageRouter() { } const claims = jwt.decode(token) as Claims - const { userId, pageId, recommendation } = req.body as { + const { userId, pageId, recommendation, highlightIds } = req.body as { userId: string pageId: string recommendation: Recommendation + highlightIds?: string[] } if (!userId || !pageId || !recommendation) { return res.status(400).send({ errorCode: 'BAD_DATA' }) @@ -188,7 +189,8 @@ export function pageRouter() { const recommendedPageId = await addRecommendation( ctx, page, - recommendation + recommendation, + highlightIds ) if (!recommendedPageId) { logger.error('Failed to add recommendation to page')