discourse/app/controllers/excerpt_controller.rb

41 lines
1.3 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require_dependency 'post_excerpt_serializer'
class ExcerptController < ApplicationController
def show
requires_parameter(:url)
uri = URI.parse(params[:url])
route = Rails.application.routes.recognize_path(uri.path)
case route[:controller]
when 'topics'
2013-02-07 23:45:24 +08:00
# If we have a post number, retrieve the last post. Otherwise, first post.
2013-02-06 03:16:51 +08:00
topic_posts = Post.where(topic_id: route[:topic_id].to_i).order(:post_number)
post = route.has_key?(:post_number) ? topic_posts.last : topic_posts.first
guardian.ensure_can_see!(post)
render json: post, serializer: PostExcerptSerializer, root: false
2013-02-06 03:16:51 +08:00
when 'users'
user = User.where(username_lower: route[:username].downcase).first
guardian.ensure_can_see!(user)
render json: user, serializer: UserExcerptSerializer, root: false
2013-02-07 23:45:24 +08:00
when 'list'
2013-02-06 03:16:51 +08:00
if route[:action] == 'category'
category = Category.where(slug: route[:category]).first
guardian.ensure_can_see!(category)
render json: category, serializer: CategoryExcerptSerializer, root: false
2013-02-06 03:16:51 +08:00
end
else
2013-02-07 23:45:24 +08:00
render nothing: true, status: 404
2013-02-06 03:16:51 +08:00
end
rescue ActionController::RoutingError, Discourse::NotFound
render nothing: true, status: 404
end
end