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:
Julian Lam
2024-01-24 11:44:10 -05:00
parent c9feb92539
commit 0e016c6ecd
3 changed files with 76 additions and 4 deletions

View File

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

View File

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