From 6ce7e802a62ca517494591a054908f4146794d41 Mon Sep 17 00:00:00 2001 From: Moon Man Date: Sun, 31 Dec 2023 15:48:42 -0500 Subject: [PATCH] likes collection --- src/collection.ts | 12 ++++++++++-- src/index.ts | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/collection.ts b/src/collection.ts index a817941..a2df82b 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -55,7 +55,7 @@ export const orderedCollectionPage = (collectionId: string, orderedItems: (strin return collection; }; -export const addLikeOrShare = async (articleId: number, person: Actor, likeOrShare: 1|2) => { +export const addLikeOrShare = async (articleId: number, person: Actor, likeOrShare: 1 | 2) => { await db("remote_users") .insert({ actor: person.id, @@ -75,7 +75,15 @@ export const addLikeOrShare = async (articleId: number, person: Actor, likeOrSha created_at: new Date() }) .onConflict().ignore() - ; + ; console.log("Like/Share added"); }; + +export const getArticleCollection = async (collectionTypeId: 1 | 2, articleId: number) => + db("collections") + .where("collection_types_id", collectionTypeId) + .where("articles_id", articleId) + .orderBy("created_at", "desc") + .then((collection) => collection.map((c) => c.value)) + ; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 27747a5..d87c463 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { userToPerson, TYPE as ACTIVITYPUB_TYPE, handleInboxPost, createArticleO import { handleFollowerGet } from "./follower.js"; import { handleWebfingerGet } from "./net.js"; import { escapeHtml } from "./util.js"; +import { getArticleCollection } from "./collection.js"; const port = parseInt(process.env.port || "8080"); const app = express(); @@ -62,6 +63,9 @@ app.get(Routes.object, async (req, res) => { const obj = createArticleObject(article, nickname); obj["@context"] = CONTEXT; + const likes = await getArticleCollection(1, article.id); + obj.likes = likes; + console.log(`returning object id: ${article.id} as json`); res.append("Content-Type", ACTIVITYPUB_TYPE); res.send(JSON.stringify(obj, null, 4)); @@ -100,6 +104,9 @@ app.get(Routes.activity, async (req, res) => { const user = await getUserById(article.users_id) as User; const activity = createArticleActivity(article, user); + const likes = await getArticleCollection(1, article.id); + activity.object.likes = likes; + console.log(`returning activity id: ${id} as json`); res.append("Content-Type", ACTIVITYPUB_TYPE); res.send(JSON.stringify(activity, null, 4));