Skip to content

Node.js based forum software built for the modern web

breaking: remove socket.emit('posts.reply')

Barış Soner Uşaklı4604a5724c39
diff --git a/src/api/helpers.js b/src/api/helpers.js
index 82a379ae40..ca94594fcc 100644
--- a/src/api/helpers.js
+++ b/src/api/helpers.js
@@ -50,8 +50,8 @@ exports.doTopicAction = async function (action, event, caller, { tids }) {
throw new Error('[[error:invalid-tid]]');
}
- const exists = (await Promise.all(tids.map(async tid => await topics.exists(tid)))).every(Boolean);
- if (!exists) {
+ const exists = await topics.exists(tids);
+ if (!exists.every(Boolean)) {
throw new Error('[[error:no-topic]]');
}
diff --git a/src/api/topics.js b/src/api/topics.js
index 75a902f39f..187bc064be 100644
--- a/src/api/topics.js
+++ b/src/api/topics.js
@@ -60,6 +60,9 @@ topicsAPI.create = async function (caller, data) {
};
topicsAPI.reply = async function (caller, data) {
+ if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
+ throw new Error('[[error:invalid-data]]');
+ }
const payload = { ...data };
apiHelpers.setDefaultPostData(caller, payload);
diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js
index e838a06c41..cf5f7f9f47 100644
--- a/src/socket.io/posts.js
+++ b/src/socket.io/posts.js
@@ -8,52 +8,13 @@ const meta = require('../meta');
const topics = require('../topics');
const user = require('../user');
const notifications = require('../notifications');
-const socketHelpers = require('./helpers');
const utils = require('../utils');
-const api = require('../api');
-const apiHelpers = require('../api/helpers');
-
-const sockets = require('.');
const SocketPosts = module.exports;
require('./posts/votes')(SocketPosts);
require('./posts/tools')(SocketPosts);
-SocketPosts.reply = async function (socket, data) {
- sockets.warnDeprecated(socket, 'POST /api/v3/topics/:tid');
-
- if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
- throw new Error('[[error:invalid-data]]');
- }
-
- apiHelpers.setDefaultPostData(socket, data);
- await meta.blacklist.test(data.req.ip);
- const shouldQueue = await posts.shouldQueue(socket.uid, data);
- if (shouldQueue) {
- return await posts.addToQueue(data);
- }
- return await postReply(socket, data);
-};
-
-async function postReply(socket, data) {
- const postData = await topics.reply(data);
- const result = {
- posts: [postData],
- 'reputation:disabled': meta.config['reputation:disabled'] === 1,
- 'downvote:disabled': meta.config['downvote:disabled'] === 1,
- };
-
- if (socket.emit) {
- socket.emit('event:new_post', result);
- }
- user.updateOnlineUsers(socket.uid);
-
- socketHelpers.notifyNew(socket.uid, 'newPost', result);
-
- return postData;
-}
-
SocketPosts.getRawPost = async function (socket, pid) {
const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
if (!canRead) {
@@ -110,11 +71,6 @@ SocketPosts.getPostSummaryByPid = async function (socket, data) {
return postsData[0];
};
-SocketPosts.getPost = async function (socket, pid) {
- sockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid');
- return await api.posts.get(socket, { pid });
-};
-
SocketPosts.getCategory = async function (socket, pid) {
return await posts.getCidByPid(pid);
};
diff --git a/test/posts.js b/test/posts.js
index a4d865cd55..d309e88486 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -22,6 +22,7 @@ const groups = require('../src/groups');
const socketPosts = require('../src/socket.io/posts');
const socketTopics = require('../src/socket.io/topics');
const apiPosts = require('../src/api/posts');
+const apiTopics = require('../src/api/topics');
const meta = require('../src/meta');
const helpers = require('./helpers');
@@ -813,18 +814,22 @@ describe('Post\'s', () => {
});
});
- it('should error with invalid data', (done) => {
- socketPosts.reply({ uid: 0 }, null, (err) => {
+ it('should error with invalid data', async () => {
+ try {
+ await apiTopics.reply({ uid: 0 }, null);
+ assert(false);
+ } catch (err) {
assert.equal(err.message, '[[error:invalid-data]]');
- done();
- });
+ }
});
- it('should error with invalid tid', (done) => {
- socketPosts.reply({ uid: 0 }, { tid: 0, content: 'derp' }, (err) => {
+ it('should error with invalid tid', async () => {
+ try {
+ await apiTopics.reply({ uid: 0 }, { tid: 0, content: 'derp' });
+ assert(false);
+ } catch (err) {
assert.equal(err.message, '[[error:invalid-data]]');
- done();
- });
+ }
});
it('should fail to get raw post because of privilege', (done) => {
@@ -855,12 +860,9 @@ describe('Post\'s', () => {
});
});
- it('should get post', (done) => {
- socketPosts.getPost({ uid: voterUid }, pid, (err, postData) => {
- assert.ifError(err);
- assert(postData);
- done();
- });
+ it('should get post', async () => {
+ const postData = await apiPosts.get({ uid: voterUid }, { pid });
+ assert(postData);
});
it('should get post category', (done) => {
@@ -975,14 +977,11 @@ describe('Post\'s', () => {
});
});
- it('should add reply to post queue', (done) => {
- socketPosts.reply({ uid: uid }, { content: 'this is a queued reply', tid: topicData.tid }, (err, result) => {
- assert.ifError(err);
- assert.strictEqual(result.queued, true);
- assert.equal(result.message, '[[success:post-queued]]');
- queueId = result.id;
- done();
- });
+ it('should add reply to post queue', async () => {
+ const result = await apiTopics.reply({ uid: uid }, { content: 'this is a queued reply', tid: topicData.tid });
+ assert.strictEqual(result.queued, true);
+ assert.equal(result.message, '[[success:post-queued]]');
+ queueId = result.id;
});
it('should load queued posts', (done) => {
@@ -1097,7 +1096,7 @@ describe('Post\'s', () => {
const result1 = await socketTopics.post({ uid: globalModUid }, { title: 'topic A', content: 'topic A content', cid: cid });
const result2 = await socketTopics.post({ uid: globalModUid }, { title: 'topic B', content: 'topic B content', cid: cid });
- const result = await socketPosts.reply({ uid: uid }, { content: 'the moved queued post', tid: result1.tid });
+ const result = await apiTopics.reply({ uid: uid }, { content: 'the moved queued post', tid: result1.tid });
await topics.merge([
result1.tid, result2.tid,
diff --git a/test/socket.io.js b/test/socket.io.js
index a2ef7c7d69..818c087365 100644
--- a/test/socket.io.js
+++ b/test/socket.io.js
@@ -121,16 +121,6 @@ describe('socket.io', () => {
});
});
- it('should reply to topic', (done) => {
- io.emit('posts.reply', { tid: tid, uid: adminUid, content: 'test post content' }, (err, result) => {
- assert.ifError(err);
- assert.equal(result.uid, adminUid);
- assert.equal(result.user.username, 'admin');
- assert.equal(result.topic.tid, tid);
- done();
- });
- });
-
it('should ban a user', (done) => {
const socketUser = require('../src/socket.io/user');
socketUser.banUsers({ uid: adminUid }, { uids: [regularUid], reason: 'spammer' }, (err) => {