1 class ChangesetCommentsController < ApplicationController
2 skip_before_action :verify_authenticity_token, :except => [:index]
3 before_action :authorize_web, :only => [:index]
4 before_action :set_locale, :only => [:index]
5 before_action :authorize, :only => [:create, :destroy, :restore]
6 before_action :require_moderator, :only => [:destroy, :restore]
7 before_action :require_allow_write_api, :only => [:create, :destroy, :restore]
8 before_action :require_public_data, :only => [:create]
9 before_action :check_api_writable, :only => [:create, :destroy, :restore]
10 before_action :check_api_readable, :except => [:create, :index]
11 before_action(:only => [:index]) { |c| c.check_database_readable(true) }
12 around_action :api_call_handle_error, :except => [:index]
13 around_action :api_call_timeout, :except => [:index]
14 around_action :web_timeout, :only => [:index]
17 # Add a comment to a changeset
19 # Check the arguments are sane
20 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
21 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
23 # Extract the arguments
27 # Find the changeset and check it is valid
28 changeset = Changeset.find(id)
29 raise OSM::APIChangesetNotYetClosedError, changeset if changeset.is_open?
31 # Add a comment to the changeset
32 comment = changeset.comments.create(:changeset => changeset,
34 :author => current_user)
36 # Notify current subscribers of the new comment
37 changeset.subscribers.visible.each do |user|
38 Notifier.changeset_comment_notification(comment, user).deliver_later if current_user != user
41 # Add the commenter to the subscribers if necessary
42 changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
44 # Return a copy of the updated changeset
45 render :xml => changeset.to_xml.to_s
49 # Sets visible flag on comment to false
51 # Check the arguments are sane
52 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
54 # Extract the arguments
58 comment = ChangesetComment.find(id)
61 comment.update(:visible => false)
63 # Return a copy of the updated changeset
64 render :xml => comment.changeset.to_xml.to_s
68 # Sets visible flag on comment to true
70 # Check the arguments are sane
71 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
73 # Extract the arguments
77 comment = ChangesetComment.find(id)
80 comment.update(:visible => true)
82 # Return a copy of the updated changeset
83 render :xml => comment.changeset.to_xml.to_s
87 # Get a feed of recent changeset comments
90 # Extract the arguments
94 changeset = Changeset.find(id)
96 # Return comments for this changeset only
97 @comments = changeset.comments.includes(:author, :changeset).limit(comments_limit)
100 @comments = ChangesetComment.includes(:author, :changeset).where(:visible => true).order("created_at DESC").limit(comments_limit).preload(:changeset)
104 respond_to do |format|
107 rescue OSM::APIBadUserInput
114 # Get the maximum number of comments to return
117 if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
120 raise OSM::APIBadUserInput, "Comments limit must be between 1 and 10000"