Node.js based forum software built for the modern web
feat: activitypub response to note retrieval via pid
Julian Lam6b517252b968
diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.jsindex 7f6ddd40d2..ac9c732866 100644--- a/src/activitypub/mocks.js+++ b/src/activitypub/mocks.js@@ -186,14 +186,10 @@ Mocks.note = async (post) => {if (post.toPid) {inReplyTo = activitypub.helpers.isUri(post.toPid) ? post.toPid : `${nconf.get('url')}/post/${post.toPid}`;const parentId = await posts.getPostField(post.toPid, 'uid');- if (activitypub.helpers.isUri(parentId)) {- to.unshift(parentId);- }- } else {- const mainPid = await topics.getTopicFieldByPid('mainPid', post.pid);- if (mainPid !== post.pid) {- inReplyTo = `${nconf.get('url')}/post/${mainPid}`;- }+ to.unshift(activitypub.helpers.isUri(parentId) ? parentId : `${nconf.get('url')}/uid/${parentId}`);+ } else if (!post.isMainPost) {+ inReplyTo = `${nconf.get('url')}/post/${post.topic.mainPid}`;+ to.unshift(activitypub.helpers.isUri(post.topic.uid) ? post.topic.uid : `${nconf.get('url')}/uid/${post.topic.uid}`);}const object = {@@ -206,6 +202,7 @@ Mocks.note = async (post) => {url: id,attributedTo: `${nconf.get('url')}/uid/${post.user.uid}`,sensitive: false, // todo+ summary: null,content: post.content,source: {content: raw,diff --git a/src/controllers/activitypub/actors.js b/src/controllers/activitypub/actors.jsindex 27dd2b8e8f..48cca7a6ca 100644--- a/src/controllers/activitypub/actors.js+++ b/src/controllers/activitypub/actors.js@@ -3,6 +3,7 @@const nconf = require('nconf');const meta = require('../../meta');+const posts = require('../../posts');const activitypub = require('../../activitypub');const Actors = module.exports;@@ -46,3 +47,15 @@ Actors.userBySlug = async function (req, res) {delete req.params.userslug;Actors.user(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 post = (await posts.getPostSummaryByPids([req.params.pid], req.uid, { stripTags: false })).pop();+ if (!post) {+ return next('route');+ }++ const payload = await activitypub.mocks.note(post);+ res.status(200).json(payload);+};diff --git a/src/posts/data.js b/src/posts/data.jsindex 61738181de..85f5ca54a4 100644--- a/src/posts/data.js+++ b/src/posts/data.js@@ -6,7 +6,7 @@ const activitypub = require('../activitypub');const utils = require('../utils');const intFields = [- 'uid', 'pid', 'tid', 'deleted', 'timestamp',+ 'uid', 'pid', 'tid', 'toPid', 'deleted', 'timestamp','upvotes', 'downvotes', 'deleterUid', 'edited','replies', 'bookmarks',];@@ -63,6 +63,9 @@ function modifyPost(post, fields) {intFields.splice(intFields.indexOf('uid'), 1);intFields.splice(intFields.indexOf('tid'), 1);}+ if (activitypub.helpers.isUri(post.toPid)) {+ intFields.splice(intFields.indexOf('toPid'), 1);+ }db.parseIntFields(post, intFields, fields);if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) {post.votes = post.upvotes - post.downvotes;diff --git a/src/posts/summary.js b/src/posts/summary.jsindex 364baad1f7..a07b8e2c67 100644--- a/src/posts/summary.js+++ b/src/posts/summary.js@@ -20,7 +20,7 @@ module.exports = function (Posts) {options.parse = options.hasOwnProperty('parse') ? options.parse : true;options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];- const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);+ const fields = ['pid', 'tid', 'toPid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);let posts = await Posts.getPostsFields(pids, fields);posts = posts.filter(Boolean);diff --git a/src/routes/activitypub.js b/src/routes/activitypub.jsindex e7fa26f24b..48d29304f0 100644--- a/src/routes/activitypub.js+++ b/src/routes/activitypub.js@@ -12,18 +12,20 @@ module.exports = function (app, middleware, controllers) {* - See middleware.activitypub.assertS2S*/- const middlewares = [middleware.activitypub.enabled, middleware.activitypub.assertS2S, middleware.exposeUid];+ const middlewares = [middleware.activitypub.enabled, middleware.activitypub.assertS2S];app.get('/actor', middlewares, controllers.activitypub.actors.application);- app.get('/uid/:uid', [middleware.activitypub.enabled, middleware.activitypub.assertS2S], controllers.activitypub.actors.user);- app.get('/user/:userslug', middlewares, controllers.activitypub.actors.userBySlug);+ app.get('/uid/:uid', middlewares, controllers.activitypub.actors.user);+ app.get('/user/:userslug', [...middlewares, middleware.exposeUid], controllers.activitypub.actors.userBySlug);- app.get('/user/:userslug/inbox', middlewares, controllers.activitypub.getInbox);- app.post('/user/:userslug/inbox', [...middlewares, middleware.activitypub.validate], controllers.activitypub.postInbox);+ app.get('/user/:userslug/inbox', [...middlewares, middleware.exposeUid], controllers.activitypub.getInbox);+ app.post('/user/:userslug/inbox', [...middlewares, middleware.activitypub.validate, middleware.exposeUid], controllers.activitypub.postInbox);- app.get('/user/:userslug/outbox', middlewares, controllers.activitypub.getOutbox);- app.post('/user/:userslug/outbox', middlewares, controllers.activitypub.postOutbox);+ app.get('/user/:userslug/outbox', [...middlewares, middleware.exposeUid], controllers.activitypub.getOutbox);+ app.post('/user/:userslug/outbox', [...middlewares, middleware.exposeUid], controllers.activitypub.postOutbox);- app.get('/user/:userslug/following', middlewares, controllers.activitypub.getFollowing);- app.get('/user/:userslug/followers', middlewares, controllers.activitypub.getFollowers);+ app.get('/user/:userslug/following', [...middlewares, middleware.exposeUid], controllers.activitypub.getFollowing);+ app.get('/user/:userslug/followers', [...middlewares, middleware.exposeUid], controllers.activitypub.getFollowers);++ app.get('/post/:pid', middlewares, controllers.activitypub.actors.note);};