Commit e58c017a739c00c6833daad526ea6e3fd9654b06
Exists in
staging
and in
1 other branch
Merge branch 'comments_api_reply' into 'master'
API: Add the replies field in comment entity Also add pagination to comments and add an option to return root comments only. See merge request !842
Showing
3 changed files
with
33 additions
and
3 deletions
Show diff stats
lib/noosfero/api/entities.rb
... | ... | @@ -183,11 +183,16 @@ module Noosfero |
183 | 183 | expose :children, :using => ArticleBase |
184 | 184 | end |
185 | 185 | |
186 | - class Comment < Entity | |
187 | - root 'comments', 'comment' | |
186 | + class CommentBase < Entity | |
188 | 187 | expose :body, :title, :id |
189 | 188 | expose :created_at, :format_with => :timestamp |
190 | 189 | expose :author, :using => Profile |
190 | + expose :reply_of, :using => CommentBase | |
191 | + end | |
192 | + | |
193 | + class Comment < CommentBase | |
194 | + root 'comments', 'comment' | |
195 | + expose :children, as: :replies, :using => Comment | |
191 | 196 | end |
192 | 197 | |
193 | 198 | class User < Entity | ... | ... |
lib/noosfero/api/v1/comments.rb
... | ... | @@ -2,9 +2,12 @@ module Noosfero |
2 | 2 | module API |
3 | 3 | module V1 |
4 | 4 | class Comments < Grape::API |
5 | + MAX_PER_PAGE = 20 | |
6 | + | |
5 | 7 | before { authenticate! } |
6 | 8 | |
7 | 9 | resource :articles do |
10 | + paginate max_per_page: MAX_PER_PAGE | |
8 | 11 | # Collect comments from articles |
9 | 12 | # |
10 | 13 | # Parameters: |
... | ... | @@ -17,7 +20,7 @@ module Noosfero |
17 | 20 | get ":id/comments" do |
18 | 21 | article = find_article(environment.articles, params[:id]) |
19 | 22 | comments = select_filtered_collection_of(article, :comments, params) |
20 | - | |
23 | + comments = comments.without_reply if(params[:without_reply].present?) | |
21 | 24 | present comments, :with => Entities::Comment, :current_person => current_person |
22 | 25 | end |
23 | 26 | ... | ... |
test/api/comments_test.rb
... | ... | @@ -78,4 +78,26 @@ class CommentsTest < ActiveSupport::TestCase |
78 | 78 | assert_not_nil comment.source |
79 | 79 | end |
80 | 80 | |
81 | + should 'paginate comments' do | |
82 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | |
83 | + 5.times { article.comments.create!(:body => "some comment", :author => user.person) } | |
84 | + params[:per_page] = 3 | |
85 | + | |
86 | + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" | |
87 | + json = JSON.parse(last_response.body) | |
88 | + assert_equal 200, last_response.status | |
89 | + assert_equal 3, json["comments"].length | |
90 | + end | |
91 | + | |
92 | + should 'return only root comments' do | |
93 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | |
94 | + comment1 = article.comments.create!(:body => "some comment", :author => user.person) | |
95 | + comment2 = article.comments.create!(:body => "another comment", :author => user.person, :reply_of_id => comment1.id) | |
96 | + params[:without_reply] = true | |
97 | + | |
98 | + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" | |
99 | + json = JSON.parse(last_response.body) | |
100 | + assert_equal 200, last_response.status | |
101 | + assert_equal [comment1.id], json["comments"].map { |c| c['id'] } | |
102 | + end | |
81 | 103 | end | ... | ... |