mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +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:
@@ -7,6 +7,7 @@ const { createHash, createSign, createVerify } = require('crypto');
|
||||
const request = require('../request');
|
||||
const db = require('../database');
|
||||
const user = require('../user');
|
||||
const posts = require('../posts');
|
||||
const utils = require('../utils');
|
||||
const ttl = require('../cache/ttl');
|
||||
|
||||
@@ -14,6 +15,10 @@ const requestCache = ttl({ ttl: 1000 * 60 * 5 }); // 5 minutes
|
||||
const actorCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours
|
||||
const ActivityPub = module.exports;
|
||||
|
||||
ActivityPub._constants = Object.freeze({
|
||||
publicAddress: 'https://www.w3.org/ns/activitystreams#Public',
|
||||
});
|
||||
|
||||
ActivityPub.helpers = require('./helpers');
|
||||
ActivityPub.inbox = require('./inbox');
|
||||
ActivityPub.mocks = require('./mocks');
|
||||
@@ -59,10 +64,19 @@ ActivityPub.getActor = async (uid, input) => {
|
||||
}
|
||||
};
|
||||
|
||||
ActivityPub.resolveInboxes = async (uid, ids) => await Promise.all(ids.map(async (id) => {
|
||||
const actor = await ActivityPub.getActor(uid, id);
|
||||
return actor.inbox;
|
||||
}));
|
||||
ActivityPub.resolveInboxes = async (uid, ids) => {
|
||||
const inboxes = new Set();
|
||||
|
||||
await Promise.all(ids.map(async (id) => {
|
||||
const actor = await ActivityPub.getActor(uid, id);
|
||||
const inbox = actor.sharedInbox || actor.inbox;
|
||||
if (inbox) {
|
||||
inboxes.add(inbox);
|
||||
}
|
||||
}));
|
||||
|
||||
return Array.from(inboxes);
|
||||
};
|
||||
|
||||
ActivityPub.getPublicKey = async (uid) => {
|
||||
let publicKey;
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -8,6 +8,7 @@ const posts = require('../posts');
|
||||
const meta = require('../meta');
|
||||
const privileges = require('../privileges');
|
||||
|
||||
const activitypubApi = require('./activitypub');
|
||||
const apiHelpers = require('./helpers');
|
||||
|
||||
const { doTopicAction } = apiHelpers;
|
||||
@@ -79,6 +80,7 @@ topicsAPI.create = async function (caller, data) {
|
||||
socketHelpers.emitToUids('event:new_post', { posts: [result.postData] }, [caller.uid]);
|
||||
socketHelpers.emitToUids('event:new_topic', result.topicData, [caller.uid]);
|
||||
socketHelpers.notifyNew(caller.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
|
||||
activitypubApi.create.post(caller, { post: result.postData });
|
||||
|
||||
return result.topicData;
|
||||
};
|
||||
@@ -113,6 +115,7 @@ topicsAPI.reply = async function (caller, data) {
|
||||
}
|
||||
|
||||
socketHelpers.notifyNew(caller.uid, 'newPost', result);
|
||||
activitypubApi.create.post(caller, { post: postData });
|
||||
|
||||
return postObj[0];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user