A platform for community discussion. Free, open, simple.
FIX: Don't publish notifications to MessageBus for inactive users (#15035)
Martin Brennan9f8ee8f1372e
diff --git a/app/models/user.rb b/app/models/user.rbindex 72b03efbedf..2b61a92fc45 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -590,6 +590,8 @@ class User < ActiveRecord::Baseenddef publish_notifications_state+ return if !self.allow_live_notifications?+# publish last notification json with the message so we can apply an updatenotification = notifications.visible.order('notifications.created_at desc').firstjson = NotificationSerializer.new(notification).as_json if notification@@ -693,6 +695,10 @@ class User < ActiveRecord::Baselast_seen_at.present?end+ def seen_since?(datetime)+ seen_before? && last_seen_at >= datetime+ end+def create_visit_record!(date, opts = {})user_stat.update_column(:days_visited, user_stat.days_visited + 1)user_visits.create!(visited_at: date, posts_read: opts[:posts_read] || 0, mobile: opts[:mobile] || false)@@ -1438,6 +1444,10 @@ class User < ActiveRecord::BaseShelvedNotification.joins(:notification).where("notifications.user_id = ?", self.id)end+ def allow_live_notifications?+ seen_since?(30.days.ago)+ end+protecteddef badge_grantdiff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex c52922d9c09..1847fa42a48 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -28,7 +28,11 @@ class PostAlerter}DiscourseEvent.trigger(:pre_notification_alert, user, payload)- MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])++ if user.allow_live_notifications?+ MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])+ end+push_notification(user, payload)DiscourseEvent.trigger(:post_notification_alert, user, payload)enddiff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex 5ee0734b71c..1a6a22588a0 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -3,7 +3,7 @@require 'rails_helper'describe User do- let(:user) { Fabricate(:user) }+ let(:user) { Fabricate(:user, last_seen_at: 1.day.ago) }def user_error_message(*keys)I18n.t(:"activerecord.errors.models.user.attributes.#{keys.join('.')}")@@ -1921,6 +1921,18 @@ describe User doexpect(message.data[:unread_private_messages]).to eq(2)expect(message.data[:unread_high_priority_notifications]).to eq(2)end++ it "does not publish to the /notification channel for users who have not been seen in > 30 days" do+ notification = Fabricate(:notification, user: user)+ notification2 = Fabricate(:notification, user: user, read: true)+ user.update(last_seen_at: 31.days.ago)++ message = MessageBus.track_publish("/notification/#{user.id}") do+ user.publish_notifications_state+ end.first++ expect(message).to eq(nil)+ endenddescribe "silenced?" dodiff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex b2335c81239..84a30aabbcb 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -848,7 +848,7 @@ describe PostAlerter doenddescribe "create_notification_alert" do- it "does not nothing for suspended users" do+ it "does nothing for suspended users" doevil_trout.update_columns(suspended_till: 1.year.from_now)post = Fabricate(:post)@@ -869,6 +869,35 @@ describe PostAlerter doexpect(messages.size).to eq(0)expect(Jobs::PushNotification.jobs.size).to eq(0)end++ it "does not publish to MessageBus /notification-alert if the user has not been seen for > 30 days, but still sends a push notification" do+ evil_trout.update_columns(last_seen_at: 31.days.ago)+ post = Fabricate(:post)++ SiteSetting.allowed_user_api_push_urls = "https://site2.com/push"+ UserApiKey.create!(user_id: evil_trout.id,+ client_id: "xxx#1",+ application_name: "iPhone1",+ scopes: ['notifications'].map { |name| UserApiKeyScope.new(name: name) },+ push_url: "https://site2.com/push")++ events = nil+ messages = MessageBus.track_publish do+ events = DiscourseEvent.track_events do+ PostAlerter.create_notification_alert(+ user: evil_trout,+ post: post,+ notification_type: Notification.types[:custom],+ excerpt: "excerpt",+ username: "username"+ )+ end+ end++ expect(events.size).to eq(2)+ expect(messages.size).to eq(0)+ expect(Jobs::PushNotification.jobs.size).to eq(1)+ endenddescribe "watching_first_post" do