2 class NotesController < ApiController
3 before_action :check_api_readable
4 before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
5 before_action :setup_user_auth, :only => [:create, :show]
6 before_action :authorize, :only => [:close, :reopen, :destroy, :comment]
10 before_action :set_locale
11 around_action :api_call_handle_error, :api_call_timeout
12 before_action :set_request_formats, :except => [:feed]
15 # Return a list of notes in a given area
17 # Figure out the bbox - we prefer a bbox argument but also
18 # support the old, deprecated, method with four arguments
20 bbox = BoundingBox.from_bbox_params(params)
22 raise OSM::APIBadUserInput, "No l was given" unless params[:l]
23 raise OSM::APIBadUserInput, "No r was given" unless params[:r]
24 raise OSM::APIBadUserInput, "No b was given" unless params[:b]
25 raise OSM::APIBadUserInput, "No t was given" unless params[:t]
27 bbox = BoundingBox.from_lrbt_params(params)
30 # Get any conditions that need to be applied
31 notes = closed_condition(Note.all)
33 # Check that the boundaries are valid
36 # Check the the bounding box is not too big
37 bbox.check_size(Settings.max_note_request_area)
39 # Find the notes we want to return
40 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
43 respond_to do |format|
55 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
57 # Check the arguments are sane
58 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
59 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
60 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
62 # Extract the arguments
63 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
64 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
65 comment = params[:text]
67 # Include in a transaction to ensure that there is always a note_comment for every note
70 @note = Note.create(:lat => lat, :lon => lon)
71 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
76 # Add a comment to the note
77 add_comment(@note, comment, "opened")
80 # Return a copy of the new note
81 respond_to do |format|
82 format.xml { render :action => :show }
83 format.json { render :action => :show }
88 # Add a comment to an existing note
91 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
93 # Check the arguments are sane
94 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
95 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
97 # Extract the arguments
99 comment = params[:text]
101 # Find the note and check it is valid
102 @note = Note.find(id)
103 raise OSM::APINotFoundError unless @note
104 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
105 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
107 # Add a comment to the note
109 add_comment(@note, comment, "commented")
112 # Return a copy of the updated note
113 respond_to do |format|
114 format.xml { render :action => :show }
115 format.json { render :action => :show }
122 # Check the arguments are sane
123 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
125 # Extract the arguments
126 id = params[:id].to_i
127 comment = params[:text]
129 # Find the note and check it is valid
130 @note = Note.find_by(:id => id)
131 raise OSM::APINotFoundError unless @note
132 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
133 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
135 # Close the note and add a comment
139 add_comment(@note, comment, "closed")
142 # Return a copy of the updated note
143 respond_to do |format|
144 format.xml { render :action => :show }
145 format.json { render :action => :show }
152 # Check the arguments are sane
153 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
155 # Extract the arguments
156 id = params[:id].to_i
157 comment = params[:text]
159 # Find the note and check it is valid
160 @note = Note.find_by(:id => id)
161 raise OSM::APINotFoundError unless @note
162 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
163 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
165 # Reopen the note and add a comment
169 add_comment(@note, comment, "reopened")
172 # Return a copy of the updated note
173 respond_to do |format|
174 format.xml { render :action => :show }
175 format.json { render :action => :show }
180 # Get a feed of recent notes and comments
182 # Get any conditions that need to be applied
183 notes = closed_condition(Note.all)
187 bbox = BoundingBox.from_bbox_params(params)
189 bbox.check_boundaries
190 bbox.check_size(Settings.max_note_request_area)
192 notes = notes.bbox(bbox)
193 @min_lon = bbox.min_lon
194 @min_lat = bbox.min_lat
195 @max_lon = bbox.max_lon
196 @max_lat = bbox.max_lat
199 # Find the comments we want to return
200 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
203 respond_to do |format|
211 # Check the arguments are sane
212 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
214 # Find the note and check it is valid
215 @note = Note.find(params[:id])
216 raise OSM::APINotFoundError unless @note
217 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
220 respond_to do |format|
229 # Delete (hide) a note
231 # Check the arguments are sane
232 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
234 # Extract the arguments
235 id = params[:id].to_i
236 comment = params[:text]
238 # Find the note and check it is valid
239 @note = Note.find(id)
240 raise OSM::APINotFoundError unless @note
241 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
243 # Mark the note as hidden
245 @note.status = "hidden"
248 add_comment(@note, comment, "hidden", :notify => false)
251 # Return a copy of the updated note
252 respond_to do |format|
253 format.xml { render :action => :show }
254 format.json { render :action => :show }
259 # Return a list of notes matching a given string
261 # Get the initial set of notes
262 @notes = closed_condition(Note.all)
264 # Add any user filter
265 if params[:display_name] || params[:user]
266 if params[:display_name]
267 @user = User.find_by(:display_name => params[:display_name])
269 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
271 @user = User.find_by(:id => params[:user])
273 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
276 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
279 # Add any text filter
280 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
282 # Add any date filter
285 from = Time.parse(params[:from]).utc
287 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
292 Time.parse(params[:to]).utc
297 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
300 @notes = if params[:sort] == "updated_at"
301 @notes.where(:updated_at => from..to)
303 @notes.where(:created_at => from..to)
307 # Choose the sort order
308 @notes = if params[:sort] == "created_at"
309 if params[:order] == "oldest"
310 @notes.order("created_at ASC")
312 @notes.order("created_at DESC")
315 if params[:order] == "oldest"
316 @notes.order("updated_at ASC")
318 @notes.order("updated_at DESC")
322 # Find the notes we want to return
323 @notes = @notes.distinct.limit(result_limit).preload(:comments)
326 respond_to do |format|
327 format.rss { render :action => :index }
328 format.xml { render :action => :index }
329 format.json { render :action => :index }
330 format.gpx { render :action => :index }
336 #------------------------------------------------------------
337 # utility functions below.
338 #------------------------------------------------------------
341 # Get the maximum number of results to return
344 if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
347 raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
355 # Generate a condition to choose which notes we want based
356 # on their status and the user's request parameters
357 def closed_condition(notes)
358 closed_since = if params[:closed]
364 if closed_since.negative?
365 notes.where.not(:status => "hidden")
366 elsif closed_since.positive?
367 notes.where(:status => "open")
368 .or(notes.where(:status => "closed")
369 .where(notes.arel_table[:closed_at].gt(Time.now.utc - closed_since.days)))
371 notes.where(:status => "open")
376 # Add a comment to a note
377 def add_comment(note, text, event, notify: true)
378 attributes = { :visible => true, :event => event, :body => text }
381 attributes[:author_id] = current_user.id
383 attributes[:author_ip] = request.remote_ip
386 comment = note.comments.create!(attributes)
388 note.comments.map(&:author).uniq.each do |user|
389 UserMailer.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?