2013-03-06 15:17:07 -05:00
|
|
|
require_dependency 'pinned_check'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class TopicViewSerializer < ApplicationSerializer
|
2013-06-20 17:20:08 -04:00
|
|
|
include PostStreamSerializerMixin
|
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
|
|
|
|
|
2015-01-28 11:01:01 -05:00
|
|
|
attributes_from_topic :id,
|
|
|
|
:title,
|
|
|
|
:fancy_title,
|
|
|
|
:posts_count,
|
|
|
|
:created_at,
|
|
|
|
:views,
|
|
|
|
:reply_count,
|
|
|
|
:participant_count,
|
|
|
|
:like_count,
|
|
|
|
:last_posted_at,
|
|
|
|
:visible,
|
|
|
|
:closed,
|
|
|
|
:archived,
|
|
|
|
:has_summary,
|
|
|
|
:archetype,
|
|
|
|
:slug,
|
|
|
|
:category_id,
|
|
|
|
:word_count,
|
|
|
|
:deleted_at
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
attributes :draft,
|
|
|
|
:draft_key,
|
2013-02-05 14:16:51 -05:00
|
|
|
:draft_sequence,
|
|
|
|
:posted,
|
2014-04-09 20:56:56 -04:00
|
|
|
:unpinned,
|
2014-04-10 01:53:31 -04:00
|
|
|
:pinned_globally,
|
2014-03-12 14:47:04 -04:00
|
|
|
:pinned, # Is topic pinned and viewer hasn't cleared the pin?
|
|
|
|
:pinned_at, # Ignores clear pin
|
2013-06-20 17:20:08 -04:00
|
|
|
:details,
|
|
|
|
:highest_post_number,
|
2013-07-12 16:38:21 -04:00
|
|
|
:last_read_post_number,
|
2014-02-05 17:54:16 -05:00
|
|
|
:deleted_by,
|
2014-07-15 17:02:43 -04:00
|
|
|
:has_deleted,
|
2014-04-01 15:29:15 -04:00
|
|
|
:actions_summary,
|
2014-09-08 11:11:56 -04:00
|
|
|
:expandable_first_post,
|
2014-12-12 11:47:20 -05:00
|
|
|
:is_warning,
|
2015-01-12 06:10:15 -05:00
|
|
|
:chunk_size,
|
|
|
|
:bookmarked
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
# TODO: Split off into proper object / serializer
|
|
|
|
def details
|
|
|
|
result = {
|
|
|
|
auto_close_at: object.topic.auto_close_at,
|
2014-10-10 12:21:44 -04:00
|
|
|
auto_close_hours: object.topic.auto_close_hours,
|
|
|
|
auto_close_based_on_last_post: object.topic.auto_close_based_on_last_post,
|
2013-06-20 17:20:08 -04:00
|
|
|
created_by: BasicUserSerializer.new(object.topic.user, scope: scope, root: false),
|
|
|
|
last_poster: BasicUserSerializer.new(object.topic.last_poster, scope: scope, root: false)
|
2013-02-05 14:16:51 -05:00
|
|
|
}
|
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
if object.topic.allowed_users.present?
|
|
|
|
result[:allowed_users] = object.topic.allowed_users.map do |user|
|
|
|
|
BasicUserSerializer.new(user, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
if object.topic.allowed_groups.present?
|
|
|
|
result[:allowed_groups] = object.topic.allowed_groups.map do |ag|
|
|
|
|
BasicGroupSerializer.new(ag, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
if object.post_counts_by_user.present?
|
|
|
|
result[:participants] = object.post_counts_by_user.map do |pc|
|
|
|
|
TopicPostCountSerializer.new({user: object.participants[pc[0]], post_count: pc[1]}, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 14:18:35 -04:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
if object.suggested_topics.try(:topics).present?
|
|
|
|
result[:suggested_topics] = object.suggested_topics.topics.map do |user|
|
|
|
|
SuggestedTopicSerializer.new(user, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
if object.links.present?
|
|
|
|
result[:links] = object.links.map do |user|
|
|
|
|
TopicLinkSerializer.new(user, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-20 17:20:08 -04: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 04:24:12 -04:00
|
|
|
else
|
|
|
|
result[:notification_level] = TopicUser.notification_levels[:regular]
|
2013-06-20 17:20:08 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-20 17:20:08 -04: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-12 12:08:23 -04:00
|
|
|
result[:can_recover] = true if scope.can_recover_topic?(object.topic)
|
2013-06-20 17:20:08 -04:00
|
|
|
result[:can_remove_allowed_users] = true if scope.can_remove_allowed_users?(object.topic)
|
|
|
|
result[:can_invite_to] = true if scope.can_invite_to?(object.topic)
|
|
|
|
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-05 17:54:16 -05:00
|
|
|
result[:can_flag_topic] = actions_summary.any? { |a| a[:can_act] }
|
2013-06-20 17:20:08 -04:00
|
|
|
result
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2014-09-08 13:23:40 -04:00
|
|
|
object.topic.private_message? && object.topic.subtype == TopicSubtype.moderator_warning
|
2014-09-08 11:11:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_is_warning?
|
|
|
|
is_warning
|
|
|
|
end
|
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
|
|
|
|
|
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
|
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
def highest_post_number
|
|
|
|
object.highest_post_number
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def last_read_post_number
|
|
|
|
object.topic_user.last_read_post_number
|
|
|
|
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?
|
|
|
|
|
2014-04-10 01:53:31 -04:00
|
|
|
def pinned_globally
|
|
|
|
object.topic.pinned_globally
|
|
|
|
end
|
|
|
|
|
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-03-12 14:47:04 -04:00
|
|
|
def pinned_at
|
|
|
|
object.topic.pinned_at
|
|
|
|
end
|
|
|
|
|
2014-02-05 17:54:16 -05:00
|
|
|
def actions_summary
|
|
|
|
result = []
|
|
|
|
return [] unless post = object.posts.try(:first)
|
|
|
|
PostActionType.topic_flag_types.each do |sym, id|
|
|
|
|
result << { id: id,
|
|
|
|
count: 0,
|
|
|
|
hidden: false,
|
|
|
|
can_act: scope.post_can_act?(post, sym)}
|
2014-07-28 13:17:37 -04:00
|
|
|
# TODO: other keys? :can_defer_flags, :acted, :can_undo
|
2014-02-05 17:54:16 -05:00
|
|
|
end
|
|
|
|
result
|
|
|
|
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
|
|
|
|
object.topic_user.try(:bookmarked)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|