From 4247f62441e9bd9972fb4930e668747a55692a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 9 Dec 2021 18:42:43 -0500 Subject: [PATCH] breaking: remove socket.emit('posts.edit') --- src/api/posts.js | 3 + src/socket.io/posts.js | 1 - src/socket.io/posts/edit.js | 16 ---- test/posts.js | 156 ++++++++++++++++++++---------------- 4 files changed, 88 insertions(+), 88 deletions(-) delete mode 100644 src/socket.io/posts/edit.js diff --git a/src/api/posts.js b/src/api/posts.js index a49e6eb9ed..34c4c304eb 100644 --- a/src/api/posts.js +++ b/src/api/posts.js @@ -46,6 +46,9 @@ postsAPI.edit = async function (caller, data) { if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) { throw new Error('[[error:invalid-data]]'); } + if (!caller.uid) { + throw new Error('[[error:not-logged-in]]'); + } // Trim and remove HTML (latter for composers that send in HTML, like redactor) const contentLen = utils.stripHTMLTags(data.content).trim().length; diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 467b4cd8a0..236f360dc6 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -17,7 +17,6 @@ const sockets = require('.'); const SocketPosts = module.exports; -require('./posts/edit')(SocketPosts); require('./posts/move')(SocketPosts); require('./posts/votes')(SocketPosts); require('./posts/tools')(SocketPosts); diff --git a/src/socket.io/posts/edit.js b/src/socket.io/posts/edit.js deleted file mode 100644 index e679c46395..0000000000 --- a/src/socket.io/posts/edit.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -const api = require('../../api'); -const websockets = require('../index'); - -module.exports = function (SocketPosts) { - SocketPosts.edit = async function (socket, data) { - websockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid'); - - if (!socket.uid) { - throw new Error('[[error:not-logged-in]]'); - } - - return await api.posts.edit(socket, data); - }; -}; diff --git a/test/posts.js b/test/posts.js index 08100a30cf..bac34f4b92 100644 --- a/test/posts.js +++ b/test/posts.js @@ -8,6 +8,9 @@ const nconf = require('nconf'); const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); +const util = require('util'); + +const sleep = util.promisify(setTimeout); const db = require('./mocks/databasemock'); const topics = require('../src/topics'); @@ -481,73 +484,90 @@ describe('Post\'s', () => { }); }); - it('should error if user is not logged in', (done) => { - socketPosts.edit({ uid: 0 }, {}, (err) => { - assert.equal(err.message, '[[error:not-logged-in]]'); - done(); - }); + it('should error if user is not logged in', async () => { + try { + await apiPosts.edit({ uid: 0 }, { pid: pid, content: 'gg' }); + } catch (err) { + return assert.equal(err.message, '[[error:not-logged-in]]'); + } + assert(false); }); - it('should error if data is invalid or missing', (done) => { - socketPosts.edit({ uid: voterUid }, {}, (err) => { - assert.equal(err.message, '[[error:invalid-data]]'); - done(); - }); + it('should error if data is invalid or missing', async () => { + try { + await apiPosts.edit({ uid: voterUid }, {}); + } catch (err) { + return assert.equal(err.message, '[[error:invalid-data]]'); + } + assert(false); }); - it('should error if title is too short', (done) => { - socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: 'a' }, (err) => { - assert.equal(err.message, `[[error:title-too-short, ${meta.config.minimumTitleLength}]]`); - done(); - }); + it('should error if title is too short', async () => { + try { + await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: 'a' }); + } catch (err) { + return assert.equal(err.message, `[[error:title-too-short, ${meta.config.minimumTitleLength}]]`); + } + assert(false); }); - it('should error if title is too long', (done) => { + it('should error if title is too long', async () => { const longTitle = new Array(meta.config.maximumTitleLength + 2).join('a'); - socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: longTitle }, (err) => { - assert.equal(err.message, `[[error:title-too-long, ${meta.config.maximumTitleLength}]]`); - done(); - }); + try { + await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: longTitle }); + } catch (err) { + return assert.equal(err.message, `[[error:title-too-long, ${meta.config.maximumTitleLength}]]`); + } + assert(false); }); - it('should error with too few tags', (done) => { + it('should error with too few tags', async () => { const oldValue = meta.config.minimumTagsPerTopic; meta.config.minimumTagsPerTopic = 1; - socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: [] }, (err) => { + try { + await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: [] }); + } catch (err) { assert.equal(err.message, `[[error:not-enough-tags, ${meta.config.minimumTagsPerTopic}]]`); meta.config.minimumTagsPerTopic = oldValue; - done(); - }); + return; + } + assert(false); }); - it('should error with too many tags', (done) => { + it('should error with too many tags', async () => { const tags = []; for (let i = 0; i < meta.config.maximumTagsPerTopic + 1; i += 1) { tags.push(`tag${i}`); } - socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: tags }, (err) => { - assert.equal(err.message, `[[error:too-many-tags, ${meta.config.maximumTagsPerTopic}]]`); - done(); - }); + try { + await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: tags }); + } catch (err) { + return assert.equal(err.message, `[[error:too-many-tags, ${meta.config.maximumTagsPerTopic}]]`); + } + assert(false); }); - it('should error if content is too short', (done) => { - socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'e' }, (err) => { - assert.equal(err.message, `[[error:content-too-short, ${meta.config.minimumPostLength}]]`); - done(); - }); + it('should error if content is too short', async () => { + try { + await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'e' }); + } catch (err) { + return assert.equal(err.message, `[[error:content-too-short, ${meta.config.minimumPostLength}]]`); + } + assert(false); }); - it('should error if content is too long', (done) => { + it('should error if content is too long', async () => { const longContent = new Array(meta.config.maximumPostLength + 2).join('a'); - socketPosts.edit({ uid: voterUid }, { pid: pid, content: longContent }, (err) => { - assert.equal(err.message, `[[error:content-too-long, ${meta.config.maximumPostLength}]]`); - done(); - }); + try { + await apiPosts.edit({ uid: voterUid }, { pid: pid, content: longContent }); + } catch (err) { + return assert.equal(err.message, `[[error:content-too-long, ${meta.config.maximumPostLength}]]`); + } + assert(false); }); it('should edit post', async () => { - const data = await socketPosts.edit({ uid: voterUid }, { + const data = await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: 'edited title', @@ -562,40 +582,34 @@ describe('Post\'s', () => { assert(!res.hasOwnProperty('bookmarks')); }); - it('should disallow post editing for new users if post was made past the threshold for editing', (done) => { + it('should disallow post editing for new users if post was made past the threshold for editing', async () => { meta.config.newbiePostEditDuration = 1; - setTimeout(() => { - socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content again', title: 'edited title again', tags: ['edited-twice'] }, (err, data) => { - assert.equal(err.message, '[[error:post-edit-duration-expired, 1]]'); - meta.config.newbiePostEditDuration = 3600; - done(); - }); - }, 1000); + await sleep(1000); + try { + await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content again', title: 'edited title again', tags: ['edited-twice'] }); + } catch (err) { + assert.equal(err.message, '[[error:post-edit-duration-expired, 1]]'); + meta.config.newbiePostEditDuration = 3600; + return; + } + assert(false); }); - it('should edit a deleted post', (done) => { - socketPosts.delete({ uid: voterUid }, { pid: pid, tid: tid }, (err) => { - assert.ifError(err); - socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited deleted content', title: 'edited deleted title', tags: ['deleted'] }, (err, data) => { - assert.ifError(err); - assert.equal(data.content, 'edited deleted content'); - assert.equal(data.editor, voterUid); - assert.equal(data.topic.title, 'edited deleted title'); - assert.equal(data.topic.tags[0].value, 'deleted'); - done(); - }); - }); + it('should edit a deleted post', async () => { + await apiPosts.delete({ uid: voterUid }, { pid: pid, tid: tid }); + const data = await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited deleted content', title: 'edited deleted title', tags: ['deleted'] }); + assert.equal(data.content, 'edited deleted content'); + assert.equal(data.editor, voterUid); + assert.equal(data.topic.title, 'edited deleted title'); + assert.equal(data.topic.tags[0].value, 'deleted'); }); - it('should edit a reply post', (done) => { - socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'edited reply' }, (err, data) => { - assert.ifError(err); - assert.equal(data.content, 'edited reply'); - assert.equal(data.editor, voterUid); - assert.equal(data.topic.isMainPost, false); - assert.equal(data.topic.renamed, false); - done(); - }); + it('should edit a reply post', async () => { + const data = await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'edited reply' }); + assert.equal(data.content, 'edited reply'); + assert.equal(data.editor, voterUid); + assert.equal(data.topic.isMainPost, false); + assert.equal(data.topic.renamed, false); }); it('should return diffs', (done) => { @@ -650,8 +664,8 @@ describe('Post\'s', () => { }); it('should delete a post diff', async () => { - await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'another edit has been made' }); - await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'most recent edit' }); + await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'another edit has been made' }); + await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'most recent edit' }); const timestamp = (await posts.diffs.list(replyPid)).pop(); await posts.diffs.delete(replyPid, timestamp, voterUid); const differentTimestamp = (await posts.diffs.list(replyPid)).pop();