2013-03-07 04:17:07 +08:00
|
|
|
require_dependency 'pinned_check'
|
2015-04-28 03:55:10 +08:00
|
|
|
require_dependency 'new_post_manager'
|
2013-03-07 04:17:07 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class TopicViewSerializer < ApplicationSerializer
|
2013-06-21 05:20:08 +08:00
|
|
|
include PostStreamSerializerMixin
|
2017-09-26 14:42:27 +08:00
|
|
|
include SuggestedTopicsMixin
|
2018-02-22 22:57:02 +08:00
|
|
|
include TopicTagsMixin
|
2017-04-12 03:11:23 +08:00
|
|
|
include ApplicationHelper
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-29 00:01:01 +08:00
|
|
|
def self.attributes_from_topic(*list)
|
|
|
|
[list].flatten.each do |attribute|
|
|
|
|
attributes(attribute)
|
|
|
|
class_eval %{def #{attribute}
|
|
|
|
object.topic.#{attribute}
|
|
|
|
end}
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-01-29 00:01:01 +08:00
|
|
|
attributes_from_topic :id,
|
|
|
|
:title,
|
|
|
|
:fancy_title,
|
|
|
|
:posts_count,
|
|
|
|
:created_at,
|
|
|
|
:views,
|
|
|
|
:reply_count,
|
|
|
|
:like_count,
|
|
|
|
:last_posted_at,
|
|
|
|
:visible,
|
|
|
|
:closed,
|
|
|
|
:archived,
|
|
|
|
:has_summary,
|
|
|
|
:archetype,
|
|
|
|
:slug,
|
|
|
|
:category_id,
|
|
|
|
:word_count,
|
2015-04-28 03:55:10 +08:00
|
|
|
:deleted_at,
|
2015-06-10 16:30:29 +08:00
|
|
|
:pending_posts_count,
|
2017-04-27 11:53:53 +08:00
|
|
|
:user_id,
|
2017-09-13 23:14:03 +08:00
|
|
|
:featured_link,
|
2017-11-29 21:52:41 +08:00
|
|
|
:featured_link_root_domain,
|
2017-09-13 23:14:03 +08:00
|
|
|
:pinned_globally,
|
|
|
|
:pinned_at,
|
|
|
|
:pinned_until
|
2015-01-29 00:01:01 +08:00
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
attributes :draft,
|
|
|
|
:draft_key,
|
2013-02-06 03:16:51 +08:00
|
|
|
:draft_sequence,
|
|
|
|
:posted,
|
2014-04-10 08:56:56 +08:00
|
|
|
:unpinned,
|
2017-09-13 23:14:03 +08:00
|
|
|
:pinned,
|
2013-06-21 05:20:08 +08:00
|
|
|
:details,
|
2018-03-24 09:44:39 +08:00
|
|
|
:current_post_number,
|
2013-06-21 05:20:08 +08:00
|
|
|
:highest_post_number,
|
2013-07-13 04:38:21 +08:00
|
|
|
:last_read_post_number,
|
2016-05-25 04:21:23 +08:00
|
|
|
:last_read_post_id,
|
2014-02-06 06:54:16 +08:00
|
|
|
:deleted_by,
|
2014-07-16 05:02:43 +08:00
|
|
|
:has_deleted,
|
2014-04-02 03:29:15 +08:00
|
|
|
:actions_summary,
|
2014-09-08 23:11:56 +08:00
|
|
|
:expandable_first_post,
|
2014-12-13 00:47:20 +08:00
|
|
|
:is_warning,
|
2015-01-12 19:10:15 +08:00
|
|
|
:chunk_size,
|
2015-12-30 10:26:21 +08:00
|
|
|
:bookmarked,
|
2016-05-05 02:02:47 +08:00
|
|
|
:message_archived,
|
2017-05-12 06:23:18 +08:00
|
|
|
:topic_timer,
|
2017-10-05 11:48:42 +08:00
|
|
|
:private_topic_timer,
|
2017-05-17 03:04:09 +08:00
|
|
|
:unicode_title,
|
2017-09-13 23:14:03 +08:00
|
|
|
:message_bus_last_id,
|
2018-03-14 03:59:12 +08:00
|
|
|
:participant_count,
|
2018-05-21 10:28:12 +08:00
|
|
|
:destination_category_id,
|
2018-06-26 11:05:25 +08:00
|
|
|
:pm_with_non_human_user
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
# TODO: Split off into proper object / serializer
|
|
|
|
def details
|
2017-03-22 11:12:02 +08:00
|
|
|
topic = object.topic
|
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
result = {
|
2017-03-22 11:12:02 +08:00
|
|
|
created_by: BasicUserSerializer.new(topic.user, scope: scope, root: false),
|
|
|
|
last_poster: BasicUserSerializer.new(topic.last_poster, scope: scope, root: false)
|
2013-02-06 03:16:51 +08:00
|
|
|
}
|
|
|
|
|
2017-04-27 11:53:53 +08:00
|
|
|
if private_message?(topic)
|
2015-12-08 18:55:34 +08:00
|
|
|
allowed_user_ids = Set.new
|
|
|
|
|
|
|
|
result[:allowed_groups] = object.topic.allowed_groups.map do |group|
|
|
|
|
allowed_user_ids.merge(GroupUser.where(group: group).pluck(:user_id))
|
|
|
|
BasicGroupSerializer.new(group, scope: scope, root: false)
|
2013-06-21 05:20:08 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-12-08 18:55:34 +08:00
|
|
|
result[:allowed_users] = object.topic.allowed_users.select do |user|
|
|
|
|
!allowed_user_ids.include?(user.id)
|
2017-09-08 13:07:22 +08:00
|
|
|
end.map! do |user|
|
2015-12-08 18:55:34 +08:00
|
|
|
BasicUserSerializer.new(user, scope: scope, root: false)
|
2013-06-21 05:20:08 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
if object.post_counts_by_user.present?
|
2018-01-03 20:05:36 +08:00
|
|
|
participants = object.post_counts_by_user.reject { |p| object.participants[p].blank? }.map do |pc|
|
2017-07-28 09:20:09 +08:00
|
|
|
TopicPostCountSerializer.new({ user: object.participants[pc[0]], post_count: pc[1] }, scope: scope, root: false)
|
2013-06-21 05:20:08 +08:00
|
|
|
end
|
2018-01-03 19:35:01 +08:00
|
|
|
result[:participants] = participants if participants.length > 0
|
2013-06-21 05:20:08 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
if object.links.present?
|
|
|
|
result[:links] = object.links.map do |user|
|
|
|
|
TopicLinkSerializer.new(user, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
if has_topic_user?
|
|
|
|
result[:notification_level] = object.topic_user.notification_level
|
|
|
|
result[:notifications_reason_id] = object.topic_user.notifications_reason_id
|
2014-05-06 16:24:12 +08:00
|
|
|
else
|
|
|
|
result[:notification_level] = TopicUser.notification_levels[:regular]
|
2013-06-21 05:20:08 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
result[:can_move_posts] = true if scope.can_move_posts?(object.topic)
|
|
|
|
result[:can_edit] = true if scope.can_edit?(object.topic)
|
|
|
|
result[:can_delete] = true if scope.can_delete?(object.topic)
|
2013-07-13 00:08:23 +08:00
|
|
|
result[:can_recover] = true if scope.can_recover_topic?(object.topic)
|
2013-06-21 05:20:08 +08:00
|
|
|
result[:can_remove_allowed_users] = true if scope.can_remove_allowed_users?(object.topic)
|
2017-10-10 16:26:56 +08:00
|
|
|
result[:can_remove_self_id] = scope.user.id if scope.can_remove_allowed_users?(object.topic, scope.user)
|
2013-06-21 05:20:08 +08:00
|
|
|
result[:can_invite_to] = true if scope.can_invite_to?(object.topic)
|
2017-02-03 01:38:25 +08:00
|
|
|
result[:can_invite_via_email] = true if scope.can_invite_via_email?(object.topic)
|
2013-06-21 05:20:08 +08:00
|
|
|
result[:can_create_post] = true if scope.can_create?(Post, object.topic)
|
|
|
|
result[:can_reply_as_new_topic] = true if scope.can_reply_as_new_topic?(object.topic)
|
2014-02-06 06:54:16 +08:00
|
|
|
result[:can_flag_topic] = actions_summary.any? { |a| a[:can_act] }
|
2017-10-02 16:04:58 +08:00
|
|
|
result[:can_convert_topic] = true if scope.can_convert_topic?(object.topic)
|
2013-06-21 05:20:08 +08:00
|
|
|
result
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-05-17 03:04:09 +08:00
|
|
|
def message_bus_last_id
|
|
|
|
object.message_bus_last_id
|
|
|
|
end
|
|
|
|
|
2014-12-13 00:47:20 +08:00
|
|
|
def chunk_size
|
|
|
|
object.chunk_size
|
|
|
|
end
|
2014-09-08 23:11:56 +08:00
|
|
|
|
|
|
|
def is_warning
|
2017-04-27 11:53:53 +08:00
|
|
|
private_message?(object.topic) && object.topic.subtype == TopicSubtype.moderator_warning
|
2014-09-08 23:11:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_is_warning?
|
|
|
|
is_warning
|
|
|
|
end
|
2015-05-14 15:21:02 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
def draft
|
|
|
|
object.draft
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
def draft_key
|
|
|
|
object.draft_key
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-05-16 14:55:07 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
def draft_sequence
|
|
|
|
object.draft_sequence
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-12-30 10:26:21 +08:00
|
|
|
def include_message_archived?
|
2017-04-27 11:53:53 +08:00
|
|
|
private_message?(object.topic)
|
2015-12-30 10:26:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_archived
|
|
|
|
object.topic.message_archived?(scope.user)
|
|
|
|
end
|
|
|
|
|
2013-07-13 04:38:21 +08:00
|
|
|
def deleted_by
|
|
|
|
BasicUserSerializer.new(object.topic.deleted_by, root: false).as_json
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Topic user stuff
|
|
|
|
def has_topic_user?
|
|
|
|
object.topic_user.present?
|
|
|
|
end
|
|
|
|
|
2018-03-24 09:44:39 +08:00
|
|
|
def current_post_number
|
2018-11-21 08:58:47 +08:00
|
|
|
object.current_post_number
|
2018-03-24 09:44:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_current_post_number?
|
2018-11-21 08:58:47 +08:00
|
|
|
object.current_post_number.present?
|
2018-03-24 09:44:39 +08:00
|
|
|
end
|
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
def highest_post_number
|
|
|
|
object.highest_post_number
|
|
|
|
end
|
|
|
|
|
2016-05-25 04:21:23 +08:00
|
|
|
def last_read_post_id
|
2018-06-27 11:11:22 +08:00
|
|
|
return nil unless last_read_post_number
|
2018-06-27 12:33:57 +08:00
|
|
|
object.filtered_post_id(last_read_post_number)
|
2016-05-25 04:21:23 +08:00
|
|
|
end
|
|
|
|
alias_method :include_last_read_post_id?, :has_topic_user?
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def last_read_post_number
|
2016-05-25 04:21:23 +08:00
|
|
|
@last_read_post_number ||= object.topic_user.last_read_post_number
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
alias_method :include_last_read_post_number?, :has_topic_user?
|
|
|
|
|
|
|
|
def posted
|
|
|
|
object.topic_user.posted?
|
|
|
|
end
|
|
|
|
alias_method :include_posted?, :has_topic_user?
|
|
|
|
|
2013-03-07 04:17:07 +08:00
|
|
|
def pinned
|
2014-04-10 08:56:56 +08:00
|
|
|
PinnedCheck.pinned?(object.topic, object.topic_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpinned
|
|
|
|
PinnedCheck.unpinned?(object.topic, object.topic_user)
|
2013-03-07 04:17:07 +08:00
|
|
|
end
|
|
|
|
|
2014-02-06 06:54:16 +08:00
|
|
|
def actions_summary
|
|
|
|
result = []
|
2017-09-08 13:07:22 +08:00
|
|
|
return [] unless post = object.posts&.first
|
2014-02-06 06:54:16 +08:00
|
|
|
PostActionType.topic_flag_types.each do |sym, id|
|
|
|
|
result << { id: id,
|
|
|
|
count: 0,
|
|
|
|
hidden: false,
|
2017-07-28 09:20:09 +08:00
|
|
|
can_act: scope.post_can_act?(post, sym) }
|
2014-07-29 01:17:37 +08:00
|
|
|
# TODO: other keys? :can_defer_flags, :acted, :can_undo
|
2014-02-06 06:54:16 +08:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
def has_deleted
|
|
|
|
object.has_deleted?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_has_deleted?
|
|
|
|
object.guardian.can_see_deleted_posts?
|
|
|
|
end
|
|
|
|
|
2014-04-02 03:29:15 +08:00
|
|
|
def expandable_first_post
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_expandable_first_post?
|
|
|
|
object.topic.expandable_first_post?
|
|
|
|
end
|
|
|
|
|
2015-01-12 19:10:15 +08:00
|
|
|
def bookmarked
|
2017-09-13 23:14:03 +08:00
|
|
|
object.topic_user&.bookmarked
|
2015-01-12 19:10:15 +08:00
|
|
|
end
|
|
|
|
|
2015-09-28 14:39:34 +08:00
|
|
|
def include_pending_posts_count?
|
|
|
|
scope.is_staff? && NewPostManager.queue_enabled?
|
2015-04-28 03:55:10 +08:00
|
|
|
end
|
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
def topic_timer
|
2017-08-12 10:18:04 +08:00
|
|
|
TopicTimerSerializer.new(object.topic.public_topic_timer, root: false)
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:48:42 +08:00
|
|
|
def include_private_topic_timer?
|
|
|
|
scope.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_topic_timer
|
|
|
|
timer = object.topic.private_topic_timer(scope.user)
|
|
|
|
TopicTimerSerializer.new(timer, root: false)
|
|
|
|
end
|
|
|
|
|
2016-12-05 20:31:43 +08:00
|
|
|
def include_featured_link?
|
|
|
|
SiteSetting.topic_featured_link_enabled
|
|
|
|
end
|
|
|
|
|
2017-11-29 21:52:41 +08:00
|
|
|
def include_featured_link_root_domain?
|
|
|
|
SiteSetting.topic_featured_link_enabled && object.topic.featured_link
|
|
|
|
end
|
|
|
|
|
2017-04-12 03:11:23 +08:00
|
|
|
def include_unicode_title?
|
2017-12-21 12:20:30 +08:00
|
|
|
object.topic.title.match?(/:[\w\-+]+:/)
|
2017-04-12 03:11:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unicode_title
|
2017-07-22 02:24:28 +08:00
|
|
|
Emoji.gsub_emoji_to_unicode(object.topic.title)
|
2017-04-12 03:11:23 +08:00
|
|
|
end
|
|
|
|
|
2017-04-27 11:53:53 +08:00
|
|
|
def include_pm_with_non_human_user?
|
|
|
|
private_message?(object.topic)
|
|
|
|
end
|
|
|
|
|
2018-05-21 10:28:12 +08:00
|
|
|
def pm_with_non_human_user
|
|
|
|
object.topic.pm_with_non_human_user?
|
|
|
|
end
|
|
|
|
|
2017-09-13 23:14:03 +08:00
|
|
|
def participant_count
|
2017-12-13 14:19:42 +08:00
|
|
|
object.participant_count
|
2017-09-13 23:14:03 +08:00
|
|
|
end
|
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
def destination_category_id
|
|
|
|
object.topic.shared_draft.category_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_destination_category_id?
|
|
|
|
scope.can_create_shared_draft? &&
|
|
|
|
object.topic.category_id == SiteSetting.shared_drafts_category.to_i &&
|
|
|
|
object.topic.shared_draft.present?
|
|
|
|
end
|
|
|
|
|
2017-04-27 11:53:53 +08:00
|
|
|
private
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def private_message?(topic)
|
|
|
|
@private_message ||= topic.private_message?
|
|
|
|
end
|
2017-04-27 11:53:53 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|