FIX: don't blow up trying to replace spoiler with post url when post is nil

FIX: don’t blow up trying to replace spoiler with post url when post is nil

I think this can be avoided by fixing this in discourse core by maing email_excerpt accepts an optional post param https://github.com/discourse/discourse/blob/08371db0cc2581bf054734b6f31fe752e8d5c945/app/helpers/user_notifications_helper.rb#L54

Will send a PR for this ^^^

diff --git a/plugin.rb b/plugin.rb
index db2f124..98fdc82 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -15,6 +15,10 @@ after_initialize do
 
   on(:reduce_cooked) do |fragment, post|
     fragment.css(".spoiler").each do |el|
+      if !post
+        el.inner_html = I18n.t("spoiler_alert.excerpt_spoiler")
+        next
+      end
       link = fragment.document.create_element("a")
       link["href"] = post.url
       link.content = I18n.t("spoiler_alert.excerpt_spoiler")
diff --git a/spec/pretty_text_spec.rb b/spec/pretty_text_spec.rb
index 37babd4..a20fc9e 100644
--- a/spec/pretty_text_spec.rb
+++ b/spec/pretty_text_spec.rb
@@ -31,4 +31,12 @@ describe PrettyText do
 
     expect(md).to eq(html)
   end
+
+  it 'doesn\'t blow up when post is nil' do
+    md = PrettyText.cook('I like watching [spoiler]my tests fail[/spoiler]')
+    md = PrettyText.format_for_email(md, nil)
+    html = "<p>I like watching <span class=\"spoiler\">spoiler</span></p>"
+
+    expect(md).to eq(html)
+  end
 end

GitHub sha: a1ce55b7

I think we should revert that and let it blow up so that we can find all the calls that needs to be fixed.

1 Like

Sure, will revert now

This PR should fix the root cause in core: Make `email_excerpt` method take an optional `post` param by OsamaSayegh · Pull Request #7570 · discourse/discourse · GitHub. I searched through the code base and I think I’ve covered all possible cases that could trigger this error.

Revert "FIX: don't blow up trying to replace spoiler with post url when post is"

We have this failing on a customer site now …

format_for_email is called without post at least in one spot

@OsamaSayegh already fixed it in

1 Like