mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Refactor a little chat uploads
This is extracted from #22390. This patch simplifies a little how we handle uploads in chat, relying on ActiveRecord mechanisms instead of calling custom methods. This also makes `Chat::Message#validate_message` a “real” AR validation, meaning it will run automatically when `#valid?` is called.
This commit is contained in:
committed by
Loïc Guitaut
parent
0db98e9d86
commit
5d2ec6461d
@@ -73,9 +73,11 @@ module Chat
|
||||
|
||||
before_save { ensure_last_editor_id }
|
||||
|
||||
validate :validate_message
|
||||
|
||||
def self.polymorphic_class_mapping = { "ChatMessage" => Chat::Message }
|
||||
|
||||
def validate_message(has_uploads:)
|
||||
def validate_message
|
||||
self.message =
|
||||
TextCleaner.clean(self.message, strip_whitespaces: true, strip_zero_width_spaces: true)
|
||||
|
||||
@@ -85,7 +87,7 @@ module Chat
|
||||
Chat::DuplicateMessageValidator.new(self).validate
|
||||
end
|
||||
|
||||
if !has_uploads && message_too_short?
|
||||
if uploads.empty? && message_too_short?
|
||||
self.errors.add(
|
||||
:base,
|
||||
I18n.t(
|
||||
@@ -103,23 +105,6 @@ module Chat
|
||||
end
|
||||
end
|
||||
|
||||
def attach_uploads(uploads)
|
||||
return if uploads.blank? || self.new_record?
|
||||
|
||||
now = Time.now
|
||||
ref_record_attrs =
|
||||
uploads.map do |upload|
|
||||
{
|
||||
upload_id: upload.id,
|
||||
target_id: self.id,
|
||||
target_type: self.class.polymorphic_name,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
}
|
||||
end
|
||||
UploadReference.insert_all!(ref_record_attrs)
|
||||
end
|
||||
|
||||
def excerpt(max_length: 50)
|
||||
# just show the URL if the whole message is a URL, because we cannot excerpt oneboxes
|
||||
return message if UrlHelper.relaxed_parse(message).is_a?(URI)
|
||||
|
||||
@@ -184,7 +184,7 @@ module Chat
|
||||
context.threads =
|
||||
::Chat::Thread
|
||||
.strict_loading
|
||||
.includes(last_message: [:user], original_message_user: :user_status)
|
||||
.includes(last_message: %i[user uploads], original_message_user: :user_status)
|
||||
.where(id: messages.map(&:thread_id).compact.uniq)
|
||||
|
||||
# Saves us having to load the same message we already have.
|
||||
|
||||
Reference in New Issue
Block a user