FEATURE: Skip sending 'linked' notifications for alert topics

FEATURE: Skip sending ‘linked’ notifications for alert topics

diff --git a/plugin.rb b/plugin.rb
index 4a418e6..8d3a457 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -102,12 +102,18 @@ after_initialize do
     scope.user&.include_alert_counts?
   end
 
+  on(:after_extract_linked_users) do |users, post|
+    if post.post_number == 1 && post.user == Discourse.system_user && post.topic.category&.slug == 'alerts' # TODO: don't hardcode the category slug
+      users.clear
+    end
+  end
+
   TopicQuery.add_custom_filter(
     DiscoursePrometheusAlertReceiver::PLUGIN_NAME
   ) do |results, topic_query|
 
     options = topic_query.options
-    category_id = Category.where(slug: 'alerts').pluck(:id).first
+    category_id = Category.where(slug: 'alerts').pluck(:id).first # TODO: don't hardcode the category slug
 
     if options[:category_id] == category_id && options[:status] == 'firing'
       results = results.firing_alerts
diff --git a/spec/integration/discourse_prometheus_alert_receiver/notification_overrides_spec.rb b/spec/integration/discourse_prometheus_alert_receiver/notification_overrides_spec.rb
new file mode 100644
index 0000000..409b774
--- /dev/null
+++ b/spec/integration/discourse_prometheus_alert_receiver/notification_overrides_spec.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+require 'rails_helper'
+
+RSpec.describe DiscoursePrometheusAlertReceiver::ReceiverController do
+  fab!(:user) { Fabricate(:user) }
+  fab!(:alert_category) { Fabricate(:category, slug: 'alerts') }
+  fab!(:runbook_topic) { Fabricate(:post, user: user).topic }
+
+  def create_topic(user:, category:)
+    topic = Fabricate(:topic, category: category, user: user)
+    post = Fabricate(:post, topic: topic, user: user, raw: <<~RAW)
+      The runbook can be found here: #{runbook_topic.url}
+    RAW
+    TopicLink.extract_from(post)
+    PostAlerter.post_created(post)
+  end
+
+  it "prevents link notifications on alert posts" do
+    create_topic(user: Discourse.system_user, category: alert_category)
+    expect(Notification.count).to eq(0)
+  end
+
+  it "doesn't break non-system posts" do
+    create_topic(user: Fabricate(:user), category: alert_category)
+    expect(Notification.count).to eq(1)
+  end
+
+  it "doesn't affect other categories" do
+    create_topic(user: Discourse.system_user, category: Fabricate(:category))
+    expect(Notification.count).to eq(1)
+  end
+
+end

GitHub sha: a1bbbb81

1 Like