From 464645f79ecc3d8412a0858dc405a7b28a4c861a Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 5 Sep 2024 10:20:36 +0700 Subject: [PATCH] Added `liked` flag to objects in inbox --- src/handlers.ts | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/handlers.ts b/src/handlers.ts index 5ac6cd48..2c2030ac 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -24,9 +24,13 @@ import { Temporal } from '@js-temporal/polyfill'; import { createHash } from 'node:crypto'; type StoredThing = { + id: string; object: string | { + id: string; content: string; - } + [key: string]: any; + }; + [key: string]: any; } import z from 'zod'; @@ -354,22 +358,31 @@ export async function inboxHandler( ctx: Context<{ Variables: HonoContextVariables }>, next: Next, ) { + const liked = (await ctx.get('db').get(['liked'])) || []; const results = (await ctx.get('db').get(['inbox'])) || []; + // Publish activity if the site settings have changed + const apCtx = fedify.createContext(ctx.req.raw as Request, { + db: ctx.get('db'), + globaldb: ctx.get('globaldb'), + }); let items: unknown[] = []; for (const result of results) { try { const db = ctx.get('globaldb'); const thing = await db.get([result]); + if (!thing) { + continue; + } // If the object is a string, it's probably a URI, so we should // look it up the db. If it's not in the db, we should just leave // it as is - if (thing && typeof thing.object === 'string') { + if (typeof thing.object === 'string') { thing.object = await db.get([thing.object]) ?? thing.object; } // Sanitize HTML content - if (thing?.object && typeof thing.object !== 'string') { + if (thing.object && typeof thing.object !== 'string') { thing.object.content = sanitizeHtml(thing.object.content, { allowedTags: ['a', 'p', 'img', 'br', 'strong', 'em', 'span'], allowedAttributes: { @@ -379,6 +392,24 @@ export async function inboxHandler( }); } + let objectId: string = ''; + if (typeof thing.object === 'string') { + objectId = thing.object; + } else if (typeof thing.object.id === 'string') { + objectId = thing.object.id; + } + + if (objectId) { + const likeId = apCtx.getObjectUri(Like, { + id: createHash('sha256').update(objectId).digest('hex'), + }); + if (liked.includes(likeId.href)) { + if (typeof thing.object !== 'string') { + thing.object.liked = true; + } + } + } + items.push(thing); } catch (err) { console.log(err);