2013-02-05 14:16:51 -05:00
|
|
|
class PostSerializer < ApplicationSerializer
|
|
|
|
|
|
|
|
# To pass in additional information we might need
|
|
|
|
attr_accessor :topic_slug
|
|
|
|
attr_accessor :topic_view
|
|
|
|
attr_accessor :parent_post
|
|
|
|
attr_accessor :add_raw
|
|
|
|
attr_accessor :single_post_link_counts
|
|
|
|
attr_accessor :draft_sequence
|
|
|
|
|
|
|
|
attributes :id,
|
2013-02-07 10:45:24 -05:00
|
|
|
:post_number,
|
2013-02-05 14:16:51 -05:00
|
|
|
:post_type,
|
2013-02-07 10:45:24 -05:00
|
|
|
:created_at,
|
2013-02-05 14:16:51 -05:00
|
|
|
:updated_at,
|
2013-02-07 10:45:24 -05:00
|
|
|
:reply_count,
|
|
|
|
:reply_to_post_number,
|
2013-02-05 14:16:51 -05:00
|
|
|
:quote_count,
|
|
|
|
:avg_time,
|
|
|
|
:incoming_link_count,
|
|
|
|
:reads,
|
|
|
|
:score,
|
|
|
|
:yours,
|
2013-02-07 10:45:24 -05:00
|
|
|
:topic_slug,
|
2013-02-05 14:16:51 -05:00
|
|
|
:topic_id,
|
|
|
|
:display_username,
|
|
|
|
:version,
|
|
|
|
:can_edit,
|
|
|
|
:can_delete,
|
2013-02-07 15:12:55 -05:00
|
|
|
:can_recover,
|
2013-02-05 14:16:51 -05:00
|
|
|
:link_counts,
|
|
|
|
:cooked,
|
|
|
|
:read,
|
|
|
|
:username,
|
|
|
|
:name,
|
|
|
|
:reply_to_user,
|
|
|
|
:bookmarked,
|
|
|
|
:raw,
|
|
|
|
:actions_summary,
|
2013-02-13 16:12:30 -05:00
|
|
|
:new_user?,
|
2013-02-06 17:36:07 -05:00
|
|
|
:moderator?,
|
2013-02-05 14:16:51 -05:00
|
|
|
:avatar_template,
|
2013-02-07 10:45:24 -05:00
|
|
|
:user_id,
|
2013-02-05 14:16:51 -05:00
|
|
|
:draft_sequence,
|
|
|
|
:hidden,
|
2013-02-07 10:45:24 -05:00
|
|
|
:hidden_reason_id,
|
2013-02-05 14:16:51 -05:00
|
|
|
:deleted_at
|
|
|
|
|
|
|
|
|
2013-02-13 16:12:30 -05:00
|
|
|
def new_user?
|
|
|
|
object.user.created_at > SiteSetting.new_user_period_days.days.ago
|
|
|
|
end
|
|
|
|
|
2013-02-06 17:36:07 -05:00
|
|
|
def moderator?
|
|
|
|
object.user.has_trust_level?(:moderator)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def avatar_template
|
|
|
|
object.user.avatar_template
|
|
|
|
end
|
|
|
|
|
|
|
|
def yours
|
|
|
|
scope.user == object.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit
|
|
|
|
scope.can_edit?(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete
|
|
|
|
scope.can_delete?(object)
|
|
|
|
end
|
|
|
|
|
2013-02-07 15:12:55 -05:00
|
|
|
def can_recover
|
|
|
|
scope.can_recover_post?(object)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def link_counts
|
|
|
|
|
|
|
|
return @single_post_link_counts if @single_post_link_counts.present?
|
|
|
|
|
|
|
|
# TODO: This could be better, just porting the old one over
|
|
|
|
@topic_view.link_counts[object.id].map do |link|
|
|
|
|
result = {}
|
|
|
|
result[:url] = link[:url]
|
|
|
|
result[:internal] = link[:internal]
|
|
|
|
result[:reflection] = link[:reflection]
|
|
|
|
result[:title] = link[:title] if link[:title].present?
|
|
|
|
result[:clicks] = link[:clicks] || 0
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cooked
|
|
|
|
if object.hidden && !scope.is_admin?
|
|
|
|
if scope.current_user && object.user_id == scope.current_user.id
|
|
|
|
I18n.t('flagging.you_must_edit')
|
|
|
|
else
|
|
|
|
I18n.t('flagging.user_must_edit')
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
else
|
2013-02-05 14:16:51 -05:00
|
|
|
object.filter_quotes(@parent_post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
|
|
|
@topic_view.read?(object.post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
def score
|
|
|
|
object.score || 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_username
|
|
|
|
object.user.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
object.cached_version
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
object.user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
object.user.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_to_user
|
|
|
|
{
|
|
|
|
username: object.reply_to_user.username,
|
|
|
|
name: object.reply_to_user.name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def bookmarked
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Summary of the actions taken on this post
|
|
|
|
def actions_summary
|
|
|
|
result = []
|
2013-03-01 07:07:44 -05:00
|
|
|
PostActionType.types.each do |sym, id|
|
2013-02-05 14:16:51 -05:00
|
|
|
next if [:bookmark].include?(sym)
|
|
|
|
count_col = "#{sym}_count".to_sym
|
|
|
|
|
|
|
|
count = object.send(count_col) if object.respond_to?(count_col)
|
|
|
|
count ||= 0
|
2013-02-07 10:45:24 -05:00
|
|
|
action_summary = {id: id,
|
|
|
|
count: count,
|
2013-02-05 14:16:51 -05:00
|
|
|
hidden: (sym == :vote),
|
|
|
|
can_act: scope.post_can_act?(object, sym, taken_actions: post_actions)}
|
|
|
|
|
2013-02-27 11:21:23 -05:00
|
|
|
# The following only applies if you're logged in
|
2013-03-04 19:42:44 -05:00
|
|
|
if action_summary[:can_act] && scope.current_user.present?
|
2013-03-01 07:07:44 -05:00
|
|
|
action_summary[:can_clear_flags] = scope.is_admin? && PostActionType.flag_types.values.include?(id)
|
2013-02-27 11:21:23 -05:00
|
|
|
|
2013-03-04 19:42:44 -05:00
|
|
|
if post_actions.present? && post_actions.has_key?(id)
|
2013-02-27 11:21:23 -05:00
|
|
|
action_summary[:acted] = true
|
|
|
|
action_summary[:can_undo] = scope.can_delete?(post_actions[id])
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# anonymize flags
|
2013-03-01 07:07:44 -05:00
|
|
|
if !scope.is_admin? && PostActionType.flag_types.values.include?(id)
|
2013-02-05 14:16:51 -05:00
|
|
|
action_summary[:count] = action_summary[:acted] ? 1 : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
result << action_summary
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def include_draft_sequence?
|
|
|
|
@draft_sequence.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_slug_title?
|
|
|
|
@topic_slug.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_raw?
|
2013-02-07 10:45:24 -05:00
|
|
|
@add_raw.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_link_counts?
|
|
|
|
return true if @single_post_link_counts.present?
|
|
|
|
|
2013-03-04 19:42:44 -05:00
|
|
|
@topic_view.present? && @topic_view.link_counts.present? && @topic_view.link_counts[object.id].present?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_read?
|
|
|
|
@topic_view.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_reply_to_user?
|
2013-03-04 19:42:44 -05:00
|
|
|
object.quoteless? && object.reply_to_user
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_bookmarked?
|
2013-03-04 19:42:44 -05:00
|
|
|
post_actions.present? && post_actions.keys.include?(PostActionType.types[:bookmark])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def post_actions
|
2013-02-07 10:45:24 -05:00
|
|
|
@post_actions ||= (@topic_view.present? && @topic_view.all_post_actions.present?) ? @topic_view.all_post_actions[object.id] : nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|