A platform for community discussion. Free, open, simple.
DEV: Make service contracts immutable
Loïc Guitautc78211cf8df6
diff --git a/lib/service/base.rb b/lib/service/base.rbindex d8def9c1481..8402bd4cdb3 100644--- a/lib/service/base.rb+++ b/lib/service/base.rb@@ -202,13 +202,18 @@ module Serviceattributes = class_name.attribute_names.map(&:to_sym)default_values = {}default_values = context[default_values_from].slice(*attributes) if default_values_from- contract = class_name.new(default_values.merge(context[:params].slice(*attributes)))+ contract =+ class_name.new(+ **default_values.merge(context[:params].slice(*attributes)),+ options: context[:options],+ )context[contract_name] = contractcontext[result_key] = Context.buildif contract.invalid?context[result_key].fail(errors: contract.errors, parameters: contract.raw_attributes)context.fail!end+ contract.freezeendprivatediff --git a/lib/service/contract_base.rb b/lib/service/contract_base.rbindex dfd40452d72..afab2bb335f 100644--- a/lib/service/contract_base.rb+++ b/lib/service/contract_base.rb@@ -8,12 +8,17 @@ class Service::ContractBasedelegate :slice, :merge, to: :to_hash- def [](key)- public_send(key)+ def initialize(*args, options: nil, **kwargs)+ @__options__ = options+ super(*args, **kwargs)+ end++ def options+ @__options__end- def []=(key, value)- public_send("#{key}=", value)+ def [](key)+ public_send(key)enddef to_hashdiff --git a/plugins/chat/app/services/chat/create_message.rb b/plugins/chat/app/services/chat/create_message.rbindex 4d70ffda7fd..05efac60f32 100644--- a/plugins/chat/app/services/chat/create_message.rb+++ b/plugins/chat/app/services/chat/create_message.rb@@ -46,6 +46,16 @@ module Chatvalidates :chat_channel_id, presence: truevalidates :message, presence: true, if: -> { upload_ids.blank? }++ after_validation do+ next if message.blank?+ self.message =+ TextCleaner.clean(+ message,+ strip_whitespaces: options.strip_whitespaces,+ strip_zero_width_spaces: true,+ )+ endendmodel :channelstep :enforce_membership@@ -57,7 +67,6 @@ module Chatpolicy :ensure_valid_thread_for_channelpolicy :ensure_thread_matches_parentmodel :uploads, optional: true- step :clean_messagemodel :message_instance, :instantiate_messagetransaction dostep :create_excerpt@@ -134,14 +143,6 @@ module Chatguardian.user.uploads.where(id: params[:upload_ids])end- def clean_message(params:, options:)- params[:message] = TextCleaner.clean(- params[:message],- strip_whitespaces: options.strip_whitespaces,- strip_zero_width_spaces: true,- )- end-def instantiate_message(channel:, guardian:, params:, uploads:, thread:, reply:, options:)channel.chat_messages.new(user: guardian.user,diff --git a/plugins/chat/app/services/chat/search_chatable.rb b/plugins/chat/app/services/chat/search_chatable.rbindex aab3a0dd996..a7e806b6a5e 100644--- a/plugins/chat/app/services/chat/search_chatable.rb+++ b/plugins/chat/app/services/chat/search_chatable.rb@@ -24,8 +24,9 @@ module Chatattribute :include_category_channels, :boolean, default: trueattribute :include_direct_message_channels, :boolean, default: trueattribute :excluded_memberships_channel_id, :integer++ after_validation { self.term = term&.downcase&.strip&.gsub(/^[@#]+/, "") }end- step :clean_termmodel :memberships, optional: truemodel :users, optional: truemodel :groups, optional: true@@ -34,10 +35,6 @@ module Chatprivate- def clean_term(params:)- params[:term] = params[:term]&.downcase&.strip&.gsub(/^[@#]+/, "")- end-def fetch_memberships(guardian:)::Chat::ChannelMembershipManager.all_for_user(guardian.user)enddiff --git a/plugins/chat/app/services/chat/update_message.rb b/plugins/chat/app/services/chat/update_message.rbindex 0a9168df1f6..b58a459abf3 100644--- a/plugins/chat/app/services/chat/update_message.rb+++ b/plugins/chat/app/services/chat/update_message.rb@@ -33,6 +33,16 @@ module Chatvalidates :message_id, presence: truevalidates :message, presence: true, if: -> { upload_ids.blank? }++ after_validation do+ next if message.blank?+ self.message =+ TextCleaner.clean(+ message,+ strip_whitespaces: options.strip_whitespaces,+ strip_zero_width_spaces: true,+ )+ endendmodel :messagemodel :uploads, optional: true@@ -40,7 +50,6 @@ module Chatmodel :membershippolicy :can_modify_channel_messagepolicy :can_modify_message- step :clean_messagetransaction dostep :modify_messagestep :update_excerpt@@ -90,14 +99,6 @@ module Chatguardian.can_edit_chat?(message)end- def clean_message(params:, options:)- params[:message] = TextCleaner.clean(- params[:message],- strip_zero_width_spaces: true,- strip_whitespaces: options.strip_whitespaces,- )- end-def modify_message(params:, message:, guardian:, uploads:)message.message = params[:message]message.last_editor_id = guardian.user.id