From 1eb5fabdb11d0f9119081ab2e95d569717f5b3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 30 Nov 2020 22:36:30 -0500 Subject: [PATCH] feat: #8900, postQueue setting for category --- install/data/defaults.json | 1 + .../en-GB/admin/manage/categories.json | 1 + src/api/topics.js | 1 - src/categories/data.js | 13 +++--- src/controllers/admin/categories.js | 2 + src/posts/queue.js | 32 +++++++++++++-- src/views/admin/manage/category.tpl | 40 +++++++++++++------ 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/install/data/defaults.json b/install/data/defaults.json index aee61b65df..c33e951044 100644 --- a/install/data/defaults.json +++ b/install/data/defaults.json @@ -20,6 +20,7 @@ "chatDeleteDuration": 0, "chatMessageDelay": 200, "newbiePostDelayThreshold": 3, + "postQueue": 0, "postQueueReputationThreshold": 0, "groupsExemptFromPostQueue": ["administrators", "Global Moderators"], "minimumPostLength": 8, diff --git a/public/language/en-GB/admin/manage/categories.json b/public/language/en-GB/admin/manage/categories.json index 0c0992fca5..1144e16433 100644 --- a/public/language/en-GB/admin/manage/categories.json +++ b/public/language/en-GB/admin/manage/categories.json @@ -11,6 +11,7 @@ "num-recent-replies": "# of Recent Replies", "ext-link": "External Link", "is-section": "Treat this category as a section", + "post-queue": "Post queue", "tag-whitelist": "Tag Whitelist", "upload-image": "Upload Image", "delete-image": "Remove", diff --git a/src/api/topics.js b/src/api/topics.js index cca32a6818..98d92022a8 100644 --- a/src/api/topics.js +++ b/src/api/topics.js @@ -21,7 +21,6 @@ topicsAPI.create = async function (caller, data) { const payload = { ...data }; payload.tags = payload.tags || []; payload.uid = caller.uid; - payload.uid = caller.uid; payload.req = apiHelpers.buildReqObject(caller); payload.timestamp = Date.now(); payload.fromQueue = false; diff --git a/src/categories/data.js b/src/categories/data.js index 4e3dbb1365..e75b825cb9 100644 --- a/src/categories/data.js +++ b/src/categories/data.js @@ -5,11 +5,12 @@ const validator = require('validator'); const db = require('../database'); const meta = require('../meta'); const plugins = require('../plugins'); +const utils = require('../utils'); const intFields = [ 'cid', 'parentCid', 'disabled', 'isSection', 'order', 'topic_count', 'post_count', 'numRecentReplies', - 'minTags', 'maxTags', + 'minTags', 'maxTags', 'postQueue', ]; module.exports = function (Categories) { @@ -63,12 +64,13 @@ module.exports = function (Categories) { }; }; -function defaultMinMaxTags(category, fields, fieldName, defaultField) { +function defaultIntField(category, fields, fieldName, defaultField) { if (!fields.length || fields.includes(fieldName)) { const useDefault = !category.hasOwnProperty(fieldName) || category[fieldName] === null || category[fieldName] === '' || - !parseInt(category[fieldName], 10); + !utils.isNumber(category[fieldName]); + category[fieldName] = useDefault ? meta.config[defaultField] : category[fieldName]; } } @@ -78,8 +80,9 @@ function modifyCategory(category, fields) { return; } - defaultMinMaxTags(category, fields, 'minTags', 'minimumTagsPerTopic'); - defaultMinMaxTags(category, fields, 'maxTags', 'maximumTagsPerTopic'); + defaultIntField(category, fields, 'minTags', 'minimumTagsPerTopic'); + defaultIntField(category, fields, 'maxTags', 'maximumTagsPerTopic'); + defaultIntField(category, fields, 'postQueue', 'postQueue'); db.parseIntFields(category, intFields, fields); diff --git a/src/controllers/admin/categories.js b/src/controllers/admin/categories.js index 796b36de25..b32b281659 100644 --- a/src/controllers/admin/categories.js +++ b/src/controllers/admin/categories.js @@ -4,6 +4,7 @@ const categories = require('../../categories'); const analytics = require('../../analytics'); const plugins = require('../../plugins'); const translator = require('../../translator'); +const meta = require('../../meta'); const categoriesController = module.exports; @@ -42,6 +43,7 @@ categoriesController.get = async function (req, res, next) { categories: data.allCategories, selectedCategory: selectedCategory, customClasses: data.customClasses, + postQueueEnabled: !!meta.config.postQueue, }); }; diff --git a/src/posts/queue.js b/src/posts/queue.js index bd8121399e..73c6fbc261 100644 --- a/src/posts/queue.js +++ b/src/posts/queue.js @@ -15,9 +15,15 @@ const socketHelpers = require('../socket.io/helpers'); module.exports = function (Posts) { Posts.shouldQueue = async function (uid, data) { - const userData = await user.getUserFields(uid, ['uid', 'reputation', 'postcount']); - const isMemberOfExempt = await groups.isMemberOfAny(userData.uid, meta.config.groupsExemptFromPostQueue); - const shouldQueue = meta.config.postQueue && !isMemberOfExempt && (!userData.uid || userData.reputation < meta.config.postQueueReputationThreshold || userData.postcount <= 0); + const [userData, isMemberOfExempt, categoryQueueEnabled] = await Promise.all([ + user.getUserFields(uid, ['uid', 'reputation', 'postcount']), + groups.isMemberOfAny(uid, meta.config.groupsExemptFromPostQueue), + isCategoryQueueEnabled(data), + ]); + + const shouldQueue = meta.config.postQueue && categoryQueueEnabled && + !isMemberOfExempt && + (!userData.uid || userData.reputation < meta.config.postQueueReputationThreshold || userData.postcount <= 0); const result = await plugins.hooks.fire('filter:post.shouldQueue', { shouldQueue: !!shouldQueue, uid: uid, @@ -26,6 +32,24 @@ module.exports = function (Posts) { return result.shouldQueue; }; + async function isCategoryQueueEnabled(data) { + const type = getType(data); + const cid = await getCid(type, data); + if (!cid) { + throw new Error('[[error:invalid-cid]]'); + } + return await categories.getCategoryField(cid, 'postQueue'); + } + + function getType(data) { + if (data.tid && data.content) { + return 'reply'; + } else if (data.cid && data.title && data.content) { + return 'topic'; + } + throw new Error('[[error:invalid-type]]'); + } + async function removeQueueNotification(id) { await notifications.rescind('post-queue-' + id); const data = await getParsedObject(id); @@ -46,7 +70,7 @@ module.exports = function (Posts) { } Posts.addToQueue = async function (data) { - const type = data.title ? 'topic' : 'reply'; + const type = getType(data); const now = Date.now(); const id = type + '-' + now; await canPost(type, data); diff --git a/src/views/admin/manage/category.tpl b/src/views/admin/manage/category.tpl index 48fb985066..332b82adf9 100644 --- a/src/views/admin/manage/category.tpl +++ b/src/views/admin/manage/category.tpl @@ -80,16 +80,6 @@ -
-
-
- -
-
-
@@ -111,9 +101,35 @@
-
- +
+
+ +
+
+
+
+
+
+
+ +
+
+
+ {{{ if postQueueEnabled }}} +
+
+
+ +
+
+ {{{ end }}}