Skip to content

Commit

Permalink
Added liked flag to objects in inbox
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Sep 5, 2024
1 parent 317666f commit bce428c
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -359,22 +363,31 @@ export async function inboxHandler(
ctx: Context<{ Variables: HonoContextVariables }>,
next: Next,
) {
const liked = (await ctx.get('db').get<string[]>(['liked'])) || [];
const results = (await ctx.get('db').get<string[]>(['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<StoredThing>([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: {
Expand All @@ -384,6 +397,22 @@ 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)) {
thing.object.liked = true;
}
}

items.push(thing);
} catch (err) {
console.log(err);
Expand Down

0 comments on commit bce428c

Please sign in to comment.