Commit 8e8f1e4d4b61d99cecbad85fc57923654a462967
Exists in
web_steps_improvements
and in
6 other branches
Merge branch 'comment_paragraph_api' into 'master'
Add api methods to comment paragraph Also improve comment listing from core API. See merge request !854
Showing
6 changed files
with
162 additions
and
1 deletions
Show diff stats
lib/noosfero/api/v1/comments.rb
@@ -20,8 +20,10 @@ module Noosfero | @@ -20,8 +20,10 @@ module Noosfero | ||
20 | get ":id/comments" do | 20 | get ":id/comments" do |
21 | article = find_article(environment.articles, params[:id]) | 21 | article = find_article(environment.articles, params[:id]) |
22 | comments = select_filtered_collection_of(article, :comments, params) | 22 | comments = select_filtered_collection_of(article, :comments, params) |
23 | + comments = comments.without_spam | ||
23 | comments = comments.without_reply if(params[:without_reply].present?) | 24 | comments = comments.without_reply if(params[:without_reply].present?) |
24 | - present comments, :with => Entities::Comment, :current_person => current_person | 25 | + comments = plugins.filter(:unavailable_comments, comments) |
26 | + present paginate(comments), :with => Entities::Comment, :current_person => current_person | ||
25 | end | 27 | end |
26 | 28 | ||
27 | get ":id/comments/:comment_id" do | 29 | get ":id/comments/:comment_id" do |
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
@@ -55,6 +55,10 @@ class CommentParagraphPlugin < Noosfero::Plugin | @@ -55,6 +55,10 @@ class CommentParagraphPlugin < Noosfero::Plugin | ||
55 | 55 | ||
56 | end | 56 | end |
57 | 57 | ||
58 | + def self.api_mount_points | ||
59 | + [CommentParagraphPlugin::API] | ||
60 | + end | ||
61 | + | ||
58 | end | 62 | end |
59 | 63 | ||
60 | require_dependency 'comment_paragraph_plugin/macros/allow_comment' | 64 | require_dependency 'comment_paragraph_plugin/macros/allow_comment' |
plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb
0 → 100644
@@ -0,0 +1,32 @@ | @@ -0,0 +1,32 @@ | ||
1 | +class CommentParagraphPlugin::API < Grape::API | ||
2 | + MAX_PER_PAGE = 20 | ||
3 | + | ||
4 | + resource :articles do | ||
5 | + paginate max_per_page: MAX_PER_PAGE | ||
6 | + get ':id/comment_paragraph_plugin/comments' do | ||
7 | + article = find_article(environment.articles, params[:id]) | ||
8 | + comments = select_filtered_collection_of(article, :comments, params) | ||
9 | + comments = comments.without_spam | ||
10 | + comments = comments.in_paragraph(params[:paragraph_uuid]) | ||
11 | + comments = comments.without_reply if(params[:without_reply].present?) | ||
12 | + present paginate(comments), :with => Noosfero::API::Entities::Comment, :current_person => current_person | ||
13 | + end | ||
14 | + | ||
15 | + {activate: true, deactivate: false}.each do |method, value| | ||
16 | + post ":id/comment_paragraph_plugin/#{method}" do | ||
17 | + authenticate! | ||
18 | + article = find_article(environment.articles, params[:id]) | ||
19 | + return forbidden! unless article.comment_paragraph_plugin_enabled? && article.allow_edit?(current_person) | ||
20 | + article.comment_paragraph_plugin_activate = value | ||
21 | + article.save! | ||
22 | + present_partial article, :with => Noosfero::API::Entities::Article | ||
23 | + end | ||
24 | + end | ||
25 | + | ||
26 | + get ':id/comment_paragraph_plugin/comments/count' do | ||
27 | + article = find_article(environment.articles, params[:id]) | ||
28 | + comments = select_filtered_collection_of(article, :comments, params) | ||
29 | + comments.group(:paragraph_uuid).count | ||
30 | + end | ||
31 | + end | ||
32 | +end |
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +require File.join(Rails.root,'lib','noosfero','api','entities') | ||
2 | +module Noosfero | ||
3 | + module API | ||
4 | + module Entities | ||
5 | + class Comment < CommentBase | ||
6 | + expose :paragraph_uuid | ||
7 | + expose :comment_paragraph_selected_area | ||
8 | + expose :comment_paragraph_selected_content | ||
9 | + end | ||
10 | + end | ||
11 | + end | ||
12 | +end |
@@ -0,0 +1,83 @@ | @@ -0,0 +1,83 @@ | ||
1 | +require_relative '../test_helper' | ||
2 | +require_relative '../../../../test/api/test_helper' | ||
3 | + | ||
4 | +class APITest < ActiveSupport::TestCase | ||
5 | + | ||
6 | + def setup | ||
7 | + login_api | ||
8 | + environment.enable_plugin(CommentParagraphPlugin) | ||
9 | + end | ||
10 | + | ||
11 | + should 'return custom parameters for each comment' do | ||
12 | + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false) | ||
13 | + comment = fast_create(Comment, paragraph_uuid: '1', source_id: article.id, author_id: fast_create(Person).id) | ||
14 | + comment.comment_paragraph_selected_area = 'area' | ||
15 | + comment.comment_paragraph_selected_content = 'content' | ||
16 | + comment.save! | ||
17 | + params[:paragraph_uuid] = '1' | ||
18 | + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" | ||
19 | + | ||
20 | + json = JSON.parse(last_response.body) | ||
21 | + assert_equivalent ['1'], json['comments'].map {|c| c['paragraph_uuid']} | ||
22 | + assert_equivalent ['area'], json['comments'].map {|c| c['comment_paragraph_selected_area']} | ||
23 | + assert_equivalent ['content'], json['comments'].map {|c| c['comment_paragraph_selected_content']} | ||
24 | + end | ||
25 | + | ||
26 | + should 'return comments that belongs to a paragraph' do | ||
27 | + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false) | ||
28 | + comment1 = fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id) | ||
29 | + comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id) | ||
30 | + comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) | ||
31 | + params[:paragraph_uuid] = '1' | ||
32 | + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" | ||
33 | + | ||
34 | + json = JSON.parse(last_response.body) | ||
35 | + assert_equivalent [comment1.id], json['comments'].map {|c| c['id']} | ||
36 | + end | ||
37 | + | ||
38 | + {activate: true, deactivate: false}.each do |method, value| | ||
39 | + should "#{method} paragraph comment in an article" do | ||
40 | + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :author_id => person.id) | ||
41 | + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}?#{params.to_query}" | ||
42 | + json = JSON.parse(last_response.body) | ||
43 | + assert_equal value, json["article"]["setting"]["comment_paragraph_plugin_activate"] | ||
44 | + end | ||
45 | + | ||
46 | + should "not allow #{method} paragraph comment when not logged in" do | ||
47 | + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing") | ||
48 | + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}" | ||
49 | + assert_equal 401, last_response.status | ||
50 | + end | ||
51 | + | ||
52 | + should "not allow #{method} paragraph comment when user does not have permission to edit article" do | ||
53 | + author = create_user.person | ||
54 | + article = fast_create(TextArticle, :profile_id => author.id, :name => "Some thing", :author_id => author.id) | ||
55 | + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}?#{params.to_query}" | ||
56 | + assert_equal 403, last_response.status | ||
57 | + end | ||
58 | + end | ||
59 | + | ||
60 | + should 'return comment counts grouped by paragraph' do | ||
61 | + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false) | ||
62 | + fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id) | ||
63 | + fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id) | ||
64 | + fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) | ||
65 | + fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) | ||
66 | + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments/count?#{params.to_query}" | ||
67 | + | ||
68 | + json = JSON.parse(last_response.body) | ||
69 | + assert_equal({"1"=>1, ""=>1, "2"=>2}, json) | ||
70 | + end | ||
71 | + | ||
72 | + should 'filter comments marked as spam' do | ||
73 | + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false) | ||
74 | + comment1 = fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id) | ||
75 | + comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id, spam: true) | ||
76 | + comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id, spam: true) | ||
77 | + params[:paragraph_uuid] = '1' | ||
78 | + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" | ||
79 | + | ||
80 | + json = JSON.parse(last_response.body) | ||
81 | + assert_equivalent [comment1.id], json['comments'].map {|c| c['id']} | ||
82 | + end | ||
83 | +end |
test/api/comments_test.rb
@@ -100,4 +100,32 @@ class CommentsTest < ActiveSupport::TestCase | @@ -100,4 +100,32 @@ class CommentsTest < ActiveSupport::TestCase | ||
100 | assert_equal 200, last_response.status | 100 | assert_equal 200, last_response.status |
101 | assert_equal [comment1.id], json["comments"].map { |c| c['id'] } | 101 | assert_equal [comment1.id], json["comments"].map { |c| c['id'] } |
102 | end | 102 | end |
103 | + | ||
104 | + should 'call plugin hotspot to filter unavailable comments' do | ||
105 | + class Plugin1 < Noosfero::Plugin | ||
106 | + def unavailable_comments(scope) | ||
107 | + scope.where(:user_agent => 'Jack') | ||
108 | + end | ||
109 | + end | ||
110 | + Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) | ||
111 | + Environment.default.enable_plugin(Plugin1) | ||
112 | + | ||
113 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
114 | + c1 = fast_create(Comment, source_id: article.id, body: "comment 1") | ||
115 | + c2 = fast_create(Comment, source_id: article.id, body: "comment 2", :user_agent => 'Jack') | ||
116 | + | ||
117 | + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" | ||
118 | + json = JSON.parse(last_response.body) | ||
119 | + assert_equal ["comment 2"], json["comments"].map {|c| c["body"]} | ||
120 | + end | ||
121 | + | ||
122 | + should 'do not return comments marked as spam' do | ||
123 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
124 | + c1 = fast_create(Comment, source_id: article.id, body: "comment 1", spam: true) | ||
125 | + c2 = fast_create(Comment, source_id: article.id, body: "comment 2") | ||
126 | + | ||
127 | + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" | ||
128 | + json = JSON.parse(last_response.body) | ||
129 | + assert_equal ["comment 2"], json["comments"].map {|c| c["body"]} | ||
130 | + end | ||
103 | end | 131 | end |