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_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
257 respond_to do |format|
263 # Return a list of notes matching a given string
265 # Get the initial set of notes
266 @notes = closed_condition(Note.all)
268 # Add any user filter
269 if params[:display_name] || params[:user]
270 if params[:display_name]
271 @user = User.find_by(:display_name => params[:display_name])
273 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
275 @user = User.find_by(:id => params[:user])
277 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
280 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
283 # Add any text filter
284 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
286 # Add any date filter
289 from = Time.parse(params[:from]).utc
291 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
296 Time.parse(params[:to]).utc
301 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
304 @notes = if params[:sort] == "updated_at"
305 @notes.where(:updated_at => from..to)
307 @notes.where(:created_at => from..to)
311 # Choose the sort order
312 @notes = if params[:sort] == "created_at"
313 if params[:order] == "oldest"
314 @notes.order("created_at ASC")
316 @notes.order("created_at DESC")
319 if params[:order] == "oldest"
320 @notes.order("updated_at ASC")
322 @notes.order("updated_at DESC")
326 # Find the notes we want to return
327 @notes = @notes.distinct.limit(result_limit).preload(:comments)
330 respond_to do |format|
331 format.rss { render :action => :index }
332 format.xml { render :action => :index }
333 format.json { render :action => :index }
334 format.gpx { render :action => :index }
340 #------------------------------------------------------------
341 # utility functions below.
342 #------------------------------------------------------------
345 # Get the maximum number of results to return
348 if params[:limit].to_i.positive? && params[:limit].to_i <= Settings.max_note_query_limit
351 raise OSM::APIBadUserInput, "Note limit must be between 1 and #{Settings.max_note_query_limit}"
354 Settings.default_note_query_limit
359 # Generate a condition to choose which notes we want based
360 # on their status and the user's request parameters
361 def closed_condition(notes)
362 closed_since = if params[:closed]
363 params[:closed].to_i.days
365 Note::DEFAULT_FRESHLY_CLOSED_LIMIT
368 if closed_since.negative?
369 notes.where.not(:status => "hidden")
370 elsif closed_since.positive?
371 notes.where(:status => "open")
372 .or(notes.where(:status => "closed")
373 .where(notes.arel_table[:closed_at].gt(Time.now.utc - closed_since)))
375 notes.where(:status => "open")
380 # Add a comment to a note
381 def add_comment(note, text, event, notify: true)
382 attributes = { :visible => true, :event => event, :body => text }
385 attributes[:author_id] = current_user.id
387 attributes[:author_ip] = request.remote_ip
390 comment = note.comments.create!(attributes)
392 note.comments.map(&:author).uniq.each do |user|
393 UserMailer.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?