From 36f2021186a96f4bff7490af4fae25922a281e28 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 12 Feb 2021 12:25:27 -0500 Subject: [PATCH] refactor: move post queue retrival code to posts.getQueuedPosts --- src/controllers/mods.js | 49 +++-------------------------------------- src/posts/queue.js | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/controllers/mods.js b/src/controllers/mods.js index 33538a6ad4..11330db6f9 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -1,11 +1,7 @@ 'use strict'; -const validator = require('validator'); - -const db = require('../database'); const user = require('../user'); -const topics = require('../topics'); -const categories = require('../categories'); +const posts = require('../posts'); const flags = require('../flags'); const analytics = require('../analytics'); const plugins = require('../plugins'); @@ -160,8 +156,8 @@ modsController.postQueue = async function (req, res, next) { const page = parseInt(req.query.page, 10) || 1; const postsPerPage = 20; - const [ids, isAdminOrGlobalMod, moderatedCids, categoriesData] = await Promise.all([ - db.getSortedSetRange('post:queue', 0, -1), + let postData = await posts.getQueuedPosts(); + const [isAdminOrGlobalMod, moderatedCids, categoriesData] = await Promise.all([ user.isAdminOrGlobalMod(req.uid), user.getModeratedCids(req.uid), helpers.getSelectedCategory(cid), @@ -171,7 +167,6 @@ modsController.postQueue = async function (req, res, next) { return next(); } - let postData = await getQueuedPosts(ids); postData = postData.filter(p => p && (!categoriesData.selectedCids.length || categoriesData.selectedCids.includes(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]]' }]), }); }; - -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; -} diff --git a/src/posts/queue.js b/src/posts/queue.js index 19fc0b1131..a5ce03e9a5 100644 --- a/src/posts/queue.js +++ b/src/posts/queue.js @@ -1,6 +1,7 @@ 'use strict'; const _ = require('lodash'); +const validator = require('validator'); const db = require('../database'); const user = require('../user'); @@ -11,9 +12,49 @@ const categories = require('../categories'); const notifications = require('../notifications'); const privileges = require('../privileges'); const plugins = require('../plugins'); +const utils = require('../utils'); const socketHelpers = require('../socket.io/helpers'); 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) { const [userData, isMemberOfExempt, categoryQueueEnabled] = await Promise.all([ user.getUserFields(uid, ['uid', 'reputation', 'postcount']),