A platform for community discussion. Free, open, simple.
DEV: Refactor some core services
Loïc Guitaute95edd079b5e
diff --git a/app/controllers/admin/config/about_controller.rb b/app/controllers/admin/config/about_controller.rbindex 0e0cb0acd3a..e92b1b657d9 100644--- a/app/controllers/admin/config/about_controller.rb+++ b/app/controllers/admin/config/about_controller.rb@@ -36,8 +36,8 @@ class Admin::Config::AboutController < Admin::AdminControllerendsettings_map.each do |name, value|- UpdateSiteSetting.call(- guardian: guardian,+ SiteSetting::Update.call(+ guardian:,setting_name: name,new_value: value,allow_changing_hidden: %i[diff --git a/app/controllers/admin/site_settings_controller.rb b/app/controllers/admin/site_settings_controller.rbindex e832e945273..eb86c9789d2 100644--- a/app/controllers/admin/site_settings_controller.rb+++ b/app/controllers/admin/site_settings_controller.rb@@ -39,10 +39,10 @@ class Admin::SiteSettingsController < Admin::AdminControllerprevious_value = value_or_default(SiteSetting.get(id)) if update_existing_users- UpdateSiteSetting.call(service_params.merge(setting_name: id, new_value: value)) do+ SiteSetting::Update.call(service_params.merge(setting_name: id, new_value: value)) doon_success doif update_existing_users- SiteSettingUpdateExistingUsers.call(id, result.new_value, previous_value)+ SiteSettingUpdateExistingUsers.call(id, result.contract.new_value, previous_value)endrender body: nilenddiff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rbindex e40c9116502..8a2f668414d 100644--- a/app/controllers/admin/users_controller.rb+++ b/app/controllers/admin/users_controller.rb@@ -124,7 +124,7 @@ class Admin::UsersController < Admin::StaffControlleron_success dorender_json_dump(suspension: {- suspend_reason: result.reason,+ suspend_reason: result.contract.reason,full_suspend_reason: result.full_reason,suspended_till: result.user.suspended_till,suspended_at: result.user.suspended_at,diff --git a/app/services/admin_notices/dismiss.rb b/app/services/admin_notices/dismiss.rbindex 8d6215eeea1..0d2f76484e7 100644--- a/app/services/admin_notices/dismiss.rb+++ b/app/services/admin_notices/dismiss.rb@@ -3,10 +3,8 @@class AdminNotices::Dismissinclude Service::Base- model :admin_notice, optional: true-policy :invalid_access-+ model :admin_notice, optional: truetransaction dostep :destroystep :reset_problem_check@@ -14,14 +12,14 @@ class AdminNotices::Dismissprivate- def fetch_admin_notice(id:)- AdminNotice.find_by(id: id)- end-def invalid_access(guardian:)guardian.is_admin?end+ def fetch_admin_notice(id:)+ AdminNotice.find_by(id: id)+ end+def destroy(admin_notice:)return if admin_notice.blank?diff --git a/app/services/site_setting/update.rb b/app/services/site_setting/update.rbnew file mode 100644index 00000000000..47a7ced2272--- /dev/null+++ b/app/services/site_setting/update.rb@@ -0,0 +1,59 @@+# frozen_string_literal: true++class SiteSetting::Update+ include Service::Base++ policy :current_user_is_admin+ contract do+ attribute :setting_name+ attribute :new_value+ attribute :allow_changing_hidden, :boolean, default: false++ before_validation do+ self.setting_name = setting_name&.to_sym+ self.new_value = new_value.to_s.strip+ end++ validates :setting_name, presence: true++ after_validation do+ next if setting_name.blank?+ self.new_value =+ case SiteSetting.type_supervisor.get_type(setting_name)+ when :integer+ new_value.tr("^-0-9", "").to_i+ when :file_size_restriction+ new_value.tr("^0-9", "").to_i+ when :uploaded_image_list+ new_value.blank? ? "" : Upload.get_from_urls(new_value.split("|")).to_a+ when :upload+ Upload.get_from_url(new_value) || ""+ else+ new_value+ end+ end+ end+ policy :setting_is_visible+ policy :setting_is_configurable+ step :save++ private++ def current_user_is_admin(guardian:)+ guardian.is_admin?+ end++ def setting_is_visible(contract:)+ contract.allow_changing_hidden || !SiteSetting.hidden_settings.include?(contract.setting_name)+ end++ def setting_is_configurable(contract:)+ return true if !SiteSetting.plugins[contract.setting_name]++ Discourse.plugins_by_name[SiteSetting.plugins[contract.setting_name]].configurable?+ end++ def save(contract:, guardian:)+ SiteSetting.set_and_log(contract.setting_name, contract.new_value, guardian.user)+ end+enddiff --git a/app/services/update_site_setting.rb b/app/services/update_site_setting.rbdeleted file mode 100644index d0c92491860..00000000000--- a/app/services/update_site_setting.rb+++ /dev/null@@ -1,57 +0,0 @@-# frozen_string_literal: true--class UpdateSiteSetting- include Service::Base-- policy :current_user_is_admin- contract do- attribute :setting_name- attribute :new_value- attribute :allow_changing_hidden, :boolean, default: false-- before_validation { self.setting_name = setting_name&.to_sym }-- validates :setting_name, presence: true- end- policy :setting_is_visible- policy :setting_is_configurable- step :cleanup_value- step :save-- private-- def current_user_is_admin(guardian:)- guardian.is_admin?- end-- def setting_is_visible(contract:)- contract.allow_changing_hidden || !SiteSetting.hidden_settings.include?(contract.setting_name)- end-- def setting_is_configurable(contract:)- return true if !SiteSetting.plugins[contract.setting_name]-- Discourse.plugins_by_name[SiteSetting.plugins[contract.setting_name]].configurable?- end-- def cleanup_value(contract:)- new_value = contract.new_value- new_value = new_value.strip if new_value.is_a?(String)-- case SiteSetting.type_supervisor.get_type(contract.setting_name)- when :integer- new_value = new_value.tr("^-0-9", "").to_i if new_value.is_a?(String)- when :file_size_restriction- new_value = new_value.tr("^0-9", "").to_i if new_value.is_a?(String)- when :uploaded_image_list- new_value = new_value.blank? ? "" : Upload.get_from_urls(new_value.split("|")).to_a- when :upload- new_value = Upload.get_from_url(new_value) || ""- end- context[:new_value] = new_value- end-- def save(contract:, new_value:, guardian:)- SiteSetting.set_and_log(contract.setting_name, new_value, guardian.user)- end-enddiff --git a/spec/services/admin_notices/dismiss_spec.rb b/spec/services/admin_notices/dismiss_spec.rbindex d16f439d0c7..821ed029ca2 100644--- a/spec/services/admin_notices/dismiss_spec.rb+++ b/spec/services/admin_notices/dismiss_spec.rb@@ -1,10 +1,14 @@# frozen_string_literal: trueRSpec.describe(AdminNotices::Dismiss) do- subject(:result) { described_class.call(id: admin_notice.id, guardian: current_user.guardian) }+ subject(:result) { described_class.call(**params, **dependencies) }- let!(:admin_notice) { Fabricate(:admin_notice, identifier: "problem.test") }- let!(:problem_check) { Fabricate(:problem_check_tracker, identifier: "problem.test", blips: 3) }+ fab!(:current_user) { Fabricate(:admin) }+ fab!(:admin_notice) { Fabricate(:admin_notice, identifier: "problem.test") }+ fab!(:problem_check) { Fabricate(:problem_check_tracker, identifier: "problem.test", blips: 3) }++ let(:params) { { id: admin_notice.id } }+ let(:dependencies) { { guardian: current_user.guardian } }context "when user is not allowed to perform the action" dofab!(:current_user) { Fabricate(:user) }@@ -13,22 +17,14 @@ RSpec.describe(AdminNotices::Dismiss) doendcontext "when the admin notice has already been dismissed" do- fab!(:current_user) { Fabricate(:admin) }-before { admin_notice.destroy! }it { is_expected.to run_successfully }end- context "when user is allowed to perform the action" do- fab!(:current_user) { Fabricate(:admin) }-+ context "when everything's ok" doit { is_expected.to run_successfully }- it "sets the service result as successful" do- expect(result).to be_a_success- end-it "destroys the admin notice" doexpect { result }.to change { AdminNotice.count }.from(1).to(0)enddiff --git a/spec/services/site_setting/update_spec.rb b/spec/services/site_setting/update_spec.rbnew file mode 100644index 00000000000..509373fd9b1--- /dev/null+++ b/spec/services/site_setting/update_spec.rb@@ -0,0 +1,79 @@+# frozen_string_literal: true++RSpec.describe SiteSetting::Update do+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of :setting_name }+ end++ describe ".call" do+ subject(:result) { described_class.call(**params, **options, **dependencies) }++ fab!(:admin)+ let(:params) { { setting_name:, new_value: } }+ let(:options) { { allow_changing_hidden: } }+ let(:dependencies) { { guardian: } }+ let(:setting_name) { :title }+ let(:new_value) { "blah whatever" }+ let(:guardian) { admin.guardian }+ let(:allow_changing_hidden) { false }++ context "when setting_name is blank" do+ let(:setting_name) { nil }++ it { is_expected.to fail_a_contract }+ end++ context "when a non-admin user tries to change a setting" do+ let(:guardian) { Guardian.new }++ it { is_expected.to fail_a_policy(:current_user_is_admin) }+ end++ context "when the user changes a hidden setting" do+ let(:setting_name) { :max_category_nesting }+ let(:new_value) { 3 }++ context "when allow_changing_hidden is false" do+ it { is_expected.to fail_a_policy(:setting_is_visible) }+ end++ context "when allow_changing_hidden is true" do+ let(:allow_changing_hidden) { true }++ it { is_expected.to run_successfully }++ it "updates the specified setting" do+ expect { result }.to change { SiteSetting.max_category_nesting }.to(3)+ end+ end+ end++ context "when the user changes a visible setting" do+ let(:new_value) { "hello this is title" }++ it { is_expected.to run_successfully }++ it "updates the specified setting" do+ expect { result }.to change { SiteSetting.title }.to(new_value)+ end++ it "creates an entry in the staff action logs" do+ expect { result }.to change {+ UserHistory.where(+ action: UserHistory.actions[:change_site_setting],+ subject: "title",+ ).count+ }.by(1)+ end++ context "when value needs cleanup" do+ let(:setting_name) { :max_image_size_kb }+ let(:new_value) { "8zf843" }++ it "cleans up the new setting value before using it" do+ expect { result }.to change { SiteSetting.max_image_size_kb }.to(8843)+ end+ end+ end+ end+enddiff --git a/spec/services/update_site_setting_spec.rb b/spec/services/update_site_setting_spec.rbdeleted file mode 100644index fda557efc8f..00000000000--- a/spec/services/update_site_setting_spec.rb+++ /dev/null@@ -1,74 +0,0 @@-# frozen_string_literal: true--RSpec.describe UpdateSiteSetting do- subject(:result) { described_class.call(params) }-- describe described_class::Contract, type: :model do- subject(:contract) { described_class.new }-- it { is_expected.to validate_presence_of :setting_name }- end-- fab!(:admin)- let(:params) { { setting_name:, new_value:, guardian:, allow_changing_hidden: } }- let(:setting_name) { :title }- let(:new_value) { "blah whatever" }- let(:guardian) { admin.guardian }- let(:allow_changing_hidden) { false }-- context "when setting_name is blank" do- let(:setting_name) { nil }-- it { is_expected.to fail_a_contract }- end-- context "when a non-admin user tries to change a setting" do- let(:guardian) { Guardian.new }-- it { is_expected.to fail_a_policy(:current_user_is_admin) }- end-- context "when the user changes a hidden setting" do- let(:setting_name) { :max_category_nesting }- let(:new_value) { 3 }-- context "when allow_changing_hidden is false" do- it { is_expected.to fail_a_policy(:setting_is_visible) }- end-- context "when allow_changing_hidden is true" do- let(:allow_changing_hidden) { true }-- it { is_expected.to run_successfully }-- it "updates the specified setting" do- expect { result }.to change { SiteSetting.max_category_nesting }.to(3)- end- end- end-- context "when the user changes a visible setting" do- let(:new_value) { "hello this is title" }-- it { is_expected.to run_successfully }-- it "updates the specified setting" do- expect { result }.to change { SiteSetting.title }.to(new_value)- end-- it "creates an entry in the staff action logs" do- expect { result }.to change {- UserHistory.where(action: UserHistory.actions[:change_site_setting], subject: "title").count- }.by(1)- end-- context "when value needs cleanup" do- let(:setting_name) { :max_image_size_kb }- let(:new_value) { "8zf843" }-- it "cleans up the new setting value before using it" do- expect { result }.to change { SiteSetting.max_image_size_kb }.to(8843)- end- end- end-enddiff --git a/spec/services/user/silence_spec.rb b/spec/services/user/silence_spec.rbindex 690553c53d4..10047ba9fba 100644--- a/spec/services/user/silence_spec.rb+++ b/spec/services/user/silence_spec.rb@@ -21,13 +21,14 @@ RSpec.describe User::Silence doenddescribe ".call" do- subject(:result) { described_class.call(params) }+ subject(:result) { described_class.call(**params, **dependencies) }fab!(:admin)fab!(:user)fab!(:other_user) { Fabricate(:user) }- let(:params) { { guardian:, user_id:, reason:, silenced_till:, other_user_ids:, message: } }+ let(:params) { { user_id:, reason:, silenced_till:, other_user_ids:, message: } }+ let(:dependencies) { { guardian: } }let(:guardian) { admin.guardian }let(:user_id) { user.id }let(:other_user_ids) { other_user.id }@@ -41,53 +42,47 @@ RSpec.describe User::Silence doit { is_expected.to fail_a_contract }end- context "when data is valid" do- context "when provided user does not exist" do- let(:user_id) { 0 }+ context "when provided user does not exist" do+ let(:user_id) { 0 }- it { is_expected.to fail_to_find_a_model(:user) }+ it { is_expected.to fail_to_find_a_model(:user) }+ end++ context "when user is already silenced" do+ before { UserSilencer.silence(user, admin) }++ it { is_expected.to fail_a_policy(:not_silenced_already) }+ end++ context "when all users cannot be silenced" do+ let(:other_user_ids) { [other_user.id, Fabricate(:admin).id].join(",") }++ it { is_expected.to fail_a_policy(:can_silence_all_users) }+ end++ context "when everything's ok" do+ before { allow(User::Action::TriggerPostAction).to receive(:call) }++ it "silences all provided users" do+ result+ expect([user, other_user].map(&:reload)).to all be_silenced+ end++ it "enqueues jobs to send an email" do+ expect { result }.to change { Jobs::CriticalUserEmail.jobs.size }.by(2)+ end++ it "exposes the full reason in the result object" do+ expect(result[:full_reason]).to eq("spam\n\nit was spam")end- context "when provided user exists" do- context "when user is already silenced" do- before { UserSilencer.silence(user, admin) }-- it { is_expected.to fail_a_policy(:not_silenced_already) }- end-- context "when user is not already silenced" do- context "when all users cannot be silenced" do- let(:other_user_ids) { [other_user.id, Fabricate(:admin).id].join(",") }-- it { is_expected.to fail_a_policy(:can_silence_all_users) }- end-- context "when all users can be silenced" do- before { allow(User::Action::TriggerPostAction).to receive(:call) }-- it "silences all provided users" do- result- expect([user, other_user].map(&:reload)).to all be_silenced- end-- it "enqueues jobs to send an email" do- expect { result }.to change { Jobs::CriticalUserEmail.jobs.size }.by(2)- end-- it "exposes the full reason in the result object" do- expect(result[:full_reason]).to eq("spam\n\nit was spam")- end-- it "triggers a post action" do- result- expect(User::Action::TriggerPostAction).to have_received(:call).with(- guardian:,- post: nil,- contract: result[:contract],- )- end- end- end+ it "triggers a post action" do+ result+ expect(User::Action::TriggerPostAction).to have_received(:call).with(+ guardian:,+ post: nil,+ contract: result[:contract],+ )endendenddiff --git a/spec/services/user/suspend_spec.rb b/spec/services/user/suspend_spec.rbindex 1bb4c1e0bc6..aacd924c4e2 100644--- a/spec/services/user/suspend_spec.rb+++ b/spec/services/user/suspend_spec.rb@@ -21,13 +21,14 @@ RSpec.describe User::Suspend doenddescribe ".call" do- subject(:result) { described_class.call(params) }+ subject(:result) { described_class.call(**params, **dependencies) }fab!(:admin)fab!(:user)fab!(:other_user) { Fabricate(:user) }- let(:params) { { guardian:, user_id:, reason:, suspend_until:, other_user_ids:, message: } }+ let(:params) { { user_id:, reason:, suspend_until:, other_user_ids:, message: } }+ let(:dependencies) { { guardian: } }let(:guardian) { admin.guardian }let(:user_id) { user.id }let(:other_user_ids) { other_user.id }@@ -41,51 +42,45 @@ RSpec.describe User::Suspend doit { is_expected.to fail_a_contract }end- context "when data is valid" do- context "when provided user does not exist" do- let(:user_id) { 0 }+ context "when provided user does not exist" do+ let(:user_id) { 0 }- it { is_expected.to fail_to_find_a_model(:user) }+ it { is_expected.to fail_to_find_a_model(:user) }+ end++ context "when user is already suspended" do+ before do+ UserSuspender.new(user, by_user: admin, suspended_till: suspend_until, reason:).suspend+ end++ it { is_expected.to fail_a_policy(:not_suspended_already) }+ end++ context "when all users cannot be suspended" do+ let(:other_user_ids) { [other_user.id, Fabricate(:admin).id].join(",") }++ it { is_expected.to fail_a_policy(:can_suspend_all_users) }+ end++ context "when everything's ok" do+ before { allow(User::Action::TriggerPostAction).to receive(:call) }++ it "suspends all provided users" do+ result+ expect([user, other_user].map(&:reload)).to all be_suspended+ end++ it "triggers a post action" do+ result+ expect(User::Action::TriggerPostAction).to have_received(:call).with(+ guardian:,+ post: nil,+ contract: result[:contract],+ )end- context "when provided user exists" do- context "when user is already suspended" do- before do- UserSuspender.new(user, by_user: admin, suspended_till: suspend_until, reason:).suspend- end-- it { is_expected.to fail_a_policy(:not_suspended_already) }- end-- context "when user is not already suspended" do- context "when all users cannot be suspended" do- let(:other_user_ids) { [other_user.id, Fabricate(:admin).id].join(",") }-- it { is_expected.to fail_a_policy(:can_suspend_all_users) }- end-- context "when all users can be suspended" do- before { allow(User::Action::TriggerPostAction).to receive(:call) }-- it "suspends all provided users" do- result- expect([user, other_user].map(&:reload)).to all be_suspended- end-- it "triggers a post action" do- result- expect(User::Action::TriggerPostAction).to have_received(:call).with(- guardian:,- post: nil,- contract: result[:contract],- )- end-- it "exposes the full reason in the result object" do- expect(result[:full_reason]).to eq("spam\n\nit was spam")- end- end- end+ it "exposes the full reason in the result object" do+ expect(result[:full_reason]).to eq("spam\n\nit was spam")endendend