Compare commits
2 Commits
38c26c07cd
...
f205c0db0b
Author | SHA1 | Date |
---|---|---|
Moon Man | f205c0db0b | |
Moon Man | 55d9edb7e7 |
|
@ -1,11 +1,11 @@
|
||||||
import { readFileSync } from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
import markdownit from "markdown-it";
|
import markdownit from "markdown-it";
|
||||||
import { Article, buildCanonicalUrl, getById as getArticleById } from "./article.js";
|
import { Article, buildCanonicalUrl, getArticleById } from "./article.js";
|
||||||
import { User, getByActor } from "./user.js";
|
import { User, getByActor } from "./user.js";
|
||||||
import { fillRoute, reverseRoute } 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, keyIdFromNickname } from "./user.js";
|
import { getUserById, keyIdFromNickname } 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";
|
||||||
|
|
|
@ -17,28 +17,28 @@ export const zArticle = z.object({
|
||||||
|
|
||||||
export type Article = z.infer<typeof zArticle>;
|
export type Article = z.infer<typeof zArticle>;
|
||||||
|
|
||||||
export const getBySlug = async (slug: string): Promise<Article | null> => {
|
export const getArticleBySlug = async (slug: string): Promise<Article | null> => {
|
||||||
return db<Article>("articles")
|
return db<Article>("articles")
|
||||||
.where("slug", slug)
|
.where("slug", slug)
|
||||||
.first()
|
.first()
|
||||||
.then((rec) => !!rec ? fixDates<Article>(rec) : null)
|
.then((rec) => !!rec ? fixDates<Article>(rec) : null)
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getById = async (articleId: number): Promise<Article | null> => {
|
export const getArticleById = async (articleId: number): Promise<Article | null> => {
|
||||||
return db<Article>("articles")
|
return db<Article>("articles")
|
||||||
.where("id", articleId)
|
.where("id", articleId)
|
||||||
.first()
|
.first()
|
||||||
.then((rec) => !!rec ? fixDates<Article>(rec) : null)
|
.then((rec) => !!rec ? fixDates<Article>(rec) : null)
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getByUserId = async (userId: number): Promise<Article[]> =>
|
export const getArticlesByUserId = async (userId: number): Promise<Article[]> =>
|
||||||
db<Article>("articles")
|
db<Article>("articles")
|
||||||
.where("users_id", userId)
|
.where("users_id", userId)
|
||||||
.orderBy("created_at", "desc")
|
.orderBy("created_at", "desc")
|
||||||
.then((articles) => articles.map((a) => fixDates<Article>(a)))
|
.then((articles) => articles.map((a) => fixDates<Article>(a)))
|
||||||
;
|
;
|
||||||
|
|
||||||
export const insert = async (userId: number, slug: string, title: string): Promise<Article> => {
|
export const insertArticle = async (userId: number, slug: string, title: string): Promise<Article> => {
|
||||||
const data: Record<string, any> = {
|
const data: Record<string, any> = {
|
||||||
users_id: userId,
|
users_id: userId,
|
||||||
slug,
|
slug,
|
||||||
|
@ -56,7 +56,7 @@ export const insert = async (userId: number, slug: string, title: string): Promi
|
||||||
} as Article))
|
} as Article))
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAll = async (count: number, lastId = Number.MAX_SAFE_INTEGER) =>
|
export const getAllArticles = async (count: number, lastId = Number.MAX_SAFE_INTEGER) =>
|
||||||
db<Article>("articles")
|
db<Article>("articles")
|
||||||
.where("id", "<", lastId)
|
.where("id", "<", lastId)
|
||||||
.orderBy("created_at", "desc")
|
.orderBy("created_at", "desc")
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import markdownit from 'markdown-it'
|
import markdownit from 'markdown-it'
|
||||||
import { slugRegex } from "./article.js";
|
import { slugRegex } from "./article.js";
|
||||||
import { newUser, get as getUserByNickname, getFollowerInboxes, keyIdFromNickname } from "./user.js";
|
import { newUser, getUserByNickname, getFollowerInboxes, keyIdFromNickname } from "./user.js";
|
||||||
import { insert as insertArticle } from "./article.js";
|
import { insertArticle } from "./article.js";
|
||||||
import { add as addToOutbox } from "./outbox.js";
|
import { add as addToOutbox } from "./outbox.js";
|
||||||
import { createArticleActivity, sendAll } from "./activity.js";
|
import { createArticleActivity, sendAll } from "./activity.js";
|
||||||
import { fillRoute } from "./router.js";
|
import { fillRoute } from "./router.js";
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { TYPE } from "./activity.js";
|
||||||
import { CollectionEntry, orderedCollection } from "./collection.js";
|
import { CollectionEntry, orderedCollection } from "./collection.js";
|
||||||
import db from "./db.js";
|
import db from "./db.js";
|
||||||
import { fillRoute } from "./router.js";
|
import { fillRoute } from "./router.js";
|
||||||
import { get as getUserByNickname } from "./user.js";
|
import { getUserByNickname } from "./user.js";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
export interface Follower {
|
export interface Follower {
|
||||||
|
|
12
src/index.ts
12
src/index.ts
|
@ -1,9 +1,9 @@
|
||||||
import express, { Request } from "express";
|
import express, { Request } from "express";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import { toCollection as getOutbox } from "./outbox.js";
|
import { toCollection as getOutbox } from "./outbox.js";
|
||||||
import { getById as getUserById, getNickname, get as getUserByNickname, getId as getUserId, User } from "./user.js";
|
import { getUserById, getUserNicknameById, getUserByNickname, User } from "./user.js";
|
||||||
import { Routes } from "./router.js";
|
import { Routes } from "./router.js";
|
||||||
import { getBySlug as getArticleBySlug, getById, getByUserId, getAll as getAllArticles } from "./article.js";
|
import { getArticleBySlug, getArticleById, getArticlesByUserId, getAllArticles } from "./article.js";
|
||||||
import { userToPerson, TYPE as ACTIVITYPUB_TYPE, handleInboxPost, createArticleObject, CONTEXT, createArticleActivity } from "./activity.js";
|
import { userToPerson, TYPE as ACTIVITYPUB_TYPE, handleInboxPost, createArticleObject, CONTEXT, createArticleActivity } from "./activity.js";
|
||||||
import { handleFollowerGet } from "./follower.js";
|
import { handleFollowerGet } from "./follower.js";
|
||||||
import { handleWebfingerGet } from "./net.js";
|
import { handleWebfingerGet } from "./net.js";
|
||||||
|
@ -48,7 +48,7 @@ app.post(Routes.inbox, handleInboxPost);
|
||||||
app.get(Routes.followers, handleFollowerGet);
|
app.get(Routes.followers, handleFollowerGet);
|
||||||
|
|
||||||
app.get(Routes.object, async (req, res) => {
|
app.get(Routes.object, async (req, res) => {
|
||||||
const article = await getById(parseInt(req.params.id));
|
const article = await getArticleById(parseInt(req.params.id));
|
||||||
|
|
||||||
if (article) {
|
if (article) {
|
||||||
console.log("Request headers for object route:", req.headers);
|
console.log("Request headers for object route:", req.headers);
|
||||||
|
@ -58,7 +58,7 @@ app.get(Routes.object, async (req, res) => {
|
||||||
"application/ld+json",
|
"application/ld+json",
|
||||||
"json"
|
"json"
|
||||||
])) {
|
])) {
|
||||||
const nickname = await getNickname(article.users_id) as string;
|
const nickname = await getUserNicknameById(article.users_id) as string;
|
||||||
|
|
||||||
const obj = createArticleObject(article, nickname);
|
const obj = createArticleObject(article, nickname);
|
||||||
obj["@context"] = CONTEXT;
|
obj["@context"] = CONTEXT;
|
||||||
|
@ -91,7 +91,7 @@ app.get(Routes.activity, async (req, res) => {
|
||||||
res.status(501).end();
|
res.status(501).end();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const article = await getById(id);
|
const article = await getArticleById(id);
|
||||||
|
|
||||||
if (article) {
|
if (article) {
|
||||||
console.log("Request headers for activity route:", req.headers);
|
console.log("Request headers for activity route:", req.headers);
|
||||||
|
@ -164,7 +164,7 @@ app.get(Routes.actor, async (req, res) => {
|
||||||
}
|
}
|
||||||
else if (req.accepts("html")) {
|
else if (req.accepts("html")) {
|
||||||
console.log(`returning actor: ${nickname} as html`);
|
console.log(`returning actor: ${nickname} as html`);
|
||||||
const posts = (await getByUserId(actor.id)).map((a) => `<li><a href="/${a.slug}.html">${escapeHtml(a.title)}</a></li>`).join("");
|
const posts = (await getArticlesByUserId(actor.id)).map((a) => `<li><a href="/${a.slug}.html">${escapeHtml(a.title)}</a></li>`).join("");
|
||||||
|
|
||||||
const body = `<!DOCTYPE html>
|
const body = `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { actorSchema } from "./activitypub_types.js";
|
||||||
import type { Actor } from "./activitypub_types.js";
|
import type { Actor } from "./activitypub_types.js";
|
||||||
import type { Request, Response } from "express";
|
import type { Request, Response } from "express";
|
||||||
import { fillRoute } from "./router.js";
|
import { fillRoute } from "./router.js";
|
||||||
import { get as getUserByNickname } from "./user.js";
|
import { getUserByNickname } from "./user.js";
|
||||||
|
|
||||||
export const flattenHeaders = (headers: Record<string, string>) =>
|
export const flattenHeaders = (headers: Record<string, string>) =>
|
||||||
Object.entries(headers).map(([key, value]) => `${key}: ${value}`);
|
Object.entries(headers).map(([key, value]) => `${key}: ${value}`);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import db from "./db.js";
|
import db from "./db.js";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { get as getUserByNickname } from "./user.js";
|
import { getUserByNickname } from "./user.js";
|
||||||
import { fillRoute } from "./router.js";
|
import { fillRoute } from "./router.js";
|
||||||
import { getByUserId as getArticlesByUserId, getById as getArticleById, Article } from "./article.js";
|
import { getArticleById, Article } from "./article.js";
|
||||||
import { createArticleActivity, deleteArticleActivity } from "./activity.js";
|
import { createArticleActivity, deleteArticleActivity } from "./activity.js";
|
||||||
import { orderedCollection } from "./collection.js";
|
import { orderedCollection } from "./collection.js";
|
||||||
|
|
||||||
|
|
10
src/user.ts
10
src/user.ts
|
@ -27,13 +27,13 @@ export const zUser = z.object({
|
||||||
|
|
||||||
export type User = z.infer<typeof zUser>;
|
export type User = z.infer<typeof zUser>;
|
||||||
|
|
||||||
export const get = async (nickname: string) =>
|
export const getUserByNickname = async (nickname: string) =>
|
||||||
db<User>("users")
|
db<User>("users")
|
||||||
.where("nickname", nickname)
|
.where("nickname", nickname)
|
||||||
.first()
|
.first()
|
||||||
;
|
;
|
||||||
|
|
||||||
export const getById = async (id: number) =>
|
export const getUserById = async (id: number) =>
|
||||||
db<User>("users")
|
db<User>("users")
|
||||||
.where("id", id)
|
.where("id", id)
|
||||||
.first()
|
.first()
|
||||||
|
@ -45,12 +45,12 @@ export const getByActor = async (actor: string) => {
|
||||||
const matchArray = actor.match(EXTRACT_NICKNAME);
|
const matchArray = actor.match(EXTRACT_NICKNAME);
|
||||||
if (matchArray) {
|
if (matchArray) {
|
||||||
const nickname = matchArray[1];
|
const nickname = matchArray[1];
|
||||||
return get(nickname);
|
return getUserByNickname(nickname);
|
||||||
}
|
}
|
||||||
else return null;
|
else return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getNickname = async (userId: number): Promise<string | null> =>
|
export const getUserNicknameById = async (userId: number): Promise<string | null> =>
|
||||||
db("users")
|
db("users")
|
||||||
.select("nickname")
|
.select("nickname")
|
||||||
.where("id", userId)
|
.where("id", userId)
|
||||||
|
@ -58,7 +58,7 @@ export const getNickname = async (userId: number): Promise<string | null> =>
|
||||||
.then((rec) => !!rec ? rec.nickname : null)
|
.then((rec) => !!rec ? rec.nickname : null)
|
||||||
;
|
;
|
||||||
|
|
||||||
export const getId = async (nickname: string): Promise<number | null> =>
|
export const getUserIdByNickname = async (nickname: string): Promise<number | null> =>
|
||||||
db("users")
|
db("users")
|
||||||
.select("id")
|
.select("id")
|
||||||
.where("nickname", nickname)
|
.where("nickname", nickname)
|
||||||
|
|
Loading…
Reference in New Issue