From 83d2f9ef7828e7afffe14d041b6b79d437b11b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20David=20Mart=C3=ADnez=20Cubillos?= Date: Wed, 10 May 2023 12:34:39 -0500 Subject: [PATCH] FEATURE: Default to subcategory when parent category does not allow posting (#21228) added site toggle functionality through site settings added tests to implemented feature Introduced suggested correction renamed find_new_topic method and deleted click_new_topic_button method --- .../discourse/app/mixins/open-composer.js | 8 ++++ .../app/routes/build-category-route.js | 29 +++++++++++++-- config/locales/server.en.yml | 1 + config/site_settings.yml | 3 ++ .../composer/default_to_subcategory_spec.rb | 37 +++++++++++++++++++ spec/system/page_objects/pages/category.rb | 4 ++ 6 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 spec/system/composer/default_to_subcategory_spec.rb diff --git a/app/assets/javascripts/discourse/app/mixins/open-composer.js b/app/assets/javascripts/discourse/app/mixins/open-composer.js index 1e06c9cf396..128bb838001 100644 --- a/app/assets/javascripts/discourse/app/mixins/open-composer.js +++ b/app/assets/javascripts/discourse/app/mixins/open-composer.js @@ -6,6 +6,14 @@ import { getOwner } from "discourse-common/lib/get-owner"; export default Mixin.create({ openComposer(controller) { let categoryId = controller.get("category.id"); + + if ( + !controller.canCreateTopicOnCategory && + this.siteSettings.default_subcategory_on_read_only_category + ) { + categoryId = controller.get("defaultSubcategory.id"); + } + if ( categoryId && controller.category.isUncategorizedCategory && diff --git a/app/assets/javascripts/discourse/app/routes/build-category-route.js b/app/assets/javascripts/discourse/app/routes/build-category-route.js index 9134bc71947..40bb36abee1 100644 --- a/app/assets/javascripts/discourse/app/routes/build-category-route.js +++ b/app/assets/javascripts/discourse/app/routes/build-category-route.js @@ -153,14 +153,33 @@ export default (filterArg, params) => { setupController(controller, model) { const topics = this.topics, category = model.category, - canCreateTopic = topics.get("can_create_topic"), - canCreateTopicOnCategory = - canCreateTopic && category.get("permission") === PermissionType.FULL; + canCreateTopic = topics.get("can_create_topic"); + + let canCreateTopicOnCategory = + canCreateTopic && category.get("permission") === PermissionType.FULL; + + let defaultSubcategory; + let canCreateTopicOnSubCategory; + + if ( + !canCreateTopicOnCategory && + this.siteSettings.default_subcategory_on_read_only_category + ) { + defaultSubcategory = category.subcategories.find((subcategory) => { + return subcategory.get("permission") === PermissionType.FULL; + }); + + canCreateTopicOnSubCategory = !!defaultSubcategory; + } this.controllerFor("navigation/category").setProperties({ canCreateTopicOnCategory, - cannotCreateTopicOnCategory: !canCreateTopicOnCategory, + cannotCreateTopicOnCategory: !( + canCreateTopicOnCategory || canCreateTopicOnSubCategory + ), canCreateTopic, + canCreateTopicOnSubCategory, + defaultSubcategory, }); let topicOpts = { @@ -174,6 +193,8 @@ export default (filterArg, params) => { expandAllPinned: true, canCreateTopic, canCreateTopicOnCategory, + canCreateTopicOnSubCategory, + defaultSubcategory, }; const p = category.get("params"); diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 20c826ce50b..96bd778bf44 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1554,6 +1554,7 @@ en: post_excerpt_maxlength: "Maximum length of a post excerpt / summary." topic_excerpt_maxlength: "Maximum length of a topic excerpt / summary, generated from the first post in a topic." + default_subcategory_on_read_only_category: "Enables 'New Topic' button and selects a default subcategory to post on categories where the user is not allowed to create a new topic." show_pinned_excerpt_mobile: "Show excerpt on pinned topics in mobile view." show_pinned_excerpt_desktop: "Show excerpt on pinned topics in desktop view." post_onebox_maxlength: "Maximum length of a oneboxed Discourse post in characters." diff --git a/config/site_settings.yml b/config/site_settings.yml index 6f99c27d387..efdcc93adb9 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -958,6 +958,9 @@ posting: ja: 120 zh_CN: 120 zh_TW: 120 + default_subcategory_on_read_only_category: + client: true + default: false show_pinned_excerpt_mobile: client: true default: true diff --git a/spec/system/composer/default_to_subcategory_spec.rb b/spec/system/composer/default_to_subcategory_spec.rb new file mode 100644 index 00000000000..7de89c9fcd1 --- /dev/null +++ b/spec/system/composer/default_to_subcategory_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +describe "Default to Subcategory when parent Category doesn't allow posting", + type: :system, + js: true do + fab!(:user) { Fabricate(:user) } + fab!(:group) { Fabricate(:group) } + fab!(:group_user) { Fabricate(:group_user, user: user, group: group) } + fab!(:category) { Fabricate(:private_category, group: group, permission_type: 3) } + fab!(:subcategory) do + Fabricate(:private_category, parent_category_id: category.id, group: group, permission_type: 1) + end + let(:category_page) { PageObjects::Pages::Category.new } + before { sign_in(user) } + + describe "Setting enabled and can't post on parent category" do + before { SiteSetting.default_subcategory_on_read_only_category = true } + + it "should have 'New Topic' button enabled and default Subcategory set in the composer" do + category_page.visit(category) + expect(category_page).to have_button("New Topic", disabled: false) + category_page.new_topic_button.click + select_kit = + PageObjects::Components::SelectKit.new(page.find("#reply-control.open .category-chooser")) + expect(select_kit).to have_selected_value(subcategory.id) + end + end + + describe "Setting disabled and can't post on parent category" do + before { SiteSetting.default_subcategory_on_read_only_category = false } + + it "should have 'New Topic' button disabled" do + category_page.visit(category) + expect(category_page).to have_button("New Topic", disabled: true) + end + end +end diff --git a/spec/system/page_objects/pages/category.rb b/spec/system/page_objects/pages/category.rb index fb0669fcecf..6862fc4199c 100644 --- a/spec/system/page_objects/pages/category.rb +++ b/spec/system/page_objects/pages/category.rb @@ -65,6 +65,10 @@ module PageObjects find(".select-category-template").click end + def new_topic_button + find("#create-topic") + end + CATEGORY_NAVIGATION_NEW_NAV_ITEM_SELECTOR = ".category-navigation .nav-item_new" def has_no_new_topics?