A platform for community discussion. Free, open, simple.
DEV: Refactor StartReply & StopReply services a bit
Loïc Guitautedbd5e08f973
diff --git a/lib/service/base.rb b/lib/service/base.rbindex 62d2cde87bc..83c6eca8288 100644--- a/lib/service/base.rb+++ b/lib/service/base.rb@@ -129,6 +129,9 @@ module Service# @!visibility privateclass Step+ class DefaultValuesNotAllowed < StandardError+ end+attr_reader :name, :method_name, :class_namedef initialize(name, method_name = name, class_name: nil)@@ -157,7 +160,8 @@ module Serviceobject = class_name&.new(context)method = object&.method(:call) || instance.method(method_name)if !object && method.parameters.any? { _1[0] != :keyreq }- raise "In #{type} '#{name}': default values in step implementations are not allowed. Maybe they could be defined in a params or options block?"+ raise DefaultValuesNotAllowed,+ "In #{type} '#{name}': default values in step implementations are not allowed. Maybe they could be defined in a params or options block?"endargs = context.slice(*method.parameters.select { _1[0] == :keyreq }.map(&:last))context[result_key][:object] = object if object@@ -195,8 +199,10 @@ module Servicecontext[result_key].fail(invalid: true)context.fail!end- rescue ArgumentError => exception- context[result_key].fail(exception: exception, not_found: true)+ rescue Failure, DefaultValuesNotAllowed+ raise+ rescue => exception+ context[result_key].fail(exception:, not_found: true)context.fail!endenddiff --git a/plugins/chat/app/services/chat/start_reply.rb b/plugins/chat/app/services/chat/start_reply.rbindex 5362fac0796..355facc6fd8 100644--- a/plugins/chat/app/services/chat/start_reply.rb+++ b/plugins/chat/app/services/chat/start_reply.rb@@ -18,33 +18,32 @@ module Chat# @return [Service::Base::Context]params doattribute :channel_id, :integer+ attribute :thread_id, :integer+validates :channel_id, presence: true- attribute :thread_id, :integer+ def channel_name+ return "/chat-reply/#{channel_id}/thread/#{thread_id}" if thread_id+ "/chat-reply/#{channel_id}"+ endendmodel :presence_channelstep :generate_client_id- step :join_chat_reply_presence_channel+ try(PresenceChannel::InvalidAccess) { step :join_chat_reply_presence_channel }privatedef fetch_presence_channel(params:)- name = "/chat-reply/#{params.channel_id}"- name += "/thread/#{params.thread_id}" if params.thread_id- PresenceChannel.new(name)- rescue PresenceChannel::NotFound- nil+ PresenceChannel.new(params.channel_name)enddef generate_client_idcontext[:client_id] = SecureRandom.hexend- def join_chat_reply_presence_channel(presence_channel:, guardian:)- presence_channel.present(user_id: guardian.user.id, client_id: context.client_id)- rescue PresenceChannel::InvalidAccess- fail!("Presence channel not accessible by the user: #{guardian.user.id}")+ def join_chat_reply_presence_channel(presence_channel:, guardian:, client_id:)+ presence_channel.present(user_id: guardian.user.id, client_id:)endendenddiff --git a/plugins/chat/app/services/chat/stop_reply.rb b/plugins/chat/app/services/chat/stop_reply.rbindex ef986777175..9a6d883c647 100644--- a/plugins/chat/app/services/chat/stop_reply.rb+++ b/plugins/chat/app/services/chat/stop_reply.rb@@ -18,12 +18,16 @@ module Chat# @return [Service::Base::Context]params doattribute :channel_id, :integer- validates :channel_id, presence: true-attribute :client_id, :string+ attribute :thread_id, :integer++ validates :channel_id, presence: truevalidates :client_id, presence: true- attribute :thread_id, :integer+ def channel_name+ return "/chat-reply/#{channel_id}/thread/#{thread_id}" if thread_id+ "/chat-reply/#{channel_id}"+ endendmodel :presence_channel@@ -32,11 +36,7 @@ module Chatprivatedef fetch_presence_channel(params:)- name = "/chat-reply/#{params.channel_id}"- name += "/thread/#{params.thread_id}" if params.thread_id- PresenceChannel.new(name)- rescue PresenceChannel::NotFound- nil+ PresenceChannel.new(params.channel_name)enddef leave_chat_reply_presence_channel(presence_channel:, params:, guardian:)diff --git a/plugins/chat/lib/chat_sdk/channel.rb b/plugins/chat/lib/chat_sdk/channel.rbindex a2862212609..3e11e47a65e 100644--- a/plugins/chat/lib/chat_sdk/channel.rb+++ b/plugins/chat/lib/chat_sdk/channel.rb@@ -53,13 +53,7 @@ module ChatSDKenddef start_reply(channel_id:, thread_id: nil, guardian:)- Chat::StartReply.call(- guardian: guardian,- params: {- channel_id: channel_id,- thread_id: thread_id,- },- ) do+ Chat::StartReply.call(guardian:, params: { channel_id:, thread_id: }) doon_success { |client_id:| client_id }on_model_not_found(:presence_channel) { raise "Chat::Channel or Chat::Thread not found." }end@@ -84,14 +78,7 @@ module ChatSDKenddef stop_reply(channel_id:, thread_id: nil, client_id:, guardian:)- Chat::StopReply.call(- guardian: guardian,- params: {- client_id: client_id,- channel_id: channel_id,- thread_id: thread_id,- },- ) do+ Chat::StopReply.call(guardian:, params: { client_id:, channel_id:, thread_id: }) doon_model_not_found(:presence_channel) { raise "Chat::Channel or Chat::Thread not found." }endenddiff --git a/plugins/chat/spec/services/chat/start_reply_spec.rb b/plugins/chat/spec/services/chat/start_reply_spec.rbindex fef057c43be..2005e8a2391 100644--- a/plugins/chat/spec/services/chat/start_reply_spec.rb+++ b/plugins/chat/spec/services/chat/start_reply_spec.rb@@ -2,9 +2,27 @@RSpec.describe Chat::StartReply dodescribe described_class::Contract, type: :model do- subject(:contract) { described_class.new }-it { is_expected.to validate_presence_of :channel_id }++ describe "#channel_name" do+ subject(:contract) { described_class.new(channel_id: 1, thread_id:) }++ context "when thread_id is not present" do+ let(:thread_id) { nil }++ it "returns only the channel name" do+ expect(contract.channel_name).to eq("/chat-reply/1")+ end+ end++ context "when thread_id is present" do+ let(:thread_id) { 2 }++ it "returns the channel name with thread_id" do+ expect(contract.channel_name).to eq("/chat-reply/1/thread/2")+ end+ end+ endenddescribe ".call" do@@ -20,6 +38,12 @@ RSpec.describe Chat::StartReply dobefore { channel.add(guardian.user) }+ context "when data is invalid" do+ let(:params) { { channel_id: nil } }++ it { is_expected.to fail_a_contract }+ end+context "when the channel is not found" dobefore { params[:channel_id] = 999 }@@ -32,22 +56,26 @@ RSpec.describe Chat::StartReply doit { is_expected.to fail_to_find_a_model(:presence_channel) }end- it "generates a client id" do- expect(result.client_id).to be_present- end-- it "joins the presence channel" do- expect { result }.to change {- PresenceChannel.new("/chat-reply/#{channel.id}/thread/#{thread.id}").count- }.by(1)- end-context "when the user is not part of the channel" dofab!(:channel) { Fabricate(:private_category_channel, threading_enabled: true) }before { params[:thread_id] = nil }- it { is_expected.to fail_a_step(:join_chat_reply_presence_channel) }+ it { is_expected.to fail_with_exception(PresenceChannel::InvalidAccess) }+ end++ context "when everything’s ok" do+ it { is_expected.to run_successfully }++ it "generates a client id" do+ expect(result.client_id).to be_present+ end++ it "joins the presence channel" do+ expect { result }.to change {+ PresenceChannel.new("/chat-reply/#{channel.id}/thread/#{thread.id}").count+ }.by(1)+ endendendenddiff --git a/plugins/chat/spec/services/chat/stop_reply_spec.rb b/plugins/chat/spec/services/chat/stop_reply_spec.rbindex fd6cc1bb104..bb95eb5b7ea 100644--- a/plugins/chat/spec/services/chat/stop_reply_spec.rb+++ b/plugins/chat/spec/services/chat/stop_reply_spec.rb@@ -6,6 +6,26 @@ RSpec.describe Chat::StopReply doit { is_expected.to validate_presence_of :channel_id }it { is_expected.to validate_presence_of :client_id }++ describe "#channel_name" do+ subject(:contract) { described_class.new(channel_id: 1, thread_id:) }++ context "when thread_id is not present" do+ let(:thread_id) { nil }++ it "returns only the channel name" do+ expect(contract.channel_name).to eq("/chat-reply/1")+ end+ end++ context "when thread_id is present" do+ let(:thread_id) { 2 }++ it "returns the channel name with thread_id" do+ expect(contract.channel_name).to eq("/chat-reply/1/thread/2")+ end+ end+ endenddescribe ".call" do@@ -14,7 +34,8 @@ RSpec.describe Chat::StopReply dofab!(:user)fab!(:channel) { Fabricate(:chat_channel, threading_enabled: true) }fab!(:thread) { Fabricate(:chat_thread, channel: channel) }- fab!(:client_id) do++ let!(:client_id) doChat::StartReply.call(params: {channel_id: channel.id,@@ -23,13 +44,18 @@ RSpec.describe Chat::StopReply doguardian: user.guardian,).client_idend-let(:guardian) { user.guardian }- let(:params) { { client_id: client_id, channel_id: channel.id, thread_id: thread.id } }+ let(:params) { { client_id:, channel_id: channel.id, thread_id: thread.id } }let(:dependencies) { { guardian: } }before { channel.add(guardian.user) }+ context "when data is invalid" do+ let(:params) { { channel_id: nil } }++ it { is_expected.to fail_a_contract }+ end+context "when the channel is not found" dobefore { params[:channel_id] = 999 }@@ -42,10 +68,16 @@ RSpec.describe Chat::StopReply doit { is_expected.to fail_to_find_a_model(:presence_channel) }end- it "leaves the presence channel" do- presence_channel = PresenceChannel.new("/chat-reply/#{channel.id}/thread/#{thread.id}")+ context "when everything’s ok" do+ let!(:presence_channel) do+ PresenceChannel.new("/chat-reply/#{channel.id}/thread/#{thread.id}")+ end++ it { is_expected.to run_successfully }- expect { result }.to change { presence_channel.count }.by(-1)+ it "leaves the presence channel" do+ expect { result }.to change { presence_channel.count }.by(-1)+ endendendenddiff --git a/spec/lib/service/steps_inspector_spec.rb b/spec/lib/service/steps_inspector_spec.rbindex 6da91ac3ac2..3f1e6f2452e 100644--- a/spec/lib/service/steps_inspector_spec.rb+++ b/spec/lib/service/steps_inspector_spec.rb@@ -263,6 +263,20 @@ RSpec.describe Service::StepsInspector doendend+ context "when an exception occurs inside the model step" do+ before do+ class DummyService+ def fetch_model+ raise "BOOM"+ end+ end+ end++ it "returns an error related to the exception" do+ expect(error).to match(/BOOM \([^(]*RuntimeError[^)]*\)/)+ end+ end+context "when the model has errors" dobefore doclass DummyService