Hide spoilers from push notification excerpts

Hide spoilers from push notification excerpts

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 6add9b4..e193460 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1,3 +1,5 @@
 en:
   site_settings:
-    spoiler_enabled: "Enable the spoiler plugin. If you change this, you must rebake all posts with: \"rake posts:rebake\"."
+    spoiler_enabled: 'Enable the spoiler plugin. If you change this, you must rebake all posts with: "rake posts:rebake".'
+  spoiler_alert:
+    excerpt_spoiler: "spoiler"
diff --git a/plugin.rb b/plugin.rb
index 8e39fff..f3cdb5a 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -23,4 +23,7 @@ after_initialize do
     fragment.css(".spoiler").remove
   end
 
+  on(:pre_notification_alert) do |user, payload|
+    payload[:excerpt] = payload[:excerpt].gsub(/\<span class=\"spoiler\"\>.*\<\/span\>/, "[#{I18n.t 'spoiler_alert.excerpt_spoiler'}]")
+  end
 end
diff --git a/spec/post_alerter_spec.rb b/spec/post_alerter_spec.rb
new file mode 100644
index 0000000..faaa963
--- /dev/null
+++ b/spec/post_alerter_spec.rb
@@ -0,0 +1,24 @@
+require 'rails_helper'
+
+RSpec.describe PostAlerter do
+  it 'can hide spoilers from notifications' do
+    user = Fabricate(:user)
+    reply = Fabricate(:post, topic: Fabricate(:topic), reply_to_post_number: 1, raw: 'This here is a <span class="spoiler">secret code</span>')
+
+    events = DiscourseEvent.track_events do
+      PostAlerter.new.create_notification(user, Notification.types[:replied], reply)
+    end
+
+    payload = {
+     notification_type: Notification.types[:replied],
+     post_number: reply.post_number,
+     topic_title: reply.topic.title,
+     topic_id: reply.topic.id,
+     excerpt: "This here is a [#{I18n.t 'spoiler_alert.excerpt_spoiler'}]",
+     username: reply.username,
+     post_url: reply.url
+    }
+
+    expect(events).to include(event_name: :pre_notification_alert, params: [user, payload])
+  end
+end

GitHub sha: 3ebe2b18

1 Like