discourse/app/serializers/topic_view_serializer.rb

300 lines
6.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-05 13:16:51 -06:00
class TopicViewSerializer < ApplicationSerializer
include PostStreamSerializerMixin
include SuggestedTopicsMixin
include TopicTagsMixin
2017-04-11 14:11:23 -05:00
include ApplicationHelper
2013-02-05 13:16:51 -06: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 13:16:51 -06:00
end
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,
:pinned_until,
:image_url,
:slow_mode_seconds
)
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,
:bookmarks,
:message_archived,
:topic_timer,
:unicode_title,
:message_bus_last_id,
:participant_count,
:destination_category_id,
:pm_with_non_human_user,
:queued_posts_count,
:show_read_indicator,
:requested_group_name,
:thumbnails,
:user_last_posted_at,
:is_shared_draft,
:slow_mode_enabled_until
)
2013-02-05 13:16:51 -06:00
has_one :details, serializer: TopicViewDetailsSerializer, root: false, embed: :objects
has_many :pending_posts, serializer: TopicPendingPostSerializer, root: false, embed: :objects
2013-02-05 13:16:51 -06:00
has_one :published_page, embed: :objects
def details
object
2013-02-05 13:16:51 -06:00
end
def message_bus_last_id
object.message_bus_last_id
end
def chunk_size
object.chunk_size
end
def is_warning
object.personal_message && object.topic.subtype == TopicSubtype.moderator_warning
end
def include_is_warning?
is_warning
end
def draft
object.draft
2013-02-05 13:16:51 -06:00
end
def draft_key
object.draft_key
2013-02-07 09:45:24 -06:00
end
def draft_sequence
object.draft_sequence
2013-02-05 13:16:51 -06:00
end
def include_message_archived?
object.personal_message
end
def message_archived
object.topic.message_archived?(scope.user)
end
2013-07-12 15:38:21 -05:00
def deleted_by
BasicUserSerializer.new(object.topic.deleted_by, root: false).as_json
end
2013-02-05 13:16:51 -06:00
# Topic user stuff
def has_topic_user?
object.topic_user.present?
end
def current_post_number
object.current_post_number
end
def include_current_post_number?
object.current_post_number.present?
end
def highest_post_number
object.highest_post_number
end
def last_read_post_id
return nil unless last_read_post_number
object.filtered_post_id(last_read_post_number)
end
alias_method :include_last_read_post_id?, :has_topic_user?
2013-02-05 13:16:51 -06:00
def last_read_post_number
@last_read_post_number ||= object.topic_user.last_read_post_number
2013-02-05 13:16:51 -06: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?
def pinned
PinnedCheck.pinned?(object.topic, object.topic_user)
end
def unpinned
PinnedCheck.unpinned?(object.topic, object.topic_user)
end
2014-02-05 16:54:16 -06:00
def actions_summary
object.actions_summary
2014-02-05 16:54:16 -06:00
end
2013-02-05 13:16:51 -06:00
def has_deleted
object.has_deleted?
end
def include_has_deleted?
object.guardian.can_see_deleted_posts?
end
def expandable_first_post
true
end
def include_expandable_first_post?
object.topic.expandable_first_post?
end
2015-01-12 05:10:15 -06:00
def bookmarked
object.has_bookmarks?
2015-01-12 05:10:15 -06:00
end
def bookmarks
object.bookmarks
end
def topic_timer
TopicTimerSerializer.new(object.topic.public_topic_timer, root: false)
end
def include_featured_link?
SiteSetting.topic_featured_link_enabled
end
def include_featured_link_root_domain?
SiteSetting.topic_featured_link_enabled && object.topic.featured_link
end
2017-04-11 14:11:23 -05:00
def include_unicode_title?
object.topic.title.match?(/:[\w\-+]+:/)
2017-04-11 14:11:23 -05:00
end
def unicode_title
2017-07-21 13:24:28 -05:00
Emoji.gsub_emoji_to_unicode(object.topic.title)
2017-04-11 14:11:23 -05:00
end
def include_pm_with_non_human_user?
object.personal_message
end
2018-05-20 21:28:12 -05:00
def pm_with_non_human_user
object.topic.pm_with_non_human_user?
end
def participant_count
object.participant_count
end
def destination_category_id
object.topic.shared_draft.category_id
end
def include_destination_category_id?
scope.can_see_shared_draft? && SiteSetting.shared_drafts_enabled? && object.topic.shared_draft.present?
end
def is_shared_draft
include_destination_category_id?
end
alias_method :include_is_shared_draft?, :include_destination_category_id?
def include_pending_posts?
scope.authenticated? && object.queued_posts_enabled
end
def queued_posts_count
object.queued_posts_count
end
def include_queued_posts_count?
scope.is_staff? && object.queued_posts_enabled
2018-06-07 00:28:18 -05:00
end
def show_read_indicator
object.show_read_indicator?
end
def requested_group_name
Group
.joins(:group_users)
.where(
id: object.topic.custom_fields['requested_group_id'].to_i,
group_users: { user_id: scope.user.id, owner: true }
)
.pluck_first(:name)
end
def include_requested_group_name?
object.personal_message && object.topic.custom_fields['requested_group_id']
end
def include_published_page?
SiteSetting.enable_page_publishing? &&
scope.is_staff? &&
object.published_page.present? &&
!SiteSetting.secure_media
end
FEATURE: Include optimized thumbnails for topics (#9215) This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this: - Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact - A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake - Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued - Topic lists and topics now include a `thumbnails` key, which includes all the available images: ``` "thumbnails": [ { "max_width": null, "max_height": null, "url": "//example.com/original-image.png", "width": 1380, "height": 1840 }, { "max_width": 1024, "max_height": 1024, "url": "//example.com/optimized-image.png", "width": 768, "height": 1024 } ] ``` - Themes can request additional thumbnail sizes by using a modifier in their `about.json` file: ``` "modifiers": { "topic_thumbnail_sizes": [ [200, 200], [800, 800] ], ... ``` Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated - Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 03:07:50 -05:00
def thumbnails
extra_sizes = ThemeModifierHelper.new(request: scope.request).topic_thumbnail_sizes
object.topic.thumbnail_info(enqueue_if_missing: true, extra_sizes: extra_sizes)
end
def user_last_posted_at
object.topic_user.last_posted_at
end
def include_user_last_posted_at?
has_topic_user? && object.topic.slow_mode_seconds.to_i > 0
end
def slow_mode_enabled_until
object.topic.slow_mode_topic_timer&.execute_at
end
2013-02-05 13:16:51 -06:00
end