mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: move post queue retrival code to posts.getQueuedPosts
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const validator = require('validator');
|
|
||||||
|
|
||||||
const db = require('../database');
|
|
||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
const topics = require('../topics');
|
const posts = require('../posts');
|
||||||
const categories = require('../categories');
|
|
||||||
const flags = require('../flags');
|
const flags = require('../flags');
|
||||||
const analytics = require('../analytics');
|
const analytics = require('../analytics');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
@@ -160,8 +156,8 @@ modsController.postQueue = async function (req, res, next) {
|
|||||||
const page = parseInt(req.query.page, 10) || 1;
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
const postsPerPage = 20;
|
const postsPerPage = 20;
|
||||||
|
|
||||||
const [ids, isAdminOrGlobalMod, moderatedCids, categoriesData] = await Promise.all([
|
let postData = await posts.getQueuedPosts();
|
||||||
db.getSortedSetRange('post:queue', 0, -1),
|
const [isAdminOrGlobalMod, moderatedCids, categoriesData] = await Promise.all([
|
||||||
user.isAdminOrGlobalMod(req.uid),
|
user.isAdminOrGlobalMod(req.uid),
|
||||||
user.getModeratedCids(req.uid),
|
user.getModeratedCids(req.uid),
|
||||||
helpers.getSelectedCategory(cid),
|
helpers.getSelectedCategory(cid),
|
||||||
@@ -171,7 +167,6 @@ modsController.postQueue = async function (req, res, next) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
let postData = await getQueuedPosts(ids);
|
|
||||||
postData = postData.filter(p => p &&
|
postData = postData.filter(p => p &&
|
||||||
(!categoriesData.selectedCids.length || categoriesData.selectedCids.includes(p.category.cid)) &&
|
(!categoriesData.selectedCids.length || categoriesData.selectedCids.includes(p.category.cid)) &&
|
||||||
(isAdminOrGlobalMod || moderatedCids.includes(String(p.category.cid))));
|
(isAdminOrGlobalMod || moderatedCids.includes(String(p.category.cid))));
|
||||||
@@ -195,41 +190,3 @@ modsController.postQueue = async function (req, res, next) {
|
|||||||
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[pages:post-queue]]' }]),
|
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[pages:post-queue]]' }]),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
async function getQueuedPosts(ids) {
|
|
||||||
const keys = ids.map(id => `post:queue:${id}`);
|
|
||||||
const postData = await db.getObjects(keys);
|
|
||||||
postData.forEach((data) => {
|
|
||||||
if (data) {
|
|
||||||
data.data = JSON.parse(data.data);
|
|
||||||
data.data.timestampISO = utils.toISOString(data.data.timestamp);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const uids = postData.map(data => data && data.uid);
|
|
||||||
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
|
|
||||||
postData.forEach((postData, index) => {
|
|
||||||
if (postData) {
|
|
||||||
postData.user = userData[index];
|
|
||||||
postData.data.rawContent = validator.escape(String(postData.data.content));
|
|
||||||
postData.data.title = validator.escape(String(postData.data.title || ''));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await Promise.all(postData.map(p => addMetaData(p)));
|
|
||||||
return postData;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function addMetaData(postData) {
|
|
||||||
if (!postData) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
postData.topic = { cid: 0 };
|
|
||||||
if (postData.data.cid) {
|
|
||||||
postData.topic = { cid: parseInt(postData.data.cid, 10) };
|
|
||||||
} else if (postData.data.tid) {
|
|
||||||
postData.topic = await topics.getTopicFields(postData.data.tid, ['title', 'cid']);
|
|
||||||
}
|
|
||||||
postData.category = await categories.getCategoryData(postData.topic.cid);
|
|
||||||
const result = await plugins.hooks.fire('filter:parse.post', { postData: postData.data });
|
|
||||||
postData.data.content = result.postData.content;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const validator = require('validator');
|
||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
@@ -11,9 +12,49 @@ const categories = require('../categories');
|
|||||||
const notifications = require('../notifications');
|
const notifications = require('../notifications');
|
||||||
const privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
|
const utils = require('../utils');
|
||||||
const socketHelpers = require('../socket.io/helpers');
|
const socketHelpers = require('../socket.io/helpers');
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
|
Posts.getQueuedPosts = async () => {
|
||||||
|
const ids = await db.getSortedSetRange('post:queue', 0, -1);
|
||||||
|
const keys = ids.map(id => `post:queue:${id}`);
|
||||||
|
const postData = await db.getObjects(keys);
|
||||||
|
postData.forEach((data) => {
|
||||||
|
if (data) {
|
||||||
|
data.data = JSON.parse(data.data);
|
||||||
|
data.data.timestampISO = utils.toISOString(data.data.timestamp);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const uids = postData.map(data => data && data.uid);
|
||||||
|
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
|
||||||
|
postData.forEach((postData, index) => {
|
||||||
|
if (postData) {
|
||||||
|
postData.user = userData[index];
|
||||||
|
postData.data.rawContent = validator.escape(String(postData.data.content));
|
||||||
|
postData.data.title = validator.escape(String(postData.data.title || ''));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(postData.map(p => addMetaData(p)));
|
||||||
|
return postData;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function addMetaData(postData) {
|
||||||
|
if (!postData) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
postData.topic = { cid: 0 };
|
||||||
|
if (postData.data.cid) {
|
||||||
|
postData.topic = { cid: parseInt(postData.data.cid, 10) };
|
||||||
|
} else if (postData.data.tid) {
|
||||||
|
postData.topic = await topics.getTopicFields(postData.data.tid, ['title', 'cid']);
|
||||||
|
}
|
||||||
|
postData.category = await categories.getCategoryData(postData.topic.cid);
|
||||||
|
const result = await plugins.hooks.fire('filter:parse.post', { postData: postData.data });
|
||||||
|
postData.data.content = result.postData.content;
|
||||||
|
}
|
||||||
|
|
||||||
Posts.shouldQueue = async function (uid, data) {
|
Posts.shouldQueue = async function (uid, data) {
|
||||||
const [userData, isMemberOfExempt, categoryQueueEnabled] = await Promise.all([
|
const [userData, isMemberOfExempt, categoryQueueEnabled] = await Promise.all([
|
||||||
user.getUserFields(uid, ['uid', 'reputation', 'postcount']),
|
user.getUserFields(uid, ['uid', 'reputation', 'postcount']),
|
||||||
|
|||||||
Reference in New Issue
Block a user