2013-06-28 13:55:34 -04:00
|
|
|
class PostSerializer < BasicPostSerializer
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# To pass in additional information we might need
|
2017-08-11 22:18:04 -04:00
|
|
|
INSTANCE_VARS ||= [
|
2015-04-23 13:33:29 -04:00
|
|
|
:topic_view,
|
|
|
|
:parent_post,
|
|
|
|
:add_raw,
|
2015-09-01 20:45:09 -04:00
|
|
|
:add_title,
|
2015-04-23 13:33:29 -04:00
|
|
|
:single_post_link_counts,
|
|
|
|
:draft_sequence,
|
|
|
|
:post_actions,
|
|
|
|
:all_post_actions
|
|
|
|
]
|
2015-01-24 00:04:14 -05:00
|
|
|
|
|
|
|
INSTANCE_VARS.each do |v|
|
|
|
|
self.send(:attr_accessor, v)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-28 13:55:34 -04:00
|
|
|
attributes :post_number,
|
2013-02-05 14:16:51 -05:00
|
|
|
:post_type,
|
|
|
|
: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,
|
|
|
|
:topic_id,
|
2014-10-10 12:21:44 -04:00
|
|
|
:topic_slug,
|
2015-09-01 20:45:09 -04:00
|
|
|
:topic_title,
|
|
|
|
:topic_html_title,
|
|
|
|
:category_id,
|
2013-02-05 14:16:51 -05:00
|
|
|
:display_username,
|
2014-02-10 16:59:36 -05:00
|
|
|
:primary_group_name,
|
2016-08-16 12:34:04 -04:00
|
|
|
:primary_group_flair_url,
|
|
|
|
:primary_group_flair_bg_color,
|
2016-08-26 17:15:37 -04:00
|
|
|
:primary_group_flair_color,
|
2013-02-05 14:16:51 -05:00
|
|
|
:version,
|
|
|
|
:can_edit,
|
|
|
|
:can_delete,
|
2013-02-07 15:12:55 -05:00
|
|
|
:can_recover,
|
2016-01-11 10:26:00 -05:00
|
|
|
:can_wiki,
|
2013-02-05 14:16:51 -05:00
|
|
|
:link_counts,
|
|
|
|
:read,
|
2013-06-25 18:39:20 -04:00
|
|
|
:user_title,
|
2013-02-05 14:16:51 -05:00
|
|
|
:reply_to_user,
|
|
|
|
:bookmarked,
|
|
|
|
:raw,
|
|
|
|
:actions_summary,
|
2013-02-06 17:36:07 -05:00
|
|
|
:moderator?,
|
2014-05-11 21:28:24 -04:00
|
|
|
:admin?,
|
2013-05-23 23:32:57 -04:00
|
|
|
:staff?,
|
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-07-10 14:56:00 -04:00
|
|
|
:trust_level,
|
2013-07-12 12:08:23 -04:00
|
|
|
:deleted_at,
|
2013-07-22 03:48:24 -04:00
|
|
|
:deleted_by,
|
2013-11-15 17:28:16 -05:00
|
|
|
:user_deleted,
|
2014-03-13 10:47:37 -04:00
|
|
|
:edit_reason,
|
2014-05-13 08:53:11 -04:00
|
|
|
:can_view_edit_history,
|
2014-05-14 14:38:04 -04:00
|
|
|
:wiki,
|
2014-08-01 14:53:35 -04:00
|
|
|
:user_custom_fields,
|
2014-09-04 13:04:22 -04:00
|
|
|
:static_doc,
|
2015-07-24 16:39:03 -04:00
|
|
|
:via_email,
|
2016-04-20 15:29:27 -04:00
|
|
|
:is_auto_generated,
|
2016-01-11 06:42:06 -05:00
|
|
|
:action_code,
|
|
|
|
:action_code_who
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-01-24 00:04:14 -05:00
|
|
|
def initialize(object, opts)
|
|
|
|
super(object, opts)
|
|
|
|
PostSerializer::INSTANCE_VARS.each do |name|
|
|
|
|
if opts.include? name
|
|
|
|
self.send("#{name}=", opts[name])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-10 12:21:44 -04:00
|
|
|
def topic_slug
|
2015-10-02 01:00:00 -04:00
|
|
|
object.topic && object.topic.slug
|
2014-10-10 12:21:44 -04:00
|
|
|
end
|
|
|
|
|
2015-09-01 20:45:09 -04:00
|
|
|
def include_topic_title?
|
|
|
|
@add_title
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_topic_html_title?
|
|
|
|
@add_title
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_category_id?
|
|
|
|
@add_title
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_title
|
|
|
|
object.topic.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_html_title
|
|
|
|
object.topic.fancy_title
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_id
|
|
|
|
object.topic.category_id
|
|
|
|
end
|
|
|
|
|
2013-02-06 17:36:07 -05:00
|
|
|
def moderator?
|
2014-09-25 11:44:48 -04:00
|
|
|
!!(object.try(:user).try(:moderator?))
|
2014-05-11 21:28:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def admin?
|
2014-09-25 11:44:48 -04:00
|
|
|
!!(object.try(:user).try(:admin?))
|
2013-02-06 17:36:07 -05:00
|
|
|
end
|
|
|
|
|
2013-05-23 23:32:57 -04:00
|
|
|
def staff?
|
2014-09-25 11:44:48 -04:00
|
|
|
!!(object.try(:user).try(:staff?))
|
2013-05-23 23:32:57 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05: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-07 15:12:55 -05:00
|
|
|
def can_recover
|
|
|
|
scope.can_recover_post?(object)
|
|
|
|
end
|
|
|
|
|
2016-01-11 10:26:00 -05:00
|
|
|
def can_wiki
|
|
|
|
scope.can_wiki?(object)
|
|
|
|
end
|
|
|
|
|
2013-06-28 13:55:34 -04:00
|
|
|
def display_username
|
2013-09-03 17:19:29 -04:00
|
|
|
object.user.try(:name)
|
2013-06-28 13:55:34 -04:00
|
|
|
end
|
|
|
|
|
2014-02-10 16:59:36 -05:00
|
|
|
def primary_group_name
|
2014-04-23 22:42:04 -04: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-10 16:59:36 -05:00
|
|
|
end
|
|
|
|
|
2016-08-16 12:34:04 -04:00
|
|
|
def primary_group_flair_url
|
2016-08-26 17:15:37 -04:00
|
|
|
object.user.try(:primary_group).try(:flair_url)
|
2016-08-16 12:34:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def primary_group_flair_bg_color
|
2016-08-26 17:15:37 -04:00
|
|
|
object.user.try(:primary_group).try(:flair_bg_color)
|
|
|
|
end
|
|
|
|
|
|
|
|
def primary_group_flair_color
|
|
|
|
object.user.try(:primary_group).try(:flair_color)
|
2016-08-16 12:34:04 -04:00
|
|
|
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 read
|
|
|
|
@topic_view.read?(object.post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
def score
|
|
|
|
object.score || 0
|
|
|
|
end
|
|
|
|
|
2013-06-25 18:39:20 -04:00
|
|
|
def user_title
|
2014-09-25 11:44:48 -04:00
|
|
|
object.try(:user).try(:title)
|
2013-06-25 18:39:20 -04:00
|
|
|
end
|
|
|
|
|
2013-04-03 21:24:10 -04:00
|
|
|
def trust_level
|
2014-09-25 11:44:48 -04:00
|
|
|
object.try(:user).try(:trust_level)
|
2013-04-03 21:24:10 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def reply_to_user
|
|
|
|
{
|
|
|
|
username: object.reply_to_user.username,
|
2015-09-11 04:14:34 -04:00
|
|
|
avatar_template: object.reply_to_user.avatar_template
|
2013-02-05 14:16:51 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def bookmarked
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-07-10 16:19:42 -04:00
|
|
|
def deleted_by
|
|
|
|
BasicUserSerializer.new(object.deleted_by, root: false).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_deleted_by?
|
2013-07-11 11:18:25 -04:00
|
|
|
scope.is_staff? && object.deleted_by.present?
|
2013-07-10 16:19:42 -04:00
|
|
|
end
|
|
|
|
|
2015-01-24 00:04:14 -05:00
|
|
|
# Helper function to decide between #post_actions and @all_post_actions
|
|
|
|
def actions
|
|
|
|
return post_actions if post_actions.present?
|
|
|
|
return all_post_actions[object.id] if all_post_actions.present?
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# 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)
|
2015-07-14 15:12:00 -04:00
|
|
|
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)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-25 16:36:57 -04:00
|
|
|
if sym == :notify_user && scope.current_user.present? && scope.current_user == object.user
|
2015-07-14 15:12:00 -04:00
|
|
|
summary.delete(:can_act)
|
2014-07-25 16:36:57 -04:00
|
|
|
end
|
|
|
|
|
2013-02-27 11:21:23 -05:00
|
|
|
# The following only applies if you're logged in
|
2015-07-14 15:12:00 -04:00
|
|
|
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
|
2013-03-14 12:15:24 -04:00
|
|
|
end
|
2013-02-27 11:21:23 -05:00
|
|
|
|
2015-01-24 00:04:14 -05:00
|
|
|
if actions.present? && actions.has_key?(id)
|
2015-07-14 15:12:00 -04:00
|
|
|
summary[:acted] = true
|
|
|
|
summary[:can_undo] = true if scope.can_delete?(actions[id])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-03 19:48:03 -04:00
|
|
|
# only show public data
|
|
|
|
unless scope.is_staff? || PostActionType.public_types.values.include?(id)
|
2015-07-14 15:12:00 -04:00
|
|
|
summary[:count] = summary[:acted] ? 1 : 0
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-07-14 15:12:00 -04:00
|
|
|
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
|
2013-02-05 14:16:51 -05:00
|
|
|
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?
|
2014-04-30 14:21:43 -04:00
|
|
|
@add_raw.present? && (!object.hidden || scope.user.try(:staff?) || yours)
|
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?
|
2014-07-30 21:38:03 -04:00
|
|
|
!(SiteSetting.suppress_reply_when_quoting && object.reply_quoted?) && object.reply_to_user
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_bookmarked?
|
2015-01-24 00:04:14 -05:00
|
|
|
actions.present? && actions.keys.include?(PostActionType.types[:bookmark])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
def include_display_username?
|
|
|
|
SiteSetting.enable_names?
|
|
|
|
end
|
|
|
|
|
2014-03-13 10:47:37 -04:00
|
|
|
def can_view_edit_history
|
2014-10-27 17:06:43 -04:00
|
|
|
scope.can_view_edit_history?(object)
|
2014-03-13 10:47:37 -04:00
|
|
|
end
|
|
|
|
|
2014-05-14 14:38:04 -04:00
|
|
|
def user_custom_fields
|
|
|
|
@topic_view.user_custom_fields[object.user_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_custom_fields?
|
2017-08-11 22:18:04 -04:00
|
|
|
(@topic_view&.user_custom_fields || {})[object.user_id]
|
2014-05-14 14:38:04 -04:00
|
|
|
end
|
|
|
|
|
2014-08-01 14:53:35 -04:00
|
|
|
def static_doc
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_static_doc?
|
2015-04-23 13:33:29 -04:00
|
|
|
object.is_first_post? && Discourse.static_doc_topic_ids.include?(object.topic_id)
|
2014-08-01 14:53:35 -04:00
|
|
|
end
|
|
|
|
|
2014-09-04 13:04:22 -04:00
|
|
|
def include_via_email?
|
|
|
|
object.via_email?
|
|
|
|
end
|
|
|
|
|
2016-04-20 15:29:27 -04:00
|
|
|
def is_auto_generated
|
|
|
|
object.incoming_email.try(:is_auto_generated)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_is_auto_generated?
|
|
|
|
object.via_email? && is_auto_generated
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def version
|
|
|
|
scope.is_staff? ? object.version : object.public_version
|
|
|
|
end
|
|
|
|
|
2015-07-24 16:39:03 -04:00
|
|
|
def include_action_code?
|
|
|
|
object.action_code.present?
|
|
|
|
end
|
|
|
|
|
2016-01-11 06:42:06 -05:00
|
|
|
def action_code_who
|
|
|
|
post_custom_fields["action_code_who"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_action_code_who?
|
|
|
|
include_action_code? && action_code_who.present?
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
private
|
|
|
|
|
2014-08-04 11:29:01 -04:00
|
|
|
def post_actions
|
2017-08-11 22:18:04 -04:00
|
|
|
@post_actions ||= (@topic_view&.all_post_actions || {})[object.id]
|
2014-08-04 11:29:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def active_flags
|
2017-08-11 22:18:04 -04:00
|
|
|
@active_flags ||= (@topic_view&.all_active_flags || {})[object.id]
|
2014-08-04 11:29:01 -04:00
|
|
|
end
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
def post_custom_fields
|
2017-08-11 22:18:04 -04:00
|
|
|
@post_custom_fields ||= if @topic_view
|
|
|
|
(@topic_view.post_custom_fields || {})[object.id] || {}
|
|
|
|
else
|
|
|
|
object.custom_fields
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|