From c718b7293ecb76533b5914ef531e0f51f30f634a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 24 Jun 2020 16:02:57 -0400 Subject: [PATCH] feat: #3783, min/max tags per category --- .../components/schemas/CategoryObject.yaml | 8 ++- public/openapi/read.yaml | 8 +++ src/categories/create.js | 2 + src/categories/data.js | 21 +++++++- src/posts/edit.js | 2 + src/socket.io/posts/edit.js | 4 -- src/topics/create.js | 2 +- src/topics/index.js | 2 + src/topics/tags.js | 15 +++++- src/views/admin/manage/category.tpl | 26 ++++++++-- test/topics.js | 52 +++++++++++++++++++ 11 files changed, 130 insertions(+), 12 deletions(-) diff --git a/public/openapi/components/schemas/CategoryObject.yaml b/public/openapi/components/schemas/CategoryObject.yaml index fa8af2d41d..1599975b3c 100644 --- a/public/openapi/components/schemas/CategoryObject.yaml +++ b/public/openapi/components/schemas/CategoryObject.yaml @@ -62,4 +62,10 @@ CategoryObject: description: The number of posts in the category totalTopicCount: type: number - description: The number of topics in the category \ No newline at end of file + description: The number of topics in the category + minTags: + type: number + description: Minimum tags per topic in this category + maxTags: + type: number + description: Maximum tags per topic in this category \ No newline at end of file diff --git a/public/openapi/read.yaml b/public/openapi/read.yaml index 11d072ed54..8d8dadd8e8 100644 --- a/public/openapi/read.yaml +++ b/public/openapi/read.yaml @@ -3130,6 +3130,10 @@ paths: type: number totalTopicCount: type: number + minTags: + type: number + maxTags: + type: number /api/categories: get: tags: @@ -3655,6 +3659,10 @@ paths: type: array items: type: string + minTags: + type: number + maxTags: + type: number thread_tools: type: array items: diff --git a/src/categories/create.js b/src/categories/create.js index 83b84b023e..b724ef031b 100644 --- a/src/categories/create.js +++ b/src/categories/create.js @@ -143,6 +143,8 @@ module.exports = function (Categories) { destination.class = source.class; destination.image = source.image; destination.imageClass = source.imageClass; + destination.minTags = source.minTags; + destination.maxTags = source.maxTags; if (copyParent) { destination.parentCid = source.parentCid || 0; diff --git a/src/categories/data.js b/src/categories/data.js index 47b4a9e2b9..832ed4ca97 100644 --- a/src/categories/data.js +++ b/src/categories/data.js @@ -1,12 +1,14 @@ 'use strict'; -var validator = require('validator'); +const validator = require('validator'); -var db = require('../database'); +const db = require('../database'); +const meta = require('../meta'); const intFields = [ 'cid', 'parentCid', 'disabled', 'isSection', 'order', 'topic_count', 'post_count', 'numRecentReplies', + 'minTags', 'maxTags', ]; module.exports = function (Categories) { @@ -59,6 +61,21 @@ function modifyCategory(category, fields) { return; } + if (!fields.length || fields.includes('minTags')) { + const useDefault = !category.hasOwnProperty('minTags') || + category.minTags === null || + category.minTags === '' || + !parseInt(category.minTags, 10); + category.minTags = useDefault ? meta.config.minimumTagsPerTopic : category.minTags; + } + if (!fields.length || fields.includes('maxTags')) { + const useDefault = !category.hasOwnProperty('maxTags') || + category.maxTags === null || + category.maxTags === '' || + !parseInt(category.maxTags, 10); + category.maxTags = useDefault ? meta.config.maximumTagsPerTopic : category.maxTags; + } + db.parseIntFields(category, intFields, fields); if (category.hasOwnProperty('name')) { diff --git a/src/posts/edit.js b/src/posts/edit.js index 67d0b898e5..7adc5ba2de 100644 --- a/src/posts/edit.js +++ b/src/posts/edit.js @@ -117,6 +117,8 @@ module.exports = function (Posts) { throw new Error('[[error:no-privileges]]'); } } + await topics.validateTags(data.tags, topicData.cid); + const results = await plugins.fireHook('filter:topic.edit', { req: data.req, topic: newTopicData, data: data }); await db.setObject('topic:' + tid, results.topic); await topics.updateTopicTags(tid, data.tags); diff --git a/src/socket.io/posts/edit.js b/src/socket.io/posts/edit.js index 6499ea3eea..c4dfa1365a 100644 --- a/src/socket.io/posts/edit.js +++ b/src/socket.io/posts/edit.js @@ -25,10 +25,6 @@ module.exports = function (SocketPosts) { throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'); } else if (data.title && data.title.length > meta.config.maximumTitleLength) { throw new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'); - } else if (data.tags && data.tags.length < meta.config.minimumTagsPerTopic) { - throw new Error('[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]'); - } else if (data.tags && data.tags.length > meta.config.maximumTagsPerTopic) { - throw new Error('[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]'); } else if (meta.config.minimumPostLength !== 0 && contentLen < meta.config.minimumPostLength) { throw new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'); } else if (contentLen > meta.config.maximumPostLength) { diff --git a/src/topics/create.js b/src/topics/create.js index 884446a549..6943e4204d 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -68,7 +68,7 @@ module.exports = function (Topics) { data.content = utils.rtrim(data.content); } check(data.title, meta.config.minimumTitleLength, meta.config.maximumTitleLength, 'title-too-short', 'title-too-long'); - check(data.tags, meta.config.minimumTagsPerTopic, meta.config.maximumTagsPerTopic, 'not-enough-tags', 'too-many-tags'); + await Topics.validateTags(data.tags, data.cid); check(data.content, meta.config.minimumPostLength, meta.config.maximumPostLength, 'content-too-short', 'content-too-long'); const [categoryExists, canCreate, canTag] = await Promise.all([ diff --git a/src/topics/index.js b/src/topics/index.js index 6a63680d89..6d44360565 100644 --- a/src/topics/index.js +++ b/src/topics/index.js @@ -160,6 +160,8 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, rev topicData.posts = posts; topicData.category = category; topicData.tagWhitelist = tagWhitelist[0]; + topicData.minTags = category.minTags; + topicData.maxTags = category.maxTags; topicData.thread_tools = threadTools.tools; topicData.isFollowing = followData[0].following; topicData.isNotFollowing = !followData[0].following && !followData[0].ignoring; diff --git a/src/topics/tags.js b/src/topics/tags.js index 88a8f3a69b..db49719e14 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -19,7 +19,7 @@ module.exports = function (Topics) { return; } const result = await plugins.fireHook('filter:tags.filter', { tags: tags, tid: tid }); - tags = _.uniq(result.tags).slice(0, meta.config.maximumTagsPerTopic || 5) + tags = _.uniq(result.tags) .map(tag => utils.cleanUpTag(tag, meta.config.maximumTagLength)) .filter(tag => tag && tag.length >= (meta.config.minimumTagLength || 3)); @@ -32,6 +32,19 @@ module.exports = function (Topics) { await Promise.all(tags.map(tag => updateTagCount(tag))); }; + Topics.validateTags = async function (tags, cid) { + if (!Array.isArray(tags)) { + throw new Error('[[error:invalid-data]]'); + } + tags = _.uniq(tags); + const categoryData = await categories.getCategoryFields(cid, ['minTags', 'maxTags']); + if (tags.length < parseInt(categoryData.minTags, 10)) { + throw new Error('[[error:not-enough-tags, ' + categoryData.minTags + ']]'); + } else if (tags.length > parseInt(categoryData.maxTags, 10)) { + throw new Error('[[error:too-many-tags, ' + categoryData.maxTags + ']]'); + } + }; + async function filterCategoryTags(tags, tid) { const cid = await Topics.getTopicField(tid, 'cid'); const tagWhitelist = await categories.getTagWhitelist([cid]); diff --git a/src/views/admin/manage/category.tpl b/src/views/admin/manage/category.tpl index 776bb0e1de..1d862db40e 100644 --- a/src/views/admin/manage/category.tpl +++ b/src/views/admin/manage/category.tpl @@ -95,9 +95,29 @@ -
-
- +
+
+
+ + +
+
+
+
+ + +
+
+
+
+
+
+ +
diff --git a/test/topics.js b/test/topics.js index 06c25afa1b..4aede4c9d9 100644 --- a/test/topics.js +++ b/test/topics.js @@ -1782,6 +1782,58 @@ describe('Topic\'s', function () { tags = await topics.getTopicTags(tid); assert.deepStrictEqual(tags, ['tag2', 'tag4', 'tag6']); }); + + it('should respect minTags', async () => { + const oldValue = meta.config.minimumTagsPerTopic; + meta.config.minimumTagsPerTopic = 2; + let err; + try { + await topics.post({ uid: adminUid, tags: ['tag4'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); + } catch (_err) { + err = _err; + } + assert.equal(err.message, '[[error:not-enough-tags, ' + meta.config.minimumTagsPerTopic + ']]'); + meta.config.minimumTagsPerTopic = oldValue; + }); + + it('should respect maxTags', async () => { + const oldValue = meta.config.maximumTagsPerTopic; + meta.config.maximumTagsPerTopic = 2; + let err; + try { + await topics.post({ uid: adminUid, tags: ['tag1', 'tag2', 'tag3'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); + } catch (_err) { + err = _err; + } + assert.equal(err.message, '[[error:too-many-tags, ' + meta.config.maximumTagsPerTopic + ']]'); + meta.config.maximumTagsPerTopic = oldValue; + }); + + it('should respect minTags per category', async () => { + const minTags = 2; + await categories.setCategoryField(topic.categoryId, 'minTags', minTags); + let err; + try { + await topics.post({ uid: adminUid, tags: ['tag4'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); + } catch (_err) { + err = _err; + } + assert.equal(err.message, '[[error:not-enough-tags, ' + minTags + ']]'); + await db.deleteObjectField('category:' + topic.categoryId, 'minTags'); + }); + + it('should respect maxTags per category', async () => { + const maxTags = 2; + await categories.setCategoryField(topic.categoryId, 'maxTags', maxTags); + let err; + try { + await topics.post({ uid: adminUid, tags: ['tag1', 'tag2', 'tag3'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); + } catch (_err) { + err = _err; + } + assert.equal(err.message, '[[error:too-many-tags, ' + maxTags + ']]'); + await db.deleteObjectField('category:' + topic.categoryId, 'maxTags'); + }); }); describe('follow/unfollow', function () {