Skip to content

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

DEV: Handle both string and object tag formats (#37207)

Krzysztof Kotlarek45538c7c11e6
diff --git a/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/modal/edit-rule.gjs b/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/modal/edit-rule.gjs
index e9952573b09..6a5f5b2954b 100644
--- a/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/modal/edit-rule.gjs
+++ b/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/modal/edit-rule.gjs
@@ -6,16 +6,23 @@ import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import DModal from "discourse/components/d-modal";
import { popupAjaxError } from "discourse/lib/ajax-error";
+import { bind } from "discourse/lib/decorators";
import CategoryChooser from "discourse/select-kit/components/category-chooser";
import ComboBox from "discourse/select-kit/components/combo-box";
import TagChooser from "discourse/select-kit/components/tag-chooser";
import { eq } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
+import getTagName from "../../lib/get-tag-name";
import ChannelData from "../channel-data";
export default class EditRule extends Component {
@service siteSettings;
+ @bind
+ onTagsChanged(tags) {
+ this.args.model.rule.tags = (tags || []).map((tag) => getTagName(tag));
+ }
+
@action
async save(rule) {
try {
@@ -200,7 +207,7 @@ export default class EditRule extends Component {
@name="tags"
@tags={{@model.rule.tags}}
@everyTag="true"
- @onChange={{fn (mut @model.rule.tags)}}
+ @onChange={{this.onTagsChanged}}
/>
</td>
</tr>
diff --git a/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/rule-row.gjs b/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/rule-row.gjs
index e7ff9d344e5..b29bc5a13b6 100644
--- a/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/rule-row.gjs
+++ b/plugins/discourse-chat-integration/admin/assets/javascripts/admin/components/rule-row.gjs
@@ -6,6 +6,7 @@ import DButton from "discourse/components/d-button";
import categoryLink from "discourse/helpers/category-link";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { i18n } from "discourse-i18n";
+import getTagName from "../lib/get-tag-name";
export default class RuleRow extends Component {
@service siteSettings;
@@ -22,6 +23,14 @@ export default class RuleRow extends Component {
return this.args.rule.type === "group_mention";
}
+ get displayTags() {
+ const tags = this.args.rule.tags;
+ if (!tags) {
+ return null;
+ }
+ return tags.map((tag) => getTagName(tag)).join(", ");
+ }
+
@action
delete(rule) {
rule
@@ -62,8 +71,8 @@ export default class RuleRow extends Component {
<td>
{{#if this.siteSettings.tagging_enabled}}
- {{#if @rule.tags}}
- {{@rule.tags}}
+ {{#if this.displayTags}}
+ {{this.displayTags}}
{{else}}
{{i18n "chat_integration.all_tags"}}
{{/if}}
diff --git a/plugins/discourse-chat-integration/admin/assets/javascripts/admin/lib/get-tag-name.js b/plugins/discourse-chat-integration/admin/assets/javascripts/admin/lib/get-tag-name.js
new file mode 100644
index 00000000000..21dc8cc736b
--- /dev/null
+++ b/plugins/discourse-chat-integration/admin/assets/javascripts/admin/lib/get-tag-name.js
@@ -0,0 +1,5 @@
+export default function getTagName(tag) {
+ // TODO(https://github.com/discourse/discourse/pull/36678): The string check can be
+ // removed using .discourse-compatibility once the PR is merged.
+ return typeof tag === "string" ? tag : tag.name;
+}
diff --git a/plugins/discourse-chat-integration/spec/system/rule_tags_spec.rb b/plugins/discourse-chat-integration/spec/system/rule_tags_spec.rb
new file mode 100644
index 00000000000..2b126f5ac31
--- /dev/null
+++ b/plugins/discourse-chat-integration/spec/system/rule_tags_spec.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+RSpec.describe "Chat Integration Rule Tags", type: :system do
+ fab!(:admin)
+ fab!(:tag1) { Fabricate(:tag, name: "support") }
+ fab!(:tag2) { Fabricate(:tag, name: "bug") }
+ fab!(:category)
+ fab!(:channel) do
+ DiscourseChatIntegration::Channel.create!(
+ provider: "discord",
+ data: {
+ name: "test-channel",
+ webhook_url: "https://discord.com/api/webhooks/test",
+ },
+ )
+ end
+
+ let(:tag_chooser) { PageObjects::Components::SelectKit.new(".tag-chooser") }
+ let(:rule_modal) { PageObjects::Modals::Base.new }
+
+ before do
+ SiteSetting.tagging_enabled = true
+ SiteSetting.chat_integration_enabled = true
+ SiteSetting.chat_integration_discord_enabled = true
+ sign_in(admin)
+ end
+
+ it "can edit a rule with existing tags and add new ones" do
+ DiscourseChatIntegration::Rule.create!(
+ channel: channel,
+ filter: "watch",
+ category_id: category.id,
+ tags: ["support"],
+ )
+
+ visit("/admin/plugins/chat-integration/discord")
+
+ within(".channel-details table tbody tr") do
+ expect(page).to have_content("support")
+ find(".edit").click
+ end
+
+ expect(rule_modal).to be_open
+
+ tag_chooser.expand
+ tag_chooser.search("bug")
+ tag_chooser.select_row_by_name("bug")
+
+ expect(tag_chooser.component).to have_content("support")
+ expect(tag_chooser.component).to have_content("bug")
+
+ find("#save-rule").click
+
+ expect(rule_modal).to be_closed
+
+ within(".channel-details table tbody tr") { expect(page).to have_text("support, bug") }
+ end
+end