Files
once-campfire/app/models/message.rb
Raul Popadineti 03d1c45d97 Refactor message loading in RoomsController to use combined scopes
- Simplified message queries in RoomsController#find_messages by replacing multiple includes and preloads with consolidated scopes: with_creator, with_attachment_details, and with_boosts.
- Defined new scopes in Message model to handle rich text, attachments, and boosts associations for cleaner and more maintainable code.
2025-09-17 13:39:30 +03:00

45 lines
1.1 KiB
Ruby

class Message < ApplicationRecord
include Attachment, Broadcasts, Mentionee, Pagination, Searchable
belongs_to :room, touch: true
belongs_to :creator, class_name: "User", default: -> { Current.user }
has_many :boosts, dependent: :destroy
has_rich_text :body
before_create -> { self.client_message_id ||= Random.uuid } # Bots don't care
after_create_commit -> { room.receive(self) }
scope :ordered, -> { order(:created_at) }
scope :with_creator, -> { preload(creator: :avatar_attachment) }
scope :with_attachment_details, -> {
with_rich_text_body_and_embeds
with_attached_attachment
.includes(attachment_blob: :variant_records)
}
scope :with_boosts, -> { includes(boosts: :booster) }
def plain_text_body
body.to_plain_text.presence || attachment&.filename&.to_s || ""
end
def to_key
[ client_message_id ]
end
def content_type
case
when attachment? then "attachment"
when sound.present? then "sound"
else "text"
end.inquiry
end
def sound
plain_text_body.match(/\A\/play (?<name>\w+)\z/) do |match|
Sound.find_by_name match[:name]
end
end
end