A platform for community discussion. Free, open, simple.
FIX: Improve event rendering in emails (#38420)
Natalie Tay1cc77a6934a9
diff --git a/plugins/discourse-calendar/plugin.rb b/plugins/discourse-calendar/plugin.rbindex cfa612d8bb5..9c37ce3bf5f 100644--- a/plugins/discourse-calendar/plugin.rb+++ b/plugins/discourse-calendar/plugin.rb@@ -538,18 +538,61 @@ after_initialize dofragment.css(".discourse-post-event").each do |event_node|+ tz = event_node["data-timezone"] || "UTC"starts_at = event_node["data-start"]ends_at = event_node["data-end"]- dates = "#{starts_at} (#{event_node["data-timezone"] || "UTC"})"- dates = "#{dates} → #{ends_at} (#{event_node["data-timezone"] || "UTC"})" if ends_at++ formatted_start =+ begin+ DateTime.parse(starts_at).strftime("%B %-d, %Y %-I:%M %p")+ rescue StandardError+ starts_at+ end+ dates = "#{formatted_start} (#{tz})"++ if ends_at+ formatted_end =+ begin+ DateTime.parse(ends_at).strftime("%B %-d, %Y %-I:%M %p")+ rescue StandardError+ ends_at+ end+ dates = "#{dates} → #{formatted_end} (#{tz})"+ endevent_name = event_node["data-name"] || post.topic.title- event_node.replace <<~TXT- <div style='border:1px solid #dedede'>- <p><a href="#{Discourse.base_url}#{post.url}">#{CGI.escape_html(event_name)}</a></p>- <p>#{CGI.escape_html(dates)}</p>- </div>- TXT+ location = event_node["data-location"]+ url = event_node["data-url"]++ rows = +""+ rows << <<~HTML+ <tr>+ <td style="padding: 12px;">+ <a href="#{Discourse.base_url}#{post.url}" style="font-weight: bold; font-size: 1.1em;">#{CGI.escape_html(event_name)}</a>+ </td>+ </tr>+ <tr>+ <td style="padding: 0 12px 12px; color: #666;">#{CGI.escape_html(dates)}</td>+ </tr>+ HTML++ rows << <<~HTML if location.present?+ <tr>+ <td style="padding: 0 12px 12px; color: #666;">#{CGI.escape_html(location)}</td>+ </tr>+ HTML++ rows << <<~HTML if url.present?+ <tr>+ <td style="padding: 0 12px 12px;"><a href="#{CGI.escape_html(url)}">#{CGI.escape_html(url)}</a></td>+ </tr>+ HTML++ event_node.replace <<~HTML+ <table cellspacing="0" cellpadding="0" border="0" style="border: 1px solid #dedede; margin-bottom: 10px;">+ #{rows}+ </table>+ HTMLendendenddiff --git a/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rb b/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rbindex 49947a4cbbc..b139fcc0463 100644--- a/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rb+++ b/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rb@@ -10,20 +10,18 @@ describe PrettyText docontext "with a public event" dodescribe "An event is displayed in an email" do- let(:user_1) { Fabricate(:user, admin: true) }+ fab!(:user_1, :user) { Fabricate(:user, admin: true) }context "when the event has no name" dolet(:post_1) { create_post_with_event(user_1) }- it "displays the topic title" do+ it "displays the topic title with formatted date" docooked = PrettyText.cook(post_1.raw)+ result = PrettyText.format_for_email(cooked, post_1)- expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)- <div style='border:1px solid #dedede'>- <p><a href="#{Discourse.base_url}#{post_1.url}">#{post_1.topic.title}</a></p>- <p>2018-06-05T18:39:50.000Z (UTC)</p>- </div>- HTML+ expect(result).to include(post_1.topic.title)+ expect(result).to include("June 5, 2018 6:39 PM (UTC)")+ expect(result).to include("table")endend@@ -35,39 +33,30 @@ describe PrettyText doit "displays the event name" docooked = PrettyText.cook(post_1.raw)+ result = PrettyText.format_for_email(cooked, post_1)- expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)- <div style='border:1px solid #dedede'>- <p><a href="#{Discourse.base_url}#{post_1.url}">Pancakes event</a></p>- <p>2018-06-05T18:39:50.000Z (UTC)</p>- </div>- HTML+ expect(result).to include("Pancakes event")+ expect(result).to include("font-weight: bold")endit "properly escapes title" docooked = PrettyText.cook(post_2.raw)+ result = PrettyText.format_for_email(cooked, post_2)- expect(PrettyText.format_for_email(cooked, post_2)).to match_html(<<~HTML)- <div style='border:1px solid #dedede'>- <p><a href="#{Discourse.base_url}#{post_2.url}">Pancakes event <a>with html chars</a></a></p>- <p>2018-06-05T18:39:50.000Z (UTC)</p>- </div>- HTML+ expect(result).to include("Pancakes event <a>with html chars</a>")endendcontext "when the event has an end date" dolet(:post_1) { create_post_with_event(user_1, 'end="2018-06-22"') }- it "displays the end date" do+ it "displays the formatted end date" docooked = PrettyText.cook(post_1.raw)+ result = PrettyText.format_for_email(cooked, post_1)- expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)- <div style='border:1px solid #dedede'>- <p><a href="#{Discourse.base_url}#{post_1.url}">#{post_1.topic.title}</a></p>- <p>2018-06-05T18:39:50.000Z (UTC) → 2018-06-22 (UTC)</p>- </div>- HTML+ expect(result).to include("June 5, 2018 6:39 PM (UTC)")+ expect(result).to include("→")+ expect(result).to include("June 22, 2018 12:00 AM (UTC)")endend@@ -76,13 +65,33 @@ describe PrettyText doit "uses the timezone" docooked = PrettyText.cook(post_1.raw)+ result = PrettyText.format_for_email(cooked, post_1)- expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)- <div style='border:1px solid #dedede'>- <p><a href="#{Discourse.base_url}#{post_1.url}">#{post_1.topic.title}</a></p>- <p>2018-06-05T18:39:50.000Z (America/New_York)</p>- </div>- HTML+ expect(result).to include("(America/New_York)")+ expect(result).to include("June 5, 2018 6:39 PM")+ end+ end++ context "when the event has a location" do+ let(:post_1) { create_post_with_event(user_1, 'location="Conference Room A"') }++ it "displays the location" do+ cooked = PrettyText.cook(post_1.raw)+ result = PrettyText.format_for_email(cooked, post_1)++ expect(result).to include("Conference Room A")+ end+ end++ context "when the event has a url" do+ let(:post_1) { create_post_with_event(user_1, 'url="https://example.com/meeting"') }++ it "displays the url" do+ cooked = PrettyText.cook(post_1.raw)+ result = PrettyText.format_for_email(cooked, post_1)++ expect(result).to include('href="https://example.com/meeting"')+ expect(result).to include("https://example.com/meeting")endendend