diff --git a/lib/service/base.rb b/lib/service/base.rb index d8def9c1481..8402bd4cdb3 100644 --- a/lib/service/base.rb +++ b/lib/service/base.rb @@ -202,13 +202,18 @@ module Service attributes = 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] = contract context[result_key] = Context.build if contract.invalid? context[result_key].fail(errors: contract.errors, parameters: contract.raw_attributes) context.fail! end + contract.freeze end private diff --git a/lib/service/contract_base.rb b/lib/service/contract_base.rb index dfd40452d72..afab2bb335f 100644 --- a/lib/service/contract_base.rb +++ b/lib/service/contract_base.rb @@ -8,12 +8,17 @@ class Service::ContractBase delegate :slice, :merge, to: :to_hash - def [](key) - public_send(key) + def initialize(*args, options: nil, **kwargs) + @__options__ = options + super(*args, **kwargs) end - def []=(key, value) - public_send("#{key}=", value) + def options + @__options__ + end + + def [](key) + public_send(key) end def to_hash diff --git a/plugins/chat/app/services/chat/create_message.rb b/plugins/chat/app/services/chat/create_message.rb index 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 Chat validates :chat_channel_id, presence: true validates :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, + ) + end end model :channel step :enforce_membership @@ -57,7 +67,6 @@ module Chat policy :ensure_valid_thread_for_channel policy :ensure_thread_matches_parent model :uploads, optional: true - step :clean_message model :message_instance, :instantiate_message transaction do step :create_excerpt @@ -134,14 +143,6 @@ module Chat guardian.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.rb index 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 Chat attribute :include_category_channels, :boolean, default: true attribute :include_direct_message_channels, :boolean, default: true attribute :excluded_memberships_channel_id, :integer + + after_validation { self.term = term&.downcase&.strip&.gsub(/^[@#]+/, "") } end - step :clean_term model :memberships, optional: true model :users, optional: true model :groups, optional: true @@ -34,10 +35,6 @@ module Chat private - def clean_term(params:) - params[:term] = params[:term]&.downcase&.strip&.gsub(/^[@#]+/, "") - end - def fetch_memberships(guardian:) ::Chat::ChannelMembershipManager.all_for_user(guardian.user) end diff --git a/plugins/chat/app/services/chat/update_message.rb b/plugins/chat/app/services/chat/update_message.rb index 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 Chat validates :message_id, presence: true validates :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, + ) + end end model :message model :uploads, optional: true @@ -40,7 +50,6 @@ module Chat model :membership policy :can_modify_channel_message policy :can_modify_message - step :clean_message transaction do step :modify_message step :update_excerpt @@ -90,14 +99,6 @@ module Chat guardian.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