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)
38 @min_lon = bbox.min_lon
39 @min_lat = bbox.min_lat
40 @max_lon = bbox.max_lon
41 @max_lat = bbox.max_lat
43 # Find the notes we want to return
44 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
47 respond_to do |format|
58 # Check the arguments are sane
59 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
61 # Find the note and check it is valid
62 @note = Note.find(params[:id])
63 raise OSM::APINotFoundError unless @note
64 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
67 respond_to do |format|
79 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
81 # Check the arguments are sane
82 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
83 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
84 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
86 # Extract the arguments
87 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
88 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
89 comment = params[:text]
91 # Include in a transaction to ensure that there is always a note_comment for every note
94 @note = Note.create(:lat => lat, :lon => lon)
95 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
100 # Add a comment to the note
101 add_comment(@note, comment, "opened")
104 # Return a copy of the new note
105 respond_to do |format|
106 format.xml { render :action => :show }
107 format.json { render :action => :show }
112 # Delete (hide) a note
114 # Check the arguments are sane
115 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
117 # Extract the arguments
118 id = params[:id].to_i
119 comment = params[:text]
121 # Find the note and check it is valid
122 @note = Note.find(id)
123 raise OSM::APINotFoundError unless @note
124 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
126 # Mark the note as hidden
128 @note.status = "hidden"
131 add_comment(@note, comment, "hidden", :notify => false)
134 # Return a copy of the updated note
135 respond_to do |format|
136 format.xml { render :action => :show }
137 format.json { render :action => :show }
142 # Add a comment to an existing note
145 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
147 # Check the arguments are sane
148 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
149 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
151 # Extract the arguments
152 id = params[:id].to_i
153 comment = params[:text]
155 # Find the note and check it is valid
156 @note = Note.find(id)
157 raise OSM::APINotFoundError unless @note
158 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
159 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
161 # Add a comment to the note
163 add_comment(@note, comment, "commented")
166 # Return a copy of the updated note
167 respond_to do |format|
168 format.xml { render :action => :show }
169 format.json { render :action => :show }
176 # Check the arguments are sane
177 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
179 # Extract the arguments
180 id = params[:id].to_i
181 comment = params[:text]
183 # Find the note and check it is valid
184 @note = Note.find_by(:id => id)
185 raise OSM::APINotFoundError unless @note
186 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
187 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
189 # Close the note and add a comment
193 add_comment(@note, comment, "closed")
196 # Return a copy of the updated note
197 respond_to do |format|
198 format.xml { render :action => :show }
199 format.json { render :action => :show }
206 # Check the arguments are sane
207 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
209 # Extract the arguments
210 id = params[:id].to_i
211 comment = params[:text]
213 # Find the note and check it is valid
214 @note = Note.find_by(:id => id)
215 raise OSM::APINotFoundError unless @note
216 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
217 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
219 # Reopen the note and add a comment
223 add_comment(@note, comment, "reopened")
226 # Return a copy of the updated note
227 respond_to do |format|
228 format.xml { render :action => :show }
229 format.json { render :action => :show }
234 # Get a feed of recent notes and comments
236 # Get any conditions that need to be applied
237 notes = closed_condition(Note.all)
241 bbox = BoundingBox.from_bbox_params(params)
243 bbox.check_boundaries
244 bbox.check_size(Settings.max_note_request_area)
246 notes = notes.bbox(bbox)
247 @min_lon = bbox.min_lon
248 @min_lat = bbox.min_lat
249 @max_lon = bbox.max_lon
250 @max_lat = bbox.max_lat
253 # Find the comments we want to return
254 @comments = NoteComment.where(:note => notes)
255 .order(:created_at => :desc).limit(result_limit)
256 .preload(:author, :note => { :comments => :author })
259 respond_to do |format|
265 # Return a list of notes matching a given string
267 # Get the initial set of notes
268 @notes = closed_condition(Note.all)
270 # Add any user filter
271 if params[:display_name] || params[:user]
272 if params[:display_name]
273 @user = User.find_by(:display_name => params[:display_name])
275 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
277 @user = User.find_by(:id => params[:user])
279 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
282 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
285 # Add any text filter
286 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
288 # Add any date filter
291 from = Time.parse(params[:from]).utc
293 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
298 Time.parse(params[:to]).utc
303 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
306 @notes = if params[:sort] == "updated_at"
307 @notes.where(:updated_at => from..to)
309 @notes.where(:created_at => from..to)
313 # Choose the sort order
314 @notes = if params[:sort] == "created_at"
315 if params[:order] == "oldest"
316 @notes.order("created_at ASC")
318 @notes.order("created_at DESC")
321 if params[:order] == "oldest"
322 @notes.order("updated_at ASC")
324 @notes.order("updated_at DESC")
328 # Find the notes we want to return
329 @notes = @notes.distinct.limit(result_limit).preload(:comments)
332 respond_to do |format|
333 format.rss { render :action => :index }
334 format.xml { render :action => :index }
335 format.json { render :action => :index }
336 format.gpx { render :action => :index }
342 #------------------------------------------------------------
343 # utility functions below.
344 #------------------------------------------------------------
347 # Get the maximum number of results to return
350 if params[:limit].to_i.positive? && params[:limit].to_i <= Settings.max_note_query_limit
353 raise OSM::APIBadUserInput, "Note limit must be between 1 and #{Settings.max_note_query_limit}"
356 Settings.default_note_query_limit
361 # Generate a condition to choose which notes we want based
362 # on their status and the user's request parameters
363 def closed_condition(notes)
364 closed_since = if params[:closed]
365 params[:closed].to_i.days
367 Note::DEFAULT_FRESHLY_CLOSED_LIMIT
370 if closed_since.negative?
371 notes.where.not(:status => "hidden")
372 elsif closed_since.positive?
373 notes.where(:status => "open")
374 .or(notes.where(:status => "closed")
375 .where(notes.arel_table[:closed_at].gt(Time.now.utc - closed_since)))
377 notes.where(:status => "open")
382 # Add a comment to a note
383 def add_comment(note, text, event, notify: true)
384 attributes = { :visible => true, :event => event, :body => text }
387 attributes[:author_id] = current_user.id
389 attributes[:author_ip] = request.remote_ip
392 comment = note.comments.create!(attributes)
394 note.comments.map(&:author).uniq.each do |user|
395 UserMailer.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?