DEV: Make service contracts immutable

We decided to make contracts immutable once their validations have run.
Indeed, it doesn’t make a lot of sense to modify a contract value
outside the contract itself.

If processing is needed, then it should happen inside the contract
itself.
This commit is contained in:
Loïc Guitaut
2024-10-28 16:04:39 +01:00
committed by Loïc Guitaut
parent 6d918c6307
commit c78211cf8d
5 changed files with 37 additions and 28 deletions

View File

@ -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,

View File

@ -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

View File

@ -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