Skip to content

A platform for community discussion. Free, open, simple.

FIX: User profiles are oneboxed badly in emails (#38403)

Natalie Taye986ed884390
diff --git a/lib/email/styles.rb b/lib/email/styles.rb
index 8643a8bd2f0..10be4f3505f 100644
--- a/lib/email/styles.rb
+++ b/lib/email/styles.rb
@@ -159,6 +159,44 @@ module Email
)
style(".onebox-avatar-inline", ONEBOX_INLINE_AVATAR_STYLE)
+ # User onebox: convert to table layout for email clients
+ @fragment
+ .css(".user-onebox")
+ .each do |onebox|
+ img = onebox.at_css(".aspect-image img")
+ next unless img
+
+ img["width"] = "80"
+ img["height"] = "80"
+ img["style"] = "width: 80px; height: 80px; display: block;"
+
+ h3 = onebox.at_css("h3")
+ h3["style"] = "font-size: 1.17em; margin: 0;" if h3
+
+ info_div = h3&.next_element
+ if info_div
+ info_div.css(".full-name, .location").each { |el| add_styles(el, "margin-right: 10px") }
+ end
+
+ joined = onebox.at_css(".user-onebox--joined")
+ joined["style"] = "color: #919191;" if joined
+
+ top_content = [h3, info_div, onebox.at_css("p")].compact.map(&:to_html).join
+ joined_html = joined ? joined.to_html : ""
+
+ onebox.inner_html = <<~HTML
+ <table cellspacing="0" cellpadding="0" border="0">
+ <tr>
+ <td rowspan="2" valign="top" style="width: 90px;">#{img.to_html}</td>
+ <td valign="top">#{top_content}</td>
+ </tr>
+ <tr>
+ <td valign="bottom">#{joined_html}</td>
+ </tr>
+ </table>
+ HTML
+ end
+
@fragment.css(".github-body-container .excerpt").remove
@fragment.css("aside.quote blockquote > p").each { |p| p["style"] = "padding: 0;" }
diff --git a/spec/lib/email/styles_spec.rb b/spec/lib/email/styles_spec.rb
index e3f2602f6a5..8dd3fc7aa78 100644
--- a/spec/lib/email/styles_spec.rb
+++ b/spec/lib/email/styles_spec.rb
@@ -244,6 +244,50 @@ RSpec.describe Email::Styles do
expect(fragment.to_s.squish).to match(%r{^<blockquote.+</blockquote>$})
end
+ it "styles user onebox avatar and layout for email" do
+ user_onebox_html = <<~HTML
+ <aside class="onebox">
+ <article class="onebox-body user-onebox">
+ <div class="aspect-image" style="--aspect-ratio:144/144;">
+ <img class="avatar" src="#{Discourse.base_url}/user_avatar/test.localhost/testuser/144/1_2.png">
+ </div>
+ <h3><a href="#{Discourse.base_url}/u/testuser">@testuser</a></h3>
+ <div>
+ <span class="full-name">Test User</span>
+ <span class="location">Earth</span>
+ </div>
+ <p>Bio here</p>
+ <div class="user-onebox--joined">Joined Jan 1, 2020</div>
+ </article>
+ <div class="clearfix"></div>
+ </aside>
+ HTML
+
+ fragment = html_fragment(user_onebox_html)
+
+ avatar_td = fragment.at("td[rowspan='2']")
+ expect(avatar_td).to be_present
+ expect(avatar_td["valign"]).to eq("top")
+ expect(avatar_td["style"]).to include("width:90px")
+
+ avatar_img = avatar_td.at("img")
+ expect(avatar_img["width"]).to eq("80")
+ expect(avatar_img["height"]).to eq("80")
+ expect(avatar_img["style"]).to include("width:80px")
+ expect(avatar_img["style"]).to include("height:80px")
+
+ username_h3 = fragment.at(".user-onebox h3")
+ expect(username_h3["style"]).to include("margin:0")
+ expect(username_h3["style"]).not_to include("margin:30px")
+
+ expect(fragment.at(".full-name")["style"]).to include("margin-right:10px")
+ expect(fragment.at(".location")["style"]).to include("margin-right:10px")
+
+ joined_td = fragment.css("td[valign='bottom']").last
+ expect(joined_td).to be_present
+ expect(joined_td.at(".user-onebox--joined")["style"]).to include("color:#919191")
+ end
+
it "removes GitHub excerpts" do
stub_request(:head, "https://github.com/discourse/discourse/pull/1253").to_return(
status: 200,