A platform for community discussion. Free, open, simple.
DEV: Move chat message rebake into a service object (#38020)
Joffrey JAFFEUX13fd9a049768
diff --git a/plugins/chat/app/controllers/chat/chat_controller.rb b/plugins/chat/app/controllers/chat/chat_controller.rbindex 1af55e9d3c6..90fa5024604 100644--- a/plugins/chat/app/controllers/chat/chat_controller.rb+++ b/plugins/chat/app/controllers/chat/chat_controller.rb@@ -2,12 +2,8 @@module Chatclass ChatController < ::Chat::BaseController- # Other endpoints use set_channel_and_chatable_with_access_check, but- # these endpoints require a standalone find because they need to be- # able to get deleted channels and recover them.- before_action :find_chat_message, only: %i[rebake]before_action :set_channel_and_chatable_with_access_check,- except: %i[respond set_user_chat_status dismiss_retention_reminder]+ except: %i[respond set_user_chat_status dismiss_retention_reminder rebake]def respondrender@@ -27,9 +23,20 @@ module Chatenddef rebake- guardian.ensure_can_rebake_chat_message!(@message)- @message.rebake!(invalidate_oneboxes: true)- render json: success_json+ Chat::RebakeMessage.call(service_params) do+ on_success { render(json: success_json) }+ on_failure { render(json: failed_json, status: :unprocessable_entity) }+ on_model_not_found(:channel) { raise Discourse::NotFound }+ on_failed_policy(:can_access_channel) { raise Discourse::InvalidAccess }+ on_model_not_found(:message) { raise Discourse::NotFound }+ on_failed_policy(:can_rebake) { raise Discourse::InvalidAccess }+ on_failed_contract do |contract|+ render(+ json: failed_json.merge(errors: contract.errors.full_messages),+ status: :bad_request,+ )+ end+ endenddef set_user_chat_status@@ -70,35 +77,5 @@ module Chat).generate_markdownrender json: success_json.merge(markdown: markdown)end-- private-- def preloaded_chat_message_query- query =- Chat::Message- .includes(in_reply_to: [:user, chat_webhook_event: [:incoming_chat_webhook]])- .includes(:revisions)- .includes(user: :primary_group)- .includes(chat_webhook_event: :incoming_chat_webhook)- .includes(reactions: :user)- .includes(:bookmarks)- .includes(uploads: { optimized_videos: :optimized_upload })- .includes(chat_channel: :chatable)- .includes(:thread)- .includes(:chat_mentions)-- query = query.includes(user: :user_status) if SiteSetting.enable_user_status-- query- end-- def find_chat_message- @message = preloaded_chat_message_query.with_deleted- @message = @message.where(chat_channel_id: params[:chat_channel_id]) if params[- :chat_channel_id- ]- @message = @message.find_by(id: params[:message_id])- raise Discourse::NotFound unless @message- endendenddiff --git a/plugins/chat/app/services/chat/rebake_message.rb b/plugins/chat/app/services/chat/rebake_message.rbnew file mode 100644index 00000000000..afde79d3b0f--- /dev/null+++ b/plugins/chat/app/services/chat/rebake_message.rb@@ -0,0 +1,59 @@+# frozen_string_literal: true++module Chat+ # Service responsible for rebaking a chat message.+ #+ # @example+ # Chat::RebakeMessage.call(params: { message_id: 2, chat_channel_id: 1 }, guardian: guardian)+ #+ class RebakeMessage+ include Service::Base++ # @!method self.call(guardian:, params:)+ # @param [Guardian] guardian+ # @param [Hash] params+ # @option params [Integer] :message_id+ # @option params [Integer] :chat_channel_id+ # @return [Service::Base::Context]++ params do+ attribute :message_id, :integer+ attribute :chat_channel_id, :integer++ validates :message_id, presence: true+ validates :chat_channel_id, presence: true+ end++ model :channel+ policy :can_access_channel+ model :message+ policy :can_rebake++ step :rebake_message++ private++ def fetch_channel(params:)+ Chat::Channel.includes(:chatable).find_by(id: params.chat_channel_id)+ end++ def can_access_channel(guardian:, channel:)+ guardian.can_join_chat_channel?(channel)+ end++ def fetch_message(params:, channel:)+ Chat::Message+ .includes(chat_channel: :chatable)+ .with_deleted+ .find_by(id: params.message_id, chat_channel_id: channel.id)+ end++ def can_rebake(guardian:, message:)+ guardian.can_rebake_chat_message?(message)+ end++ def rebake_message(message:)+ message.rebake!(invalidate_oneboxes: true)+ end+ end+enddiff --git a/plugins/chat/spec/requests/chat_controller_spec.rb b/plugins/chat/spec/requests/chat_controller_spec.rbindex aca6fe419cf..58c67622df3 100644--- a/plugins/chat/spec/requests/chat_controller_spec.rb+++ b/plugins/chat/spec/requests/chat_controller_spec.rb@@ -33,88 +33,44 @@ RSpec.describe Chat::ChatController dodescribe "#rebake" dofab!(:chat_message) { Fabricate(:chat_message, chat_channel: chat_channel, user: user) }- context "as staff" do- it "rebakes the post" do- sign_in(Fabricate(:admin))-- expect_enqueued_with(- job: Jobs::Chat::ProcessMessage,- args: {- chat_message_id: chat_message.id,- },- ) do- put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"-- expect(response.status).to eq(200)- end+ it "works" do+ sign_in(admin)+ put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"+ expect(response.status).to eq(200)+ end++ context "when message does not exist" do+ it "returns a 404" do+ sign_in(admin)+ put "/chat/#{chat_channel.id}/-999/rebake.json"+ expect(response.status).to eq(404)end+ end- it "does not interfere with core's guardian can_rebake? for posts" do- sign_in(Fabricate(:admin))- put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"- expect(response.status).to eq(200)- post = Fabricate(:post)- put "/posts/#{post.id}/rebake.json"- expect(response.status).to eq(200)+ context "when channel does not exist" do+ it "returns a 404" do+ sign_in(admin)+ put "/chat/-999/#{chat_message.id}/rebake.json"+ expect(response.status).to eq(404)end+ end- it "does not rebake the post when channel is read_only" do- chat_message.chat_channel.update!(status: :read_only)- sign_in(Fabricate(:admin))+ context "when user cannot access the channel" do+ fab!(:inaccessible_channel, :private_category_channel)- put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"+ it "returns a 403" do+ sign_in(Fabricate(:user))+ put "/chat/#{inaccessible_channel.id}/-999/rebake.json"expect(response.status).to eq(403)end-- context "when cooked has changed" do- it "marks the message as dirty" do- sign_in(Fabricate(:admin))- chat_message.update!(message: "new content")-- expect_enqueued_with(- job: Jobs::Chat::ProcessMessage,- args: {- chat_message_id: chat_message.id,- },- ) do- put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"-- expect(response.status).to eq(200)- end- end- endend- context "when not staff" do- it "forbids non staff to rebake" do+ context "when user cannot rebake" do+ it "returns a 403" dosign_in(Fabricate(:user))put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"expect(response.status).to eq(403)end-- context "as TL3 user" do- it "forbids less then TL4 user tries to rebake" do- sign_in(Fabricate(:user, trust_level: TrustLevel[3]))- put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"- expect(response.status).to eq(403)- end- end-- context "as TL4 user" do- it "allows TL4 users to rebake" do- sign_in(Fabricate(:user, trust_level: TrustLevel[4]))- put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"- expect(response.status).to eq(200)- end-- it "does not rebake the post when channel is read_only" do- chat_message.chat_channel.update!(status: :read_only)- sign_in(Fabricate(:user, trust_level: TrustLevel[4]))-- put "/chat/#{chat_channel.id}/#{chat_message.id}/rebake.json"- expect(response.status).to eq(403)- end- endendenddiff --git a/plugins/chat/spec/services/chat/rebake_message_spec.rb b/plugins/chat/spec/services/chat/rebake_message_spec.rbnew file mode 100644index 00000000000..bf681ef0059--- /dev/null+++ b/plugins/chat/spec/services/chat/rebake_message_spec.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++RSpec.describe Chat::RebakeMessage do+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:message_id) }+ it { is_expected.to validate_presence_of(:chat_channel_id) }+ end++ describe ".call" do+ subject(:result) { described_class.call(params:, **dependencies) }++ fab!(:admin)+ fab!(:message, :chat_message)++ let(:guardian) { Guardian.new(admin) }+ let(:params) { { message_id: message.id, chat_channel_id: message.chat_channel_id } }+ let(:dependencies) { { guardian: } }++ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ end++ context "when params are not valid" do+ let(:params) { {} }++ it { is_expected.to fail_a_contract }+ end++ context "when the channel does not exist" do+ let(:params) { { message_id: message.id, chat_channel_id: -1 } }++ it { is_expected.to fail_to_find_a_model(:channel) }+ end++ context "when the user cannot access the channel" do+ fab!(:private_channel) do+ Fabricate(+ :private_category_channel,+ group: Fabricate(:group),+ chatable: Fabricate(:private_category, group: Fabricate(:group)),+ )+ end++ let(:current_user) { Fabricate(:user) }+ let(:guardian) { Guardian.new(current_user) }+ let(:params) { { message_id: -999, chat_channel_id: private_channel.id } }++ it { is_expected.to fail_a_policy(:can_access_channel) }+ end++ context "when the message does not exist" do+ let(:params) { { message_id: -1, chat_channel_id: message.chat_channel_id } }++ it { is_expected.to fail_to_find_a_model(:message) }+ end++ context "when the message does not belong to the channel" do+ let(:other_channel) { Fabricate(:category_channel) }+ let(:params) { { message_id: message.id, chat_channel_id: other_channel.id } }++ it { is_expected.to fail_to_find_a_model(:message) }+ end++ context "when the user does not have permission to rebake" do+ let(:current_user) { Fabricate(:user) }+ let(:guardian) { Guardian.new(current_user) }++ it { is_expected.to fail_a_policy(:can_rebake) }+ end++ context "when the user has permission to rebake" do+ it { is_expected.to run_successfully }++ it "enqueues a process message job" do+ expect_enqueued_with(+ job: Jobs::Chat::ProcessMessage,+ args: {+ chat_message_id: message.id,+ },+ ) { result }+ end+ end+ end+end