Skip to content

Node.js based forum software built for the modern web

feat: Create(Note) on new topic or reply

Julian Lam0e016c6ecd93
diff --git a/src/activitypub/index.js b/src/activitypub/index.js
index 57227a2e20..478c755c6b 100644
--- a/src/activitypub/index.js
+++ b/src/activitypub/index.js
@@ -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;
diff --git a/src/api/activitypub.js b/src/api/activitypub.js
index b8f9813f21..dba7a972c1 100644
--- a/src/api/activitypub.js
+++ b/src/api/activitypub.js
@@ -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);
+};
diff --git a/src/api/topics.js b/src/api/topics.js
index 7a6cabf966..873071cf4a 100644
--- a/src/api/topics.js
+++ b/src/api/topics.js
@@ -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];
};