FIX: Correctly publish messages unconditionally to admins (#13053)

FIX: Correctly publish messages unconditionally to admins (#13053)

Under certain conditions admins would miss messages when posting action in topics where they have permission.

This also fixes an error where we would sometimes explode when publishing to an empty group.

diff --git a/app/models/user_action.rb b/app/models/user_action.rb
index 60b6002..0ad0a3d 100644
--- a/app/models/user_action.rb
+++ b/app/models/user_action.rb
@@ -283,14 +283,14 @@ class UserAction < ActiveRecord::Base
           update_like_count(user_id, hash[:action_type], 1)
         end
 
-        # move into Topic perhaps
         group_ids = nil
         if topic && topic.category && topic.category.read_restricted
-          group_ids = topic.category.groups.pluck("groups.id")
+          group_ids = [Group::AUTO_GROUPS[:admins]]
+          group_ids.concat(topic.category.groups.pluck("groups.id"))
         end
 
         if action.user
-          MessageBus.publish("/u/#{action.user.username.downcase}", action.id, user_ids: [user_id], group_ids: group_ids)
+          MessageBus.publish("/u/#{action.user.username_lower}", action.id, user_ids: [user_id], group_ids: group_ids)
         end
 
         action
diff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rb
index f4485f1..d1d781f 100644
--- a/spec/models/user_action_spec.rb
+++ b/spec/models/user_action_spec.rb
@@ -35,15 +35,18 @@ describe UserAction do
       }.merge(opts))
     end
 
-    describe "integration" do
-      before do
-        # Create some test data using a helper
-        log_test_action
-        log_test_action(action_type: UserAction::GOT_PRIVATE_MESSAGE)
-        log_test_action(action_type: UserAction::NEW_TOPIC, target_topic_id: public_topic.id, target_post_id: public_post.id)
-        log_test_action(action_type: UserAction::BOOKMARK)
+    it "allows publishing when group is deleted" do
+      public_topic.category.update!(read_restricted: true)
+
+      m = MessageBus.track_publish("/u/#{user.username_lower}") do
+        log_test_action(target_topic_id: public_topic.id, target_post_id: public_post.id)
       end
 
+      expect(m[0].group_ids).to eq([Group::AUTO_GROUPS[:admins]])
+      expect(m[0].user_ids).to eq([user.id])
+    end
+
+    describe "integration" do
       def stats_for_user(viewer = nil)
         UserAction.stats(user.id, Guardian.new(viewer)).map { |r| r.action_type.to_i }.sort
       end
@@ -53,6 +56,12 @@ describe UserAction do
       end
 
       it 'includes the events correctly' do
+        # Create some test data using a helper
+        log_test_action
+        log_test_action(action_type: UserAction::GOT_PRIVATE_MESSAGE)
+        log_test_action(action_type: UserAction::NEW_TOPIC, target_topic_id: public_topic.id, target_post_id: public_post.id)
+        log_test_action(action_type: UserAction::BOOKMARK)
+
         Jobs.run_immediately!
         PostActionNotifier.enable
 

GitHub sha: 058b5310

This commit appears in #13053 which was approved by CvX. It was merged by SamSaffron.