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)
21 elsif params[:l] && params[:r] && params[:b] && params[:t]
22 bbox = BoundingBox.from_lrbt_params(params)
24 raise OSM::APIBadUserInput, "The parameter bbox is required"
27 # Get any conditions that need to be applied
28 notes = closed_condition(Note.all)
30 # Check that the boundaries are valid
33 # Check the the bounding box is not too big
34 bbox.check_size(Settings.max_note_request_area)
35 @min_lon = bbox.min_lon
36 @min_lat = bbox.min_lat
37 @max_lon = bbox.max_lon
38 @max_lat = bbox.max_lat
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|
55 # Check the arguments are sane
56 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
58 # Find the note and check it is valid
59 @note = Note.find(params[:id])
60 raise OSM::APINotFoundError unless @note
61 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
64 respond_to do |format|
76 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
78 # Check the arguments are sane
79 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
80 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
81 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
83 # Extract the arguments
84 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
85 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
86 comment = params[:text]
88 # Include in a transaction to ensure that there is always a note_comment for every note
91 @note = Note.create(:lat => lat, :lon => lon)
92 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
97 # Add a comment to the note
98 add_comment(@note, comment, "opened")
101 # Return a copy of the new note
102 respond_to do |format|
103 format.xml { render :action => :show }
104 format.json { render :action => :show }
109 # Delete (hide) a note
111 # Check the arguments are sane
112 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
114 # Extract the arguments
115 id = params[:id].to_i
116 comment = params[:text]
118 # Find the note and check it is valid
119 @note = Note.find(id)
120 raise OSM::APINotFoundError unless @note
121 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
123 # Mark the note as hidden
125 @note.status = "hidden"
128 add_comment(@note, comment, "hidden", :notify => false)
131 # Return a copy of the updated note
132 respond_to do |format|
133 format.xml { render :action => :show }
134 format.json { render :action => :show }
139 # Add a comment to an existing note
141 # Check the arguments are sane
142 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
143 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
145 # Extract the arguments
146 id = params[:id].to_i
147 comment = params[:text]
149 # Find the note and check it is valid
150 @note = Note.find(id)
151 raise OSM::APINotFoundError unless @note
152 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
153 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
155 # Add a comment to the note
157 add_comment(@note, comment, "commented")
160 # Return a copy of the updated note
161 respond_to do |format|
162 format.xml { render :action => :show }
163 format.json { render :action => :show }
170 # Check the arguments are sane
171 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
173 # Extract the arguments
174 id = params[:id].to_i
175 comment = params[:text]
177 # Find the note and check it is valid
178 @note = Note.find_by(:id => id)
179 raise OSM::APINotFoundError unless @note
180 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
181 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
183 # Close the note and add a comment
187 add_comment(@note, comment, "closed")
190 # Return a copy of the updated note
191 respond_to do |format|
192 format.xml { render :action => :show }
193 format.json { render :action => :show }
200 # Check the arguments are sane
201 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
203 # Extract the arguments
204 id = params[:id].to_i
205 comment = params[:text]
207 # Find the note and check it is valid
208 @note = Note.find_by(:id => id)
209 raise OSM::APINotFoundError unless @note
210 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
211 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
213 # Reopen the note and add a comment
217 add_comment(@note, comment, "reopened")
220 # Return a copy of the updated note
221 respond_to do |format|
222 format.xml { render :action => :show }
223 format.json { render :action => :show }
228 # Get a feed of recent notes and comments
230 # Get any conditions that need to be applied
231 notes = closed_condition(Note.all)
232 notes = bbox_condition(notes)
234 # Find the comments we want to return
235 @comments = NoteComment.where(:note => notes)
236 .order(:created_at => :desc).limit(result_limit)
237 .preload(:author, :note => { :comments => :author })
240 respond_to do |format|
246 # Return a list of notes matching a given string
248 # Get the initial set of notes
249 @notes = closed_condition(Note.all)
250 @notes = bbox_condition(@notes)
252 # Add any user filter
253 if params[:display_name] || params[:user]
254 if params[:display_name]
255 @user = User.find_by(:display_name => params[:display_name])
257 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
259 @user = User.find_by(:id => params[:user])
261 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
264 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
267 # Add any text filter
268 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
270 # Add any date filter
273 from = Time.parse(params[:from]).utc
275 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
280 Time.parse(params[:to]).utc
285 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
288 @notes = if params[:sort] == "updated_at"
289 @notes.where(:updated_at => from..to)
291 @notes.where(:created_at => from..to)
295 # Choose the sort order
296 @notes = if params[:sort] == "created_at"
297 if params[:order] == "oldest"
298 @notes.order("created_at ASC")
300 @notes.order("created_at DESC")
303 if params[:order] == "oldest"
304 @notes.order("updated_at ASC")
306 @notes.order("updated_at DESC")
310 # Find the notes we want to return
311 @notes = @notes.distinct.limit(result_limit).preload(:comments)
314 respond_to do |format|
315 format.rss { render :action => :index }
316 format.xml { render :action => :index }
317 format.json { render :action => :index }
318 format.gpx { render :action => :index }
324 #------------------------------------------------------------
325 # utility functions below.
326 #------------------------------------------------------------
329 # Get the maximum number of results to return
332 if params[:limit].to_i.positive? && params[:limit].to_i <= Settings.max_note_query_limit
335 raise OSM::APIBadUserInput, "Note limit must be between 1 and #{Settings.max_note_query_limit}"
338 Settings.default_note_query_limit
343 # Generate a condition to choose which notes we want based
344 # on their status and the user's request parameters
345 def closed_condition(notes)
346 closed_since = if params[:closed]
347 params[:closed].to_i.days
349 Note::DEFAULT_FRESHLY_CLOSED_LIMIT
352 if closed_since.negative?
353 notes.where.not(:status => "hidden")
354 elsif closed_since.positive?
355 notes.where(:status => "open")
356 .or(notes.where(:status => "closed")
357 .where(notes.arel_table[:closed_at].gt(Time.now.utc - closed_since)))
359 notes.where(:status => "open")
364 # Generate a condition to choose which notes we want based
365 # on the user's bounding box request parameters
366 def bbox_condition(notes)
368 bbox = BoundingBox.from_bbox_params(params)
370 bbox.check_boundaries
371 bbox.check_size(Settings.max_note_request_area)
373 @min_lon = bbox.min_lon
374 @min_lat = bbox.min_lat
375 @max_lon = bbox.max_lon
376 @max_lat = bbox.max_lat
385 # Add a comment to a note
386 def add_comment(note, text, event, notify: true)
387 attributes = { :visible => true, :event => event, :body => text }
389 if doorkeeper_token || current_token
390 author = current_user if scope_enabled?(:write_notes)
392 author = current_user
396 attributes[:author_id] = author.id
398 attributes[:author_ip] = request.remote_ip
401 comment = note.comments.create!(attributes)
403 note.comments.map(&:author).uniq.each do |user|
404 UserMailer.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?