From 5f5680dbafbb8bb5d35cf2aac64aecdba9b9b65d Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 6 Sep 2024 16:10:53 +0200 Subject: [PATCH] Revert "FIX: supports groups field in post_created_edited (#28773)" (#28781) This reverts commit eb3a1c72179c179af358daf41b167e337cc7b454. --- .../admin/components/automation-field.gjs | 2 - .../components/fields/da-groups-field.gjs | 55 -------------- .../app/models/discourse_automation/field.rb | 6 -- .../automation/config/locales/client.en.yml | 3 - ..._fields_for_post_created_edited_trigger.rb | 22 ------ .../discourse_automation/event_handlers.rb | 10 +-- .../triggers/post_created_edited.rb | 2 +- .../spec/triggers/post_created_edited_spec.rb | 52 +++++--------- .../components/da-groups-field-test.js | 71 ------------------- 9 files changed, 22 insertions(+), 201 deletions(-) delete mode 100644 plugins/automation/admin/assets/javascripts/admin/components/fields/da-groups-field.gjs delete mode 100644 plugins/automation/db/migrate/20240906091215_update_fields_for_post_created_edited_trigger.rb delete mode 100644 plugins/automation/test/javascripts/integration/components/da-groups-field-test.js diff --git a/plugins/automation/admin/assets/javascripts/admin/components/automation-field.gjs b/plugins/automation/admin/assets/javascripts/admin/components/automation-field.gjs index 9aeb2db0237..56e8576ec5c 100644 --- a/plugins/automation/admin/assets/javascripts/admin/components/automation-field.gjs +++ b/plugins/automation/admin/assets/javascripts/admin/components/automation-field.gjs @@ -10,7 +10,6 @@ import DaCustomFields from "./fields/da-custom-fields"; import DaDateTimeField from "./fields/da-date-time-field"; import DaEmailGroupUserField from "./fields/da-email-group-user-field"; import DaGroupField from "./fields/da-group-field"; -import DaGroupsField from "./fields/da-groups-field"; import DaKeyValueField from "./fields/da-key-value-field"; import DaMessageField from "./fields/da-message-field"; import DaPeriodField from "./fields/da-period-field"; @@ -42,7 +41,6 @@ const FIELD_COMPONENTS = { "trust-levels": DaTrustLevelsField, category: DaCategoryField, group: DaGroupField, - groups: DaGroupsField, choices: DaChoicesField, category_notification_level: DaCategoryNotificationlevelField, email_group_user: DaEmailGroupUserField, diff --git a/plugins/automation/admin/assets/javascripts/admin/components/fields/da-groups-field.gjs b/plugins/automation/admin/assets/javascripts/admin/components/fields/da-groups-field.gjs deleted file mode 100644 index 5324e66bd59..00000000000 --- a/plugins/automation/admin/assets/javascripts/admin/components/fields/da-groups-field.gjs +++ /dev/null @@ -1,55 +0,0 @@ -import { tracked } from "@glimmer/tracking"; -import { hash } from "@ember/helper"; -import { action } from "@ember/object"; -import Group from "discourse/models/group"; -import GroupChooser from "select-kit/components/group-chooser"; -import BaseField from "./da-base-field"; -import DAFieldDescription from "./da-field-description"; -import DAFieldLabel from "./da-field-label"; - -export default class GroupsField extends BaseField { - @tracked allGroups = []; - - constructor() { - super(...arguments); - - Group.findAll({ - ignore_automatic: this.args.field.extra.ignore_automatic ?? false, - }).then((groups) => { - if (this.isDestroying || this.isDestroyed) { - return; - } - - this.allGroups = groups; - }); - } - - get maximum() { - return this.args.field.extra.maximum ?? 10; - } - - @action - setGroupField(groupIds) { - this.mutValue(groupIds); - } - - -} diff --git a/plugins/automation/app/models/discourse_automation/field.rb b/plugins/automation/app/models/discourse_automation/field.rb index 4dea55a2b26..567598fc251 100644 --- a/plugins/automation/app/models/discourse_automation/field.rb +++ b/plugins/automation/app/models/discourse_automation/field.rb @@ -188,12 +188,6 @@ module DiscourseAutomation "type" => "integer", }, }, - "groups" => { - "value" => { - "type" => "array", - "items" => [{ type: "integer" }], - }, - }, "email_group_user" => { "value" => { "type" => "array", diff --git a/plugins/automation/config/locales/client.en.yml b/plugins/automation/config/locales/client.en.yml index 9af98e2c8c5..28224d80fc0 100644 --- a/plugins/automation/config/locales/client.en.yml +++ b/plugins/automation/config/locales/client.en.yml @@ -156,9 +156,6 @@ en: restricted_group: label: Group description: Optional, will trigger only if the post's topic is a private message in this group's inbox - restricted_groups: - label: Groups - description: Optional, will trigger only if the post's topic is a private message in one of the group inboxes ignore_group_members: label: Ignore group members description: Skip the trigger if sender is a member of the Group specified above diff --git a/plugins/automation/db/migrate/20240906091215_update_fields_for_post_created_edited_trigger.rb b/plugins/automation/db/migrate/20240906091215_update_fields_for_post_created_edited_trigger.rb deleted file mode 100644 index 32197a71aa2..00000000000 --- a/plugins/automation/db/migrate/20240906091215_update_fields_for_post_created_edited_trigger.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -class UpdateFieldsForPostCreatedEditedTrigger < ActiveRecord::Migration[7.1] - def up - execute <<-SQL - UPDATE discourse_automation_fields - SET - component = 'groups', - name = 'restricted_groups', - metadata = jsonb_set(metadata, '{value}', to_jsonb(ARRAY[(metadata->>'value')::int])) - FROM discourse_automation_automations - WHERE discourse_automation_fields.automation_id = discourse_automation_automations.id - AND discourse_automation_automations.trigger = 'post_created_edited' - AND discourse_automation_fields.name = 'restricted_group' - AND discourse_automation_fields.component = 'group'; - SQL - end - - def down - raise ActiveRecord::IrreversibleMigration - end -end diff --git a/plugins/automation/lib/discourse_automation/event_handlers.rb b/plugins/automation/lib/discourse_automation/event_handlers.rb index 7a801a65da6..e1bb3b3b27c 100644 --- a/plugins/automation/lib/discourse_automation/event_handlers.rb +++ b/plugins/automation/lib/discourse_automation/event_handlers.rb @@ -54,15 +54,15 @@ module DiscourseAutomation next if (restricted_tags["value"] & topic.tags.map(&:name)).empty? end - restricted_group_ids = automation.trigger_field("restricted_groups")["value"] - if restricted_group_ids.present? + restricted_group_id = automation.trigger_field("restricted_group")["value"] + if restricted_group_id.present? next if !topic.private_message? target_group_ids = topic.allowed_groups.pluck(:id) - next if (restricted_group_ids & target_group_ids).empty? + next if restricted_group_id != target_group_ids.first - ignore_group_members = automation.trigger_field("ignore_group_members")["value"] - next if ignore_group_members && post.user.in_any_groups?(restricted_group_ids) + ignore_group_members = automation.trigger_field("ignore_group_members") + next if ignore_group_members["value"] && post.user.in_any_groups?([restricted_group_id]) end ignore_automated = automation.trigger_field("ignore_automated") diff --git a/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb b/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb index 9bf62631592..9735a55efcc 100644 --- a/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb +++ b/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb @@ -14,7 +14,7 @@ DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::POST_CREATED } field :restricted_category, component: :category field :restricted_tags, component: :tags - field :restricted_groups, component: :groups + field :restricted_group, component: :group field :ignore_automated, component: :boolean field :ignore_group_members, component: :boolean field :valid_trust_levels, component: :"trust-levels" diff --git a/plugins/automation/spec/triggers/post_created_edited_spec.rb b/plugins/automation/spec/triggers/post_created_edited_spec.rb index 363171eea28..159377400b2 100644 --- a/plugins/automation/spec/triggers/post_created_edited_spec.rb +++ b/plugins/automation/spec/triggers/post_created_edited_spec.rb @@ -164,50 +164,30 @@ describe "PostCreatedEdited" do context "when group is restricted" do fab!(:target_group) { Fabricate(:group, messageable_level: Group::ALIAS_LEVELS[:everyone]) } - fab!(:another_group) { Fabricate(:group, messageable_level: Group::ALIAS_LEVELS[:everyone]) } before do automation.upsert_field!( - "restricted_groups", - "groups", - { value: [target_group.id, another_group.id] }, + "restricted_group", + "group", + { value: target_group.id }, target: "trigger", ) end - context "when PM is not sent to the group" do - it "doesnt fire the trigger" do - list = - capture_contexts do - PostCreator.create( - user, - basic_topic_params.merge( - target_group_names: [Fabricate(:group).name], - archetype: Archetype.private_message, - ), - ) - end + it "fires the trigger" do + list = + capture_contexts do + PostCreator.create( + user, + basic_topic_params.merge( + target_group_names: [target_group.name], + archetype: Archetype.private_message, + ), + ) + end - expect(list.length).to eq(0) - end - end - - context "when PM is sent to the group" do - it "fires the trigger" do - list = - capture_contexts do - PostCreator.create( - user, - basic_topic_params.merge( - target_group_names: [target_group.name], - archetype: Archetype.private_message, - ), - ) - end - - expect(list.length).to eq(1) - expect(list[0]["kind"]).to eq("post_created_edited") - end + expect(list.length).to eq(1) + expect(list[0]["kind"]).to eq("post_created_edited") end context "when the topic is not a PM" do diff --git a/plugins/automation/test/javascripts/integration/components/da-groups-field-test.js b/plugins/automation/test/javascripts/integration/components/da-groups-field-test.js deleted file mode 100644 index 07fd87fcde3..00000000000 --- a/plugins/automation/test/javascripts/integration/components/da-groups-field-test.js +++ /dev/null @@ -1,71 +0,0 @@ -import { getOwner } from "@ember/owner"; -import { render } from "@ember/test-helpers"; -import { hbs } from "ember-cli-htmlbars"; -import { module, test } from "qunit"; -import { setupRenderingTest } from "discourse/tests/helpers/component-test"; -import pretender, { response } from "discourse/tests/helpers/create-pretender"; -import selectKit from "discourse/tests/helpers/select-kit-helper"; -import AutomationFabricators from "discourse/plugins/automation/admin/lib/fabricators"; - -module("Integration | Component | da-groups-field", function (hooks) { - setupRenderingTest(hooks); - - hooks.beforeEach(function () { - this.automation = new AutomationFabricators(getOwner(this)).automation(); - - pretender.get("/groups/search.json", () => { - return response([ - { - id: 1, - name: "cats", - flair_url: "fa-bars", - flair_bg_color: "CC000A", - flair_color: "FFFFFA", - }, - { - id: 2, - name: "dogs", - flair_url: "fa-bars", - flair_bg_color: "CC000A", - flair_color: "FFFFFA", - }, - ]); - }); - }); - - test("set value", async function (assert) { - this.field = new AutomationFabricators(getOwner(this)).field({ - component: "groups", - }); - - await render( - hbs`` - ); - - await selectKit().expand(); - await selectKit().selectRowByValue(1); - - assert.deepEqual(this.field.metadata.value, [1]); - }); - - test("supports a maxmimum value", async function (assert) { - this.field = new AutomationFabricators(getOwner(this)).field({ - component: "groups", - extra: { maximum: 1 }, - }); - - await render( - hbs`` - ); - - await selectKit().expand(); - await selectKit().selectRowByValue(1); - - assert.deepEqual(this.field.metadata.value, [1]); - - await selectKit().expand(); - await selectKit().selectRowByValue(2); - - assert.deepEqual(this.field.metadata.value, [2]); - }); -});