]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changeset_comments_controller.rb
Add changeset comment search api with filtering by author and time
[rails.git] / app / controllers / api / changeset_comments_controller.rb
1 module Api
2   class ChangesetCommentsController < ApiController
3     include QueryMethods
4
5     before_action :check_api_writable, :except => [:index]
6     before_action :authorize, :except => [:index]
7
8     authorize_resource
9
10     before_action :require_public_data, :only => [:create]
11
12     before_action :set_request_formats
13
14     ##
15     # show all comments or search for a subset
16     def index
17       @comments = ChangesetComment.includes(:author).where(:visible => true).order("created_at DESC")
18       @comments = query_conditions_time(@comments)
19       @comments = query_conditions_user(@comments, :author)
20       @comments = query_limit(@comments)
21     end
22
23     ##
24     # Add a comment to a changeset
25     def create
26       # Check the arguments are sane
27       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
28       raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
29       raise OSM::APIRateLimitExceeded if rate_limit_exceeded?
30
31       # Extract the arguments
32       id = params[:id].to_i
33       body = params[:text]
34
35       # Find the changeset and check it is valid
36       changeset = Changeset.find(id)
37       raise OSM::APIChangesetNotYetClosedError, changeset if changeset.open?
38
39       # Add a comment to the changeset
40       comment = changeset.comments.create(:changeset => changeset,
41                                           :body => body,
42                                           :author => current_user)
43
44       # Notify current subscribers of the new comment
45       changeset.subscribers.visible.each do |user|
46         UserMailer.changeset_comment_notification(comment, user).deliver_later if current_user != user
47       end
48
49       # Add the commenter to the subscribers if necessary
50       changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
51
52       # Return a copy of the updated changeset
53       @changeset = changeset
54       render "api/changesets/show"
55
56       respond_to do |format|
57         format.xml
58         format.json
59       end
60     end
61
62     ##
63     # Sets visible flag on comment to false
64     def destroy
65       # Check the arguments are sane
66       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
67
68       # Extract the arguments
69       id = params[:id].to_i
70
71       # Find the changeset
72       comment = ChangesetComment.find(id)
73
74       # Hide the comment
75       comment.update(:visible => false)
76
77       # Return a copy of the updated changeset
78       @changeset = comment.changeset
79       render "api/changesets/show"
80
81       respond_to do |format|
82         format.xml
83         format.json
84       end
85     end
86
87     ##
88     # Sets visible flag on comment to true
89     def restore
90       # Check the arguments are sane
91       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
92
93       # Extract the arguments
94       id = params[:id].to_i
95
96       # Find the changeset
97       comment = ChangesetComment.find(id)
98
99       # Unhide the comment
100       comment.update(:visible => true)
101
102       # Return a copy of the updated changeset
103       @changeset = comment.changeset
104       render "api/changesets/show"
105
106       respond_to do |format|
107         format.xml
108         format.json
109       end
110     end
111
112     private
113
114     ##
115     # Check if the current user has exceed the rate limit for comments
116     def rate_limit_exceeded?
117       recent_comments = current_user.changeset_comments.where(:created_at => Time.now.utc - 1.hour..).count
118
119       recent_comments >= current_user.max_changeset_comments_per_hour
120     end
121   end
122 end