comments.rb
2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Api
module V1
class Comments < Grape::API
MAX_PER_PAGE = 20
resource :articles do
paginate max_per_page: MAX_PER_PAGE
# Collect comments from articles
#
# Parameters:
# reference_id - comment id used as reference to collect comment
# oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
# limit - amount of comments returned. The default value is 20
#
# Example Request:
# GET /articles/12/comments?oldest&limit=10&reference_id=23
get ":id/comments" do
article = find_article(environment.articles, params[:id])
comments = select_filtered_collection_of(article, :comments, params)
comments = comments.without_spam
comments = comments.without_reply if(params[:without_reply].present?)
comments = plugins.filter(:unavailable_comments, comments)
present paginate(comments), :with => Entities::Comment, :current_person => current_person
end
get ":id/comments/:comment_id" do
article = find_article(environment.articles, params[:id])
present article.comments.find(params[:comment_id]), :with => Entities::Comment, :current_person => current_person
end
# Example Request:
# POST api/v1/articles/12/comments?private_token=2298743290432&body=new comment&title=New
post ":id/comments" do
authenticate!
article = find_article(environment.articles, params[:id])
options = params.select { |key,v| !['id','private_token'].include?(key) }.merge(:author => current_person, :source => article)
begin
comment = Comment.create!(options)
rescue ActiveRecord::RecordInvalid => e
render_api_error!(e.message, 400)
end
present comment, :with => Entities::Comment, :current_person => current_person
end
end
end
end
end