Skip to content

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

FIX: Hide badge notifications for disabled badges or when badges are disabled (#36987)

Alan Guo Xiang Tan121eb1b0f85a
diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb
index ac32ac61c73..2c9f691d549 100644
--- a/app/controllers/notifications_controller.rb
+++ b/app/controllers/notifications_controller.rb
@@ -57,6 +57,7 @@ class NotificationsController < ApplicationController
notifications =
Notification.filter_inaccessible_topic_notifications(current_user.guardian, notifications)
+ notifications = Notification.filter_disabled_badge_notifications(notifications)
notifications = Notification.populate_acting_user(notifications)
@@ -88,6 +89,7 @@ class NotificationsController < ApplicationController
notifications = notifications.offset(offset).limit(limit)
notifications =
Notification.filter_inaccessible_topic_notifications(current_user.guardian, notifications)
+ notifications = Notification.filter_disabled_badge_notifications(notifications)
notifications = Notification.populate_acting_user(notifications)
render_json_dump(
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 8857ad7e731..9930709a351 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -241,6 +241,28 @@ class Notification < ActiveRecord::Base
notifications.select { |n| n.topic_id.blank? || accessible_topic_ids.include?(n.topic_id) }
end
+ def self.filter_disabled_badge_notifications(notifications)
+ return notifications if notifications.blank?
+
+ if !SiteSetting.enable_badges
+ return notifications.reject { |n| n.notification_type == types[:granted_badge] }
+ end
+
+ badge_ids =
+ notifications.filter_map do |n|
+ n.data_hash[:badge_id] if n.notification_type == types[:granted_badge]
+ end
+
+ return notifications if badge_ids.empty?
+
+ disabled_badge_ids = Badge.where(id: badge_ids, enabled: false).distinct.pluck(:id)
+
+ notifications.reject do |n|
+ n.notification_type == types[:granted_badge] &&
+ disabled_badge_ids.include?(n.data_hash[:badge_id])
+ end
+ end
+
# Be wary of calling this frequently. O(n) JSON parsing can suck.
def data_hash
@data_hash ||=
diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb
index 5c7b4be8ad4..ef2cce5bca4 100644
--- a/spec/models/notification_spec.rb
+++ b/spec/models/notification_spec.rb
@@ -858,4 +858,39 @@ RSpec.describe Notification do
end
end
end
+
+ describe ".filter_disabled_badge_notifications" do
+ fab!(:enabled_badge) { Fabricate(:badge, enabled: true) }
+ fab!(:disabled_badge) { Fabricate(:badge, enabled: false) }
+
+ fab!(:enabled_badge_notification) do
+ BadgeGranter.send_notification(user.id, user.username, user.locale, enabled_badge)
+ end
+
+ fab!(:disabled_badge_notification) do
+ BadgeGranter.send_notification(user.id, user.username, user.locale, disabled_badge)
+ end
+
+ fab!(:regular_notification) { Fabricate(:notification, user: user) }
+
+ it "filters all badge notifications when enable_badges is false" do
+ SiteSetting.enable_badges = false
+
+ result =
+ Notification.filter_disabled_badge_notifications(
+ [enabled_badge_notification, disabled_badge_notification, regular_notification],
+ )
+
+ expect(result).to contain_exactly(regular_notification)
+ end
+
+ it "filters badge notifications for badges that are disabled" do
+ result =
+ Notification.filter_disabled_badge_notifications(
+ [enabled_badge_notification, disabled_badge_notification, regular_notification],
+ )
+
+ expect(result).to contain_exactly(enabled_badge_notification, regular_notification)
+ end
+ end
end
diff --git a/spec/requests/notifications_controller_spec.rb b/spec/requests/notifications_controller_spec.rb
index 92cf27ce250..1a7c9bf2469 100644
--- a/spec/requests/notifications_controller_spec.rb
+++ b/spec/requests/notifications_controller_spec.rb
@@ -428,6 +428,69 @@ RSpec.describe NotificationsController do
end
end
+ context "with notifications for disabled badges" do
+ fab!(:enabled_badge) { Fabricate(:badge, enabled: true) }
+ fab!(:disabled_badge) { Fabricate(:badge, enabled: false) }
+
+ fab!(:enabled_badge_notification) do
+ BadgeGranter.send_notification(user.id, user.username, user.locale, enabled_badge)
+ end
+
+ fab!(:disabled_badge_notification) do
+ BadgeGranter.send_notification(user.id, user.username, user.locale, disabled_badge)
+ end
+
+ context "with 'recent' filter" do
+ it "filters badge notifications for badges that are disabled" do
+ get "/notifications.json", params: { recent: true }
+
+ expect(response.status).to eq(200)
+
+ expect(response.parsed_body["notifications"].map { |n| n["id"] }).to contain_exactly(
+ notification.id,
+ enabled_badge_notification.id,
+ )
+ end
+
+ it "filters all badge notifications when `enable_badges` site setting is false" do
+ SiteSetting.enable_badges = false
+
+ get "/notifications.json", params: { recent: true }
+
+ expect(response.status).to eq(200)
+
+ expect(response.parsed_body["notifications"].map { |n| n["id"] }).to contain_exactly(
+ notification.id,
+ )
+ end
+ end
+
+ context "without 'recent' filter" do
+ it "filters badge notifications for badges that are disabled" do
+ get "/notifications.json"
+
+ expect(response.status).to eq(200)
+
+ expect(response.parsed_body["notifications"].map { |n| n["id"] }).to contain_exactly(
+ notification.id,
+ enabled_badge_notification.id,
+ )
+ end
+
+ it "filters all badge notifications when `enable_badges` site setting is false" do
+ SiteSetting.enable_badges = false
+
+ get "/notifications.json"
+
+ expect(response.status).to eq(200)
+
+ expect(response.parsed_body["notifications"].map { |n| n["id"] }).to contain_exactly(
+ notification.id,
+ )
+ end
+ end
+ end
+
context "with `show_user_menu_avatars` setting enabled" do
before { SiteSetting.show_user_menu_avatars = true }