Cache actors

This commit is contained in:
Shadowfacts 2019-02-24 10:21:14 -05:00
parent 81e2fca2aa
commit 6b4ea192e1
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 80 additions and 11 deletions

View File

@ -41,8 +41,13 @@ export interface Delete extends Activity {
export interface Actor {
id: string;
name: string;
inbox: string;
endpoints?: {
sharedInbox?: string;
}
publicKey: {
publicKeyPem: string;
};
icon: string | object | (string | object)[];
}

View File

@ -12,7 +12,7 @@ export default async function actor(router: Router) {
],
"type": "Person",
"id": `https://${domain}/ap/actor`,
"preferredUsername": "shadowfacts",
"preferredUsername": "blog",
"name": "shadowfacts' blog",
"icon": {
"type": "Image",

View File

@ -24,7 +24,59 @@ function createActivity(article: Article): Create {
return createObject;
}
export async function fetchActor(url: string): Promise<Actor> {
export async function getActor(url: string, db: Database, forceUpdate: boolean = false): Promise<Actor> {
if (forceUpdate) {
try {
const cached = await getCachedActor(url, db);
if (cached) return cached;
} catch (err) {
console.error(`Encountered error getting cached actor ${url}`, err);
}
}
const remote = await fetchActor(url);
cacheActor(remote, db);
return remote;
}
export async function getCachedActor(url: string, db: Database): Promise<Actor> {
return new Promise((resolve, reject) => {
db.get("SELECT * FROM actors WHERE id = $id", {
$id: url
}, (err, result) => {
if (err) {
reject(err);
} else {
resolve({
id: result.id,
name: result.display_name,
inbox: result.inbox,
icon: result.icon_url,
publicKey: {
publicKeyPem: result.public_key_pem
}
} as Actor);
}
});
});
}
async function cacheActor(actor: Actor, db: Database) {
function getIconUrl(icon: string | object): string {
return icon instanceof String ? icon : (icon as any).url;
}
const iconUrl: string = actor.icon instanceof Array ? getIconUrl(actor.icon[0]) : getIconUrl(actor.icon);
db.run("INSERT OR REPLACE INTO actors(id, display_name, inbox, icon_url, public_key_pem) VALUES($id, $display_name, $inbox, $icon_url, $public_key_pem)", {
$id: actor.id,
$display_name: actor.name,
$inbox: actor.inbox,
$icon_url: iconUrl,
$public_key_pem: actor.publicKey.publicKeyPem
}, (err) => {
if (err) console.error(`Encountered error caching actor ${actor.id}`, err);
});
}
async function fetchActor(url: string): Promise<Actor> {
return new Promise((resolve, reject) => {
request({
url,
@ -35,7 +87,7 @@ export async function fetchActor(url: string): Promise<Actor> {
json: true
}, (err, res) => {
if (err) reject(err);
else resolve(<Actor>res.body);
else resolve(res.body as Actor);
});
});
}
@ -74,6 +126,7 @@ export async function signAndSend(activity: Activity, inbox: string) {
}
async function sendToFollowers(activity: Create, db: Database) {
// TODO: only send to unique inboxes
db.each("SELECT inbox FROM followers", (err, result) => {
if (err) {
console.log("Error getting followers: ", err);

View File

@ -1,6 +1,6 @@
import { Router, Request, Response } from "express";
import uuidv4 from "uuid/v4";
import { fetchActor, signAndSend } from "./federate";
import { getActor, signAndSend } from "./federate";
import { Activity, Follow, Accept, Undo, Create, Note, Delete } from "./activity";
import { Database } from "sqlite3";
import { URL } from "url";
@ -39,7 +39,8 @@ async function handleFollow(activity: Activity, req: Request, res: Response) {
res.end();
return;
}
const actor = await fetchActor(follow.actor);
const db = req.app.get("db") as Database;
const actor = await getActor(follow.actor, db, true); // always force re-fetch the actor on follow
const acceptObject = <Accept>{
"@context": [
"https://www.w3.org/ns/activitystreams",
@ -51,8 +52,8 @@ async function handleFollow(activity: Activity, req: Request, res: Response) {
"object": follow
};
signAndSend(acceptObject, actor.inbox);
const db = req.app.get("db") as Database;
const serverInbox = new URL("/inbox", actor.inbox).toString();
// prefer shared server inbox
const serverInbox = actor.endpoints && actor.endpoints.sharedInbox ? actor.endpoints.sharedInbox : actor.inbox;
db.run("INSERT OR IGNORE INTO followers(id, inbox) VALUES($id, $inbox)", {
$id: actor.id,
$inbox: serverInbox
@ -87,6 +88,7 @@ function sanitizeStatus(content: string): string {
async function handleCreateNote(create: Create, req: Request, res: Response) {
const note = create.object as Note;
const db = req.app.get("db") as Database;
getActor(note.attributedTo, db); // get and cache the actor if it's not already cached
const sanitizedContent = sanitizeStatus(note.content);
db.run("INSERT OR IGNORE INTO notes(id, content, attributed_to, in_reply_to, conversation, published) VALUES($id, $content, $attributed_to, $in_reply_to, $conversation, $published)", {
$id: note.id,

View File

@ -1,20 +1,28 @@
import crypto, { createVerify } from "crypto";
import { Request, Response, NextFunction } from "express";
import { fetchActor } from "../federate";
import { getActor } from "../federate";
import { IncomingHttpHeaders } from "http";
import { Database } from "sqlite3";
export = async (req: Request, res: Response, next: NextFunction) => {
if (req.method !== "POST") {
next();
return;
}
const actor = await fetchActor(req.body.actor as string);
const db = req.app.get("db") as Database;
const actor = await getActor(req.body.actor as string, db);
if (validate(req, actor.publicKey.publicKeyPem)) {
next();
} else {
// if the first check fails, force re-fetch the actor and try again
const actor = await getActor(req.body.actor as string, db, true);
if (validate(req, actor.publicKey.publicKeyPem)) {
next();
} else {
console.log(`Could not validate HTTP signature for ${req.body.actor}`);
res.status(401).end("Could not validate HTTP signature");
}
}
};
function validate(req: Request, publicKeyPem: string): boolean {

View File

@ -14,6 +14,7 @@ const db = new (sqlite3.verbose().Database)(process.env.DB_PATH!);
db.run("CREATE TABLE IF NOT EXISTS followers (id TEXT PRIMARY KEY, inbox TEXT)");
db.run("CREATE TABLE IF NOT EXISTS articles (id TEXT PRIMARY KEY, article_doc TEXT, conversation TEXT, has_federated INT)");
db.run("CREATE TABLE IF NOT EXISTS notes (id TEXT PRIMARY KEY, content TEXT, attributed_to TEXT, in_reply_to TEXT, conversation TEXT, published TEXT)");
db.run("CREATE TABLE IF NOT EXISTS actors (id TEXT PRIMARY KEY, display_name TEXT, inbox TEXT, icon_url TEXT, public_key_pem TEXT)")
async function generate(): Promise<Page[]> {
generators.copy();