feat: rewrite getRawPost to async/await

promisify SocketPosts
v1.18.x
Barış Soner Uşaklı 6 years ago
parent 973075cfbc
commit b734defbfa

@ -113,7 +113,7 @@ function onMessage(socket, payload) {
return null; return null;
}, Namespaces); }, Namespaces);
if (!methodToCall) { if (!methodToCall || typeof methodToCall !== 'function') {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
winston.warn('[socket.io] Unrecognized message: ' + eventName); winston.warn('[socket.io] Unrecognized message: ' + eventName);
} }
@ -146,17 +146,19 @@ function onMessage(socket, payload) {
} }
}, },
function (next) { function (next) {
let callbackCalled = false; async function tryAsyncFunc(done) {
function nextOnce(err, res) { try {
if (callbackCalled) { return; } const result = await methodToCall(socket, params);
callbackCalled = true; done(null, result);
next(err, res); } catch (err) {
done(err);
} }
const returned = methodToCall(socket, params, nextOnce); }
if (returned && typeof returned.then === 'function') {
returned.then((payload) => { if (methodToCall.constructor && methodToCall.constructor.name === 'AsyncFunction') {
nextOnce(null, payload); tryAsyncFunc(next);
}, next); } else {
methodToCall(socket, params, next);
} }
}, },
], function (err, result) { ], function (err, result) {

@ -70,35 +70,19 @@ function postReply(socket, data, callback) {
], callback); ], callback);
} }
SocketPosts.getRawPost = function (socket, pid, callback) { SocketPosts.getRawPost = async function (socket, pid) {
async.waterfall([ const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
function (next) {
privileges.posts.can('topics:read', pid, socket.uid, next);
},
function (canRead, next) {
if (!canRead) { if (!canRead) {
return next(new Error('[[error:no-privileges]]')); throw new Error('[[error:no-privileges]]');
} }
posts.getPostFields(pid, ['content', 'deleted'], next);
}, const postData = await posts.getPostFields(pid, ['content', 'deleted']);
function (postData, next) {
if (postData.deleted) { if (postData.deleted) {
return next(new Error('[[error:no-post]]')); throw new Error('[[error:no-post]]');
} }
next(null, postData); postData.pid = pid;
}, const result = await plugins.fireHook('filter:post.getRawPost', { uid: socket.uid, postData: postData });
function (postData, next) { return result.postData.content;
plugins.fireHook('filter:post.getRawPost', Object.assign(postData, {
pid: pid,
}), next);
},
], function (err, postData) {
if (err) {
return callback(err);
}
callback(null, postData.content);
});
}; };
SocketPosts.getTimestampByIndex = function (socket, data, callback) { SocketPosts.getTimestampByIndex = function (socket, data, callback) {
@ -269,3 +253,5 @@ function acceptOrReject(method, socket, data, callback) {
}, },
], callback); ], callback);
} }
require('../promisify')(SocketPosts);

Loading…
Cancel
Save