Skip to content

Node.js based forum software built for the modern web

refactor: async/await socket.io/posts

Barış Soner Uşaklıe93ef0d7fda5
diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js
index 0e82c3f554..97f7ec3b0a 100644
--- a/src/socket.io/posts.js
+++ b/src/socket.io/posts.js
@@ -1,20 +1,19 @@
'use strict';
-var async = require('async');
-
-var posts = require('../posts');
-var privileges = require('../privileges');
-var plugins = require('../plugins');
-var meta = require('../meta');
-var topics = require('../topics');
+const db = require('../database');
+const posts = require('../posts');
+const privileges = require('../privileges');
+const plugins = require('../plugins');
+const meta = require('../meta');
+const topics = require('../topics');
const categories = require('../categories');
-var user = require('../user');
-var socketHelpers = require('./helpers');
-var utils = require('../utils');
+const user = require('../user');
+const socketHelpers = require('./helpers');
+const utils = require('../utils');
-var apiController = require('../controllers/api');
+const apiController = require('../controllers/api');
-var SocketPosts = module.exports;
+const SocketPosts = module.exports;
require('./posts/edit')(SocketPosts);
require('./posts/move')(SocketPosts);
@@ -23,51 +22,35 @@ require('./posts/bookmarks')(SocketPosts);
require('./posts/tools')(SocketPosts);
require('./posts/diffs')(SocketPosts);
-SocketPosts.reply = function (socket, data, callback) {
+SocketPosts.reply = async function (socket, data) {
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
socketHelpers.setDefaultPostData(data, socket);
-
- async.waterfall([
- function (next) {
- meta.blacklist.test(data.req.ip, next);
- },
- function (next) {
- posts.shouldQueue(socket.uid, data, next);
- },
- function (shouldQueue, next) {
- if (shouldQueue) {
- posts.addToQueue(data, next);
- } else {
- postReply(socket, data, next);
- }
- },
- ], callback);
+ 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);
};
-function postReply(socket, data, callback) {
- async.waterfall([
- function (next) {
- topics.reply(data, next);
- },
- function (postData, next) {
- var result = {
- posts: [postData],
- 'reputation:disabled': meta.config['reputation:disabled'] === 1,
- 'downvote:disabled': meta.config['downvote:disabled'] === 1,
- };
+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,
+ };
- next(null, postData);
+ socket.emit('event:new_post', result);
- socket.emit('event:new_post', result);
+ user.updateOnlineUsers(socket.uid);
- user.updateOnlineUsers(socket.uid);
+ socketHelpers.notifyNew(socket.uid, 'newPost', result);
- socketHelpers.notifyNew(socket.uid, 'newPost', result);
- },
- ], callback);
+ return postData;
}
SocketPosts.getRawPost = async function (socket, pid) {
@@ -85,173 +68,117 @@ SocketPosts.getRawPost = async function (socket, pid) {
return result.postData.content;
};
-SocketPosts.getTimestampByIndex = function (socket, data, callback) {
- var pid;
- var db = require('../database');
-
- async.waterfall([
- function (next) {
- if (data.index < 0) {
- data.index = 0;
- }
- if (data.index === 0) {
- topics.getTopicField(data.tid, 'mainPid', next);
- } else {
- db.getSortedSetRange('tid:' + data.tid + ':posts', data.index - 1, data.index - 1, next);
- }
- },
- function (_pid, next) {
- pid = Array.isArray(_pid) ? _pid[0] : _pid;
- if (!pid) {
- return callback(null, 0);
- }
- privileges.posts.can('topics:read', pid, socket.uid, next);
- },
- function (canRead, next) {
- if (!canRead) {
- return next(new Error('[[error:no-privileges]]'));
- }
- posts.getPostFields(pid, ['timestamp'], next);
- },
- function (postData, next) {
- next(null, postData.timestamp);
- },
- ], callback);
+SocketPosts.getTimestampByIndex = async function (socket, data) {
+ if (data.index < 0) {
+ data.index = 0;
+ }
+ let pid;
+ if (data.index === 0) {
+ pid = topics.getTopicField(data.tid, 'mainPid');
+ } else {
+ pid = db.getSortedSetRange('tid:' + data.tid + ':posts', data.index - 1, data.index - 1);
+ }
+ pid = Array.isArray(pid) ? pid[0] : pid;
+ if (!pid) {
+ return 0;
+ }
+
+ const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
+ if (!canRead) {
+ throw new Error('[[error:no-privileges]]');
+ }
+ return await posts.getPostField(pid, 'timestamp');
};
-SocketPosts.getPost = function (socket, pid, callback) {
- apiController.getPostData(pid, socket.uid, callback);
+SocketPosts.getPost = async function (socket, pid) {
+ return await apiController.getPostData(pid, socket.uid);
};
-SocketPosts.loadMoreBookmarks = function (socket, data, callback) {
- loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data, callback);
+SocketPosts.loadMoreBookmarks = async function (socket, data) {
+ return await loadMorePosts('uid:' + data.uid + ':bookmarks', socket.uid, data);
};
-SocketPosts.loadMoreUserPosts = function (socket, data, callback) {
- async.waterfall([
- function (next) {
- categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read', next);
- },
- function (cids, next) {
- const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids');
- loadMorePosts(keys, socket.uid, data, next);
- },
- ], callback);
+SocketPosts.loadMoreUserPosts = async function (socket, data) {
+ const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
+ const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids');
+ return await loadMorePosts(keys, socket.uid, data);
};
-SocketPosts.loadMoreBestPosts = function (socket, data, callback) {
- async.waterfall([
- function (next) {
- categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read', next);
- },
- function (cids, next) {
- const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids:votes');
- loadMorePosts(keys, socket.uid, data, next);
- },
- ], callback);
+SocketPosts.loadMoreBestPosts = async function (socket, data) {
+ const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read');
+ const keys = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':pids:votes');
+ return await loadMorePosts(keys, socket.uid, data);
};
-SocketPosts.loadMoreUpVotedPosts = function (socket, data, callback) {
- loadMorePosts('uid:' + data.uid + ':upvote', socket.uid, data, callback);
+SocketPosts.loadMoreUpVotedPosts = async function (socket, data) {
+ return await loadMorePosts('uid:' + data.uid + ':upvote', socket.uid, data);
};
-SocketPosts.loadMoreDownVotedPosts = function (socket, data, callback) {
- loadMorePosts('uid:' + data.uid + ':downvote', socket.uid, data, callback);
+SocketPosts.loadMoreDownVotedPosts = async function (socket, data) {
+ return await loadMorePosts('uid:' + data.uid + ':downvote', socket.uid, data);
};
-function loadMorePosts(set, uid, data, callback) {
+async function loadMorePosts(set, uid, data) {
if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
- var start = Math.max(0, parseInt(data.after, 10));
- var stop = start + 9;
+ const start = Math.max(0, parseInt(data.after, 10));
+ const stop = start + 9;
- posts.getPostSummariesFromSet(set, uid, start, stop, callback);
+ return await posts.getPostSummariesFromSet(set, uid, start, stop);
}
-SocketPosts.getCategory = function (socket, pid, callback) {
- posts.getCidByPid(pid, callback);
+SocketPosts.getCategory = async function (socket, pid) {
+ return await posts.getCidByPid(pid);
};
-SocketPosts.getPidIndex = function (socket, data, callback) {
+SocketPosts.getPidIndex = async function (socket, data) {
if (!data) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
- posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);
+ return await posts.getPidIndex(data.pid, data.tid, data.topicPostSort);
};
-SocketPosts.getReplies = function (socket, pid, callback) {
+SocketPosts.getReplies = async function (socket, pid) {
if (!utils.isNumber(pid)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
- var postPrivileges;
- async.waterfall([
- function (next) {
- posts.getPidsFromSet('pid:' + pid + ':replies', 0, -1, false, next);
- },
- function (pids, next) {
- async.parallel({
- posts: function (next) {
- posts.getPostsByPids(pids, socket.uid, next);
- },
- privileges: function (next) {
- privileges.posts.get(pids, socket.uid, next);
- },
- }, next);
- },
- function (results, next) {
- postPrivileges = results.privileges;
-
- topics.addPostData(results.posts, socket.uid, next);
- },
- function (postData, next) {
- postData.forEach(function (postData, index) {
- posts.modifyPostByPrivilege(postData, postPrivileges[index]);
- });
- postData = postData.filter(function (postData, index) {
- return postData && postPrivileges[index].read;
- });
- next(null, postData);
- },
- ], callback);
+
+ const pids = await posts.getPidsFromSet('pid:' + pid + ':replies', 0, -1, false);
+
+ var [postData, postPrivileges] = await Promise.all([
+ posts.getPostsByPids(pids, socket.uid),
+ privileges.posts.get(pids, socket.uid),
+ ]);
+ postData = await topics.addPostData(postData, socket.uid);
+ postData.forEach((postData, index) => posts.modifyPostByPrivilege(postData, postPrivileges[index]));
+ postData = postData.filter((postData, index) => postData && postPrivileges[index].read);
+ return postData;
};
-SocketPosts.accept = function (socket, data, callback) {
- acceptOrReject(posts.submitFromQueue, socket, data, callback);
+SocketPosts.accept = async function (socket, data) {
+ await acceptOrReject(posts.submitFromQueue, socket, data);
};
-SocketPosts.reject = function (socket, data, callback) {
- acceptOrReject(posts.removeFromQueue, socket, data, callback);
+SocketPosts.reject = async function (socket, data) {
+ await acceptOrReject(posts.removeFromQueue, socket, data);
};
-SocketPosts.editQueuedContent = function (socket, data, callback) {
+async function acceptOrReject(method, socket, data) {
+ const canEditQueue = await posts.canEditQueue(socket.uid, data.id);
+ if (!canEditQueue) {
+ throw new Error('[[error:no-privileges]]');
+ }
+ await method(data.id);
+}
+
+SocketPosts.editQueuedContent = async function (socket, data) {
if (!data || !data.id || !data.content) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
- async.waterfall([
- function (next) {
- posts.editQueuedContent(socket.uid, data.id, data.content, next);
- },
- function (next) {
- plugins.fireHook('filter:parse.post', { postData: data }, next);
- },
- ], callback);
+ await posts.editQueuedContent(socket.uid, data.id, data.content);
+ return await plugins.fireHook('filter:parse.post', { postData: data });
};
-function acceptOrReject(method, socket, data, callback) {
- async.waterfall([
- function (next) {
- posts.canEditQueue(socket.uid, data.id, next);
- },
- function (canEditQueue, next) {
- if (!canEditQueue) {
- return callback(new Error('[[error:no-privileges]]'));
- }
-
- method(data.id, next);
- },
- ], callback);
-}
-
require('../promisify')(SocketPosts);
diff --git a/src/socket.io/posts/bookmarks.js b/src/socket.io/posts/bookmarks.js
index b77ce526a1..fa73ed43d6 100644
--- a/src/socket.io/posts/bookmarks.js
+++ b/src/socket.io/posts/bookmarks.js
@@ -1,14 +1,14 @@
'use strict';
-var helpers = require('./helpers');
+const helpers = require('./helpers');
module.exports = function (SocketPosts) {
- SocketPosts.bookmark = function (socket, data, callback) {
- helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data, callback);
+ SocketPosts.bookmark = async function (socket, data) {
+ return await helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data);
};
- SocketPosts.unbookmark = function (socket, data, callback) {
- helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data, callback);
+ SocketPosts.unbookmark = async function (socket, data) {
+ return await helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data);
};
};
diff --git a/src/socket.io/posts/diffs.js b/src/socket.io/posts/diffs.js
index eddd9e3f1b..6f4ae21c63 100644
--- a/src/socket.io/posts/diffs.js
+++ b/src/socket.io/posts/diffs.js
@@ -1,46 +1,30 @@
'use strict';
-var async = require('async');
-var posts = require('../../posts');
-var privileges = require('../../privileges');
+const posts = require('../../posts');
+const privileges = require('../../privileges');
module.exports = function (SocketPosts) {
- SocketPosts.getDiffs = function (socket, data, callback) {
- async.waterfall([
- async.apply(privilegeCheck, data.pid, socket.uid),
- function (next) {
- posts.diffs.list(data.pid, next);
- },
- function (timestamps, next) {
- timestamps.unshift(Date.now());
- next(null, timestamps);
- },
- ], callback);
+ SocketPosts.getDiffs = async function (socket, data) {
+ await privilegeCheck(data.pid, socket.uid);
+ const timestamps = await posts.diffs.list(data.pid);
+ timestamps.unshift(Date.now());
+ return timestamps;
};
- SocketPosts.showPostAt = function (socket, data, callback) {
- privilegeCheck(data.pid, socket.uid, function (err) {
- if (err) {
- return callback(err);
- }
-
- posts.diffs.load(data.pid, data.since, socket.uid, callback);
- });
+ SocketPosts.showPostAt = async function (socket, data) {
+ await privilegeCheck(data.pid, socket.uid);
+ return await posts.diffs.load(data.pid, data.since, socket.uid);
};
- function privilegeCheck(pid, uid, callback) {
- async.parallel({
- deleted: async.apply(posts.getPostField, pid, 'deleted'),
- privileges: async.apply(privileges.posts.get, [pid], uid),
- }, function (err, payload) {
- if (err) {
- return callback(err);
- }
-
- payload.privileges = payload.privileges[0];
+ async function privilegeCheck(pid, uid) {
+ const [deleted, privilegesData] = await Promise.all([
+ posts.getPostField(pid, 'deleted'),
+ privileges.posts.get([pid], uid),
+ ]);
- const allowed = payload.privileges['posts:history'] && (payload.deleted ? payload.privileges['posts:view_deleted'] : true);
- callback(!allowed ? new Error('[[error:no-privileges]]') : null);
- });
+ const allowed = privilegesData[0]['posts:history'] && (deleted ? privilegesData[0]['posts:view_deleted'] : true);
+ if (!allowed) {
+ throw new Error('[[error:no-privileges]]');
+ }
}
};
diff --git a/src/socket.io/posts/edit.js b/src/socket.io/posts/edit.js
index 8d1bc773a5..6499ea3eea 100644
--- a/src/socket.io/posts/edit.js
+++ b/src/socket.io/posts/edit.js
@@ -1,81 +1,69 @@
'use strict';
-var async = require('async');
-var validator = require('validator');
-var _ = require('lodash');
+const validator = require('validator');
+const _ = require('lodash');
-var posts = require('../../posts');
-var groups = require('../../groups');
-var events = require('../../events');
-var meta = require('../../meta');
-var utils = require('../../utils');
-var websockets = require('../index');
+const posts = require('../../posts');
+const groups = require('../../groups');
+const events = require('../../events');
+const meta = require('../../meta');
+const utils = require('../../utils');
+const websockets = require('../index');
module.exports = function (SocketPosts) {
- SocketPosts.edit = function (socket, data, callback) {
+ SocketPosts.edit = async function (socket, data) {
if (!socket.uid) {
- return callback(new Error('[[error:not-logged-in]]'));
+ throw new Error('[[error:not-logged-in]]');
} else if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
var contentLen = utils.stripHTMLTags(data.content).trim().length;
if (data.title && data.title.length < meta.config.minimumTitleLength) {
- return callback(new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'));
+ throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]');
} else if (data.title && data.title.length > meta.config.maximumTitleLength) {
- return callback(new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'));
+ throw new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]');
} else if (data.tags && data.tags.length < meta.config.minimumTagsPerTopic) {
- return callback(new Error('[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]'));
+ throw new Error('[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]');
} else if (data.tags && data.tags.length > meta.config.maximumTagsPerTopic) {
- return callback(new Error('[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]'));
+ throw new Error('[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]');
} else if (meta.config.minimumPostLength !== 0 && contentLen < meta.config.minimumPostLength) {
- return callback(new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'));
+ throw new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]');
} else if (contentLen > meta.config.maximumPostLength) {
- return callback(new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]'));
+ throw new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]');
}
data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
- var editResult;
- async.waterfall([
- function (next) {
- posts.edit(data, next);
- },
- function (result, next) {
- editResult = result;
- if (result.topic.renamed) {
- events.log({
- type: 'topic-rename',
- uid: socket.uid,
- ip: socket.ip,
- tid: result.topic.tid,
- oldTitle: validator.escape(String(result.topic.oldTitle)),
- newTitle: validator.escape(String(result.topic.title)),
- });
- }
+ const editResult = await posts.edit(data);
+ if (editResult.topic.renamed) {
+ await events.log({
+ type: 'topic-rename',
+ uid: socket.uid,
+ ip: socket.ip,
+ tid: editResult.topic.tid,
+ oldTitle: validator.escape(String(editResult.topic.oldTitle)),
+ newTitle: validator.escape(String(editResult.topic.title)),
+ });
+ }
+
+ if (!editResult.post.deleted) {
+ websockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult);
+ return editResult.post;
+ }
- if (!result.post.deleted) {
- websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result);
- return callback(null, result.post);
- }
+ const memberData = await groups.getMembersOfGroups([
+ 'administrators',
+ 'Global Moderators',
+ 'cid:' + editResult.topic.cid + ':privileges:moderate',
+ 'cid:' + editResult.topic.cid + ':privileges:groups:moderate',
+ ]);
- groups.getMembersOfGroups([
- 'administrators',
- 'Global Moderators',
- 'cid:' + result.topic.cid + ':privileges:moderate',
- 'cid:' + result.topic.cid + ':privileges:groups:moderate',
- ], next);
- },
- function (results, next) {
- var uids = _.uniq(_.flatten(results).concat(socket.uid.toString()));
- uids.forEach(function (uid) {
- websockets.in('uid_' + uid).emit('event:post_edited', editResult);
- });
- next(null, editResult.post);
- },
- ], callback);
+ const uids = _.uniq(_.flatten(memberData).concat(socket.uid.toString()));
+ uids.forEach(uid => websockets.in('uid_' + uid).emit('event:post_edited', editResult));
+ return editResult.post;
};
};
diff --git a/src/socket.io/posts/helpers.js b/src/socket.io/posts/helpers.js
index c5c41746ec..bce4ddb4c8 100644
--- a/src/socket.io/posts/helpers.js
+++ b/src/socket.io/posts/helpers.js
@@ -1,82 +1,62 @@
'use strict';
-var async = require('async');
-var posts = require('../../posts');
-var plugins = require('../../plugins');
-var websockets = require('../index');
-var socketHelpers = require('../helpers');
+const posts = require('../../posts');
+const plugins = require('../../plugins');
+const websockets = require('../index');
+const socketHelpers = require('../helpers');
-var helpers = module.exports;
+const helpers = module.exports;
-helpers.postCommand = function (socket, command, eventName, notification, data, callback) {
+helpers.postCommand = async function (socket, command, eventName, notification, data) {
if (!socket.uid) {
- return callback(new Error('[[error:not-logged-in]]'));
+ throw new Error('[[error:not-logged-in]]');
}
if (!data || !data.pid) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
if (!data.room_id) {
- return callback(new Error('[[error:invalid-room-id, ' + data.room_id + ' ]]'));
+ throw new Error('[[error:invalid-room-id, ' + data.room_id + ' ]]');
}
+ const [exists, deleted] = await Promise.all([
+ posts.exists(data.pid),
+ posts.getPostField(data.pid, 'deleted'),
+ ]);
- async.waterfall([
- function (next) {
- async.parallel({
- exists: function (next) {
- posts.exists(data.pid, next);
- },
- deleted: function (next) {
- posts.getPostField(data.pid, 'deleted', next);
- },
- }, next);
- },
- function (results, next) {
- if (!results.exists) {
- return next(new Error('[[error:invalid-pid]]'));
- }
+ if (!exists) {
+ throw new Error('[[error:invalid-pid]]');
+ }
- if (results.deleted) {
- return next(new Error('[[error:post-deleted]]'));
- }
+ if (deleted) {
+ throw new Error('[[error:post-deleted]]');
+ }
- /*
- hooks:
- filter:post.upvote
- filter:post.downvote
- filter:post.unvote
- filter:post.bookmark
- filter:post.unbookmark
- */
- plugins.fireHook('filter:post.' + command, { data: data, uid: socket.uid }, next);
- },
- function (filteredData, next) {
- executeCommand(socket, command, eventName, notification, filteredData.data, next);
- },
- ], callback);
+ /*
+ hooks:
+ filter:post.upvote
+ filter:post.downvote
+ filter:post.unvote
+ filter:post.bookmark
+ filter:post.unbookmark
+ */
+ const filteredData = await plugins.fireHook('filter:post.' + command, { data: data, uid: socket.uid });
+ return await executeCommand(socket, command, eventName, notification, filteredData.data);
};
-function executeCommand(socket, command, eventName, notification, data, callback) {
- async.waterfall([
- function (next) {
- posts[command](data.pid, socket.uid, next);
- },
- function (result, next) {
- if (result && eventName) {
- websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
- websockets.in(data.room_id).emit('event:' + eventName, result);
- }
-
- if (result && command === 'upvote') {
- socketHelpers.upvote(result, notification);
- } else if (result && notification) {
- socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
- } else if (result && command === 'unvote') {
- socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
- }
- next(null, result);
- },
- ], callback);
+async function executeCommand(socket, command, eventName, notification, data) {
+ const result = await posts[command](data.pid, socket.uid);
+ if (result && eventName) {
+ websockets.in('uid_' + socket.uid).emit('posts.' + command, result);
+ websockets.in(data.room_id).emit('event:' + eventName, result);
+ }
+ if (result && command === 'upvote') {
+ socketHelpers.upvote(result, notification);
+ } else if (result && notification) {
+ socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, command, notification);
+ } else if (result && command === 'unvote') {
+ socketHelpers.rescindUpvoteNotification(data.pid, socket.uid);
+ }
+ return result;
}
diff --git a/src/socket.io/posts/move.js b/src/socket.io/posts/move.js
index 1716d066f3..fe8a293738 100644
--- a/src/socket.io/posts/move.js
+++ b/src/socket.io/posts/move.js
@@ -1,40 +1,31 @@
'use strict';
-var async = require('async');
-var privileges = require('../../privileges');
-var topics = require('../../topics');
-var socketHelpers = require('../helpers');
+const privileges = require('../../privileges');
+const topics = require('../../topics');
+const socketHelpers = require('../helpers');
module.exports = function (SocketPosts) {
- SocketPosts.movePost = function (socket, data, callback) {
- SocketPosts.movePosts(socket, { pids: [data.pid], tid: data.tid }, callback);
+ SocketPosts.movePost = async function (socket, data) {
+ await SocketPosts.movePosts(socket, { pids: [data.pid], tid: data.tid });
};
- SocketPosts.movePosts = function (socket, data, callback) {
+ SocketPosts.movePosts = async function (socket, data) {
if (!socket.uid) {
- return callback(new Error('[[error:not-logged-in]]'));
+ throw new Error('[[error:not-logged-in]]');
}
if (!data || !Array.isArray(data.pids) || !data.tid) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
- async.eachSeries(data.pids, function (pid, next) {
- async.waterfall([
- function (next) {
- privileges.posts.canMove(pid, socket.uid, next);
- },
- function (canMove, next) {
- if (!canMove) {
- return next(new Error('[[error:no-privileges]]'));
- }
- topics.movePostToTopic(socket.uid, pid, data.tid, next);
- },
- function (next) {
- socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
- next();
- },
- ], next);
- }, callback);
+ for (const pid of data.pids) {
+ /* eslint-disable no-await-in-loop */
+ const canMove = await privileges.posts.canMove(pid, socket.uid);
+ if (!canMove) {
+ throw new Error('[[error:no-privileges]]');
+ }
+ await topics.movePostToTopic(socket.uid, pid, data.tid);
+ socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
+ }
};
};
diff --git a/src/socket.io/posts/tools.js b/src/socket.io/posts/tools.js
index 2266a0ac38..4fd2a1ad22 100644
--- a/src/socket.io/posts/tools.js
+++ b/src/socket.io/posts/tools.js
@@ -1,253 +1,172 @@
'use strict';
-var async = require('async');
-
-var posts = require('../../posts');
-var topics = require('../../topics');
-var events = require('../../events');
-var websockets = require('../index');
-var socketTopics = require('../topics');
-var privileges = require('../../privileges');
-var plugins = require('../../plugins');
-var social = require('../../social');
-var user = require('../../user');
+const posts = require('../../posts');
+const topics = require('../../topics');
+const events = require('../../events');
+const websockets = require('../index');
+const socketTopics = require('../topics');
+const privileges = require('../../privileges');
+const plugins = require('../../plugins');
+const social = require('../../social');
+const user = require('../../user');
+const utils = require('../../utils');
module.exports = function (SocketPosts) {
- SocketPosts.loadPostTools = function (socket, data, callback) {
+ SocketPosts.loadPostTools = async function (socket, data) {
if (!data || !data.pid || !data.cid) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
- async.waterfall([
- function (next) {
- async.parallel({
- posts: function (next) {
- posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid', 'ip'], next);
- },
- isAdmin: function (next) {
- user.isAdministrator(socket.uid, next);
- },
- isGlobalMod: function (next) {
- user.isGlobalModerator(socket.uid, next);
- },
- isModerator: function (next) {
- user.isModerator(socket.uid, data.cid, next);
- },
- canEdit: function (next) {
- privileges.posts.canEdit(data.pid, socket.uid, next);
- },
- canDelete: function (next) {
- privileges.posts.canDelete(data.pid, socket.uid, next);
- },
- canPurge: function (next) {
- privileges.posts.canPurge(data.pid, socket.uid, next);
- },
- canFlag: function (next) {
- privileges.posts.canFlag(data.pid, socket.uid, next);
- },
- bookmarked: function (next) {
- posts.hasBookmarked(data.pid, socket.uid, next);
- },
- tools: function (next) {
- plugins.fireHook('filter:post.tools', { pid: data.pid, uid: socket.uid, tools: [] }, next);
- },
- postSharing: function (next) {
- social.getActivePostSharing(next);
- },
- history: async.apply(posts.diffs.exists, data.pid),
- }, next);
- },
- function (results, next) {
- var posts = results.posts;
- posts.tools = results.tools.tools;
- posts.bookmarked = results.bookmarked;
- posts.selfPost = socket.uid && socket.uid === posts.uid;
- posts.display_edit_tools = results.canEdit.flag;
- posts.display_delete_tools = results.canDelete.flag;
- posts.display_purge_tools = results.canPurge;
- posts.display_flag_tools = socket.uid && !posts.selfPost && results.canFlag.flag;
- posts.display_moderator_tools = posts.display_edit_tools || posts.display_delete_tools;
- posts.display_move_tools = results.isAdmin || results.isModerator;
- posts.display_change_owner_tools = results.isAdmin || results.isModerator;
- posts.display_ip_ban = (results.isAdmin || results.isGlobalMod) && !posts.selfPost;
- posts.display_history = results.history;
- posts.toolsVisible = posts.tools.length || posts.display_moderator_tools;
- if (!results.isAdmin && !results.isGlobalMod && !results.isModerator) {
- posts.ip = undefined;
- }
- next(null, results);
- },
- ], callback);
+ const results = await utils.promiseParallel({
+ posts: posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid', 'ip']),
+ isAdmin: user.isAdministrator(socket.uid),
+ isGlobalMod: user.isGlobalModerator(socket.uid),
+ isModerator: user.isModerator(socket.uid, data.cid),
+ canEdit: privileges.posts.canEdit(data.pid, socket.uid),
+ canDelete: privileges.posts.canDelete(data.pid, socket.uid),
+ canPurge: privileges.posts.canPurge(data.pid, socket.uid),
+ canFlag: privileges.posts.canFlag(data.pid, socket.uid),
+ bookmarked: posts.hasBookmarked(data.pid, socket.uid),
+ tools: plugins.fireHook('filter:post.tools', { pid: data.pid, uid: socket.uid, tools: [] }),
+ postSharing: social.getActivePostSharing(),
+ history: posts.diffs.exists(data.pid),
+ });
+
+ const postData = results.posts;
+ postData.tools = results.tools.tools;
+ postData.bookmarked = results.bookmarked;
+ postData.selfPost = socket.uid && socket.uid === postData.uid;
+ postData.display_edit_tools = results.canEdit.flag;
+ postData.display_delete_tools = results.canDelete.flag;
+ postData.display_purge_tools = results.canPurge;
+ postData.display_flag_tools = socket.uid && !postData.selfPost && results.canFlag.flag;
+ postData.display_moderator_tools = postData.display_edit_tools || postData.display_delete_tools;
+ postData.display_move_tools = results.isAdmin || results.isModerator;
+ postData.display_change_owner_tools = results.isAdmin || results.isModerator;
+ postData.display_ip_ban = (results.isAdmin || results.isGlobalMod) && !postData.selfPost;
+ postData.display_history = results.history;
+ postData.toolsVisible = postData.tools.length || postData.display_moderator_tools;
+
+ if (!results.isAdmin && !results.isGlobalMod && !results.isModerator) {
+ postData.ip = undefined;
+ }
+ return results;
};
- SocketPosts.delete = function (socket, data, callback) {
+ SocketPosts.delete = async function (socket, data) {
if (!data || !data.pid) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
+ }
+ const postData = await posts.tools.delete(socket.uid, data.pid);
+ const results = await isMainAndLastPost(data.pid);
+ if (results.isMain && results.isLast) {
+ await deleteOrRestoreTopicOf('delete', data.pid, socket);
}
- var postData;
- async.waterfall([
- function (next) {
- posts.tools.delete(socket.uid, data.pid, next);
- },
- function (_postData, next) {
- postData = _postData;
- isMainAndLastPost(data.pid, next);
- },
- function (results, next) {
- if (results.isMain && results.isLast) {
- deleteOrRestoreTopicOf('delete', data.pid, socket, next);
- } else {
- next();
- }
- },
- function (next) {
- websockets.in('topic_' + data.tid).emit('event:post_deleted', postData);
- events.log({
- type: 'post-delete',
- uid: socket.uid,
- pid: data.pid,
- tid: postData.tid,
- ip: socket.ip,
- });
+ websockets.in('topic_' + data.tid).emit('event:post_deleted', postData);
- next();
- },
- ], callback);
+ await events.log({
+ type: 'post-delete',
+ uid: socket.uid,
+ pid: data.pid,
+ tid: postData.tid,
+ ip: socket.ip,
+ });
};
- SocketPosts.restore = function (socket, data, callback) {
+ SocketPosts.restore = async function (socket, data) {
if (!data || !data.pid) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
- var postData;
- async.waterfall([
- function (next) {
- posts.tools.restore(socket.uid, data.pid, next);
- },
- function (_postData, next) {
- postData = _postData;
- isMainAndLastPost(data.pid, next);
- },
- function (results, next) {
- if (results.isMain && results.isLast) {
- deleteOrRestoreTopicOf('restore', data.pid, socket, next);
- } else {
- setImmediate(next);
- }
- },
- function (next) {
- websockets.in('topic_' + data.tid).emit('event:post_restored', postData);
-
- events.log({
- type: 'post-restore',
- uid: socket.uid,
- pid: data.pid,
- tid: postData.tid,
- ip: socket.ip,
- });
-
- setImmediate(next);
- },
- ], callback);
+ const postData = await posts.tools.restore(socket.uid, data.pid);
+ const results = await isMainAndLastPost(data.pid);
+ if (results.isMain && results.isLast) {
+ await deleteOrRestoreTopicOf('restore', data.pid, socket);
+ }
+ websockets.in('topic_' + data.tid).emit('event:post_restored', postData);
+
+ await events.log({
+ type: 'post-restore',
+ uid: socket.uid,
+ pid: data.pid,
+ tid: postData.tid,
+ ip: socket.ip,
+ });
};
- SocketPosts.deletePosts = function (socket, data, callback) {
+ SocketPosts.deletePosts = async function (socket, data) {
if (!data || !Array.isArray(data.pids)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
+ }
+ for (const pid of data.pids) {
+ /* eslint-disable no-await-in-loop */
+ await SocketPosts.delete(socket, { pid: pid, tid: data.tid });
}
- async.eachSeries(data.pids, function (pid, next) {
- SocketPosts.delete(socket, { pid: pid, tid: data.tid }, next);
- }, callback);
};
- SocketPosts.purgePosts = function (socket, data, callback) {
+ SocketPosts.purgePosts = async function (socket, data) {
if (!data || !Array.isArray(data.pids)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
+ }
+ for (const pid of data.pids) {
+ /* eslint-disable no-await-in-loop */
+ await SocketPosts.purge(socket, { pid: pid, tid: data.tid });
}
- async.eachSeries(data.pids, function (pid, next) {
- SocketPosts.purge(socket, { pid: pid, tid: data.tid }, next);
- }, callback);
};
- SocketPosts.purge = function (socket, data, callback) {
+ SocketPosts.purge = async function (socket, data) {
if (!data || !parseInt(data.pid, 10)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
+ }
+
+
+ const results = await isMainAndLastPost(data.pid);
+ if (results.isMain && !results.isLast) {
+ throw new Error('[[error:cant-purge-main-post]]');
}
- var postData;
- var topicData;
- var isMainAndLast = false;
- async.waterfall([
- function (next) {
- isMainAndLastPost(data.pid, next);
- },
- function (results, next) {
- if (results.isMain && !results.isLast) {
- return next(new Error('[[error:cant-purge-main-post]]'));
- }
- isMainAndLast = results.isMain && results.isLast;
- posts.getPostFields(data.pid, ['toPid', 'tid'], next);
- },
- function (_postData, next) {
- postData = _postData;
- postData.pid = data.pid;
- posts.tools.purge(socket.uid, data.pid, next);
- },
- function (next) {
- websockets.in('topic_' + data.tid).emit('event:post_purged', postData);
- topics.getTopicFields(data.tid, ['title', 'cid'], next);
- },
- function (_topicData, next) {
- topicData = _topicData;
- events.log({
- type: 'post-purge',
- uid: socket.uid,
- pid: data.pid,
- ip: socket.ip,
- tid: postData.tid,
- title: String(topicData.title),
- }, next);
- },
- function (next) {
- if (isMainAndLast) {
- socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid }, next);
- } else {
- setImmediate(next);
- }
- },
- ], callback);
+ const isMainAndLast = results.isMain && results.isLast;
+ const postData = await posts.getPostFields(data.pid, ['toPid', 'tid']);
+ postData.pid = data.pid;
+
+ await posts.tools.purge(socket.uid, data.pid);
+
+ websockets.in('topic_' + data.tid).emit('event:post_purged', postData);
+ const topicData = await topics.getTopicFields(data.tid, ['title', 'cid']);
+
+ await events.log({
+ type: 'post-purge',
+ uid: socket.uid,
+ pid: data.pid,
+ ip: socket.ip,
+ tid: postData.tid,
+ title: String(topicData.title),
+ });
+
+ if (isMainAndLast) {
+ await socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid });
+ }
};
- function deleteOrRestoreTopicOf(command, pid, socket, callback) {
- async.waterfall([
- function (next) {
- posts.getTopicFields(pid, ['tid', 'cid', 'deleted'], next);
- },
- function (topic, next) {
- if (command === 'delete' && !topic.deleted) {
- socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid }, next);
- } else if (command === 'restore' && topic.deleted) {
- socketTopics.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid }, next);
- } else {
- setImmediate(next);
- }
- },
- ], callback);
+ async function deleteOrRestoreTopicOf(command, pid, socket) {
+ const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
+ if (command === 'delete' && !topic.deleted) {
+ await socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid });
+ } else if (command === 'restore' && topic.deleted) {
+ await socketTopics.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid });
+ }
}
- function isMainAndLastPost(pid, callback) {
- async.parallel({
- isMain: function (next) {
- posts.isMain(pid, next);
- },
- isLast: function (next) {
- posts.getTopicFields(pid, ['postcount'], function (err, topic) {
- next(err, topic ? topic.postcount === 1 : false);
- });
- },
- }, callback);
+ async function isMainAndLastPost(pid) {
+ const [isMain, topicData] = await Promise.all([
+ posts.isMain(pid),
+ posts.getTopicFields(pid, ['postcount']),
+ ]);
+ return {
+ isMain: isMain,
+ isLast: topicData && topicData.postcount === 1,
+ };
}
SocketPosts.changeOwner = async function (socket, data) {
diff --git a/src/socket.io/posts/votes.js b/src/socket.io/posts/votes.js
index 34bf8c0fbd..3e53a2435b 100644
--- a/src/socket.io/posts/votes.js
+++ b/src/socket.io/posts/votes.js
@@ -1,103 +1,74 @@
'use strict';
-var async = require('async');
-
-var db = require('../../database');
-var user = require('../../user');
-var posts = require('../../posts');
-var privileges = require('../../privileges');
-var meta = require('../../meta');
-var helpers = require('./helpers');
+const db = require('../../database');
+const user = require('../../user');
+const posts = require('../../posts');
+const privileges = require('../../privileges');
+const meta = require('../../meta');
+const helpers = require('./helpers');
module.exports = function (SocketPosts) {
- SocketPosts.getVoters = function (socket, data, callback) {
+ SocketPosts.getVoters = async function (socket, data) {
if (!data || !data.pid || !data.cid) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
}
const showDownvotes = !meta.config['downvote:disabled'];
- async.waterfall([
- function (next) {
- if (meta.config.votesArePublic) {
- return next(null, true);
- }
- privileges.categories.isAdminOrMod(data.cid, socket.uid, next);
- },
- function (isAdminOrMod, next) {
- if (!isAdminOrMod) {
- return next(new Error('[[error:no-privileges]]'));
- }
+ const canSeeVotes = meta.config.votesArePublic || await privileges.categories.isAdminOrMod(data.cid, socket.uid);
+ if (!canSeeVotes) {
+ throw new Error('[[error:no-privileges]]');
+ }
+ const [upvoteUids, downvoteUids] = await Promise.all([
+ db.getSetMembers('pid:' + data.pid + ':upvote'),
+ showDownvotes ? db.getSetMembers('pid:' + data.pid + ':downvote') : [],
+ ]);
+
+ const [upvoters, downvoters] = await Promise.all([
+ user.getUsersFields(upvoteUids, ['username', 'userslug', 'picture']),
+ user.getUsersFields(downvoteUids, ['username', 'userslug', 'picture']),
+ ]);
- async.parallel({
- upvoteUids: function (next) {
- db.getSetMembers('pid:' + data.pid + ':upvote', next);
- },
- downvoteUids: function (next) {
- if (!showDownvotes) {
- return setImmediate(next, null, []);
- }
- db.getSetMembers('pid:' + data.pid + ':downvote', next);
- },
- }, next);
- },
- function (results, next) {
- async.parallel({
- upvoters: function (next) {
- user.getUsersFields(results.upvoteUids, ['username', 'userslug', 'picture'], next);
- },
- downvoters: function (next) {
- user.getUsersFields(results.downvoteUids, ['username', 'userslug', 'picture'], next);
- },
- }, next);
- },
- function (results, next) {
- results.upvoteCount = results.upvoters.length;
- results.downvoteCount = results.downvoters.length;
- results.showDownvotes = showDownvotes;
- next(null, results);
- },
- ], callback);
+ return {
+ upvoteCount: upvoters.length,
+ downvoteCount: downvoters.length,
+ showDownvotes: showDownvotes,
+ upvoters: upvoters,
+ downvoters: downvoters,
+ };
};
- SocketPosts.getUpvoters = function (socket, pids, callback) {
+ SocketPosts.getUpvoters = async function (socket, pids) {
if (!Array.isArray(pids)) {
- return callback(new Error('[[error:invalid-data]]'));
+ throw new Error('[[error:invalid-data]]');
+ }
+ const data = await posts.getUpvotedUidsByPids(pids);
+ if (!data.length) {
+ return [];
}
- async.waterfall([
- function (next) {
- posts.getUpvotedUidsByPids(pids, next);
- },
- function (data, next) {
- if (!data.length) {
- return callback(null, []);
- }
-
- async.map(data, function (uids, next) {
- var otherCount = 0;
- if (uids.length > 6) {
- otherCount = uids.length - 5;
- uids = uids.slice(0, 5);
- }
- user.getUsernamesByUids(uids, function (err, usernames) {
- next(err, {
- otherCount: otherCount,
- usernames: usernames,
- });
- });
- }, next);
- },
- ], callback);
+ const result = await Promise.all(data.map(async function (uids) {
+ let otherCount = 0;
+ if (uids.length > 6) {
+ otherCount = uids.length - 5;
+ uids = uids.slice(0, 5);
+ }
+ const usernames = await user.getUsernamesByUids(uids);
+ return {
+ otherCount: otherCount,
+ usernames: usernames,
+ };
+ }));
+ return result;
};
- SocketPosts.upvote = function (socket, data, callback) {
- helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data, callback);
+ SocketPosts.upvote = async function (socket, data) {
+ return await helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
};
- SocketPosts.downvote = function (socket, data, callback) {
- helpers.postCommand(socket, 'downvote', 'voted', '', data, callback);
+ SocketPosts.downvote = async function (socket, data) {
+ return await helpers.postCommand(socket, 'downvote', 'voted', '', data);
};
- SocketPosts.unvote = function (socket, data, callback) {
- helpers.postCommand(socket, 'unvote', 'voted', '', data, callback);
+ SocketPosts.unvote = async function (socket, data) {
+ return await helpers.postCommand(socket, 'unvote', 'voted', '', data);
};
};
diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js
index 7c84109046..f84ca7e83f 100644
--- a/src/socket.io/topics.js
+++ b/src/socket.io/topics.js
@@ -143,3 +143,5 @@ SocketTopics.isModerator = function (socket, tid, callback) {
SocketTopics.getTopic = function (socket, tid, callback) {
apiController.getTopicData(tid, socket.uid, callback);
};
+
+require('../promisify')(SocketTopics);