mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-09 08:10:08 +09:00
Reuse native plain text conversion for attachment-free content
This commit is contained in:
@@ -21,7 +21,15 @@ class Message < ApplicationRecord
|
||||
scope :with_presentation, -> { with_creator.with_attachment_details.with_boosts.preload(:room) }
|
||||
|
||||
def plain_text_body
|
||||
body.to_plain_text.presence || attachment&.filename&.to_s || ""
|
||||
content = body.body
|
||||
text = if content && content.fragment.find_all(ActionText::Attachment.tag_name).empty?
|
||||
# ActionText::Content#to_plain_text duplicates the fragment to replace
|
||||
# attachments. Without them, reuse its native Fragment conversion instead.
|
||||
content.fragment.to_plain_text.dup
|
||||
else
|
||||
body.to_plain_text
|
||||
end
|
||||
text.presence || attachment&.filename&.to_s || ""
|
||||
end
|
||||
|
||||
def content_type
|
||||
|
||||
@@ -17,6 +17,28 @@ class MessageTest < ActiveSupport::TestCase
|
||||
assert_not Message.new(body: "🔥 💯").plain_text_body.all_emoji?
|
||||
end
|
||||
|
||||
test "plain text keeps Action Text formatting and attachment conversion" do
|
||||
bodies = [ nil, "", "Hello & goodbye", "<div>First<br>second</div>", "<p>One</p><p>Two</p>",
|
||||
"<ul><li>First</li><li><strong>Second</strong></li></ul>", "<blockquote>Quoted</blockquote>",
|
||||
"<pre><code>one\n two</code></pre>", "<table><tr><td>one</td><td>two</td></tr></table>",
|
||||
"<div><em>Unclosed", "😄🤘", "<div>Hi #{mention_attachment_for(:david)}</div>" ]
|
||||
bodies.each do |html|
|
||||
message = Message.new(body: html)
|
||||
assert_equal message.body.to_plain_text, message.plain_text_body, html.inspect
|
||||
end
|
||||
end
|
||||
|
||||
test "plain text follows body replacement and does not expose mutable cached text" do
|
||||
message = Message.new(body: "<p>First</p>")
|
||||
message.plain_text_body.replace("Caller edit")
|
||||
assert_equal "First", message.plain_text_body
|
||||
message.body = "<p>Replacement</p>"
|
||||
assert_equal "Replacement", message.plain_text_body
|
||||
message = messages(:first)
|
||||
message.update!(body: "<p>Reloaded body</p>")
|
||||
assert_equal message.body.to_plain_text, message.reload.plain_text_body
|
||||
end
|
||||
|
||||
test "mentionees" do
|
||||
message = Message.new room: rooms(:pets), body: "<div>Hey #{mention_attachment_for(:david)}</div>", creator: users(:jason), client_message_id: "earth"
|
||||
assert_equal [ users(:david) ], message.mentionees
|
||||
|
||||
Reference in New Issue
Block a user