Skip to content

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

DEV: Refactor admin search index into a service (#38959)

Martin Brennanb54c49cca7c3
diff --git a/app/controllers/admin/search_controller.rb b/app/controllers/admin/search_controller.rb
index 6c258a2e3fe..a7134c60ead 100644
--- a/app/controllers/admin/search_controller.rb
+++ b/app/controllers/admin/search_controller.rb
@@ -6,28 +6,30 @@ class Admin::SearchController < Admin::AdminController
def index
respond_to do |format|
format.json do
- render_json_dump(
- settings:
- SiteSetting.all_settings(
- filter_names: params[:filter_names],
- filter_area: params[:filter_area],
- filter_plugin: params[:plugin],
- filter_categories: Array.wrap(params[:categories]),
- include_locale_setting:
- params[:filter_area].blank? || params[:filter_area] == "localization",
- basic_attributes: true,
- ),
- themes_and_components:
- serialize_data(Theme.include_relations.order(:name), BasicThemeSerializer),
- reports: Reports::ListQuery.call(admin: current_user.admin?),
- upcoming_changes:
- UpcomingChanges::List.call(
- guardian: current_user.guardian,
- options: {
- filter_statuses: %w[experimental alpha beta stable],
- },
- ).upcoming_changes,
- )
+ Admin::Search::List.call(service_params) do |result|
+ on_success do |settings:, themes_and_components:, reports:, upcoming_changes:|
+ themes_and_components_json =
+ ActiveModel::ArraySerializer.new(
+ themes_and_components,
+ each_serializer: BasicThemeSerializer,
+ scope: guardian,
+ ).as_json
+
+ render_json_dump(
+ settings:,
+ themes_and_components: themes_and_components_json,
+ reports:,
+ upcoming_changes:,
+ )
+ end
+ on_failed_contract do |contract|
+ render(
+ json: failed_json.merge(errors: contract.errors.full_messages),
+ status: :bad_request,
+ )
+ end
+ on_failed_policy(:current_user_is_admin) { raise Discourse::InvalidAccess }
+ end
end
format.html { render body: nil }
diff --git a/app/services/admin/search/list.rb b/app/services/admin/search/list.rb
new file mode 100644
index 00000000000..b8ad1126839
--- /dev/null
+++ b/app/services/admin/search/list.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+module Admin
+ module Search
+ class List
+ include Service::Base
+
+ RESULT_FILTER_STATUSES = %w[experimental alpha beta stable].freeze
+
+ params do
+ attribute :filter_names, :array
+ attribute :filter_area, :string
+ attribute :plugin, :string
+ attribute :categories, :array
+
+ def include_locale_setting?
+ filter_area.blank? || filter_area == "localization"
+ end
+ end
+
+ policy :current_user_is_admin
+ model :settings
+ model :themes_and_components
+ model :reports
+ model :upcoming_changes
+
+ private
+
+ def current_user_is_admin(guardian:)
+ guardian.is_admin?
+ end
+
+ def fetch_settings(params:)
+ SiteSetting.all_settings(
+ filter_names: params.filter_names,
+ filter_area: params.filter_area,
+ filter_plugin: params.plugin,
+ filter_categories: params.categories,
+ include_locale_setting: params.include_locale_setting?,
+ basic_attributes: true,
+ )
+ end
+
+ def fetch_themes_and_components(guardian:)
+ Theme.all.order(:name).to_a
+ end
+
+ def fetch_reports(guardian:)
+ Reports::ListQuery.call(admin: true)
+ end
+
+ def fetch_upcoming_changes(guardian:)
+ UpcomingChanges::List.call(
+ guardian:,
+ options: {
+ filter_statuses: RESULT_FILTER_STATUSES,
+ },
+ ).upcoming_changes
+ end
+ end
+ end
+end
diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb
index 750705888ea..f690c70a01b 100644
--- a/lib/site_setting_extension.rb
+++ b/lib/site_setting_extension.rb
@@ -338,12 +338,12 @@ module SiteSettingExtension
filter_area: nil
)
locale_setting_hash = {
- setting: "default_locale",
- humanized_name: humanized_names("default_locale"),
+ setting: :default_locale,
+ humanized_name: humanized_names(:default_locale),
default: SiteSettings::DefaultsProvider::DEFAULT_LOCALE,
category: "required",
primary_area: "localization",
- description: description("default_locale"),
+ description: description(:default_locale),
type: SiteSetting.types[SiteSetting.types[:locale_enum]],
preview: nil,
value: self.default_locale,
diff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rb
index 95ac92b8175..d11e545efae 100644
--- a/spec/models/site_setting_spec.rb
+++ b/spec/models/site_setting_spec.rb
@@ -168,22 +168,22 @@ RSpec.describe SiteSetting do
describe "#all_settings" do
it "does not include the `default_locale` setting if include_locale_setting is false" do
- expect(SiteSetting.all_settings.map { |s| s[:setting] }).to include("default_locale")
+ expect(SiteSetting.all_settings.map { |s| s[:setting] }).to include(:default_locale)
expect(
SiteSetting.all_settings(include_locale_setting: false).map { |s| s[:setting] },
- ).not_to include("default_locale")
+ ).not_to include(:default_locale)
end
it "does not include the `default_locale` setting if filter_categories are specified" do
expect(
SiteSetting.all_settings(filter_categories: ["branding"]).map { |s| s[:setting] },
- ).not_to include("default_locale")
+ ).not_to include(:default_locale)
end
it "does not include the `default_locale` setting if filter_plugin is specified" do
expect(
SiteSetting.all_settings(filter_plugin: "chat").map { |s| s[:setting] },
- ).not_to include("default_locale")
+ ).not_to include(:default_locale)
end
it "includes only settings for the specified category" do
diff --git a/spec/requests/admin/search_controller_spec.rb b/spec/requests/admin/search_controller_spec.rb
index 6a5c065960e..5968d1290e1 100644
--- a/spec/requests/admin/search_controller_spec.rb
+++ b/spec/requests/admin/search_controller_spec.rb
@@ -6,6 +6,17 @@ RSpec.describe Admin::SearchController do
before { sign_in(admin) }
describe "#index" do
+ it "includes settings, themes and components, reports, and upcoming changes" do
+ get "/admin/search/all.json"
+
+ expect(response.status).to eq(200)
+
+ expect(response.parsed_body["settings"]).to be_present
+ expect(response.parsed_body["themes_and_components"]).to be_present
+ expect(response.parsed_body["reports"]).to be_present
+ expect(response.parsed_body["upcoming_changes"]).to be_present
+ end
+
it "includes default_locale setting in search results for general searches" do
get "/admin/search/all.json"
diff --git a/spec/requests/api/schemas/json/admin_user_response.json b/spec/requests/api/schemas/json/admin_user_response.json
index 8f6bbab6e64..dd03a154016 100644
--- a/spec/requests/api/schemas/json/admin_user_response.json
+++ b/spec/requests/api/schemas/json/admin_user_response.json
@@ -507,28 +507,22 @@
"type": "object",
"additionalProperties": false,
"properties": {
- "type": {
- "type": "string",
- "enum": ["object"]
+ "name": { "type": "string" },
+ "humanized_name": { "type": "string" },
+ "description": { "type": "string" },
+ "enabled": { "type": "boolean" },
+ "specific_groups": {
+ "type": "array",
+ "items": { "type": "string" }
},
- "properties": {
- "name": { "type": "string" },
- "humanized_name": { "type": "string" },
- "description": { "type": "string" },
- "enabled": { "type": "boolean" },
- "specific_groups": {
- "type": "array",
- "items": { "type": "string" }
- },
- "reason": {
- "type": "string",
- "enum": [
- "enabled_for_everyone",
- "enabled_for_no_one",
- "in_specific_groups",
- "not_in_specific_groups"
- ]
- }
+ "reason": {
+ "type": "string",
+ "enum": [
+ "enabled_for_everyone",
+ "enabled_for_no_one",
+ "in_specific_groups",
+ "not_in_specific_groups"
+ ]
}
},
"required": [
diff --git a/spec/services/admin/search/list_spec.rb b/spec/services/admin/search/list_spec.rb
new file mode 100644
index 00000000000..7c49cbed4e5
--- /dev/null
+++ b/spec/services/admin/search/list_spec.rb
@@ -0,0 +1,115 @@
+# frozen_string_literal: true
+
+RSpec.describe Admin::Search::List do
+ describe Admin::Search::List::Contract, type: :model do
+ it "accepts the supported search filters" do
+ contract =
+ described_class.new(
+ filter_names: %w[min_topic_title_length default_locale],
+ filter_area: "localization",
+ plugin: "discourse-sample-plugin",
+ categories: %w[reports localization],
+ )
+
+ expect(contract.filter_names).to eq(%w[min_topic_title_length default_locale])
+ expect(contract.filter_area).to eq("localization")
+ expect(contract.plugin).to eq("discourse-sample-plugin")
+ expect(contract.categories).to eq(%w[reports localization])
+ end
+
+ describe "#include_locale_setting?" do
+ it "returns true when filter_area is blank" do
+ contract = described_class.new(filter_area: nil)
+ expect(contract.include_locale_setting?).to eq(true)
+ end
+
+ it "returns true when filter_area is empty string" do
+ contract = described_class.new(filter_area: "")
+ expect(contract.include_locale_setting?).to eq(true)
+ end
+
+ it "returns true when filter_area is localization" do
+ contract = described_class.new(filter_area: "localization")
+ expect(contract.include_locale_setting?).to eq(true)
+ end
+
+ it "returns false when filter_area is something else" do
+ contract = described_class.new(filter_area: "reports")
+ expect(contract.include_locale_setting?).to eq(false)
+ end
+ end
+ end
+
+ describe ".call" do
+ subject(:result) { Admin::Search::List.call(params:, **dependencies) }
+
+ fab!(:admin)
+ fab!(:theme) { Fabricate(:theme, name: "Alpha Theme") }
+ fab!(:component) { Fabricate(:theme, name: "Beta Component", component: true) }
+
+ let(:guardian) { admin.guardian }
+ let(:dependencies) { { guardian: } }
+ let(:filter_names) { %w[min_topic_title_length default_locale] }
+ let(:filter_area) { nil }
+ let(:plugin) { nil }
+ let(:categories) { [] }
+ let(:params) { { filter_names:, filter_area:, plugin:, categories: } }
+
+ before do
+ mock_upcoming_change_metadata(
+ {
+ enable_upload_debug_mode: {
+ impact: "other,developers",
+ status: :experimental,
+ impact_type: "other",
+ impact_role: "developers",
+ },
+ },
+ )
+ end
+
+ context "when the current user is not an admin" do
+ let(:guardian) { Fabricate(:user).guardian }
+
+ it { is_expected.to fail_a_policy(:current_user_is_admin) }
+ end
+
+ context "when the search area is blank" do
+ it { is_expected.to run_successfully }
+
+ it "returns the aggregated payload" do
+ expect(result.settings.map { |setting| setting[:setting] }).to include(
+ :default_locale,
+ :min_topic_title_length,
+ )
+ expect(result.themes_and_components.map { |theme| theme[:name] }).to eq(
+ ["Alpha Theme", "Beta Component", "Foundation", "Horizon"],
+ )
+ expect(result.reports.length).to eq(Reports::ListQuery.call(admin: true).length)
+ expect(result.upcoming_changes.length).to be > 0
+ expect(
+ result.upcoming_changes.find { |change| change[:setting] == :enable_upload_debug_mode },
+ ).to be_present
+ end
+ end
+
+ context "when the search area is not localization" do
+ let(:filter_area) { "reports" }
+
+ it "excludes the default locale setting" do
+ expect(result.settings.map { |setting| setting[:setting] }).not_to include(:default_locale)
+ end
+ end
+
+ context "with a single category value" do
+ let(:categories) { ["dashboard"] }
+ let(:filter_names) { nil }
+
+ it "only includes settings of that category" do
+ expect(
+ result.settings.map { |setting| SiteSetting.categories[setting[:setting]] }.uniq,
+ ).to eq(["dashboard"])
+ end
+ end
+ end
+end
diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb
index 83de1aa9cbc..12f207d787d 100644
--- a/spec/support/helpers.rb
+++ b/spec/support/helpers.rb
@@ -354,6 +354,8 @@ module Helpers
end
def clear_mocked_upcoming_change_metadata
+ return if @original_upcoming_changes_metadata.blank?
+
SiteSetting.instance_variable_set(
:@upcoming_change_metadata,
@original_upcoming_changes_metadata,