discourse/app/serializers/topic_list_item_serializer.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class TopicListItemSerializer < ListableTopicSerializer
2018-02-14 04:46:25 +08:00
include TopicTagsMixin
2013-02-06 03:16:51 +08:00
2013-02-26 00:42:20 +08:00
attributes :views,
:like_count,
2013-11-19 01:48:26 +08:00
:has_summary,
2013-04-03 04:52:51 +08:00
:archetype,
2013-10-24 02:40:39 +08:00
:last_poster_username,
:category_id,
:op_like_count,
:pinned_globally,
:bookmarked_post_numbers,
:liked_post_numbers,
:featured_link,
:featured_link_root_domain,
:allowed_user_count
2013-02-06 03:16:51 +08:00
has_many :posters, serializer: TopicPosterSerializer, embed: :objects
has_many :participants, serializer: TopicPosterSerializer, embed: :objects
2013-02-06 03:16:51 +08:00
def posters
object.posters || object.posters_summary || []
2013-02-06 03:16:51 +08:00
end
def op_like_count
object.first_post && object.first_post.like_count
end
def last_poster_username
posters.find { |poster| poster.user.id == object.last_post_user_id }.try(:user).try(:username)
end
def category_id
# If it's a shared draft, show the destination topic instead
if object.includes_destination_category && object.shared_draft
return object.shared_draft.category_id
end
object.category_id
end
def participants
object.participants_summary || []
end
def include_bookmarked_post_numbers?
include_post_action? :bookmark
end
def include_liked_post_numbers?
include_post_action? :like
end
def include_post_action?(action)
object.user_data &&
object.user_data.post_action_data &&
object.user_data.post_action_data.key?(PostActionType.types[action])
end
def liked_post_numbers
object.user_data.post_action_data[PostActionType.types[:like]]
end
def bookmarked_post_numbers
object.user_data.post_action_data[PostActionType.types[:bookmark]]
end
def include_participants?
object.private_message?
end
def include_op_like_count?
2015-01-05 14:54:38 +08:00
# PERF: long term we probably want a cheaper way of looking stuff up
# this is rather odd code, but we need to have op_likes loaded somehow
# simplest optimisation is adding a cache column on topic.
object.association(:first_post).loaded?
end
def include_featured_link?
SiteSetting.topic_featured_link_enabled
end
def include_featured_link_root_domain?
SiteSetting.topic_featured_link_enabled && object.featured_link.present?
end
def allowed_user_count
# Don't use count as it will result in a query
object.allowed_users.length
end
def include_allowed_user_count?
object.private_message?
end
2013-02-06 03:16:51 +08:00
end