2 class ChangesetCommentsController < ApiController
3 before_action :check_api_writable
4 before_action :check_api_readable, :except => [:create]
5 before_action :authorize
9 before_action :require_public_data, :only => [:create]
10 before_action :set_request_formats
11 around_action :api_call_handle_error
12 around_action :api_call_timeout
15 # Add a comment to a changeset
17 # Check the arguments are sane
18 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
19 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
20 raise OSM::APIRateLimitExceeded if rate_limit_exceeded?
22 # Extract the arguments
26 # Find the changeset and check it is valid
27 changeset = Changeset.find(id)
28 raise OSM::APIChangesetNotYetClosedError, changeset if changeset.open?
30 # Add a comment to the changeset
31 comment = changeset.comments.create(:changeset => changeset,
33 :author => current_user)
35 # Notify current subscribers of the new comment
36 changeset.subscribers.visible.each do |user|
37 UserMailer.changeset_comment_notification(comment, user).deliver_later if current_user != user
40 # Add the commenter to the subscribers if necessary
41 changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
43 # Return a copy of the updated changeset
44 @changeset = changeset
45 render "api/changesets/changeset"
47 respond_to do |format|
54 # Sets visible flag on comment to false
56 # Check the arguments are sane
57 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
59 # Extract the arguments
63 comment = ChangesetComment.find(id)
66 comment.update(:visible => false)
68 # Return a copy of the updated changeset
69 @changeset = comment.changeset
70 render "api/changesets/changeset"
72 respond_to do |format|
79 # Sets visible flag on comment to true
81 # Check the arguments are sane
82 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
84 # Extract the arguments
88 comment = ChangesetComment.find(id)
91 comment.update(:visible => true)
93 # Return a copy of the updated changeset
94 @changeset = comment.changeset
95 render "api/changesets/changeset"
97 respond_to do |format|
106 # Check if the current user has exceed the rate limit for comments
107 def rate_limit_exceeded?
108 recent_comments = current_user.changeset_comments.where("created_at >= ?", Time.now.utc - 1.hour).count
110 recent_comments >= current_user.max_changeset_comments_per_hour