2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class TopicViewSerializer < ApplicationSerializer
|
2013-06-20 17:20:08 -04:00
|
|
|
include PostStreamSerializerMixin
|
2017-09-26 02:42:27 -04:00
|
|
|
include SuggestedTopicsMixin
|
2018-02-22 09:57:02 -05:00
|
|
|
include TopicTagsMixin
|
2017-04-11 15:11:23 -04:00
|
|
|
include ApplicationHelper
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-01-28 11:01:01 -05: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-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05: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,
|
|
|
|
:deleted_at,
|
|
|
|
:user_id,
|
|
|
|
:featured_link,
|
|
|
|
:featured_link_root_domain,
|
|
|
|
:pinned_globally,
|
|
|
|
:pinned_at,
|
2019-10-22 00:02:49 -04:00
|
|
|
:pinned_until,
|
|
|
|
:image_url
|
2019-01-03 12:03:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
attributes(
|
|
|
|
:draft,
|
|
|
|
:draft_key,
|
|
|
|
:draft_sequence,
|
|
|
|
:posted,
|
|
|
|
:unpinned,
|
|
|
|
:pinned,
|
|
|
|
:current_post_number,
|
|
|
|
:highest_post_number,
|
|
|
|
:last_read_post_number,
|
|
|
|
:last_read_post_id,
|
|
|
|
:deleted_by,
|
|
|
|
:has_deleted,
|
|
|
|
:actions_summary,
|
|
|
|
:expandable_first_post,
|
|
|
|
:is_warning,
|
|
|
|
:chunk_size,
|
|
|
|
:bookmarked,
|
2020-04-15 19:20:44 -04:00
|
|
|
:bookmark_reminder_at,
|
2019-01-03 12:03:01 -05:00
|
|
|
:message_archived,
|
|
|
|
:topic_timer,
|
|
|
|
:private_topic_timer,
|
|
|
|
:unicode_title,
|
|
|
|
:message_bus_last_id,
|
|
|
|
:participant_count,
|
|
|
|
:destination_category_id,
|
|
|
|
:pm_with_non_human_user,
|
2019-08-27 08:09:00 -04:00
|
|
|
:queued_posts_count,
|
2020-03-24 05:12:52 -04:00
|
|
|
:show_read_indicator,
|
2020-04-08 12:52:36 -04:00
|
|
|
:requested_group_name,
|
2019-01-03 12:03:01 -05:00
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
has_one :details, serializer: TopicViewDetailsSerializer, root: false, embed: :objects
|
|
|
|
has_many :pending_posts, serializer: TopicPendingPostSerializer, root: false, embed: :objects
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2020-04-08 12:52:36 -04:00
|
|
|
has_one :published_page, embed: :objects
|
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
def details
|
|
|
|
object
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-05-16 15:04:09 -04:00
|
|
|
def message_bus_last_id
|
|
|
|
object.message_bus_last_id
|
|
|
|
end
|
|
|
|
|
2014-12-12 11:47:20 -05:00
|
|
|
def chunk_size
|
|
|
|
object.chunk_size
|
|
|
|
end
|
2014-09-08 11:11:56 -04:00
|
|
|
|
|
|
|
def is_warning
|
2019-04-12 09:55:27 -04:00
|
|
|
object.personal_message && object.topic.subtype == TopicSubtype.moderator_warning
|
2014-09-08 11:11:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_is_warning?
|
|
|
|
is_warning
|
|
|
|
end
|
2015-05-14 03:21:02 -04:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
def draft
|
|
|
|
object.draft
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
def draft_key
|
|
|
|
object.draft_key
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-05-16 02:55:07 -04:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
def draft_sequence
|
|
|
|
object.draft_sequence
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-12-29 21:26:21 -05:00
|
|
|
def include_message_archived?
|
2019-04-12 09:55:27 -04:00
|
|
|
object.personal_message
|
2015-12-29 21:26:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_archived
|
|
|
|
object.topic.message_archived?(scope.user)
|
|
|
|
end
|
|
|
|
|
2013-07-12 16:38:21 -04:00
|
|
|
def deleted_by
|
|
|
|
BasicUserSerializer.new(object.topic.deleted_by, root: false).as_json
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Topic user stuff
|
|
|
|
def has_topic_user?
|
|
|
|
object.topic_user.present?
|
|
|
|
end
|
|
|
|
|
2018-03-23 21:44:39 -04:00
|
|
|
def current_post_number
|
2018-11-20 19:58:47 -05:00
|
|
|
object.current_post_number
|
2018-03-23 21:44:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_current_post_number?
|
2018-11-20 19:58:47 -05:00
|
|
|
object.current_post_number.present?
|
2018-03-23 21:44:39 -04:00
|
|
|
end
|
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
def highest_post_number
|
|
|
|
object.highest_post_number
|
|
|
|
end
|
|
|
|
|
2016-05-24 16:21:23 -04:00
|
|
|
def last_read_post_id
|
2018-06-26 23:11:22 -04:00
|
|
|
return nil unless last_read_post_number
|
2018-06-27 00:33:57 -04:00
|
|
|
object.filtered_post_id(last_read_post_number)
|
2016-05-24 16:21:23 -04:00
|
|
|
end
|
|
|
|
alias_method :include_last_read_post_id?, :has_topic_user?
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def last_read_post_number
|
2016-05-24 16:21:23 -04:00
|
|
|
@last_read_post_number ||= object.topic_user.last_read_post_number
|
2013-02-05 14:16:51 -05: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-06 15:17:07 -05:00
|
|
|
def pinned
|
2014-04-09 20:56:56 -04:00
|
|
|
PinnedCheck.pinned?(object.topic, object.topic_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpinned
|
|
|
|
PinnedCheck.unpinned?(object.topic, object.topic_user)
|
2013-03-06 15:17:07 -05:00
|
|
|
end
|
|
|
|
|
2014-02-05 17:54:16 -05:00
|
|
|
def actions_summary
|
2019-04-12 09:55:27 -04:00
|
|
|
object.actions_summary
|
2014-02-05 17:54:16 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-15 17:02:43 -04:00
|
|
|
def has_deleted
|
|
|
|
object.has_deleted?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_has_deleted?
|
|
|
|
object.guardian.can_see_deleted_posts?
|
|
|
|
end
|
|
|
|
|
2014-04-01 15:29:15 -04:00
|
|
|
def expandable_first_post
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_expandable_first_post?
|
|
|
|
object.topic.expandable_first_post?
|
|
|
|
end
|
|
|
|
|
2015-01-12 06:10:15 -05:00
|
|
|
def bookmarked
|
2020-03-12 01:20:56 -04:00
|
|
|
if SiteSetting.enable_bookmarks_with_reminders?
|
|
|
|
object.has_bookmarks?
|
|
|
|
else
|
|
|
|
object.topic_user&.bookmarked
|
|
|
|
end
|
2015-01-12 06:10:15 -05:00
|
|
|
end
|
|
|
|
|
2020-04-15 19:20:44 -04:00
|
|
|
def include_bookmark_reminder_at?
|
|
|
|
SiteSetting.enable_bookmarks_with_reminders? && bookmarked
|
|
|
|
end
|
|
|
|
|
|
|
|
def bookmark_reminder_at
|
|
|
|
object.first_post_bookmark_reminder_at
|
|
|
|
end
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
def topic_timer
|
2017-08-11 22:18:04 -04:00
|
|
|
TopicTimerSerializer.new(object.topic.public_topic_timer, root: false)
|
2017-03-21 23:12:02 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:48:42 -04: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 07:31:43 -05:00
|
|
|
def include_featured_link?
|
|
|
|
SiteSetting.topic_featured_link_enabled
|
|
|
|
end
|
|
|
|
|
2017-11-29 08:52:41 -05:00
|
|
|
def include_featured_link_root_domain?
|
|
|
|
SiteSetting.topic_featured_link_enabled && object.topic.featured_link
|
|
|
|
end
|
|
|
|
|
2017-04-11 15:11:23 -04:00
|
|
|
def include_unicode_title?
|
2017-12-20 23:20:30 -05:00
|
|
|
object.topic.title.match?(/:[\w\-+]+:/)
|
2017-04-11 15:11:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def unicode_title
|
2017-07-21 14:24:28 -04:00
|
|
|
Emoji.gsub_emoji_to_unicode(object.topic.title)
|
2017-04-11 15:11:23 -04:00
|
|
|
end
|
|
|
|
|
2017-04-26 23:53:53 -04:00
|
|
|
def include_pm_with_non_human_user?
|
2019-04-12 09:55:27 -04:00
|
|
|
object.personal_message
|
2017-04-26 23:53:53 -04:00
|
|
|
end
|
|
|
|
|
2018-05-20 22:28:12 -04:00
|
|
|
def pm_with_non_human_user
|
|
|
|
object.topic.pm_with_non_human_user?
|
|
|
|
end
|
|
|
|
|
2017-09-13 11:14:03 -04:00
|
|
|
def participant_count
|
2017-12-13 01:19:42 -05:00
|
|
|
object.participant_count
|
2017-09-13 11:14:03 -04:00
|
|
|
end
|
|
|
|
|
2018-03-13 15:59:12 -04: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
|
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
def include_pending_posts?
|
|
|
|
scope.authenticated? && object.queued_posts_enabled
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
def queued_posts_count
|
|
|
|
object.queued_posts_count
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
2019-04-12 09:55:27 -04:00
|
|
|
def include_queued_posts_count?
|
|
|
|
scope.is_staff? && object.queued_posts_enabled
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2019-08-27 08:09:00 -04:00
|
|
|
|
|
|
|
def show_read_indicator
|
|
|
|
object.show_read_indicator?
|
|
|
|
end
|
2020-03-24 05:12:52 -04:00
|
|
|
|
|
|
|
def requested_group_name
|
|
|
|
if scope&.user
|
|
|
|
group = Group
|
|
|
|
.joins('JOIN group_users ON groups.id = group_users.group_id')
|
|
|
|
.find_by(
|
|
|
|
id: object.topic.custom_fields['requested_group_id'].to_i,
|
|
|
|
group_users: { user_id: scope.user.id, owner: true }
|
|
|
|
)
|
|
|
|
|
|
|
|
group.name if group
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_requested_group_name?
|
|
|
|
object.personal_message
|
|
|
|
end
|
2020-04-08 12:52:36 -04:00
|
|
|
|
|
|
|
def include_published_page?
|
|
|
|
SiteSetting.enable_page_publishing? && scope.is_staff? && object.published_page.present?
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|