diff --git a/app/models/user.rb b/app/models/user.rb index b13d0e1e407..3c3d4abc9d6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -405,9 +405,11 @@ class User < ActiveRecord::Base ) AS y " - recent = User.exec_sql(sql, user_id: id, - type: Notification.types[:private_message]).values.map do |id, read| - [id.to_i, read == 't'.freeze] + recent = User.exec_sql(sql, + user_id: id, + type: Notification.types[:private_message] + ).values.map! do |id, read| + [id.to_i, read] end MessageBus.publish("/notification/#{id}", diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9599ba22f79..65461bfd241 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' require_dependency 'user' describe User do + let(:user) { Fabricate(:user) } context 'validations' do it { is_expected.to validate_presence_of :username } @@ -1520,4 +1521,19 @@ describe User do expect(User.human_users).to eq([user]) end end + + describe '#publish_notifications_state' do + it 'should publish the right message' do + notification = Fabricate(:notification, user: user) + notification2 = Fabricate(:notification, user: user, read: true) + + message = MessageBus.track_publish do + user.publish_notifications_state + end.find { |m| m.channel = "/notification/#{user.id}" } + + expect(message.data[:recent]).to eq([ + [notification2.id, true], [notification.id, false] + ]) + end + end end