mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-28 09:31:17 +01:00
refactor: get rid of exists db call, use promise.parallel
This commit is contained in:
@@ -10,7 +10,6 @@ const topics = require('../topics');
|
|||||||
const categories = require('../categories');
|
const categories = require('../categories');
|
||||||
const groups = require('../groups');
|
const groups = require('../groups');
|
||||||
const privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
const utils = require('../utils');
|
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
Posts.create = async function (data) {
|
Posts.create = async function (data) {
|
||||||
@@ -26,16 +25,7 @@ module.exports = function (Posts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data.toPid) {
|
if (data.toPid) {
|
||||||
const toPidExists = await Posts.exists(data.toPid);
|
await checkToPid(data.toPid);
|
||||||
const toPidDeleted = await Posts.getPostField(data.toPid, 'deleted');
|
|
||||||
const canViewToPid = await privileges.posts.can('posts:view_deleted', data.toPid, uid);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!utils.isNumber(data.toPid) || !toPidExists ||
|
|
||||||
(toPidDeleted && !canViewToPid)
|
|
||||||
) {
|
|
||||||
throw new Error('[[error:invalid-pid]]');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const pid = await db.incrObjectField('global', 'nextPid');
|
const pid = await db.incrObjectField('global', 'nextPid');
|
||||||
@@ -90,4 +80,15 @@ module.exports = function (Posts) {
|
|||||||
db.incrObjectField(`post:${postData.toPid}`, 'replies'),
|
db.incrObjectField(`post:${postData.toPid}`, 'replies'),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function checkToPid(toPid, uid) {
|
||||||
|
const [toPost, canViewToPid] = await Promise.all([
|
||||||
|
Posts.getPostFields(toPid, ['pid', 'deleted']),
|
||||||
|
privileges.posts.can('posts:view_deleted', toPid, uid),
|
||||||
|
]);
|
||||||
|
const toPidExists = !!toPost.pid;
|
||||||
|
if (!toPidExists || (toPost.deleted && !canViewToPid)) {
|
||||||
|
throw new Error('[[error:invalid-pid]]');
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user