2013-06-29 01:55:34 +08:00
|
|
|
class PostSerializer < BasicPostSerializer
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# To pass in additional information we might need
|
2013-11-16 06:28:16 +08:00
|
|
|
attr_accessor :topic_slug,
|
|
|
|
:topic_view,
|
|
|
|
:parent_post,
|
|
|
|
:add_raw,
|
|
|
|
:single_post_link_counts,
|
|
|
|
:draft_sequence,
|
|
|
|
:post_actions
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-29 01:55:34 +08:00
|
|
|
attributes :post_number,
|
2013-02-06 03:16:51 +08:00
|
|
|
:post_type,
|
|
|
|
:updated_at,
|
2013-02-07 23:45:24 +08:00
|
|
|
:reply_count,
|
|
|
|
:reply_to_post_number,
|
2013-02-06 03:16:51 +08:00
|
|
|
:quote_count,
|
|
|
|
:avg_time,
|
|
|
|
:incoming_link_count,
|
|
|
|
:reads,
|
|
|
|
:score,
|
|
|
|
:yours,
|
2013-02-07 23:45:24 +08:00
|
|
|
:topic_slug,
|
2013-02-06 03:16:51 +08:00
|
|
|
:topic_id,
|
|
|
|
:display_username,
|
2014-02-11 05:59:36 +08:00
|
|
|
:primary_group_name,
|
2013-02-06 03:16:51 +08:00
|
|
|
:version,
|
|
|
|
:can_edit,
|
|
|
|
:can_delete,
|
2013-02-08 04:12:55 +08:00
|
|
|
:can_recover,
|
2013-02-06 03:16:51 +08:00
|
|
|
:link_counts,
|
|
|
|
:read,
|
2013-06-26 06:39:20 +08:00
|
|
|
:user_title,
|
2013-02-06 03:16:51 +08:00
|
|
|
:reply_to_user,
|
|
|
|
:bookmarked,
|
|
|
|
:raw,
|
|
|
|
:actions_summary,
|
2013-02-07 06:36:07 +08:00
|
|
|
:moderator?,
|
2014-05-12 09:28:24 +08:00
|
|
|
:admin?,
|
2013-05-24 11:32:57 +08:00
|
|
|
:staff?,
|
2013-02-07 23:45:24 +08:00
|
|
|
:user_id,
|
2013-02-06 03:16:51 +08:00
|
|
|
:draft_sequence,
|
|
|
|
:hidden,
|
2013-02-07 23:45:24 +08:00
|
|
|
:hidden_reason_id,
|
2013-07-11 02:56:00 +08:00
|
|
|
:trust_level,
|
2013-07-13 00:08:23 +08:00
|
|
|
:deleted_at,
|
2013-07-22 15:48:24 +08:00
|
|
|
:deleted_by,
|
2013-11-16 06:28:16 +08:00
|
|
|
:user_deleted,
|
2014-03-13 22:47:37 +08:00
|
|
|
:edit_reason,
|
2014-05-13 20:53:11 +08:00
|
|
|
:can_view_edit_history,
|
2014-05-15 02:38:04 +08:00
|
|
|
:wiki,
|
2014-08-02 02:53:35 +08:00
|
|
|
:user_custom_fields,
|
2014-09-05 01:04:22 +08:00
|
|
|
:static_doc,
|
|
|
|
:via_email
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-07 06:36:07 +08:00
|
|
|
def moderator?
|
2014-05-12 09:28:24 +08:00
|
|
|
!!(object.user && object.user.moderator?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin?
|
|
|
|
!!(object.user && object.user.admin?)
|
2013-02-07 06:36:07 +08:00
|
|
|
end
|
|
|
|
|
2013-05-24 11:32:57 +08:00
|
|
|
def staff?
|
2014-05-12 09:28:24 +08:00
|
|
|
!!(object.user && object.user.staff?)
|
2013-05-24 11:32:57 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
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-08 04:12:55 +08:00
|
|
|
def can_recover
|
|
|
|
scope.can_recover_post?(object)
|
|
|
|
end
|
|
|
|
|
2013-06-29 01:55:34 +08:00
|
|
|
def display_username
|
2013-09-04 05:19:29 +08:00
|
|
|
object.user.try(:name)
|
2013-06-29 01:55:34 +08:00
|
|
|
end
|
|
|
|
|
2014-02-11 05:59:36 +08:00
|
|
|
def primary_group_name
|
2014-04-24 10:42:04 +08:00
|
|
|
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
|
2014-02-11 05:59:36 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08: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 read
|
|
|
|
@topic_view.read?(object.post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
def score
|
|
|
|
object.score || 0
|
|
|
|
end
|
|
|
|
|
2013-06-26 06:39:20 +08:00
|
|
|
def user_title
|
2013-09-04 05:19:29 +08:00
|
|
|
object.user.try(:title)
|
2013-06-26 06:39:20 +08:00
|
|
|
end
|
|
|
|
|
2013-04-04 09:24:10 +08:00
|
|
|
def trust_level
|
2013-09-04 05:19:29 +08:00
|
|
|
object.user.try(:trust_level)
|
2013-04-04 09:24:10 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def reply_to_user
|
|
|
|
{
|
|
|
|
username: object.reply_to_user.username,
|
2014-05-22 15:37:02 +08:00
|
|
|
avatar_template: object.reply_to_user.avatar_template,
|
|
|
|
uploaded_avatar_id: object.reply_to_user.uploaded_avatar_id
|
2013-02-06 03:16:51 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def bookmarked
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-07-11 04:19:42 +08:00
|
|
|
def deleted_by
|
|
|
|
BasicUserSerializer.new(object.deleted_by, root: false).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_deleted_by?
|
2013-07-11 23:18:25 +08:00
|
|
|
scope.is_staff? && object.deleted_by.present?
|
2013-07-11 04:19:42 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Summary of the actions taken on this post
|
|
|
|
def actions_summary
|
|
|
|
result = []
|
2013-03-01 20:07:44 +08:00
|
|
|
PostActionType.types.each do |sym, id|
|
2013-02-06 03:16:51 +08: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
|
2014-08-04 23:29:01 +08:00
|
|
|
action_summary = {
|
|
|
|
id: id,
|
|
|
|
count: count,
|
|
|
|
hidden: (sym == :vote),
|
|
|
|
can_act: scope.post_can_act?(object, sym, taken_actions: post_actions)
|
|
|
|
}
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-26 04:36:57 +08:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2013-02-28 00:21:23 +08:00
|
|
|
# The following only applies if you're logged in
|
2013-03-05 08:42:44 +08:00
|
|
|
if action_summary[:can_act] && scope.current_user.present?
|
2014-08-04 23:29:01 +08:00
|
|
|
action_summary[:can_defer_flags] = scope.is_staff? &&
|
|
|
|
PostActionType.flag_types.values.include?(id) &&
|
2014-08-05 04:59:03 +08:00
|
|
|
active_flags.present? && active_flags.has_key?(id) &&
|
|
|
|
active_flags[id].count > 0
|
2013-03-15 00:15:24 +08:00
|
|
|
end
|
2013-02-28 00:21:23 +08:00
|
|
|
|
2013-03-15 00:15:24 +08:00
|
|
|
if post_actions.present? && post_actions.has_key?(id)
|
|
|
|
action_summary[:acted] = true
|
|
|
|
action_summary[:can_undo] = scope.can_delete?(post_actions[id])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-10-04 07:48:03 +08:00
|
|
|
# only show public data
|
|
|
|
unless scope.is_staff? || PostActionType.public_types.values.include?(id)
|
2013-02-06 03:16:51 +08:00
|
|
|
action_summary[:count] = action_summary[:acted] ? 1 : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
result << action_summary
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
def include_draft_sequence?
|
|
|
|
@draft_sequence.present?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_slug_title?
|
|
|
|
@topic_slug.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_raw?
|
2014-05-01 02:21:43 +08:00
|
|
|
@add_raw.present? && (!object.hidden || scope.user.try(:staff?) || yours)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_link_counts?
|
|
|
|
return true if @single_post_link_counts.present?
|
|
|
|
|
2013-03-05 08:42:44 +08:00
|
|
|
@topic_view.present? && @topic_view.link_counts.present? && @topic_view.link_counts[object.id].present?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_read?
|
|
|
|
@topic_view.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_reply_to_user?
|
2014-07-31 09:38:03 +08:00
|
|
|
!(SiteSetting.suppress_reply_when_quoting && object.reply_quoted?) && object.reply_to_user
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_bookmarked?
|
2013-03-05 08:42:44 +08:00
|
|
|
post_actions.present? && post_actions.keys.include?(PostActionType.types[:bookmark])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
def include_display_username?
|
|
|
|
SiteSetting.enable_names?
|
|
|
|
end
|
|
|
|
|
2014-03-13 22:47:37 +08:00
|
|
|
def can_view_edit_history
|
|
|
|
scope.can_view_post_revisions?(object)
|
|
|
|
end
|
|
|
|
|
2014-05-15 02:38:04 +08:00
|
|
|
def user_custom_fields
|
|
|
|
@topic_view.user_custom_fields[object.user_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_custom_fields?
|
|
|
|
return if @topic_view.blank?
|
|
|
|
custom_fields = @topic_view.user_custom_fields
|
|
|
|
custom_fields && custom_fields[object.user_id]
|
|
|
|
end
|
|
|
|
|
2014-08-02 02:53:35 +08:00
|
|
|
def static_doc
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_static_doc?
|
|
|
|
object.post_number == 1 && Discourse.static_doc_topic_ids.include?(object.topic_id)
|
|
|
|
end
|
|
|
|
|
2014-09-05 01:04:22 +08:00
|
|
|
def include_via_email?
|
|
|
|
object.via_email?
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
private
|
|
|
|
|
2014-08-04 23:29:01 +08:00
|
|
|
def post_actions
|
|
|
|
@post_actions ||= (@topic_view.present? && @topic_view.all_post_actions.present?) ? @topic_view.all_post_actions[object.id] : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_flags
|
|
|
|
@active_flags ||= (@topic_view.present? && @topic_view.all_active_flags.present?) ? @topic_view.all_active_flags[object.id] : nil
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|