Skip to content

Node.js based forum software built for the modern web

fix: wrap calls to activitypub.out methods in setImmediate so local methods have a chance to finish before requests start going out

Julian Lam814076e07cd3
diff --git a/src/api/helpers.js b/src/api/helpers.js
index 7dec96cc30..f1d6de6bf1 100644
--- a/src/api/helpers.js
+++ b/src/api/helpers.js
@@ -80,9 +80,11 @@ exports.doTopicAction = async function (action, event, caller, { tids }) {
case 'delete': // falls through
case 'purge': {
if (utils.isNumber(cid) && parseInt(cid, 10) > 0) {
- activitypub.out.remove.context(caller.uid, tid); // 7888-style
- activitypub.out.delete.note(caller.uid, mainPid); // 1b12-style
- activitypub.out.undo.announce('cid', cid, tid); // microblogs
+ setImmediate(() => {
+ activitypub.out.remove.context(caller.uid, tid); // 7888-style
+ activitypub.out.delete.note(caller.uid, mainPid); // 1b12-style
+ activitypub.out.undo.announce('cid', cid, tid); // microblogs
+ });
}
}
}
@@ -155,19 +157,25 @@ async function executeCommand(caller, command, eventName, notification, data) {
switch (command) {
case 'upvote': {
socketHelpers.upvote(result, notification);
- await activitypub.out.like.note(caller.uid, data.pid);
+ setImmediate(() => {
+ activitypub.out.like.note(caller.uid, data.pid);
+ });
break;
}
case 'downvote': {
- await activitypub.out.dislike.note(caller.uid, data.pid);
+ setImmediate(() => {
+ activitypub.out.dislike.note(caller.uid, data.pid);
+ });
break;
}
case 'unvote': {
socketHelpers.rescindUpvoteNotification(data.pid, caller.uid);
const verb = result.was.upvoted ? 'like' : 'dislike';
- await activitypub.out.undo[verb](caller.uid, data.pid);
+ setImmediate(() => {
+ activitypub.out.undo[verb](caller.uid, data.pid);
+ });
break;
}
diff --git a/src/api/posts.js b/src/api/posts.js
index 767ea8eb1c..3ccb3e59c3 100644
--- a/src/api/posts.js
+++ b/src/api/posts.js
@@ -150,9 +150,9 @@ postsAPI.edit = async function (caller, data) {
if (!editResult.post.deleted) {
websockets.in(`topic_${editResult.topic.tid}`).emit('event:post_edited', editResult);
- setTimeout(() => {
+ setImmediate(() => {
activitypub.out.update.note(caller.uid, postObj[0]);
- }, 5000);
+ });
return returnData;
}
@@ -193,8 +193,13 @@ async function deleteOrRestore(caller, data, params) {
const [postData, { isMain, isLast }] = await Promise.all([
posts.tools[params.command](caller.uid, data.pid),
isMainAndLastPost(data.pid),
- activitypub.out.delete.note(caller.uid, data.pid),
]);
+ setImmediate(() => {
+ // todo: need undo(delete)
+ if (params.command === 'delete') {
+ activitypub.out.delete.note(caller.uid, data.pid),
+ }
+ });
if (isMain && isLast) {
await deleteOrRestoreTopicOf(params.command, data.pid, caller);
}
@@ -252,8 +257,10 @@ postsAPI.purge = async function (caller, data) {
posts.clearCachedPost(data.pid);
await Promise.all([
posts.purge(data.pid, caller.uid),
- activitypub.out.delete.note(caller.uid, data.pid),
]);
+ setImmediate(() => {
+ activitypub.out.delete.note(caller.uid, data.pid),
+ });
websockets.in(`topic_${postData.tid}`).emit('event:post_purged', postData);
const topicData = await topics.getTopicFields(postData.tid, ['title', 'cid']);
diff --git a/src/api/topics.js b/src/api/topics.js
index 45be990a9b..37d8a28776 100644
--- a/src/api/topics.js
+++ b/src/api/topics.js
@@ -81,7 +81,9 @@ topicsAPI.create = async function (caller, data) {
socketHelpers.notifyNew(caller.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
if (!isScheduling) {
- await activitypub.out.create.note(caller.uid, result.postData.pid);
+ setImmediate(() => {
+ activitypub.out.create.note(caller.uid, result.postData.pid);
+ });
}
return result.topicData;
@@ -117,7 +119,9 @@ topicsAPI.reply = async function (caller, data) {
}
socketHelpers.notifyNew(caller.uid, 'newPost', result);
- await activitypub.out.create.note(caller.uid, postData);
+ setImmediate(() => {
+ activitypub.out.create.note(caller.uid, postData);
+ });
return postData;
};
@@ -324,14 +328,16 @@ topicsAPI.move = async (caller, { tid, cid }) => {
if (!topicData.deleted) {
socketHelpers.sendNotificationToTopicOwner(tid, caller.uid, 'move', 'notifications:moved-your-topic');
- if (utils.isNumber(cid) && parseInt(cid, 10) === -1) {
- activitypub.out.remove.context(caller.uid, tid); // 7888-style
- activitypub.out.delete.note(caller.uid, topicData.mainPid); // 1b12-style
- } else {
- activitypub.out.move.context(caller.uid, tid);
- activitypub.out.announce.topic(tid);
- }
- activitypub.out.undo.announce('cid', topicData.cid, tid); // microblogs
+ setImmediate(() => {
+ if (utils.isNumber(cid) && parseInt(cid, 10) === -1) {
+ activitypub.out.remove.context(caller.uid, tid); // 7888-style
+ activitypub.out.delete.note(caller.uid, topicData.mainPid); // 1b12-style
+ } else {
+ activitypub.out.move.context(caller.uid, tid);
+ activitypub.out.announce.topic(tid);
+ }
+ activitypub.out.undo.announce('cid', topicData.cid, tid); // microblogs
+ });
}
await events.log({
diff --git a/src/messaging/delete.js b/src/messaging/delete.js
index c754e4fc9f..557108dafc 100644
--- a/src/messaging/delete.js
+++ b/src/messaging/delete.js
@@ -30,6 +30,8 @@ module.exports = function (Messaging) {
plugins.hooks.fire('action:messaging.restore', { message: msgData });
}
- activitypub.out.update.privateNote(uid, msgData);
+ setImmediate(() => {
+ activitypub.out.update.privateNote(uid, msgData);
+ });
}
};
diff --git a/src/messaging/edit.js b/src/messaging/edit.js
index 86d8b07b6c..6c366ea471 100644
--- a/src/messaging/edit.js
+++ b/src/messaging/edit.js
@@ -39,7 +39,9 @@ module.exports = function (Messaging) {
});
if (!isPublic && utils.isNumber(messages[0].fromuid)) {
- activitypub.out.update.privateNote(messages[0].fromuid, messages[0]);
+ setImmediate(() => {
+ activitypub.out.update.privateNote(messages[0].fromuid, messages[0]);
+ });
}
}
diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js
index 1c9475608d..90eae1f8b1 100644
--- a/src/messaging/notifications.js
+++ b/src/messaging/notifications.js
@@ -80,11 +80,12 @@ module.exports = function (Messaging) {
}
try {
- await Promise.all([
- sendNotification(fromUid, roomId, messageObj),
- !isPublic && utils.isNumber(fromUid) ?
- activitypub.out.create.privateNote(messageObj) : null,
- ]);
+ await sendNotification(fromUid, roomId, messageObj);
+ if (!isPublic && utils.isNumber(fromUid)) {
+ setImmediate(() => {
+ activitypub.out.create.privateNote(messageObj);
+ });
+ }
} catch (err) {
winston.error(`[messaging/notifications] Unabled to send notification\n${err.stack}`);
}