From af3dc4e53736e87c198707dc74c85a77f305ef47 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 2 Jun 2017 17:38:14 +0900 Subject: [PATCH] FIX: Publish group's categories to user when adding user to group. https://meta.discourse.org/t/joining-a-group-doesnt-take-effect-unless-the-browser-is-hard-refreshed/57357/11?u=tgxworld --- app/models/group.rb | 5 +++++ spec/models/group_spec.rb | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/group.rb b/app/models/group.rb index 1c185cc7912..dc4de2d1338 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -371,6 +371,11 @@ class Group < ActiveRecord::Base def add(user) self.users.push(user) unless self.users.include?(user) + + MessageBus.publish('/categories', { + categories: ActiveModel::ArraySerializer.new(self.categories).as_json + }, user_ids: [user.id]) + self end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index c70426e04c9..1455f098157 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -3,6 +3,7 @@ require 'rails_helper' describe Group do let(:admin) { Fabricate(:admin) } let(:user) { Fabricate(:user) } + let(:group) { Fabricate(:group) } describe '#builtin' do context "verify enum sequence" do @@ -450,4 +451,20 @@ describe Group do end end end + + describe '#add' do + context 'when adding a user into a public group' do + let(:category) { Fabricate(:category) } + + it "should publish the group's categories to the client" do + group.update!(public: true, categories: [category]) + + message = MessageBus.track_publish { group.add(user) }.first + + expect(message.data[:categories].count).to eq(1) + expect(message.data[:categories].first[:id]).to eq(category.id) + expect(message.user_ids).to eq([user.id]) + end + end + end end