From 090f5c99c2aa2f87d0b39637bb08beb46801c94a Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 24 Mar 2014 12:19:08 +1100 Subject: [PATCH] FEATURE: Live update edits --- .../discourse/controllers/topic_controller.js | 8 +++++++- .../discourse/models/post_stream.js | 15 +++++++++++++++ app/models/topic.rb | 6 ++++++ lib/post_creator.rb | 8 +------- lib/post_revisor.rb | 18 ++++++++++++++++++ 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js b/app/assets/javascripts/discourse/controllers/topic_controller.js index 8bd38af64..30e251e3e 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js @@ -360,8 +360,14 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected return; } + var postStream = topicController.get('postStream'); + if (data.type === "revised"){ + postStream.triggerChangedPost(data.id, data.updated_at); + return; + } + // Add the new post into the stream - topicController.get('postStream').triggerNewPostInStream(data.id); + postStream.triggerNewPostInStream(data.id); }); }, diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js index 90e78859c..1740c4fca 100644 --- a/app/assets/javascripts/discourse/models/post_stream.js +++ b/app/assets/javascripts/discourse/models/post_stream.js @@ -510,6 +510,21 @@ Discourse.PostStream = Em.Object.extend({ } }, + triggerChangedPost: function(postId, updatedAt) { + if (!postId) { return; } + + var postIdentityMap = this.get('postIdentityMap'), + existing = postIdentityMap.get(postId), + postStream = this; + + if (existing && existing.updated_at !== updatedAt) { + var url = "/posts/" + postId; + Discourse.ajax(url).then(function(p){ + postStream.storePost(Discourse.Post.create(p)); + }); + } + }, + /** Returns the "thread" of posts in the history of a post. diff --git a/app/models/topic.rb b/app/models/topic.rb index 249ff9371..7c0185e91 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -701,6 +701,12 @@ class Topic < ActiveRecord::Base @acting_user = u end + def secure_group_ids + @secure_group_ids ||= if self.category && self.category.read_restricted? + self.category.secure_group_ids + end + end + private def update_category_topic_count_by(num) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 25de03753..93216a582 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -137,12 +137,6 @@ class PostCreator end end - def secure_group_ids(topic) - @secure_group_ids ||= if topic.category && topic.category.read_restricted? - topic.category.secure_group_ids - end - end - def clear_possible_flags(topic) # at this point we know the topic is a PM and has been replied to ... check if we need to clear any flags # @@ -255,7 +249,7 @@ class PostCreator user: BasicUserSerializer.new(@post.user).as_json(root: false), post_number: @post.post_number }, - group_ids: secure_group_ids(@topic) + group_ids: @topic.secure_group_ids ) end diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 39882563f..0f7cedb90 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -19,12 +19,30 @@ class PostRevisor update_topic_word_counts @post.advance_draft_sequence PostAlerter.new.after_save_post(@post) + publish_revision true end private + def publish_revision + MessageBus.publish("/topic/#{@post.topic_id}",{ + id: @post.id, + post_number: @post.post_number, + updated_at: @post.updated_at, + type: "revised" + }, + group_ids: @post.topic.secure_group_ids + ) + end + + def secure_group_ids(topic) + @secure_group_ids ||= if topic.category && topic.category.read_restricted? + topic.category.secure_group_ids + end + end + def should_revise? @post.raw != @new_raw end