diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js b/app/assets/javascripts/discourse/controllers/topic_controller.js index da9a63c7345..c6bdf654fac 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js @@ -400,7 +400,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected if (data.type === "revised" || data.type === "acted"){ // TODO we could update less data for "acted" // (only post actions) - postStream.triggerChangedPost(data.id, data.updated_at); + postStream.triggerChangedPost(data.id, data.updated_at, data.type); return; } diff --git a/app/assets/javascripts/discourse/models/post.js b/app/assets/javascripts/discourse/models/post.js index 7d06c66dfa7..7517a0e9589 100644 --- a/app/assets/javascripts/discourse/models/post.js +++ b/app/assets/javascripts/discourse/models/post.js @@ -284,9 +284,30 @@ Discourse.Post = Discourse.Model.extend({ var post = this; Object.keys(otherPost).forEach(function (key) { var value = otherPost[key]; - if (typeof value !== "function") { - post.set(key, value); + var oldValue = post.get(key); + + if(!value) { + value = null; } + + if(!oldValue) { + oldValue = null; + } + + var skip = false; + + if (typeof value !== "function" && oldValue !== value) { + + // wishing for an identity map + if(key === "reply_to_user") { + skip = Em.get(value, "username") === Em.get(oldValue, "username"); + } + + if(!skip) { + post.set(key, value); + } + } + }); }, diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js index 4179812ffc5..1340683864a 100644 --- a/app/assets/javascripts/discourse/models/post_stream.js +++ b/app/assets/javascripts/discourse/models/post_stream.js @@ -504,7 +504,7 @@ Discourse.PostStream = Em.Object.extend({ } }, - triggerChangedPost: function(postId, updatedAt) { + triggerChangedPost: function(postId, updatedAt, type) { if (!postId) { return; } var postIdentityMap = this.get('postIdentityMap'), diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index efee6a1ceeb..f3c00de750f 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -218,6 +218,8 @@ class PostsController < ApplicationController def render_post_json(post) post_serializer = PostSerializer.new(post, scope: guardian, root: false) post_serializer.add_raw = true + post_serializer.topic_slug = post.topic.slug if post.topic.present? + counts = PostAction.counts_for([post], current_user) if counts && counts = counts[post.id] post_serializer.post_actions = counts diff --git a/app/models/user.rb b/app/models/user.rb index c10f36b4513..738e05d5edc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,6 +41,7 @@ class User < ActiveRecord::Base has_one :user_stat, dependent: :destroy has_one :single_sign_on_record, dependent: :destroy belongs_to :approved_by, class_name: 'User' + belongs_to :primary_group, class_name: 'Group' has_many :group_users, dependent: :destroy has_many :groups, through: :group_users diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb index f82f4af9edf..35b3951ef06 100644 --- a/app/serializers/post_serializer.rb +++ b/app/serializers/post_serializer.rb @@ -78,8 +78,13 @@ class PostSerializer < BasicPostSerializer end def primary_group_name - return nil unless object.user && @topic_view - return @topic_view.primary_group_names[object.user.primary_group_id] if object.user.primary_group_id + return nil unless object.user && object.user.primary_group_id + + if @topic_view + @topic_view.primary_group_names[object.user.primary_group_id] + else + object.user.primary_group.name if object.user.primary_group + end end def link_counts diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 735a88f9822..4b5474e5856 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -62,6 +62,17 @@ describe PostsController do let(:action) { :show } let(:params) { {id: post.id} } end + + it 'gets all the expected fields' do + # non fabricated test + new_post = create_post + xhr :get, :show, {id: new_post.id} + parsed = JSON.parse(response.body) + parsed["topic_slug"].should == new_post.topic.slug + parsed["moderator"].should == false + parsed["username"].should == new_post.user.username + parsed["cooked"].should == new_post.cooked + end end describe 'by_number' do