import { TYPE } from "./activity.js"; import { CollectionEntry, orderedCollection } from "./collection.js"; import db from "./db.js"; import { fillRoute } from "./router.js"; import { get as getUserByNickname } from "./user.js"; import { Request, Response } from "express"; export interface Follower { actor: string, nickname: string, name: string, inbox: string | null, shared_inbox: string | null }; export const getFollowers = async (userId: number): Promise => db("collections") .select("remote_users.*", "collections.value") .join("remote_users", "remote_users.actor", "=", "collections.value") .where("collection_types_id", 0) .where("users_id", userId) .orderBy("collections.created_at", "desc") ; export const addFollower = async (followeeUserId: number, actor: string, nickname: string, name: string, inbox?: string, sharedInbox?: string) => { await db("remote_users") .insert({ actor, nickname, name, inbox, shared_inbox: sharedInbox }) .onConflict().ignore(); ; await db("collections") .insert({ collection_types_id: 0, users_id: followeeUserId, value: actor, created_at: new Date() }) .onConflict().ignore() ; console.log("Follower added"); }; export const handleFollowerGet = async (req: Request<{ actor: string}>, res: Response) => { const user = await getUserByNickname(req.params.actor); if (user) { const followers = await getFollowers(user.id); const payload = orderedCollection(fillRoute("followers", user.nickname), followers); res.set("Content-Type", TYPE); res.send(JSON.stringify(payload, null, 4)); } else { res.status(403).end(); } };