From 690d2f4bd33027795f422fc37fbe439f636c69b1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 7 Apr 2017 15:05:03 +0800 Subject: [PATCH] UX: Publish topic changes when topic is published. --- .../discourse/controllers/topic.js.es6 | 42 ++++++++++--------- .../javascripts/discourse/models/topic.js.es6 | 5 +-- app/jobs/regular/publish_topic_to_category.rb | 2 + spec/jobs/publish_topic_to_category_spec.rb | 7 +++- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6 index 28c929189d4..d5b6852685b 100644 --- a/app/assets/javascripts/discourse/controllers/topic.js.es6 +++ b/app/assets/javascripts/discourse/controllers/topic.js.es6 @@ -868,7 +868,7 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, { const refresh = (args) => this.appEvents.trigger('post-stream:refresh', args); - this.messageBus.subscribe("/topic/" + this.get('model.id'), data => { + this.messageBus.subscribe(`/topic/${this.get('model.id')}`, data => { const topic = this.get('model'); if (data.notification_level_change) { @@ -878,6 +878,17 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, { } const postStream = this.get('model.postStream'); + + if (data.reload_topic) { + topic.reload().then(() => { + this.send('postChangedRoute', topic.get('post_number') || 1); + this.appEvents.trigger('header:update-topic', topic); + if (data.refresh_stream) postStream.refresh(); + }); + + return; + } + switch (data.type) { case "acted": postStream.triggerChangedPost(data.id, data.updated_at).then(() => refresh({ id: data.id, refreshLikes: true })); @@ -915,27 +926,20 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, { } } - if (data.reload_topic) { - topic.reload().then(() => { - this.send('postChangedRoute', topic.get('post_number') || 1); - this.appEvents.trigger('header:update-topic', topic); - }); - } else { - if (topic.get('isPrivateMessage') && - this.currentUser && - this.currentUser.get('id') !== data.user_id && - data.type === 'created') { + if (topic.get('isPrivateMessage') && + this.currentUser && + this.currentUser.get('id') !== data.user_id && + data.type === 'created') { - const postNumber = data.post_number; - const notInPostStream = topic.get('highest_post_number') <= postNumber; - const postNumberDifference = postNumber - topic.get('currentPost'); + const postNumber = data.post_number; + const notInPostStream = topic.get('highest_post_number') <= postNumber; + const postNumberDifference = postNumber - topic.get('currentPost'); - if (notInPostStream && - postNumberDifference > 0 && - postNumberDifference < 7) { + if (notInPostStream && + postNumberDifference > 0 && + postNumberDifference < 7) { - this._scrollToPost(data.post_number); - } + this._scrollToPost(data.post_number); } } }); diff --git a/app/assets/javascripts/discourse/models/topic.js.es6 b/app/assets/javascripts/discourse/models/topic.js.es6 index 67c6a9b40bd..34bb795a8bb 100644 --- a/app/assets/javascripts/discourse/models/topic.js.es6 +++ b/app/assets/javascripts/discourse/models/topic.js.es6 @@ -376,9 +376,8 @@ const Topic = RestModel.extend({ }, reload() { - const self = this; - return ajax('/t/' + this.get('id'), { type: 'GET' }).then(function(topic_json) { - self.updateFromJson(topic_json); + return ajax(`/t/${this.get('id')}`, { type: 'GET' }).then(topic_json => { + this.updateFromJson(topic_json); }); }, diff --git a/app/jobs/regular/publish_topic_to_category.rb b/app/jobs/regular/publish_topic_to_category.rb index ab88d5e6ae3..11424dd32f0 100644 --- a/app/jobs/regular/publish_topic_to_category.rb +++ b/app/jobs/regular/publish_topic_to_category.rb @@ -11,6 +11,8 @@ module Jobs topic.change_category_to_id(topic_status_update.category_id) topic.update_columns(visible: true) end + + MessageBus.publish("/topic/#{topic.id}", reload_topic: true, refresh_stream: true) end end end diff --git a/spec/jobs/publish_topic_to_category_spec.rb b/spec/jobs/publish_topic_to_category_spec.rb index 98226ac6717..e628d719987 100644 --- a/spec/jobs/publish_topic_to_category_spec.rb +++ b/spec/jobs/publish_topic_to_category_spec.rb @@ -40,7 +40,9 @@ RSpec.describe Jobs::PublishTopicToCategory do it 'should publish the topic to the new category correctly' do Timecop.travel(1.hour.ago) { topic.update!(visible: false) } - described_class.new.execute(topic_status_update_id: topic.topic_status_update.id) + message = MessageBus.track_publish do + described_class.new.execute(topic_status_update_id: topic.topic_status_update.id) + end.first topic.reload expect(topic.category).to eq(another_category) @@ -49,5 +51,8 @@ RSpec.describe Jobs::PublishTopicToCategory do %w{created_at bumped_at updated_at last_posted_at}.each do |attribute| expect(topic.public_send(attribute)).to be_within(1.second).of(Time.zone.now) end + + expect(message.data[:reload_topic]).to be_present + expect(message.data[:refresh_stream]).to be_present end end