mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 18:46:01 +01:00
feat: Create(Note) on new topic or reply
This is a naive WIP implementation that federates everything out publicly. It does not take category privileges into account!
This commit is contained in:
@@ -13,6 +13,7 @@ const nconf = require('nconf');
|
||||
const db = require('../database');
|
||||
const activitypub = require('../activitypub');
|
||||
const user = require('../user');
|
||||
const posts = require('../posts');
|
||||
|
||||
const activitypubApi = module.exports;
|
||||
|
||||
@@ -49,3 +50,57 @@ activitypubApi.unfollow = async (caller, { uid: actorId }) => {
|
||||
db.decrObjectField(`user:${caller.uid}`, 'followingRemoteCount'),
|
||||
]);
|
||||
};
|
||||
|
||||
activitypubApi.create = {};
|
||||
|
||||
activitypubApi.create.post = async (caller, { post }) => {
|
||||
const id = `${nconf.get('url')}/post/${post.pid}`;
|
||||
const published = new Date(post.timestamp).toISOString();
|
||||
const [userslug, raw, followers] = await Promise.all([
|
||||
user.getUserField(caller.uid, 'userslug'),
|
||||
posts.getPostField(post.pid, 'content'),
|
||||
db.getSortedSetMembers(`followersRemote:${caller.uid}`),
|
||||
]);
|
||||
|
||||
// todo: post visibility, category privileges integration
|
||||
const recipients = {
|
||||
to: [activitypub._constants.publicAddress],
|
||||
cc: [`${nconf.get('url')}/user/${userslug}/followers`],
|
||||
};
|
||||
const targets = new Set(followers);
|
||||
|
||||
let inReplyTo = null;
|
||||
if (post.toPid) {
|
||||
inReplyTo = activitypub.helpers.isUri(post.toPid) ? post.toPid : id;
|
||||
const parentId = await posts.getPostField(post.toPid, 'uid');
|
||||
if (activitypub.helpers.isUri(parentId)) {
|
||||
recipients.to.unshift(parentId);
|
||||
targets.add(parentId);
|
||||
}
|
||||
}
|
||||
|
||||
const object = {
|
||||
id,
|
||||
type: 'Note',
|
||||
...recipients,
|
||||
inReplyTo,
|
||||
published,
|
||||
url: id,
|
||||
attributedTo: `${nconf.get('url')}/user/${post.user.userslug}`,
|
||||
sensitive: false, // todo
|
||||
content: post.content,
|
||||
source: {
|
||||
content: raw,
|
||||
mediaType: 'text/markdown',
|
||||
},
|
||||
// replies: {} todo...
|
||||
};
|
||||
|
||||
const payload = {
|
||||
type: 'Create',
|
||||
...recipients,
|
||||
object,
|
||||
};
|
||||
|
||||
await activitypub.send(caller.uid, Array.from(targets), payload);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user