2   class ChangesetCommentsController < ApiController
 
   3     before_action :check_api_writable
 
   4     before_action :authorize
 
   8     before_action :require_public_data, :only => [:create]
 
  10     before_action :set_request_formats
 
  13     # Add a comment to a changeset
 
  15       # Check the arguments are sane
 
  16       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
 
  17       raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
 
  18       raise OSM::APIRateLimitExceeded if rate_limit_exceeded?
 
  20       # Extract the arguments
 
  24       # Find the changeset and check it is valid
 
  25       changeset = Changeset.find(id)
 
  26       raise OSM::APIChangesetNotYetClosedError, changeset if changeset.open?
 
  28       # Add a comment to the changeset
 
  29       comment = changeset.comments.create(:changeset => changeset,
 
  31                                           :author => current_user)
 
  33       # Notify current subscribers of the new comment
 
  34       changeset.subscribers.visible.each do |user|
 
  35         UserMailer.changeset_comment_notification(comment, user).deliver_later if current_user != user
 
  38       # Add the commenter to the subscribers if necessary
 
  39       changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
 
  41       # Return a copy of the updated changeset
 
  42       @changeset = changeset
 
  43       render "api/changesets/show"
 
  45       respond_to do |format|
 
  52     # Sets visible flag on comment to false
 
  54       # Check the arguments are sane
 
  55       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
 
  57       # Extract the arguments
 
  61       comment = ChangesetComment.find(id)
 
  64       comment.update(:visible => false)
 
  66       # Return a copy of the updated changeset
 
  67       @changeset = comment.changeset
 
  68       render "api/changesets/show"
 
  70       respond_to do |format|
 
  77     # Sets visible flag on comment to true
 
  79       # Check the arguments are sane
 
  80       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
 
  82       # Extract the arguments
 
  86       comment = ChangesetComment.find(id)
 
  89       comment.update(:visible => true)
 
  91       # Return a copy of the updated changeset
 
  92       @changeset = comment.changeset
 
  93       render "api/changesets/show"
 
  95       respond_to do |format|
 
 104     # Check if the current user has exceed the rate limit for comments
 
 105     def rate_limit_exceeded?
 
 106       recent_comments = current_user.changeset_comments.where(:created_at => Time.now.utc - 1.hour..).count
 
 108       recent_comments >= current_user.max_changeset_comments_per_hour