feat: activitypub response to note retrieval via pid

This commit is contained in:
Julian Lam
2024-01-25 16:26:39 -05:00
parent 0b3ca8e366
commit 6b517252b9
5 changed files with 34 additions and 19 deletions

View File

@@ -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,

View File

@@ -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);
};

View File

@@ -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;

View File

@@ -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);

View File

@@ -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);
};