Node.js based forum software built for the modern web
feat(activitypub): add ap-serve-cache for actors.note and actors.replies
Julian Lam632feba97850
diff --git a/src/activitypub/index.js b/src/activitypub/index.jsindex d57d84bb0c..7ef95d62b5 100644--- a/src/activitypub/index.js+++ b/src/activitypub/index.js@@ -7,6 +7,7 @@ const { cpus } = require('os');const request = require('../request');const db = require('../database');+const pubsub = require('../pubsub');const meta = require('../meta');const categories = require('../categories');const posts = require('../posts');@@ -21,6 +22,11 @@ const requestCache = ttl({max: 5000,ttl: 1000 * 60 * 5, // 5 minutes});+const serveCache = ttl({+ name: 'ap-serve-cache',+ max: 5000,+ ttl: 1000 * 60, // 1 minute+});const probeCache = ttl({name: 'ap-probe-cache',max: 500,@@ -64,8 +70,14 @@ ActivityPub._constants = Object.freeze({},});ActivityPub._cache = requestCache;+ActivityPub.serveCache = serveCache;ActivityPub._sent = new Map(); // used only in local tests+// Invalidate serve cache on post lifecycle events+pubsub.on('post:edit', pid => ActivityPub.serveCache.del(`/post/${pid}`));+pubsub.on('post:delete', pid => ActivityPub.serveCache.del(`/post/${pid}`));+pubsub.on('post:purge', pid => ActivityPub.serveCache.del(`/post/${pid}`));+ActivityPub.helpers = require('./helpers');ActivityPub.inbox = require('./inbox');diff --git a/src/controllers/activitypub/actors.js b/src/controllers/activitypub/actors.jsindex 4d8d0f491f..e835ec463b 100644--- a/src/controllers/activitypub/actors.js+++ b/src/controllers/activitypub/actors.js@@ -67,7 +67,8 @@ Actors.userBySlug = async function (req, res) {Actors.note = async function (req, res, next) {// technically a note isn't an actor, but it is here purely for organizational purposes.// but also, wouldn't it be wild if you could follow a note? lol.- const allowed = await privileges.posts.can('topics:read', req.params.pid, activitypub._constants.uid);+ const uid = req.uid || activitypub._constants.uid;+ const allowed = await privileges.posts.can('topics:read', req.params.pid, uid);if (!allowed) {return next();}@@ -77,7 +78,13 @@ Actors.note = async function (req, res, next) {return res.set('Location', req.params.pid).sendStatus(308);}- const post = (await posts.getPostSummaryByPids([req.params.pid], req.uid, {+ const cacheKey = `/post/${req.params.pid}`;+ const cached = activitypub.serveCache.get(cacheKey);+ if (cached) {+ return res.status(200).json(cached);+ }++ const post = (await posts.getPostSummaryByPids([req.params.pid], uid, {parse: false,extraFields: ['edited'],})).pop();@@ -90,6 +97,7 @@ Actors.note = async function (req, res, next) {payload.to = to;payload.cc = cc;+ activitypub.serveCache.set(cacheKey, payload);res.status(200).json(payload);};@@ -101,6 +109,12 @@ Actors.replies = async function (req, res, next) {}const page = parseInt(req.query.page, 10);+ const cacheKey = `/post/${req.params.pid}/replies${page ? `?page=${page}` : ''}`;+ const cached = activitypub.serveCache.get(cacheKey);+ if (cached) {+ return res.status(200).json(cached);+ }+let replies;try {replies = await activitypub.helpers.generateCollection({@@ -125,6 +139,7 @@ Actors.replies = async function (req, res, next) {...replies,};+ activitypub.serveCache.set(cacheKey, object);res.status(200).json(object);};