Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Show subcategory channels in category channel list (#37936)

Martin Brennan7d56e89a6b92
diff --git a/plugins/chat/app/controllers/chat/api/channels_controller.rb b/plugins/chat/app/controllers/chat/api/channels_controller.rb
index 30772dd5590..9c0127d1a4a 100644
--- a/plugins/chat/app/controllers/chat/api/channels_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channels_controller.rb
@@ -5,7 +5,16 @@ class Chat::Api::ChannelsController < Chat::ApiController
CATEGORY_CHANNEL_EDITABLE_PARAMS = %i[auto_join_users allow_channel_wide_mentions]
def index
- permitted = params.permit(:filter, :limit, :offset, :status, :chatable_id, :chatable_type)
+ permitted =
+ params.permit(
+ :filter,
+ :limit,
+ :offset,
+ :status,
+ :chatable_id,
+ :chatable_type,
+ :include_subcategories,
+ )
options = { filter: permitted[:filter], limit: (permitted[:limit] || 25).to_i }
options[:offset] = permitted[:offset].to_i
@@ -13,6 +22,12 @@ class Chat::Api::ChannelsController < Chat::ApiController
options[:chatable_id] = permitted[:chatable_id]
options[:chatable_type] = permitted[:chatable_type]
+ if options[:chatable_type] == "Category"
+ options[:include_subcategories] = ActiveModel::Type::Boolean.new.cast(
+ permitted[:include_subcategories],
+ )
+ end
+
memberships = Chat::ChannelMembershipManager.all_for_user(current_user)
channels = Chat::ChannelFetcher.secured_public_channels(guardian, options)
serialized_channels =
diff --git a/plugins/chat/assets/javascripts/discourse/components/upsert-category/chat.gjs b/plugins/chat/assets/javascripts/discourse/components/upsert-category/chat.gjs
index c440cf323fe..56a419604ea 100644
--- a/plugins/chat/assets/javascripts/discourse/components/upsert-category/chat.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/upsert-category/chat.gjs
@@ -1,21 +1,23 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
+import { array } from "@ember/helper";
+import { on } from "@ember/modifier";
import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import { LinkTo } from "@ember/routing";
import { htmlSafe } from "@ember/template";
import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
+import DToggleSwitch from "discourse/components/d-toggle-switch";
import EmptyState from "discourse/components/empty-state";
+import categoryBadge from "discourse/helpers/category-badge";
import icon from "discourse/helpers/d-icon";
import replaceEmoji from "discourse/helpers/replace-emoji";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import getURL from "discourse/lib/get-url";
+import { or } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
-function channelSettingsUrl(channel) {
- return getURL(`/chat/c/${channel.slug || "-"}/${channel.id}/info/settings`);
-}
-
function channelColorStyle(channel) {
return htmlSafe(`color: #${channel.chatable?.color || "000"}`);
}
@@ -24,16 +26,49 @@ function channelIcon(channel) {
return channel.emoji ? replaceEmoji(`:${channel.emoji}:`) : icon("d-chat");
}
+function isSubcategoryChannel(channel, categoryId) {
+ return channel.chatable?.id !== categoryId;
+}
+
export default class EditCategoryChat extends Component {
@tracked channels = null;
@tracked loading = true;
+ @tracked showSubcategoryChannels = true;
+
+ get categoryChannels() {
+ return (this.channels || []).filter(
+ (c) => !isSubcategoryChannel(c, this.args.category.id)
+ );
+ }
+
+ get subcategoryChannels() {
+ return (this.channels || []).filter((c) =>
+ isSubcategoryChannel(c, this.args.category.id)
+ );
+ }
+
+ get hasSubcategoryChannels() {
+ return this.subcategoryChannels.length > 0;
+ }
+
+ get displayedChannels() {
+ if (this.showSubcategoryChannels) {
+ return this.channels || [];
+ }
+ return this.categoryChannels;
+ }
+
+ @action
+ toggleSubcategoryChannels() {
+ this.showSubcategoryChannels = !this.showSubcategoryChannels;
+ }
@action
async loadChannels() {
try {
this.loading = true;
const result = await ajax(
- `/chat/api/channels?chatable_id=${this.args.category.id}&chatable_type=Category`
+ `/chat/api/channels?chatable_id=${this.args.category.id}&chatable_type=Category&include_subcategories=true`
);
this.channels = (result.channels || []).sort((a, b) =>
(a.title || "").localeCompare(b.title || "")
@@ -49,7 +84,16 @@ export default class EditCategoryChat extends Component {
<template>
<section class="edit-category-chat" {{didInsert this.loadChannels}}>
<ConditionalLoadingSpinner @condition={{this.loading}}>
- {{#if this.channels.length}}
+ {{#if this.displayedChannels.length}}
+ {{#if this.hasSubcategoryChannels}}
+ <div class="edit-category-chat__subcategory-toggle">
+ <DToggleSwitch
+ @state={{this.showSubcategoryChannels}}
+ {{on "click" this.toggleSubcategoryChannels}}
+ />
+ <span>{{i18n "chat.edit_category.include_subcategories"}}</span>
+ </div>
+ {{/if}}
<table class="d-table edit-category-chat__table">
<thead class="d-table__header">
<tr class="d-table__row">
@@ -63,7 +107,7 @@ export default class EditCategoryChat extends Component {
</tr>
</thead>
<tbody>
- {{#each this.channels as |channel|}}
+ {{#each this.displayedChannels as |channel|}}
<tr class="d-table__row" data-channel-id={{channel.id}}>
<td class="d-table__cell edit-category-chat__channel-name">
<span
@@ -73,6 +117,11 @@ export default class EditCategoryChat extends Component {
{{channelIcon channel}}
</span>
<span>{{channel.title}}</span>
+ {{#if (isSubcategoryChannel channel @category.id)}}
+ <div>
+ {{categoryBadge channel.chatable link=true}}
+ </div>
+ {{/if}}
</td>
<td
class="d-table__cell edit-category-chat__channel-description"
@@ -82,13 +131,13 @@ export default class EditCategoryChat extends Component {
<td
class="d-table__cell edit-category-chat__channel-actions d-table__cell-actions"
>
- <a
- href={{channelSettingsUrl channel}}
+ <LinkTo
+ @route="chat.channel.info.settings"
+ @models={{array (or channel.slug "-") channel.id}}
class="btn btn-default btn-small"
>
- {{icon "gear"}}
{{i18n "chat.edit_category.settings"}}
- </a>
+ </LinkTo>
</td>
</tr>
{{/each}}
diff --git a/plugins/chat/assets/stylesheets/common/edit-category-chat.scss b/plugins/chat/assets/stylesheets/common/edit-category-chat.scss
index 20b96360fc4..63f53065065 100644
--- a/plugins/chat/assets/stylesheets/common/edit-category-chat.scss
+++ b/plugins/chat/assets/stylesheets/common/edit-category-chat.scss
@@ -9,4 +9,11 @@
&__empty {
color: var(--primary-medium);
}
+
+ &__subcategory-toggle {
+ display: flex;
+ align-items: center;
+ gap: 0.5em;
+ margin-bottom: 1em;
+ }
}
diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml
index be412a4ee0d..47f254f5f44 100644
--- a/plugins/chat/config/locales/client.en.yml
+++ b/plugins/chat/config/locales/client.en.yml
@@ -46,7 +46,8 @@ en:
actions: "Actions"
settings: "Settings"
no_channels: "There are no chat channels associated with this category"
- no_channels_body: "You can have multiple channels per category, accessible to the same members based on the category’s Security settings. <a href='%{chatBrowseUrl}'>Create a chat channel</a>."
+ no_channels_body: "You can have multiple channels per category, accessible to the same members based on the category's Security settings. <a href='%{chatBrowseUrl}'>Create a chat channel</a>."
+ include_subcategories: "Include subcategory channels"
text_copied: Text copied to clipboard
link_copied: Link copied to clipboard
back_to_forum: "Forum"
diff --git a/plugins/chat/lib/chat/channel_fetcher.rb b/plugins/chat/lib/chat/channel_fetcher.rb
index bb21fe41150..fecdf75dba8 100644
--- a/plugins/chat/lib/chat/channel_fetcher.rb
+++ b/plugins/chat/lib/chat/channel_fetcher.rb
@@ -119,11 +119,15 @@ module Chat
if options[:chatable_id].present? && options[:chatable_type].present?
filter_chatable = options[:chatable_type].constantize&.find_by_id(options[:chatable_id])
if filter_chatable.present? && guardian.can_see_chatable?(filter_chatable)
+ chatable_ids =
+ if options[:include_subcategories] && options[:chatable_type] == "Category"
+ Category.subcategory_ids(options[:chatable_id].to_i)
+ else
+ [options[:chatable_id]]
+ end
+
channels =
- channels.where(
- chatable_id: options[:chatable_id],
- chatable_type: options[:chatable_type],
- )
+ channels.where(chatable_id: chatable_ids, chatable_type: options[:chatable_type])
end
end
diff --git a/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb b/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb
index 91785e2afbc..8b99805242c 100644
--- a/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb
+++ b/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb
@@ -374,6 +374,46 @@ describe Chat::ChannelFetcher do
).map(&:id),
).to contain_exactly(category_channel.id, other_channel.id)
end
+
+ context "with include_subcategories" do
+ fab!(:subcategory) { Fabricate(:category, parent_category: category) }
+ fab!(:subcategory_channel) { Fabricate(:category_channel, chatable: subcategory) }
+
+ it "returns channels from parent and subcategories when true" do
+ expect(
+ described_class.secured_public_channels(
+ guardian,
+ following: following,
+ chatable_id: category.id,
+ chatable_type: "Category",
+ include_subcategories: true,
+ ).map(&:id),
+ ).to contain_exactly(category_channel.id, subcategory_channel.id)
+ end
+
+ it "returns only the parent category channels when false" do
+ expect(
+ described_class.secured_public_channels(
+ guardian,
+ following: following,
+ chatable_id: category.id,
+ chatable_type: "Category",
+ include_subcategories: false,
+ ).map(&:id),
+ ).to eq([category_channel.id])
+ end
+
+ it "returns only the parent category channels when absent" do
+ expect(
+ described_class.secured_public_channels(
+ guardian,
+ following: following,
+ chatable_id: category.id,
+ chatable_type: "Category",
+ ).map(&:id),
+ ).to eq([category_channel.id])
+ end
+ end
end
context "when scoping to the user's channel memberships" do
diff --git a/plugins/chat/spec/requests/chat/api/channels_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channels_controller_spec.rb
index abebd5a2b9c..4085570959b 100644
--- a/plugins/chat/spec/requests/chat/api/channels_controller_spec.rb
+++ b/plugins/chat/spec/requests/chat/api/channels_controller_spec.rb
@@ -166,6 +166,37 @@ RSpec.describe Chat::Api::ChannelsController do
channel_2.id,
)
end
+
+ context "with include_subcategories" do
+ fab!(:subcategory) { Fabricate(:category, parent_category: category_1) }
+ fab!(:subcategory_channel) { Fabricate(:category_channel, chatable: subcategory) }
+
+ it "returns channels from parent and subcategories" do
+ get "/chat/api/channels",
+ params: {
+ chatable_id: category_1.id,
+ chatable_type: "Category",
+ include_subcategories: true,
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["channels"].map { |c| c["id"] }).to contain_exactly(
+ channel_1.id,
+ subcategory_channel.id,
+ )
+ end
+
+ it "returns only parent category channels without the param" do
+ get "/chat/api/channels",
+ params: {
+ chatable_id: category_1.id,
+ chatable_type: "Category",
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["channels"].map { |c| c["id"] }).to eq([channel_1.id])
+ end
+ end
end
end
end
diff --git a/plugins/chat/spec/system/admin/category_channel_list_spec.rb b/plugins/chat/spec/system/admin/category_channel_list_spec.rb
index 31557f5d508..6e4a85f2e71 100644
--- a/plugins/chat/spec/system/admin/category_channel_list_spec.rb
+++ b/plugins/chat/spec/system/admin/category_channel_list_spec.rb
@@ -54,4 +54,45 @@ RSpec.describe "Admin category channels list", type: :system do
expect(page).to have_css(".c-channel-settings")
end
end
+
+ context "when a category has subcategories with chat channels" do
+ fab!(:subcategory) { Fabricate(:category, parent_category: category) }
+ fab!(:subcategory_channel) { Fabricate(:chat_channel, chatable: subcategory) }
+
+ it "shows subcategory channels by default with toggle visible" do
+ visit "#{category.slug_url_without_id}/edit/chat"
+
+ expect(page).to have_css(".edit-category-chat__subcategory-toggle")
+ expect(page).to have_css(".d-table__row[data-channel-id='#{channel_1.id}']")
+ expect(page).to have_css(".d-table__row[data-channel-id='#{channel_2.id}']")
+ expect(page).to have_css(".d-table__row[data-channel-id='#{subcategory_channel.id}']")
+ expect(page.find(".d-table__row[data-channel-id='#{subcategory_channel.id}']")).to have_css(
+ ".badge-category__wrapper .badge-category__name",
+ text: subcategory.name,
+ )
+ end
+
+ it "hides subcategory channels when toggle is clicked" do
+ visit "#{category.slug_url_without_id}/edit/chat"
+
+ expect(page).to have_css(".d-table__row[data-channel-id='#{subcategory_channel.id}']")
+
+ PageObjects::Components::DToggleSwitch.new(
+ ".edit-category-chat__subcategory-toggle .d-toggle-switch__checkbox",
+ ).toggle
+
+ expect(page).to have_no_css(".d-table__row[data-channel-id='#{subcategory_channel.id}']")
+ expect(page).to have_css(".d-table__row[data-channel-id='#{channel_1.id}']")
+ expect(page).to have_css(".d-table__row[data-channel-id='#{channel_2.id}']")
+ end
+ end
+
+ context "when a category has no subcategory channels" do
+ it "does not show the subcategory toggle" do
+ visit "#{category.slug_url_without_id}/edit/chat"
+
+ expect(page).to have_css(".d-table__row[data-channel-id='#{channel_1.id}']")
+ expect(page).to have_no_css(".edit-category-chat__subcategory-toggle")
+ end
+ end
end