Node.js based forum software built for the modern web
feat: rewrite getRawPost to async/await
Barış Soner Uşaklıb734defbfaa9
diff --git a/src/socket.io/index.js b/src/socket.io/index.jsindex 4bd2362e13..7110b48dc8 100644--- a/src/socket.io/index.js+++ b/src/socket.io/index.js@@ -113,7 +113,7 @@ function onMessage(socket, payload) {return null;}, Namespaces);- if (!methodToCall) {+ if (!methodToCall || typeof methodToCall !== 'function') {if (process.env.NODE_ENV === 'development') {winston.warn('[socket.io] Unrecognized message: ' + eventName);}@@ -146,17 +146,19 @@ function onMessage(socket, payload) {}},function (next) {- let callbackCalled = false;- function nextOnce(err, res) {- if (callbackCalled) { return; }- callbackCalled = true;- next(err, res);+ async function tryAsyncFunc(done) {+ try {+ const result = await methodToCall(socket, params);+ done(null, result);+ } catch (err) {+ done(err);+ }}- const returned = methodToCall(socket, params, nextOnce);- if (returned && typeof returned.then === 'function') {- returned.then((payload) => {- nextOnce(null, payload);- }, next);++ if (methodToCall.constructor && methodToCall.constructor.name === 'AsyncFunction') {+ tryAsyncFunc(next);+ } else {+ methodToCall(socket, params, next);}},], function (err, result) {diff --git a/src/socket.io/posts.js b/src/socket.io/posts.jsindex ea1bffb06a..0e82c3f554 100644--- a/src/socket.io/posts.js+++ b/src/socket.io/posts.js@@ -70,35 +70,19 @@ function postReply(socket, data, callback) {], callback);}-SocketPosts.getRawPost = function (socket, pid, callback) {- async.waterfall([- function (next) {- privileges.posts.can('topics:read', pid, socket.uid, next);- },- function (canRead, next) {- if (!canRead) {- return next(new Error('[[error:no-privileges]]'));- }- posts.getPostFields(pid, ['content', 'deleted'], next);- },- function (postData, next) {- if (postData.deleted) {- return next(new Error('[[error:no-post]]'));- }- next(null, postData);- },- function (postData, next) {- plugins.fireHook('filter:post.getRawPost', Object.assign(postData, {- pid: pid,- }), next);- },- ], function (err, postData) {- if (err) {- return callback(err);- }+SocketPosts.getRawPost = async function (socket, pid) {+ const canRead = await privileges.posts.can('topics:read', pid, socket.uid);+ if (!canRead) {+ throw new Error('[[error:no-privileges]]');+ }- callback(null, postData.content);- });+ const postData = await posts.getPostFields(pid, ['content', 'deleted']);+ if (postData.deleted) {+ throw new Error('[[error:no-post]]');+ }+ postData.pid = pid;+ const result = await plugins.fireHook('filter:post.getRawPost', { uid: socket.uid, postData: postData });+ return result.postData.content;};SocketPosts.getTimestampByIndex = function (socket, data, callback) {@@ -269,3 +253,5 @@ function acceptOrReject(method, socket, data, callback) {},], callback);}++require('../promisify')(SocketPosts);