From aea848f164b587c36270028fc3de21c64abb321f Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 18 Mar 2013 14:45:05 -0400 Subject: [PATCH] Move MessageBus observer into PostCreator --- app/models/message_bus_observer.rb | 10 +--------- lib/post_creator.rb | 7 +++++++ spec/components/post_creator_spec.rb | 7 +++++++ spec/models/post_spec.rb | 8 -------- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/models/message_bus_observer.rb b/app/models/message_bus_observer.rb index e14aaf246e5..32665ef1438 100644 --- a/app/models/message_bus_observer.rb +++ b/app/models/message_bus_observer.rb @@ -3,15 +3,7 @@ require_dependency 'discourse_observer' # This class is responsible for notifying the message bus of various # events. class MessageBusObserver < DiscourseObserver - observe :post, :notification, :user_action, :topic - - def after_create_post(post) - MessageBus.publish("/topic/#{post.topic_id}", - id: post.id, - created_at: post.created_at, - user: BasicUserSerializer.new(post.user).as_json(root: false), - post_number: post.post_number) - end + observe :notification, :user_action, :topic def after_create_notification(notification) refresh_notification_count(notification) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index e2bf3e94430..78ec0848cad 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -110,6 +110,13 @@ class PostCreator # Update `last_posted_at` to match the post's created_at @user.update_column(:last_posted_at, post.created_at) + + # Publish the post in the message bus + MessageBus.publish("/topic/#{post.topic_id}", + id: post.id, + created_at: post.created_at, + user: BasicUserSerializer.new(post.user).as_json(root: false), + post_number: post.post_number) end post diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 1250f5d9ad4..f6ec36095b7 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -11,6 +11,7 @@ describe PostCreator do context 'new topic' do let(:category) { Fabricate(:category, user: user) } + let(:topic) { Fabricate(:topic, user: user) } let(:basic_topic_params) { {title: 'hello world topic', raw: 'my name is fred', archetype_id: 1} } let(:image_sizes) { {'http://an.image.host/image.jpg' => {'width' => 111, 'height' => 222}} } @@ -38,6 +39,12 @@ describe PostCreator do creator.create end + it 'enqueues the post on the message bus' do + MessageBus.stubs(:publish).with("/users/#{user.username}", anything) + MessageBus.expects(:publish).with("/topic/#{topic.id}", instance_of(Hash)) + PostCreator.new(user, raw: basic_topic_params[:raw], topic_id: topic.id) + end + it 'features topic users' do Jobs.stubs(:enqueue).with(:process_post, anything) Jobs.expects(:enqueue).with(:feature_topic_users, has_key(:topic_id)) diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 9d127272c32..8bc2e7aacd8 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -81,14 +81,6 @@ describe Post do end end - describe 'message bus' do - it 'enqueues the post on the message bus' do - topic = self.topic - MessageBus.expects(:publish).with("/topic/#{topic.id}", instance_of(Hash)) - Fabricate(:post, post_args) - end - end - describe "maximum images" do let(:post_no_images) { Fabricate.build(:post, post_args) } let(:post_one_image) { Fabricate.build(:post, post_args.merge(raw: "![sherlock](http://bbc.co.uk/sherlock.jpg)")) }