mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +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;
|
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);
|
|
||||||
},
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
SocketPosts.getTimestampByIndex = function (socket, data, callback) {
|
||||||
@@ -269,3 +253,5 @@ function acceptOrReject(method, socket, data, callback) {
|
|||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require('../promisify')(SocketPosts);
|
||||||
|
|||||||
Reference in New Issue
Block a user