2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2017-08-12 10:18:04 +08:00
|
|
|
INSTANCE_VARS ||= %i[
|
2015-04-24 01:33:29 +08:00
|
|
|
parent_post
|
|
|
|
add_raw
|
2015-09-02 08:45:09 +08:00
|
|
|
add_title
|
2015-04-24 01:33:29 +08:00
|
|
|
single_post_link_counts
|
|
|
|
draft_sequence
|
|
|
|
post_actions
|
2018-10-09 22:21:41 +08:00
|
|
|
all_post_actions
|
|
|
|
add_excerpt
|
2015-04-24 01:33:29 +08:00
|
|
|
]
|
2015-01-24 13:04:14 +08:00
|
|
|
|
2019-05-07 09:27:05 +08:00
|
|
|
INSTANCE_VARS.each { |v| self.public_send(:attr_accessor, v) }
|
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,
|
|
|
|
:incoming_link_count,
|
|
|
|
:reads,
|
2019-08-27 20:09:00 +08:00
|
|
|
:readers_count,
|
2013-02-06 03:16:51 +08:00
|
|
|
:score,
|
|
|
|
:yours,
|
|
|
|
:topic_id,
|
2014-10-11 00:21:44 +08:00
|
|
|
:topic_slug,
|
2015-09-02 08:45:09 +08:00
|
|
|
:topic_title,
|
|
|
|
:topic_html_title,
|
|
|
|
:category_id,
|
2013-02-06 03:16:51 +08:00
|
|
|
:display_username,
|
2014-02-11 05:59:36 +08:00
|
|
|
:primary_group_name,
|
2021-07-08 15:46:21 +08:00
|
|
|
:flair_name,
|
|
|
|
:flair_url,
|
|
|
|
:flair_bg_color,
|
|
|
|
:flair_color,
|
2023-04-25 03:04:26 +08:00
|
|
|
:flair_group_id,
|
2013-02-06 03:16:51 +08:00
|
|
|
:version,
|
|
|
|
:can_edit,
|
|
|
|
:can_delete,
|
2021-10-13 17:53:23 +08:00
|
|
|
:can_permanently_delete,
|
2013-02-08 04:12:55 +08:00
|
|
|
:can_recover,
|
2023-06-01 11:32:05 +08:00
|
|
|
:can_see_hidden_post,
|
2016-01-11 23:26:00 +08:00
|
|
|
:can_wiki,
|
2013-02-06 03:16:51 +08:00
|
|
|
:link_counts,
|
|
|
|
:read,
|
2013-06-26 06:39:20 +08:00
|
|
|
:user_title,
|
2020-06-02 02:44:41 +08:00
|
|
|
:title_is_group,
|
2013-02-06 03:16:51 +08:00
|
|
|
:reply_to_user,
|
|
|
|
:bookmarked,
|
2019-12-11 12:04:02 +08:00
|
|
|
:bookmark_reminder_at,
|
2020-04-20 11:30:04 +08:00
|
|
|
:bookmark_id,
|
|
|
|
:bookmark_name,
|
2020-07-21 08:00:39 +08:00
|
|
|
:bookmark_auto_delete_preference,
|
2013-02-06 03:16:51 +08:00
|
|
|
: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?,
|
2020-07-29 05:15:04 +08:00
|
|
|
:group_moderator,
|
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,
|
2015-07-25 04:39:03 +08:00
|
|
|
:via_email,
|
2016-04-21 03:29:27 +08:00
|
|
|
:is_auto_generated,
|
2016-01-11 19:42:06 +08:00
|
|
|
:action_code,
|
2017-09-12 21:37:47 +08:00
|
|
|
:action_code_who,
|
2021-11-08 11:32:17 +08:00
|
|
|
:action_code_path,
|
2020-11-11 20:49:53 +08:00
|
|
|
:notice,
|
2018-01-26 04:38:40 +08:00
|
|
|
:last_wiki_edit,
|
2018-10-09 22:21:41 +08:00
|
|
|
:locked,
|
2019-05-04 02:26:37 +08:00
|
|
|
:excerpt,
|
|
|
|
:reviewable_id,
|
|
|
|
:reviewable_score_count,
|
2022-04-13 22:28:09 +08:00
|
|
|
:reviewable_score_pending_count,
|
2022-08-08 21:35:26 +08:00
|
|
|
:user_suspended,
|
2022-12-06 23:10:36 +08:00
|
|
|
:user_status,
|
|
|
|
:mentioned_users
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-24 13:04:14 +08:00
|
|
|
def initialize(object, opts)
|
|
|
|
super(object, opts)
|
2019-05-07 09:27:05 +08:00
|
|
|
|
2015-01-24 13:04:14 +08:00
|
|
|
PostSerializer::INSTANCE_VARS.each do |name|
|
2019-05-07 09:27:05 +08:00
|
|
|
self.public_send("#{name}=", opts[name]) if opts.include? name
|
2015-01-24 13:04:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-11 00:21:44 +08:00
|
|
|
def topic_slug
|
2018-02-21 17:01:52 +08:00
|
|
|
topic&.slug
|
2014-10-11 00:21:44 +08:00
|
|
|
end
|
|
|
|
|
2015-09-02 08:45:09 +08:00
|
|
|
def include_topic_title?
|
|
|
|
@add_title
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_topic_html_title?
|
|
|
|
@add_title
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_category_id?
|
|
|
|
@add_title
|
|
|
|
end
|
|
|
|
|
2018-10-09 22:21:41 +08:00
|
|
|
def include_excerpt?
|
|
|
|
@add_excerpt
|
|
|
|
end
|
|
|
|
|
2015-09-02 08:45:09 +08:00
|
|
|
def topic_title
|
2018-02-21 17:01:52 +08:00
|
|
|
topic&.title
|
2015-09-02 08:45:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def topic_html_title
|
2018-02-21 17:01:52 +08:00
|
|
|
topic&.fancy_title
|
2015-09-02 08:45:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def category_id
|
2018-02-21 17:01:52 +08:00
|
|
|
topic&.category_id
|
2015-09-02 08:45:09 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 06:36:07 +08:00
|
|
|
def moderator?
|
2017-09-08 13:07:22 +08:00
|
|
|
!!(object&.user&.moderator?)
|
2014-05-12 09:28:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def admin?
|
2017-09-08 13:07:22 +08:00
|
|
|
!!(object&.user&.admin?)
|
2013-02-07 06:36:07 +08:00
|
|
|
end
|
|
|
|
|
2013-05-24 11:32:57 +08:00
|
|
|
def staff?
|
2017-09-08 13:07:22 +08:00
|
|
|
!!(object&.user&.staff?)
|
2013-05-24 11:32:57 +08:00
|
|
|
end
|
|
|
|
|
2020-07-29 05:15:04 +08:00
|
|
|
def group_moderator
|
|
|
|
!!@group_moderator
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_group_moderator?
|
|
|
|
@group_moderator ||=
|
|
|
|
begin
|
|
|
|
if @topic_view
|
|
|
|
@topic_view.category_group_moderator_user_ids.include?(object.user_id)
|
|
|
|
else
|
|
|
|
object&.user&.guardian&.is_category_group_moderator?(object&.topic&.category)
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2020-07-29 05:15:04 +08:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2021-10-13 17:53:23 +08:00
|
|
|
def can_permanently_delete
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_can_permanently_delete?
|
2022-04-01 09:03:39 +08:00
|
|
|
SiteSetting.can_permanently_delete && scope.is_admin? && object.deleted_at
|
2021-10-13 17:53:23 +08:00
|
|
|
end
|
|
|
|
|
2013-02-08 04:12:55 +08:00
|
|
|
def can_recover
|
|
|
|
scope.can_recover_post?(object)
|
|
|
|
end
|
|
|
|
|
2023-06-01 11:32:05 +08:00
|
|
|
def can_see_hidden_post
|
|
|
|
scope.can_see_hidden_post?(object)
|
|
|
|
end
|
|
|
|
|
2016-01-11 23:26:00 +08:00
|
|
|
def can_wiki
|
|
|
|
scope.can_wiki?(object)
|
|
|
|
end
|
|
|
|
|
2013-06-29 01:55:34 +08:00
|
|
|
def display_username
|
2017-09-08 13:07:22 +08:00
|
|
|
object.user&.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
|
|
|
|
|
2021-07-08 15:46:21 +08:00
|
|
|
def flair_name
|
|
|
|
object.user&.flair_group&.name
|
2016-08-17 00:34:04 +08:00
|
|
|
end
|
|
|
|
|
2021-07-08 15:46:21 +08:00
|
|
|
def flair_url
|
|
|
|
object.user&.flair_group&.flair_url
|
2016-08-27 05:15:37 +08:00
|
|
|
end
|
|
|
|
|
2021-07-08 15:46:21 +08:00
|
|
|
def flair_bg_color
|
|
|
|
object.user&.flair_group&.flair_bg_color
|
|
|
|
end
|
|
|
|
|
|
|
|
def flair_color
|
|
|
|
object.user&.flair_group&.flair_color
|
2016-08-17 00:34:04 +08:00
|
|
|
end
|
|
|
|
|
2023-04-25 03:04:26 +08:00
|
|
|
def flair_group_id
|
|
|
|
object.user&.flair_group_id
|
|
|
|
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
|
2017-09-08 13:07:22 +08:00
|
|
|
object&.user&.title
|
2013-06-26 06:39:20 +08:00
|
|
|
end
|
|
|
|
|
2020-06-02 02:44:41 +08:00
|
|
|
def title_is_group
|
|
|
|
object&.user&.title == object.user&.primary_group&.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_title_is_group?
|
|
|
|
object&.user&.title.present?
|
|
|
|
end
|
|
|
|
|
2013-04-04 09:24:10 +08:00
|
|
|
def trust_level
|
2017-09-08 13:07:22 +08:00
|
|
|
object&.user&.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,
|
2022-04-20 23:07:51 +08:00
|
|
|
name: object.reply_to_user.name,
|
2015-09-11 16:14:34 +08:00
|
|
|
avatar_template: object.reply_to_user.avatar_template,
|
2013-02-06 03:16:51 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2015-01-24 13:04:14 +08: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-06 03:16:51 +08:00
|
|
|
# Summary of the actions taken on this post
|
|
|
|
def actions_summary
|
|
|
|
result = []
|
2017-09-08 13:07:22 +08:00
|
|
|
can_see_post = scope.can_see_post?(object)
|
|
|
|
|
2022-05-10 08:42:18 +08:00
|
|
|
PostActionType.types.each do |sym, id|
|
2013-02-06 03:16:51 +08:00
|
|
|
count_col = "#{sym}_count".to_sym
|
|
|
|
|
2019-05-07 09:27:05 +08:00
|
|
|
count = object.public_send(count_col) if object.respond_to?(count_col)
|
2015-07-15 03:12:00 +08:00
|
|
|
summary = { id: id, count: count }
|
2017-09-08 13:07:22 +08:00
|
|
|
|
|
|
|
if scope.post_can_act?(
|
|
|
|
object,
|
|
|
|
sym,
|
|
|
|
opts: {
|
|
|
|
taken_actions: actions,
|
|
|
|
},
|
|
|
|
can_see_post: can_see_post,
|
|
|
|
)
|
|
|
|
summary[:can_act] = true
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-05-10 10:04:29 +08:00
|
|
|
if sym == :notify_user &&
|
|
|
|
(
|
|
|
|
(scope.current_user.present? && scope.current_user == object.user) ||
|
|
|
|
(object.user && object.user.bot?)
|
|
|
|
)
|
2015-07-15 03:12:00 +08:00
|
|
|
summary.delete(:can_act)
|
2014-07-26 04:36:57 +08:00
|
|
|
end
|
|
|
|
|
2023-07-21 21:21:07 +08:00
|
|
|
if actions.present? && SiteSetting.allow_anonymous_likes && sym == :like &&
|
|
|
|
!scope.can_delete_post_action?(actions[id])
|
|
|
|
summary.delete(:can_act)
|
|
|
|
end
|
|
|
|
|
2015-01-24 13:04:14 +08:00
|
|
|
if actions.present? && actions.has_key?(id)
|
2015-07-15 03:12:00 +08:00
|
|
|
summary[:acted] = true
|
2023-07-21 21:21:07 +08:00
|
|
|
|
2015-07-15 03:12:00 +08:00
|
|
|
summary[:can_undo] = true if scope.can_delete?(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)
|
2015-07-15 03:12:00 +08:00
|
|
|
summary[:count] = summary[:acted] ? 1 : 0
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-07-15 03:12:00 +08:00
|
|
|
summary.delete(:count) if summary[:count] == 0
|
|
|
|
|
|
|
|
# Only include it if the user can do it or it has a count
|
2022-08-22 23:40:09 +08:00
|
|
|
result << summary if summary[:can_act] || summary[:count]
|
2013-02-06 03:16:51 +08:00
|
|
|
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?
|
2017-09-08 13:07:22 +08:00
|
|
|
@add_raw.present? && (!object.hidden || scope.user&.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
|
|
|
|
|
2019-12-11 12:04:02 +08:00
|
|
|
def bookmarked
|
2020-07-10 12:35:53 +08:00
|
|
|
@bookmarked ||= post_bookmark.present?
|
2019-12-11 12:04:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_bookmark_reminder_at?
|
2020-07-10 12:35:53 +08:00
|
|
|
bookmarked
|
2019-12-11 12:04:02 +08:00
|
|
|
end
|
|
|
|
|
2020-04-20 11:30:04 +08:00
|
|
|
def include_bookmark_name?
|
2020-07-10 12:35:53 +08:00
|
|
|
bookmarked
|
2020-04-20 11:30:04 +08:00
|
|
|
end
|
|
|
|
|
2020-07-21 08:00:39 +08:00
|
|
|
def include_bookmark_auto_delete_preference?
|
|
|
|
bookmarked
|
|
|
|
end
|
|
|
|
|
2020-04-20 11:30:04 +08:00
|
|
|
def include_bookmark_id?
|
2020-07-10 12:35:53 +08:00
|
|
|
bookmarked
|
2020-04-20 11:30:04 +08:00
|
|
|
end
|
|
|
|
|
2019-12-11 12:04:02 +08:00
|
|
|
def post_bookmark
|
2022-05-23 08:07:15 +08:00
|
|
|
if @topic_view.present?
|
|
|
|
@post_bookmark ||= @topic_view.bookmarks.find { |bookmark| bookmark.bookmarkable == object }
|
2021-06-28 16:24:23 +08:00
|
|
|
else
|
2022-05-23 08:07:15 +08:00
|
|
|
@post_bookmark ||= Bookmark.find_by(user: scope.user, bookmarkable: object)
|
2021-06-28 16:24:23 +08:00
|
|
|
end
|
2019-12-11 12:04:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def bookmark_reminder_at
|
|
|
|
post_bookmark&.reminder_at
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2020-04-20 11:30:04 +08:00
|
|
|
def bookmark_name
|
|
|
|
post_bookmark&.name
|
|
|
|
end
|
|
|
|
|
2020-07-21 08:00:39 +08:00
|
|
|
def bookmark_auto_delete_preference
|
|
|
|
post_bookmark&.auto_delete_preference
|
2020-05-07 11:37:39 +08:00
|
|
|
end
|
|
|
|
|
2020-04-20 11:30:04 +08:00
|
|
|
def bookmark_id
|
|
|
|
post_bookmark&.id
|
|
|
|
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
|
2014-10-28 05:06:43 +08:00
|
|
|
scope.can_view_edit_history?(object)
|
2014-03-13 22:47:37 +08:00
|
|
|
end
|
|
|
|
|
2014-05-15 02:38:04 +08:00
|
|
|
def user_custom_fields
|
2018-11-14 01:16:50 +08:00
|
|
|
user_custom_fields_object[object.user_id]
|
2014-05-15 02:38:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_custom_fields?
|
2018-11-14 01:16:50 +08:00
|
|
|
user_custom_fields_object[object.user_id]
|
2014-05-15 02:38:04 +08:00
|
|
|
end
|
|
|
|
|
2014-08-02 02:53:35 +08:00
|
|
|
def static_doc
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_static_doc?
|
2015-04-24 01:33:29 +08:00
|
|
|
object.is_first_post? && Discourse.static_doc_topic_ids.include?(object.topic_id)
|
2014-08-02 02:53:35 +08:00
|
|
|
end
|
|
|
|
|
2014-09-05 01:04:22 +08:00
|
|
|
def include_via_email?
|
|
|
|
object.via_email?
|
|
|
|
end
|
|
|
|
|
2016-04-21 03:29:27 +08:00
|
|
|
def is_auto_generated
|
2017-09-08 13:07:22 +08:00
|
|
|
object.incoming_email&.is_auto_generated
|
2016-04-21 03:29:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_is_auto_generated?
|
|
|
|
object.via_email? && is_auto_generated
|
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
def version
|
2018-10-31 22:47:00 +08:00
|
|
|
return 1 if object.hidden && !scope.can_view_hidden_post_revisions?
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
scope.is_staff? ? object.version : object.public_version
|
|
|
|
end
|
|
|
|
|
2022-01-11 14:05:16 +08:00
|
|
|
def action_code
|
|
|
|
return "open_topic" if object.action_code == "public_topic" && SiteSetting.login_required?
|
|
|
|
object.action_code
|
|
|
|
end
|
|
|
|
|
2015-07-25 04:39:03 +08:00
|
|
|
def include_action_code?
|
|
|
|
object.action_code.present?
|
|
|
|
end
|
|
|
|
|
2016-01-11 19:42:06 +08: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
|
|
|
|
|
2021-11-08 11:32:17 +08:00
|
|
|
def action_code_path
|
|
|
|
post_custom_fields["action_code_path"]
|
2021-11-08 05:24:44 +08:00
|
|
|
end
|
|
|
|
|
2021-11-08 11:32:17 +08:00
|
|
|
def include_action_code_path?
|
|
|
|
include_action_code? && action_code_path.present?
|
2021-11-08 05:24:44 +08:00
|
|
|
end
|
|
|
|
|
2020-11-11 20:49:53 +08:00
|
|
|
def notice
|
|
|
|
post_custom_fields[Post::NOTICE]
|
2019-03-08 16:48:35 +08:00
|
|
|
end
|
|
|
|
|
2020-11-11 20:49:53 +08:00
|
|
|
def include_notice?
|
|
|
|
return false if notice.blank?
|
|
|
|
|
|
|
|
case notice["type"]
|
2019-04-19 22:53:58 +08:00
|
|
|
when Post.notices[:custom]
|
|
|
|
return true
|
|
|
|
when Post.notices[:new_user]
|
|
|
|
min_trust_level = SiteSetting.new_user_notice_tl
|
|
|
|
when Post.notices[:returning_user]
|
|
|
|
min_trust_level = SiteSetting.returning_user_notice_tl
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2019-03-11 17:19:58 +08:00
|
|
|
|
2020-11-11 20:49:53 +08:00
|
|
|
scope.user && scope.user.id != object.user_id && scope.user.has_trust_level?(min_trust_level)
|
2019-03-08 16:48:35 +08:00
|
|
|
end
|
|
|
|
|
2018-01-26 04:38:40 +08:00
|
|
|
def locked
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only show locked posts to the users who made the post and staff
|
|
|
|
def include_locked?
|
|
|
|
object.locked? && (yours || scope.is_staff?)
|
|
|
|
end
|
|
|
|
|
2017-09-12 21:37:47 +08:00
|
|
|
def last_wiki_edit
|
|
|
|
object.revisions.last.updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_last_wiki_edit?
|
2017-09-13 23:46:58 +08:00
|
|
|
object.wiki && object.post_number == 1 && object.revisions.size > 0
|
2017-09-12 21:37:47 +08:00
|
|
|
end
|
|
|
|
|
2018-05-21 10:52:57 +08:00
|
|
|
def include_hidden_reason_id?
|
|
|
|
object.hidden
|
|
|
|
end
|
|
|
|
|
2019-05-04 02:26:37 +08:00
|
|
|
# If we have a topic view, it has bulk values for the reviewable content we can use
|
|
|
|
def reviewable_id
|
|
|
|
if @topic_view.present?
|
|
|
|
for_post = @topic_view.reviewable_counts[object.id]
|
|
|
|
return for_post ? for_post[:reviewable_id] : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
reviewable&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_reviewable_id?
|
|
|
|
can_review_topic?
|
|
|
|
end
|
|
|
|
|
|
|
|
def reviewable_score_count
|
|
|
|
if @topic_view.present?
|
|
|
|
for_post = @topic_view.reviewable_counts[object.id]
|
|
|
|
return for_post ? for_post[:total] : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
reviewable_scores.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_reviewable_score_count?
|
|
|
|
can_review_topic?
|
|
|
|
end
|
|
|
|
|
|
|
|
def reviewable_score_pending_count
|
|
|
|
if @topic_view.present?
|
|
|
|
for_post = @topic_view.reviewable_counts[object.id]
|
|
|
|
return for_post ? for_post[:pending] : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
reviewable_scores.count { |rs| rs.pending? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_reviewable_score_pending_count?
|
|
|
|
can_review_topic?
|
|
|
|
end
|
|
|
|
|
2022-04-13 22:28:09 +08:00
|
|
|
def user_suspended
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_suspended?
|
|
|
|
object.user&.suspended?
|
|
|
|
end
|
|
|
|
|
2022-08-08 21:35:26 +08:00
|
|
|
def include_user_status?
|
|
|
|
SiteSetting.enable_user_status && object.user&.has_status?
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_status
|
|
|
|
UserStatusSerializer.new(object.user&.user_status, root: false)
|
|
|
|
end
|
|
|
|
|
2022-12-06 23:10:36 +08:00
|
|
|
def mentioned_users
|
2023-01-20 07:58:00 +08:00
|
|
|
users =
|
|
|
|
if @topic_view && (mentioned_users = @topic_view.mentioned_users[object.id])
|
|
|
|
mentioned_users
|
|
|
|
else
|
2023-05-26 23:12:46 +08:00
|
|
|
query = User
|
|
|
|
query = query.includes(:user_status) if SiteSetting.enable_user_status
|
|
|
|
query = query.where(username: object.mentions)
|
2023-01-20 07:58:00 +08:00
|
|
|
end
|
2022-12-06 23:10:36 +08:00
|
|
|
|
2022-12-29 18:34:59 +08:00
|
|
|
users.map { |user| BasicUserWithStatusSerializer.new(user, root: false) }
|
2022-12-06 23:10:36 +08:00
|
|
|
end
|
|
|
|
|
2023-01-20 07:58:00 +08:00
|
|
|
def include_mentioned_users?
|
|
|
|
SiteSetting.enable_user_status
|
|
|
|
end
|
|
|
|
|
2019-05-04 02:26:37 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def can_review_topic?
|
|
|
|
return @can_review_topic unless @can_review_topic.nil?
|
|
|
|
@can_review_topic = @topic_view&.can_review_topic
|
|
|
|
@can_review_topic ||= scope.can_review_topic?(object.topic)
|
|
|
|
@can_review_topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def reviewable
|
|
|
|
@reviewable ||= Reviewable.where(target: object).includes(:reviewable_scores).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def reviewable_scores
|
2023-12-07 06:25:00 +08:00
|
|
|
reviewable&.reviewable_scores.to_a
|
2019-05-04 02:26:37 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-11-14 01:16:50 +08:00
|
|
|
def user_custom_fields_object
|
|
|
|
(@topic_view&.user_custom_fields || @options[:user_custom_fields] || {})
|
|
|
|
end
|
|
|
|
|
2018-02-21 17:01:52 +08:00
|
|
|
def topic
|
|
|
|
@topic = object.topic
|
2018-11-22 22:08:37 +08:00
|
|
|
@topic ||= Topic.with_deleted.find_by(id: object.topic_id) if scope.is_staff?
|
2018-02-21 17:01:52 +08:00
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
2014-08-04 23:29:01 +08:00
|
|
|
def post_actions
|
2017-08-12 10:18:04 +08:00
|
|
|
@post_actions ||= (@topic_view&.all_post_actions || {})[object.id]
|
2014-08-04 23:29:01 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|