2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-15 02:45:29 +08:00
|
|
|
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,
|
2015-01-05 14:39:49 +08:00
|
|
|
:category_id,
|
2015-01-06 04:43:05 +08:00
|
|
|
:op_like_count,
|
2015-01-07 15:20:10 +08:00
|
|
|
:pinned_globally,
|
2015-01-07 15:50:28 +08:00
|
|
|
:bookmarked_post_numbers,
|
2016-04-26 03:55:15 +08:00
|
|
|
:liked_post_numbers,
|
2017-11-29 21:52:41 +08:00
|
|
|
:featured_link,
|
2019-11-15 03:14:23 +08:00
|
|
|
:featured_link_root_domain,
|
|
|
|
:allowed_user_count
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
has_many :posters, serializer: TopicPosterSerializer, embed: :objects
|
2014-05-12 15:32:49 +08:00
|
|
|
has_many :participants, serializer: TopicPosterSerializer, embed: :objects
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def posters
|
2020-04-23 00:24:54 +08:00
|
|
|
object.posters || object.posters_summary || []
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-01-05 14:39:49 +08:00
|
|
|
def op_like_count
|
|
|
|
object.first_post && object.first_post.like_count
|
|
|
|
end
|
|
|
|
|
2013-09-20 04:25:25 +08:00
|
|
|
def last_poster_username
|
2014-09-03 10:13:13 +08:00
|
|
|
posters.find { |poster| poster.user.id == object.last_post_user_id }.try(:user).try(:username)
|
2013-09-20 04:25:25 +08:00
|
|
|
end
|
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
def category_id
|
2018-03-29 03:36:12 +08:00
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
# If it's a shared draft, show the destination topic instead
|
2018-03-29 03:36:12 +08:00
|
|
|
if object.includes_destination_category && object.shared_draft
|
2018-03-14 03:59:12 +08:00
|
|
|
return object.shared_draft.category_id
|
|
|
|
end
|
|
|
|
|
|
|
|
object.category_id
|
|
|
|
end
|
|
|
|
|
2014-05-12 15:32:49 +08:00
|
|
|
def participants
|
|
|
|
object.participants_summary || []
|
|
|
|
end
|
|
|
|
|
2015-01-07 15:20:10 +08:00
|
|
|
def include_bookmarked_post_numbers?
|
2015-01-07 15:50:28 +08:00
|
|
|
include_post_action? :bookmark
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_liked_post_numbers?
|
|
|
|
include_post_action? :like
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_post_action?(action)
|
2015-01-07 15:20:10 +08:00
|
|
|
object.user_data &&
|
|
|
|
object.user_data.post_action_data &&
|
2015-01-07 15:50:28 +08:00
|
|
|
object.user_data.post_action_data.key?(PostActionType.types[action])
|
|
|
|
end
|
|
|
|
|
|
|
|
def liked_post_numbers
|
|
|
|
object.user_data.post_action_data[PostActionType.types[:like]]
|
2015-01-07 15:20:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def bookmarked_post_numbers
|
|
|
|
object.user_data.post_action_data[PostActionType.types[:bookmark]]
|
|
|
|
end
|
|
|
|
|
2014-05-12 15:32:49 +08:00
|
|
|
def include_participants?
|
|
|
|
object.private_message?
|
|
|
|
end
|
|
|
|
|
2015-01-05 14:39:49 +08:00
|
|
|
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.
|
2015-01-05 14:39:49 +08:00
|
|
|
object.association(:first_post).loaded?
|
|
|
|
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.featured_link.present?
|
2016-12-05 20:31:43 +08:00
|
|
|
end
|
|
|
|
|
2019-11-15 03:14:23 +08:00
|
|
|
def allowed_user_count
|
2020-09-11 15:18:23 +08:00
|
|
|
# Don't use count as it will result in a query
|
|
|
|
object.allowed_users.length
|
2019-11-15 03:14:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_allowed_user_count?
|
|
|
|
object.private_message?
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|