discourse/app/serializers/topic_view_serializer.rb

209 lines
4.5 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
class TopicViewSerializer < ApplicationSerializer
# These attributes will be delegated to the topic
2013-02-07 10:45:24 -05:00
def self.topic_attributes
2013-02-05 14:16:51 -05:00
[:id,
2013-02-07 10:45:24 -05:00
:title,
:posts_count,
:highest_post_number,
:created_at,
:views,
:reply_count,
:last_posted_at,
2013-02-05 14:16:51 -05:00
:visible,
:closed,
:pinned,
:archived,
:moderator_posts_count,
:has_best_of,
:archetype,
:slug]
end
def self.guardian_attributes
[:can_moderate, :can_edit, :can_delete, :can_invite_to, :can_move_posts]
end
2013-02-07 10:45:24 -05:00
attributes *topic_attributes
2013-02-05 14:16:51 -05:00
attributes *guardian_attributes
2013-02-07 10:45:24 -05:00
attributes :draft,
:draft_key,
2013-02-05 14:16:51 -05:00
:draft_sequence,
:post_action_visibility,
:voted_in_topic,
:can_create_post,
:can_reply_as_new_topic,
:categoryName,
:starred,
:last_read_post_number,
:posted,
:notification_level,
:notifications_reason_id,
:posts,
:at_bottom
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
has_one :created_by, serializer: BasicUserSerializer, embed: :objects
has_one :last_poster, serializer: BasicUserSerializer, embed: :objects
has_many :allowed_users, serializer: BasicUserSerializer, embed: :objects
has_many :links, serializer: TopicLinkSerializer, embed: :objects
has_many :participants, serializer: TopicPostCountSerializer, embed: :objects
has_many :suggested_topics, serializer: SuggestedTopicSerializer, embed: :objects
# Define a delegator for each attribute of the topic we want
topic_attributes.each do |ta|
class_eval %{def #{ta}
object.topic.#{ta}
end}
end
# Define the guardian attributes
guardian_attributes.each do |ga|
class_eval %{
def #{ga}
true
end
def include_#{ga}?
scope.#{ga}?(object.topic)
end
}
end
def draft
object.draft
end
def include_allowed_users?
object.topic.private_message?
end
def draft_key
object.draft_key
end
def draft_sequence
2013-02-07 10:45:24 -05:00
object.draft_sequence
2013-02-05 14:16:51 -05:00
end
def post_action_visibility
2013-02-07 10:45:24 -05:00
object.post_action_visibility
2013-02-05 14:16:51 -05:00
end
def include_post_action_visibility?
object.post_action_visibility.present?
end
def voted_in_topic
object.voted_in_topic?
end
def can_reply_as_new_topic
scope.can_reply_as_new_topic?(object.topic)
end
def include_can_reply_as_new_topic?
scope.can_create?(Post, object.topic)
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05:00
def can_create_post
2013-02-07 10:45:24 -05:00
true
2013-02-05 14:16:51 -05:00
end
def include_can_create_post?
scope.can_create?(Post, object.topic)
end
def categoryName
object.topic.category.name
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05:00
def include_categoryName?
object.topic.category.present?
end
# Topic user stuff
def has_topic_user?
object.topic_user.present?
end
def starred
object.topic_user.starred?
end
alias_method :include_starred?, :has_topic_user?
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?
def notification_level
object.topic_user.notification_level
end
alias_method :include_notification_level?, :has_topic_user?
def notifications_reason_id
2013-02-07 10:45:24 -05:00
object.topic_user.notifications_reason_id
2013-02-05 14:16:51 -05:00
end
alias_method :include_notifications_reason_id?, :has_topic_user?
def created_by
object.topic.user
end
def last_poster
object.topic.last_poster
end
def allowed_users
object.topic.allowed_users
end
def include_links?
object.links.present?
end
2013-02-07 10:45:24 -05:00
def participants
2013-02-05 14:16:51 -05:00
object.posts_count.collect {|tuple| {user: object.participants[tuple.first], post_count: tuple[1]}}
end
def include_participants?
object.initial_load? and object.posts_count.present?
end
def suggested_topics
object.suggested_topics.topics
end
def include_suggested_topics?
at_bottom and object.suggested_topics.present?
end
# Whether we're at the bottom of a topic (last page)
2013-02-07 10:45:24 -05:00
def at_bottom
2013-02-05 14:16:51 -05:00
posts.present? and (@highest_number_in_posts == object.topic.highest_post_number)
end
def posts
return @posts if @posts.present?
@posts = []
@highest_number_in_posts = 0
if object.posts.present?
object.posts.each do |p|
@highest_number_in_posts = p.post_number if p.post_number > @highest_number_in_posts
ps = PostSerializer.new(p, scope: scope, root: false)
ps.topic_slug = object.topic.slug
ps.topic_view = object
p.topic = object.topic
@posts << ps.as_json
end
end
@posts
end
end