2013-02-05 13:16:51 -06: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 09:45:24 -06:00
|
|
|
|
|
|
|
# If we have a post number, retrieve the last post. Otherwise, first post.
|
2013-02-05 13:16:51 -06: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)
|
|
|
|
|
2013-03-22 13:08:11 -05:00
|
|
|
render json: post, serializer: PostExcerptSerializer, root: false
|
2013-02-05 13:16:51 -06:00
|
|
|
when 'users'
|
|
|
|
user = User.where(username_lower: route[:username].downcase).first
|
|
|
|
guardian.ensure_can_see!(user)
|
2013-03-22 13:08:11 -05:00
|
|
|
render json: user, serializer: UserExcerptSerializer, root: false
|
2013-02-07 09:45:24 -06:00
|
|
|
when 'list'
|
2013-02-05 13:16:51 -06:00
|
|
|
if route[:action] == 'category'
|
|
|
|
category = Category.where(slug: route[:category]).first
|
|
|
|
guardian.ensure_can_see!(category)
|
2013-03-22 13:08:11 -05:00
|
|
|
render json: category, serializer: CategoryExcerptSerializer, root: false
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
else
|
2013-02-07 09:45:24 -06:00
|
|
|
render nothing: true, status: 404
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
rescue ActionController::RoutingError, Discourse::NotFound
|
|
|
|
render nothing: true, status: 404
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|