Skip to content

Node.js based forum software built for the modern web

fix: revalidate deleted state before edit broadcast (#14574)

Sebastian Albrecht0ed9157a7c41
diff --git a/src/api/posts.js b/src/api/posts.js
index c081811ec0..6a5e234cfd 100644
--- a/src/api/posts.js
+++ b/src/api/posts.js
@@ -142,17 +142,17 @@ postsAPI.edit = async function (caller, data) {
newTitle: editResult.topic.title,
});
}
- const postObj = await posts.getPostSummaryByPids([editResult.post.pid], caller.uid, { parse: false, extraFields: ['edited'] });
+ const [postObj] = await posts.getPostSummaryByPids([editResult.post.pid], caller.uid, { parse: false, extraFields: ['edited'] });
postObj.content = editResult.post.content; // re-use already parsed html
- const returnData = { ...postObj[0], ...editResult.post };
- returnData.topic = { ...postObj[0].topic, ...editResult.post.topic };
+ const returnData = { ...postObj, ...editResult.post };
+ returnData.topic = { ...postObj.topic, ...editResult.post.topic };
if (!postObj.deleted) {
const topicScheduled = await topics.getTopicField(editResult.topic.tid, 'scheduled');
websockets.in(`topic_${editResult.topic.tid}`).emit('event:post_edited', editResult);
if (!topicScheduled) {
setImmediate(() => {
- activitypub.out.update.note(caller.uid, postObj[0]);
+ activitypub.out.update.note(caller.uid, postObj);
});
}
@@ -697,4 +697,4 @@ postsAPI.changeOwner = async function (caller, data) {
})));
await Promise.all(logs);
-};
\ No newline at end of file
+};
diff --git a/test/posts.js b/test/posts.js
index 31735cea4c..4abfb6169d 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -19,6 +19,7 @@ const groups = require('../src/groups');
const socketPosts = require('../src/socket.io/posts');
const apiPosts = require('../src/api/posts');
const apiTopics = require('../src/api/topics');
+const websockets = require('../src/socket.io');
const meta = require('../src/meta');
const file = require('../src/file');
const helpers = require('./helpers');
@@ -1368,6 +1369,63 @@ describe('Post\'s', () => {
});
});
+describe('Post edit broadcasts', () => {
+ let ownerUid;
+ let moderatorUid;
+ let cid;
+
+ before(async () => {
+ ownerUid = await user.create({ username: `edit-owner-${utils.generateUUID()}` });
+ moderatorUid = await user.create({ username: `edit-moderator-${utils.generateUUID()}` });
+ ({ cid } = await categories.create({ name: `edit-race-${utils.generateUUID()}` }));
+ await groups.join('Global Moderators', moderatorUid);
+ });
+
+ it('should not broadcast an edit to the topic room after a concurrent deletion', async () => {
+ const { postData } = await topics.post({
+ uid: ownerUid,
+ cid,
+ title: 'Concurrent edit and delete',
+ content: 'Original post content',
+ });
+ const emitted = [];
+ const originalIn = websockets.in;
+ const originalGetPostData = posts.getPostData;
+ let intercept = true;
+
+ websockets.in = room => ({
+ emit: (event) => {
+ emitted.push({ room: String(room), event });
+ },
+ });
+ posts.getPostData = async (pid) => {
+ const data = await originalGetPostData(pid);
+ if (intercept && String(pid) === String(postData.pid)) {
+ intercept = false;
+ const staleData = { ...data, deleted: 0 };
+ await posts.delete(postData.pid, moderatorUid);
+ return staleData;
+ }
+ return data;
+ };
+
+ try {
+ await apiPosts.edit({ uid: ownerUid }, {
+ pid: postData.pid,
+ title: 'Edited after concurrent deletion',
+ content: 'This must stay out of the topic room',
+ });
+ } finally {
+ posts.getPostData = originalGetPostData;
+ websockets.in = originalIn;
+ }
+
+ assert.strictEqual(parseInt(await posts.getPostField(postData.pid, 'deleted'), 10), 1);
+ assert.strictEqual(emitted.some(item => item.room === `topic_${postData.tid}`), false);
+ assert.strictEqual(emitted.some(item => item.room.startsWith('uid_')), true);
+ });
+});
+
describe('Posts\'', async () => {
let files;