From 6aa447816df400b47fdd41703764741a12208c7f Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 7 Apr 2016 22:29:01 +0800 Subject: [PATCH] UX: Publish changes to TopicView when Topic is updated. --- .../discourse/controllers/topic.js.es6 | 18 ++++++++++++------ app/models/post.rb | 4 ++-- lib/post_revisor.rb | 9 ++++++++- spec/components/post_revisor_spec.rb | 16 +++++++++++++++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6 index a87f854e761..bee65a30154 100644 --- a/app/assets/javascripts/discourse/controllers/topic.js.es6 +++ b/app/assets/javascripts/discourse/controllers/topic.js.es6 @@ -661,35 +661,41 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, { case "revised": case "rebaked": { postStream.triggerChangedPost(data.id, data.updated_at).then(() => refresh({ id: data.id })); - return; + break; } case "deleted": { postStream.triggerDeletedPost(data.id, data.post_number).then(() => refresh({ id: data.id })); - return; + break; } case "recovered": { postStream.triggerRecoveredPost(data.id, data.post_number).then(() => refresh({ id: data.id })); - return; + break; } case "created": { postStream.triggerNewPostInStream(data.id).then(() => refresh()); if (this.get('currentUser.id') !== data.user_id) { Discourse.notifyBackgroundCountIncrement(); } - return; + break; } case "move_to_inbox": { topic.set("message_archived",false); - return; + break; } case "archived": { topic.set("message_archived",true); - return; + break; } default: { Em.Logger.warn("unknown topic bus message type", data); } } + + if (data.reload_topic) { + topic.reload().then(() => { + this.send('postChangedRoute', topic.get('post_number') || 1); + }); + } }); }, diff --git a/app/models/post.rb b/app/models/post.rb index 521073d3b44..6c4d9921155 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -104,7 +104,7 @@ class Post < ActiveRecord::Base end end - def publish_change_to_clients!(type) + def publish_change_to_clients!(type, options = {}) # special failsafe for posts missing topics consistency checks should fix, but message # is safe to skip return unless topic @@ -117,7 +117,7 @@ class Post < ActiveRecord::Base user_id: user_id, last_editor_id: last_editor_id, type: type - } + }.merge(options) if Topic.visible_post_types.include?(post_type) MessageBus.publish(channel, msg, group_ids: topic.secure_group_ids) diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 9881274d725..05c85d0fa5e 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -433,7 +433,14 @@ class PostRevisor end def publish_changes - @post.publish_change_to_clients!(:revised) + options = + if !@topic_changes.diff.empty? && !@topic_changes.errored? + { reload_topic: true } + else + {} + end + + @post.publish_change_to_clients!(:revised, options) end def grant_badge diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb index 4667ede9cd3..c88e7e78e41 100644 --- a/spec/components/post_revisor_spec.rb +++ b/spec/components/post_revisor_spec.rb @@ -8,7 +8,6 @@ describe PostRevisor do let(:post_args) { { user: newuser, topic: topic } } context 'TopicChanges' do - let(:topic) { Fabricate(:topic) } let(:tc) { topic.reload PostRevisor::TopicChanges.new(topic, topic.user) @@ -348,5 +347,20 @@ describe PostRevisor do expect(post.raw).to eq(" <-- whitespaces -->") end + context "#publish_changes" do + let!(:post) { Fabricate(:post, topic_id: topic.id) } + + it "should publish topic changes to clients" do + revisor = described_class.new(topic.ordered_posts.first, topic) + + messages = MessageBus.track_publish do + revisor.revise!(newuser, { title: 'this is a test topic' }) + end + + message = messages.find { |message| message.channel == "/topic/#{topic.id}" } + payload = message.data + expect(payload[:reload_topic]).to eq(true) + end + end end end