From 5f3c381dc220c23da23aa832e2fd6dd60f618a97 Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Tue, 14 Jul 2015 15:12:00 -0400
Subject: [PATCH] Only include information in `actions_summary` when we need
 it.

---
 .../javascripts/discourse/models/post.js.es6  |  1 +
 app/serializers/post_serializer.rb            | 35 ++++++++++---------
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/app/assets/javascripts/discourse/models/post.js.es6 b/app/assets/javascripts/discourse/models/post.js.es6
index 67458bd4add..04fe4d02a12 100644
--- a/app/assets/javascripts/discourse/models/post.js.es6
+++ b/app/assets/javascripts/discourse/models/post.js.es6
@@ -364,6 +364,7 @@ Post.reopenClass({
       // this area should be optimized, it is creating way too many objects per post
       json.actions_summary = json.actions_summary.map(function(a) {
         a.actionType = Discourse.Site.current().postActionTypeById(a.id);
+        a.count = a.count || 0;
         const actionSummary = ActionSummary.create(a);
         lookup[a.actionType.name_key] = actionSummary;
         return actionSummary;
diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb
index bbe0339365e..c1998a2e212 100644
--- a/app/serializers/post_serializer.rb
+++ b/app/serializers/post_serializer.rb
@@ -181,37 +181,38 @@ class PostSerializer < BasicPostSerializer
       count_col = "#{sym}_count".to_sym
 
       count = object.send(count_col) if object.respond_to?(count_col)
-      count ||= 0
-      action_summary = {
-        id: id,
-        count: count,
-        hidden: (sym == :vote),
-        can_act: scope.post_can_act?(object, sym, taken_actions: actions)
-      }
+      summary = { id: id, count: count }
+      summary[:hidden] = true if sym == :vote
+      summary[:can_act] = true if scope.post_can_act?(object, sym, taken_actions: actions)
 
       if sym == :notify_user && scope.current_user.present? && scope.current_user == object.user
-        action_summary[:can_act] = false # Don't send a pm to yourself about your own post, silly
+        summary.delete(:can_act)
       end
 
       # The following only applies if you're logged in
-      if action_summary[:can_act] && scope.current_user.present?
-        action_summary[:can_defer_flags] = scope.is_staff? &&
-                                           PostActionType.flag_types.values.include?(id) &&
-                                           active_flags.present? && active_flags.has_key?(id) &&
-                                           active_flags[id].count > 0
+      if summary[:can_act] && scope.current_user.present?
+        summary[:can_defer_flags] = true if scope.is_staff? &&
+                                                   PostActionType.flag_types.values.include?(id) &&
+                                                   active_flags.present? && active_flags.has_key?(id) &&
+                                                   active_flags[id].count > 0
       end
 
       if actions.present? && actions.has_key?(id)
-        action_summary[:acted] = true
-        action_summary[:can_undo] = scope.can_delete?(actions[id])
+        summary[:acted] = true
+        summary[:can_undo] = true if scope.can_delete?(actions[id])
       end
 
       # only show public data
       unless scope.is_staff? || PostActionType.public_types.values.include?(id)
-        action_summary[:count] = action_summary[:acted] ? 1 : 0
+        summary[:count] = summary[:acted] ? 1 : 0
       end
 
-      result << action_summary
+      summary.delete(:count) if summary[:count] == 0
+
+      # Only include it if the user can do it or it has a count
+      if summary[:can_act] || summary[:count]
+        result << summary
+      end
     end
 
     result