2 class ChangesetCommentsController < ApiController
5 before_action :check_api_writable, :except => [:index]
6 before_action :authorize, :except => [:index]
10 before_action :require_public_data, :only => [:create]
12 before_action :set_request_formats
15 # show all comments or search for a subset
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)
24 # Add a comment to a changeset
26 # Check the arguments are sane
27 raise OSM::APIBadUserInput, "No id was given" unless params[:changeset_id]
28 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
29 raise OSM::APIRateLimitExceeded if rate_limit_exceeded?
31 # Extract the arguments
32 changeset_id = params[:changeset_id].to_i
35 # Find the changeset and check it is valid
36 changeset = Changeset.find(changeset_id)
37 raise OSM::APIChangesetNotYetClosedError, changeset if changeset.open?
39 # Add a comment to the changeset
40 comment = changeset.comments.create(:changeset => changeset,
42 :author => current_user)
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
49 # Add the commenter to the subscribers if necessary
50 changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
52 # Return a copy of the updated changeset
53 @changeset = changeset
54 render "api/changesets/show"
56 respond_to do |format|
65 # Check if the current user has exceed the rate limit for comments
66 def rate_limit_exceeded?
67 recent_comments = current_user.changeset_comments.where(:created_at => Time.now.utc - 1.hour..).count
69 recent_comments >= current_user.max_changeset_comments_per_hour