mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 07:11:34 +08:00
Initial release of Discourse
This commit is contained in:
40
app/controllers/excerpt_controller.rb
Normal file
40
app/controllers/excerpt_controller.rb
Normal file
@ -0,0 +1,40 @@
|
||||
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'
|
||||
|
||||
# If we have a post number, retrieve the last post. Otherwise, first post.
|
||||
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
|
||||
when 'users'
|
||||
user = User.where(username_lower: route[:username].downcase).first
|
||||
guardian.ensure_can_see!(user)
|
||||
render :json => user, serializer: UserExcerptSerializer, root: false
|
||||
when 'list'
|
||||
if route[:action] == 'category'
|
||||
category = Category.where(slug: route[:category]).first
|
||||
guardian.ensure_can_see!(category)
|
||||
render :json => category, serializer: CategoryExcerptSerializer, root: false
|
||||
end
|
||||
else
|
||||
render nothing: true, status: 404
|
||||
end
|
||||
|
||||
rescue ActionController::RoutingError, Discourse::NotFound
|
||||
render nothing: true, status: 404
|
||||
end
|
||||
|
||||
|
||||
end
|
Reference in New Issue
Block a user