2 class NotesController < ApiController
3 layout "site", :only => [:mine]
5 before_action :check_api_readable
6 before_action :setup_user_auth, :only => [:create, :comment, :show]
7 before_action :authorize, :only => [:close, :reopen, :destroy, :comment]
11 before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
12 before_action :set_locale
13 around_action :api_call_handle_error, :api_call_timeout
16 # Return a list of notes in a given area
18 # Figure out the bbox - we prefer a bbox argument but also
19 # support the old, deprecated, method with four arguments
21 bbox = BoundingBox.from_bbox_params(params)
23 raise OSM::APIBadUserInput, "No l was given" unless params[:l]
24 raise OSM::APIBadUserInput, "No r was given" unless params[:r]
25 raise OSM::APIBadUserInput, "No b was given" unless params[:b]
26 raise OSM::APIBadUserInput, "No t was given" unless params[:t]
28 bbox = BoundingBox.from_lrbt_params(params)
31 # Get any conditions that need to be applied
32 notes = closed_condition(Note.all)
34 # Check that the boundaries are valid
37 # Check the the bounding box is not too big
38 bbox.check_size(Settings.max_note_request_area)
40 # Find the notes we want to return
41 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
44 respond_to do |format|
56 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
58 # Check the arguments are sane
59 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
60 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
61 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
63 # Extract the arguments
64 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
65 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
66 comment = params[:text]
68 # Include in a transaction to ensure that there is always a note_comment for every note
71 @note = Note.create(:lat => lat, :lon => lon)
72 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
77 # Add a comment to the note
78 add_comment(@note, comment, "opened")
81 # Return a copy of the new note
82 respond_to do |format|
83 format.xml { render :action => :show }
84 format.json { render :action => :show }
89 # Add a comment to an existing note
92 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
94 # Check the arguments are sane
95 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
96 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
98 # Extract the arguments
100 comment = params[:text]
102 # Find the note and check it is valid
103 @note = Note.find(id)
104 raise OSM::APINotFoundError unless @note
105 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
106 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
108 # Add a comment to the note
110 add_comment(@note, comment, "commented")
113 # Return a copy of the updated note
114 respond_to do |format|
115 format.xml { render :action => :show }
116 format.json { render :action => :show }
123 # Check the arguments are sane
124 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
126 # Extract the arguments
127 id = params[:id].to_i
128 comment = params[:text]
130 # Find the note and check it is valid
131 @note = Note.find_by(:id => id)
132 raise OSM::APINotFoundError unless @note
133 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
134 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
136 # Close the note and add a comment
140 add_comment(@note, comment, "closed")
143 # Return a copy of the updated note
144 respond_to do |format|
145 format.xml { render :action => :show }
146 format.json { render :action => :show }
153 # Check the arguments are sane
154 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
156 # Extract the arguments
157 id = params[:id].to_i
158 comment = params[:text]
160 # Find the note and check it is valid
161 @note = Note.find_by(:id => id)
162 raise OSM::APINotFoundError unless @note
163 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
164 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
166 # Reopen the note and add a comment
170 add_comment(@note, comment, "reopened")
173 # Return a copy of the updated note
174 respond_to do |format|
175 format.xml { render :action => :show }
176 format.json { render :action => :show }
181 # Get a feed of recent notes and comments
183 # Get any conditions that need to be applied
184 notes = closed_condition(Note.all)
188 bbox = BoundingBox.from_bbox_params(params)
190 bbox.check_boundaries
191 bbox.check_size(Settings.max_note_request_area)
193 notes = notes.bbox(bbox)
196 # Find the comments we want to return
197 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
200 respond_to do |format|
208 # Check the arguments are sane
209 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
211 # Find the note and check it is valid
212 @note = Note.find(params[:id])
213 raise OSM::APINotFoundError unless @note
214 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
217 respond_to do |format|
226 # Delete (hide) a note
228 # Check the arguments are sane
229 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
231 # Extract the arguments
232 id = params[:id].to_i
233 comment = params[:text]
235 # Find the note and check it is valid
236 @note = Note.find(id)
237 raise OSM::APINotFoundError unless @note
238 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
240 # Mark the note as hidden
242 @note.status = "hidden"
245 add_comment(@note, comment, "hidden", false)
248 # Return a copy of the updated note
249 respond_to do |format|
250 format.xml { render :action => :show }
251 format.json { render :action => :show }
256 # Return a list of notes matching a given string
258 # Get the initial set of notes
259 @notes = closed_condition(Note.all)
261 # Add any user filter
262 if params[:display_name] || params[:user]
263 if params[:display_name]
264 @user = User.find_by(:display_name => params[:display_name])
266 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
268 @user = User.find_by(:id => params[:user])
270 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
273 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
276 # Add any text filter
277 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
279 # Add any date filter
282 from = Time.parse(params[:from])
284 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
289 Time.parse(params[:to])
294 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
297 @notes = if params[:sort] == "updated_at"
298 @notes.where(:updated_at => from..to)
300 @notes.where(:created_at => from..to)
304 # Choose the sort order
305 @notes = if params[:sort] == "created_at"
306 if params[:order] == "oldest"
307 @notes.order("created_at ASC")
309 @notes.order("created_at DESC")
312 if params[:order] == "oldest"
313 @notes.order("updated_at ASC")
315 @notes.order("updated_at DESC")
319 # Find the notes we want to return
320 @notes = @notes.distinct.limit(result_limit).preload(:comments)
323 respond_to do |format|
324 format.rss { render :action => :index }
325 format.xml { render :action => :index }
326 format.json { render :action => :index }
327 format.gpx { render :action => :index }
333 #------------------------------------------------------------
334 # utility functions below.
335 #------------------------------------------------------------
338 # Get the maximum number of results to return
341 if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
344 raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
352 # Generate a condition to choose which notes we want based
353 # on their status and the user's request parameters
354 def closed_condition(notes)
355 closed_since = if params[:closed]
361 if closed_since.negative?
362 notes.where.not(:status => "hidden")
363 elsif closed_since.positive?
364 notes.where(:status => "open")
365 .or(notes.where(:status => "closed")
366 .where(notes.arel_table[:closed_at].gt(Time.now - closed_since.days)))
368 notes.where(:status => "open")
373 # Add a comment to a note
374 def add_comment(note, text, event, notify = true)
375 attributes = { :visible => true, :event => event, :body => text }
378 attributes[:author_id] = current_user.id
380 attributes[:author_ip] = request.remote_ip
383 comment = note.comments.create!(attributes)
385 note.comments.map(&:author).uniq.each do |user|
386 Notifier.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?