likes collection
This commit is contained in:
parent
4524a66c49
commit
be3d555480
|
@ -1,14 +1,15 @@
|
||||||
import { readFileSync } from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
import markdownit from "markdown-it";
|
import markdownit from "markdown-it";
|
||||||
import { Article } from "./article.js";
|
import { Article, getById as getArticleById } from "./article.js";
|
||||||
import { User, getByActor } from "./user.js";
|
import { User, getByActor } from "./user.js";
|
||||||
import { fillRoute } from "./router.js";
|
import { fillRoute, reverseRoute } from "./router.js";
|
||||||
import { streamToString, hashDigest } from "./util.js";
|
import { streamToString, hashDigest } from "./util.js";
|
||||||
import { signedFetch, SignedInit, getActor } from "./net.js";
|
import { signedFetch, SignedInit, getActor } from "./net.js";
|
||||||
import { getById as getUserById, getKeyId } from "./user.js";
|
import { getById as getUserById, getKeyId } from "./user.js";
|
||||||
import parser from "activitypub-http-signatures";
|
import parser from "activitypub-http-signatures";
|
||||||
import type { Request, Response } from "express";
|
import type { Request, Response } from "express";
|
||||||
import { addFollower } from "./follower.js";
|
import { addFollower } from "./follower.js";
|
||||||
|
import { addLikeOrShare } from "./collection.js";
|
||||||
|
|
||||||
export const CONTEXT = "https://www.w3.org/ns/activitystreams";
|
export const CONTEXT = "https://www.w3.org/ns/activitystreams";
|
||||||
export const PUBLIC = CONTEXT + "#Public";
|
export const PUBLIC = CONTEXT + "#Public";
|
||||||
|
@ -43,6 +44,37 @@ export const handleInboxPost = async (req: Request, res: Response) => {
|
||||||
res.status(202).end();
|
res.status(202).end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (activity.type === "Like") {
|
||||||
|
const articleId = reverseRoute("object", req.url)
|
||||||
|
|
||||||
|
if (articleId) {
|
||||||
|
const article = await getArticleById(parseInt(articleId));
|
||||||
|
|
||||||
|
if (article) {
|
||||||
|
const user = await getUserById(article.users_id);
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
const init: SignedInit = {
|
||||||
|
keyId: getKeyId(user.nickname),
|
||||||
|
privateKey: user.private_key
|
||||||
|
};
|
||||||
|
|
||||||
|
const liker = await getActor(activity.actor, init);
|
||||||
|
|
||||||
|
if (liker) {
|
||||||
|
await addLikeOrShare(article.id, liker, 1);
|
||||||
|
|
||||||
|
res.status(201).end();
|
||||||
|
console.log(`added like for article: ${article.id} actor: ${user.id}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(403).end();
|
||||||
|
}
|
||||||
else if (activity.type === "Follow") {
|
else if (activity.type === "Follow") {
|
||||||
console.log(JSON.stringify(activity, null, 4));
|
console.log(JSON.stringify(activity, null, 4));
|
||||||
const actor = await getByActor(activity.object);
|
const actor = await getByActor(activity.object);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { CONTEXT } from "./activity.js";
|
import { CONTEXT } from "./activity.js";
|
||||||
|
import { Actor } from "./activitypub_types.js";
|
||||||
import db from "./db.js";
|
import db from "./db.js";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
@ -53,3 +54,28 @@ export const orderedCollectionPage = (collectionId: string, orderedItems: (strin
|
||||||
|
|
||||||
return collection;
|
return collection;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const addLikeOrShare = async (articleId: number, person: Actor, likeOrShare: 1|2) => {
|
||||||
|
await db("remote_users")
|
||||||
|
.insert({
|
||||||
|
actor: person.id,
|
||||||
|
nickname: person.preferredUsername,
|
||||||
|
name: person.name,
|
||||||
|
inbox: person.inbox,
|
||||||
|
shared_inbox: person.endpoints?.sharedInbox
|
||||||
|
})
|
||||||
|
.onConflict().ignore();
|
||||||
|
;
|
||||||
|
|
||||||
|
await db("collections")
|
||||||
|
.insert({
|
||||||
|
collection_types_id: likeOrShare,
|
||||||
|
articles_id: articleId,
|
||||||
|
value: person.id,
|
||||||
|
created_at: new Date()
|
||||||
|
})
|
||||||
|
.onConflict().ignore()
|
||||||
|
;
|
||||||
|
|
||||||
|
console.log("Follower added");
|
||||||
|
};
|
|
@ -16,4 +16,10 @@ export const fillRoute = (route: Route, value: number | string) =>
|
||||||
+ (typeof value === "number"
|
+ (typeof value === "number"
|
||||||
? Routes[route].replace(":id", value.toString())
|
? Routes[route].replace(":id", value.toString())
|
||||||
: Routes[route].replace(":actor", value as string))
|
: Routes[route].replace(":actor", value as string))
|
||||||
;
|
;
|
||||||
|
|
||||||
|
export const reverseRoute = (route: Route, path: string) => {
|
||||||
|
const r = new RegExp("^" + Routes[route].replace(/:(id|actor)/, "(.+)") + "$");
|
||||||
|
const match = path.match(r)
|
||||||
|
return !!match ? match[1] : null;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue