A platform for community discussion. Free, open, simple.
FIX: Allow custom flags to be edited and deleted (#44064)
Kris293417c4942b
diff --git a/app/serializers/post_action_type_serializer.rb b/app/serializers/post_action_type_serializer.rbindex 4c3afd6f613..7a31349f0ea 100644--- a/app/serializers/post_action_type_serializer.rb+++ b/app/serializers/post_action_type_serializer.rb@@ -12,6 +12,7 @@ class PostActionTypeSerializer < ApplicationSerializer:enabled,:applies_to,:is_used,+ :system,)include ConfigurableUrls@@ -63,6 +64,12 @@ class PostActionTypeSerializer < ApplicationSerializerReviewableScore.exists?(reviewable_score_type: object.id)end+ def system+ # Flags serialized here come from registered flag settings rather than the+ # flags table, so there is no record for the admin UI to edit or delete.+ true+ end+privatedef i18n(field, **args)diff --git a/frontend/discourse/admin/components/admin-flag-item.gjs b/frontend/discourse/admin/components/admin-flag-item.gjsindex a6aac7e354b..897c015345e 100644--- a/frontend/discourse/admin/components/admin-flag-item.gjs+++ b/frontend/discourse/admin/components/admin-flag-item.gjs@@ -30,7 +30,7 @@ export default class AdminFlagItem extends Component {}get canEdit() {- return !Object.values(SYSTEM_FLAG_IDS).includes(this.args.flag.id);+ return !this.args.flag.system;}get canDelete() {diff --git a/frontend/discourse/admin/lib/constants.js b/frontend/discourse/admin/lib/constants.jsindex fb7156ad00e..2041cd42746 100644--- a/frontend/discourse/admin/lib/constants.js+++ b/frontend/discourse/admin/lib/constants.js@@ -21,18 +21,13 @@ export const API_KEY_SCOPE_MODES = ["global", "read_only", "granular"];export const SYSTEM_FLAG_IDS = {like: 2,- notify_user: 6,off_topic: 3,inappropriate: 4,+ notify_user: 6,+ notify_moderators: 7,spam: 8,+ needs_approval: 9,illegal: 10,- notify_moderators: 7,- custom_incorrect_content: 1010,- trolling: 1001,- real_life_threat: 1002,- inappropriate_name: 1003,- violent: 1011,- abuse: 1012,};export const REPORT_MODES = {diff --git a/frontend/discourse/admin/templates/admin-config/flags.gjs b/frontend/discourse/admin/templates/admin-config/flags.gjsindex a77d6739775..dacdedc7842 100644--- a/frontend/discourse/admin/templates/admin-config/flags.gjs+++ b/frontend/discourse/admin/templates/admin-config/flags.gjs@@ -1,6 +1,5 @@import Component from "@glimmer/component";import { service } from "@ember/service";-import { SYSTEM_FLAG_IDS } from "discourse/admin/lib/constants";import DBreadcrumbsItem from "discourse/ui-kit/d-breadcrumbs-item";import DNavItem from "discourse/ui-kit/d-nav-item";import DPageHeader from "discourse/ui-kit/d-page-header";@@ -12,9 +11,8 @@ class FlagsTemplate extends Component {get addFlagButtonDisabled() {return (- this.site.flagTypes.filter(- (flag) => !Object.values(SYSTEM_FLAG_IDS).includes(flag.id)- ).length >= this.siteSettings.custom_flags_limit+ this.site.flagTypes.filter((flag) => !flag.system).length >=+ this.siteSettings.custom_flags_limit);}diff --git a/frontend/discourse/tests/fixtures/concerns/notification-types.js b/frontend/discourse/tests/fixtures/concerns/notification-types.jsindex c89d6ce4ab2..9d279f00d07 100644--- a/frontend/discourse/tests/fixtures/concerns/notification-types.js+++ b/frontend/discourse/tests/fixtures/concerns/notification-types.js@@ -52,4 +52,5 @@ export const NOTIFICATION_TYPES = {following_created_topic: 801,following_replied: 802,circles_activity: 900,+ voice_invitation: 1000,};diff --git a/frontend/discourse/tests/integration/components/admin-flag-item-test.gjs b/frontend/discourse/tests/integration/components/admin-flag-item-test.gjsnew file mode 100644index 00000000000..7d59fb77a93--- /dev/null+++ b/frontend/discourse/tests/integration/components/admin-flag-item-test.gjs@@ -0,0 +1,78 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import AdminFlagItem from "discourse/admin/components/admin-flag-item";+import DMenus from "discourse/float-kit/components/d-menus";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++module("Integration | Component | AdminFlagItem", function (hooks) {+ setupRenderingTest(hooks);++ function createFlag(overrides = {}) {+ return {+ id: 1001,+ name: "colliding",+ name_key: "custom_colliding",+ description: "custom flag",+ enabled: true,+ is_used: false,+ system: false,+ ...overrides,+ };+ }++ test("custom flag is editable", async function (assert) {+ this.flag = createFlag();++ await render(+ <template>+ <DMenus />+ <AdminFlagItem @flag={{this.flag}} />+ </template>+ );++ assert+ .dom(".admin-flag-item__edit")+ .doesNotHaveAttribute("disabled", "the edit button is enabled");+ assert+ .dom(".d-table__overview-link")+ .exists("the name links to the edit route");+ });++ test("system flag is not editable", async function (assert) {+ this.flag = createFlag({ id: 3, name_key: "off_topic", system: true });++ await render(+ <template>+ <DMenus />+ <AdminFlagItem @flag={{this.flag}} />+ </template>+ );++ assert+ .dom(".admin-flag-item__edit")+ .hasAttribute("disabled", "", "the edit button is disabled");+ assert+ .dom(".d-table__overview-link")+ .doesNotExist("the name is not a link");+ });++ // Editability follows the serialized `system` flag rather than a hardcoded id+ // list, so a system flag missing from SYSTEM_FLAG_IDS stays locked.+ test("system flag absent from SYSTEM_FLAG_IDS is not editable", async function (assert) {+ this.flag = createFlag({ id: 11, name_key: "unlisted", system: true });++ await render(+ <template>+ <DMenus />+ <AdminFlagItem @flag={{this.flag}} />+ </template>+ );++ assert+ .dom(".admin-flag-item__edit")+ .hasAttribute("disabled", "", "the edit button is disabled");+ assert+ .dom(".d-table__overview-link")+ .doesNotExist("the name is not a link");+ });+});diff --git a/frontend/pretty-text/addon/emoji/data.js b/frontend/pretty-text/addon/emoji/data.jsindex b2699fb3b60..fb89489bb54 100644--- a/frontend/pretty-text/addon/emoji/data.js+++ b/frontend/pretty-text/addon/emoji/data.js@@ -4078,17 +4078,17 @@ export const replacements = {"π΅πΎ": "detective:t5","π΅πΏ": "detective:t6","π΅οΈββοΈ": "man_detective",- "π΅π»οΈββοΈ": "man_detective:t2",- "π΅πΌοΈββοΈ": "man_detective:t3",- "π΅π½οΈββοΈ": "man_detective:t4",- "π΅πΎοΈββοΈ": "man_detective:t5",- "π΅πΏοΈββοΈ": "man_detective:t6",+ "π΅π»ββοΈ": "man_detective:t2",+ "π΅πΌββοΈ": "man_detective:t3",+ "π΅π½ββοΈ": "man_detective:t4",+ "π΅πΎββοΈ": "man_detective:t5",+ "π΅πΏββοΈ": "man_detective:t6","π΅οΈββοΈ": "woman_detective",- "π΅π»οΈββοΈ": "woman_detective:t2",- "π΅πΌοΈββοΈ": "woman_detective:t3",- "π΅π½οΈββοΈ": "woman_detective:t4",- "π΅πΎοΈββοΈ": "woman_detective:t5",- "π΅πΏοΈββοΈ": "woman_detective:t6",+ "π΅π»ββοΈ": "woman_detective:t2",+ "π΅πΌββοΈ": "woman_detective:t3",+ "π΅π½ββοΈ": "woman_detective:t4",+ "π΅πΎββοΈ": "woman_detective:t5",+ "π΅πΏββοΈ": "woman_detective:t6","π": "guard","ππ»": "guard:t2","ππΌ": "guard:t3",@@ -4661,17 +4661,17 @@ export const replacements = {"ππΎ": "person_golfing:t5","ππΏ": "person_golfing:t6","ποΈββοΈ": "man_golfing",- "ππ»οΈββοΈ": "man_golfing:t2",- "ππΌοΈββοΈ": "man_golfing:t3",- "ππ½οΈββοΈ": "man_golfing:t4",- "ππΎοΈββοΈ": "man_golfing:t5",- "ππΏοΈββοΈ": "man_golfing:t6",+ "ππ»ββοΈ": "man_golfing:t2",+ "ππΌββοΈ": "man_golfing:t3",+ "ππ½ββοΈ": "man_golfing:t4",+ "ππΎββοΈ": "man_golfing:t5",+ "ππΏββοΈ": "man_golfing:t6","ποΈββοΈ": "woman_golfing",- "ππ»οΈββοΈ": "woman_golfing:t2",- "ππΌοΈββοΈ": "woman_golfing:t3",- "ππ½οΈββοΈ": "woman_golfing:t4",- "ππΎοΈββοΈ": "woman_golfing:t5",- "ππΏοΈββοΈ": "woman_golfing:t6",+ "ππ»ββοΈ": "woman_golfing:t2",+ "ππΌββοΈ": "woman_golfing:t3",+ "ππ½ββοΈ": "woman_golfing:t4",+ "ππΎββοΈ": "woman_golfing:t5",+ "ππΏββοΈ": "woman_golfing:t6","π": "person_surfing","ππ»": "person_surfing:t2","ππΌ": "person_surfing:t3",@@ -4733,17 +4733,17 @@ export const replacements = {"βΉπΎ": "person_bouncing_ball:t5","βΉπΏ": "person_bouncing_ball:t6","βΉοΈββοΈ": "man_bouncing_ball",- "βΉπ»οΈββοΈ": "man_bouncing_ball:t2",- "βΉπΌοΈββοΈ": "man_bouncing_ball:t3",- "βΉπ½οΈββοΈ": "man_bouncing_ball:t4",- "βΉπΎοΈββοΈ": "man_bouncing_ball:t5",- "βΉπΏοΈββοΈ": "man_bouncing_ball:t6",+ "βΉπ»ββοΈ": "man_bouncing_ball:t2",+ "βΉπΌββοΈ": "man_bouncing_ball:t3",+ "βΉπ½ββοΈ": "man_bouncing_ball:t4",+ "βΉπΎββοΈ": "man_bouncing_ball:t5",+ "βΉπΏββοΈ": "man_bouncing_ball:t6","βΉοΈββοΈ": "woman_bouncing_ball",- "βΉπ»οΈββοΈ": "woman_bouncing_ball:t2",- "βΉπΌοΈββοΈ": "woman_bouncing_ball:t3",- "βΉπ½οΈββοΈ": "woman_bouncing_ball:t4",- "βΉπΎοΈββοΈ": "woman_bouncing_ball:t5",- "βΉπΏοΈββοΈ": "woman_bouncing_ball:t6",+ "βΉπ»ββοΈ": "woman_bouncing_ball:t2",+ "βΉπΌββοΈ": "woman_bouncing_ball:t3",+ "βΉπ½ββοΈ": "woman_bouncing_ball:t4",+ "βΉπΎββοΈ": "woman_bouncing_ball:t5",+ "βΉπΏββοΈ": "woman_bouncing_ball:t6","π": "person_lifting_weights","ππ»": "person_lifting_weights:t2","ππΌ": "person_lifting_weights:t3",@@ -4751,17 +4751,17 @@ export const replacements = {"ππΎ": "person_lifting_weights:t5","ππΏ": "person_lifting_weights:t6","ποΈββοΈ": "man_lifting_weights",- "ππ»οΈββοΈ": "man_lifting_weights:t2",- "ππΌοΈββοΈ": "man_lifting_weights:t3",- "ππ½οΈββοΈ": "man_lifting_weights:t4",- "ππΎοΈββοΈ": "man_lifting_weights:t5",- "ππΏοΈββοΈ": "man_lifting_weights:t6",+ "ππ»ββοΈ": "man_lifting_weights:t2",+ "ππΌββοΈ": "man_lifting_weights:t3",+ "ππ½ββοΈ": "man_lifting_weights:t4",+ "ππΎββοΈ": "man_lifting_weights:t5",+ "ππΏββοΈ": "man_lifting_weights:t6","ποΈββοΈ": "woman_lifting_weights",- "ππ»οΈββοΈ": "woman_lifting_weights:t2",- "ππΌοΈββοΈ": "woman_lifting_weights:t3",- "ππ½οΈββοΈ": "woman_lifting_weights:t4",- "ππΎοΈββοΈ": "woman_lifting_weights:t5",- "ππΏοΈββοΈ": "woman_lifting_weights:t6",+ "ππ»ββοΈ": "woman_lifting_weights:t2",+ "ππΌββοΈ": "woman_lifting_weights:t3",+ "ππ½ββοΈ": "woman_lifting_weights:t4",+ "ππΎββοΈ": "woman_lifting_weights:t5",+ "ππΏββοΈ": "woman_lifting_weights:t6","π΄": "person_biking","π΄π»": "person_biking:t2","π΄πΌ": "person_biking:t3",diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rakeindex ab58baec136..d33896c44bd 100644--- a/lib/tasks/javascript.rake+++ b/lib/tasks/javascript.rake@@ -35,7 +35,7 @@ task "javascript:update_constants" => :environment doexport const API_KEY_SCOPE_MODES = #{ApiKey.scope_modes.keys.to_json}- export const SYSTEM_FLAG_IDS = #{PostActionType.types.to_json};+ export const SYSTEM_FLAG_IDS = #{Flag.unscoped.where(id: 0...Flag::MAX_SYSTEM_FLAG_ID).order(:id).pluck(:name_key, :id).to_h.to_json};export const REPORT_MODES = #{Report::MODES.to_json};diff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rbindex ff9b7398965..02841a606aa 100644--- a/spec/serializers/site_serializer_spec.rb+++ b/spec/serializers/site_serializer_spec.rb@@ -16,6 +16,29 @@ RSpec.describe SiteSerializer doendend+ describe "post_action_types" do+ it "marks flags as system when they come from registered flag settings" do+ # Plugins can replace flag settings, which makes the payload serialize+ # PostActionType records instead of Flag records. The admin UI keys+ # editability off `system`, so it has to be present on both paths.+ settings = FlagSettings.new+ settings.add(4, :inappropriate)+ settings.add(99, :custom_plugin_flag, name: "Custom plugin flag")+ PostActionType.replace_flag_settings(settings)+ # Expire the cached payload without resetting the settings we just registered.+ PostActionType.new.expire_cache+ Site.clear_cache++ serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json++ expect(serialized[:post_action_types]).to be_present+ expect(serialized[:post_action_types].map { |type| type[:system] }.uniq).to eq([true])+ ensure+ PostActionType.replace_flag_settings(nil)+ Flag.reset_flag_settings!+ end+ end+describe "#homepage_choices" doaround do |example|registrations = DiscoursePluginRegistry._raw_homepage_options.dupdiff --git a/spec/system/admin_flags_spec.rb b/spec/system/admin_flags_spec.rbindex eca8ec943e0..d9aaee83a4b 100644--- a/spec/system/admin_flags_spec.rb+++ b/spec/system/admin_flags_spec.rb@@ -163,6 +163,28 @@ describe "Admin Flags Page" do)end+ it "allows editing and deleting the first custom flag" do+ SiteSetting.custom_flags_limit = 4++ # 1001 is where the flags id sequence starts, so this is the id every site's+ # first custom flag gets. Unused custom flags must stay editable/deletable+ # whatever their id.+ Fabricate(+ :flag,+ id: 1001,+ name: "colliding",+ description: "custom flag at id 1001",+ applies_to: %w[Post Topic],+ )++ admin_flags_page.visit.click_edit_flag("custom_colliding")+ admin_flag_form_page.fill_in_description("edited description").click_save++ admin_flags_page.visit.click_delete_flag("custom_colliding").confirm_delete++ expect(admin_flags_page).to have_no_flag("custom_colliding")+ end+it "restricts actions on certain flags" doadmin_flags_page.visit