mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat: rewrite getRawPost to async/await
promisify SocketPosts
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user