Only include information in actions_summary when we need it.

This commit is contained in:
Robin Ward 2015-07-14 15:12:00 -04:00
parent 8a9fa3e5bf
commit 5f3c381dc2
2 changed files with 19 additions and 17 deletions

View file

@ -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;

View file

@ -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? &&
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