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|
59 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
61 # Check the arguments are sane
62 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
63 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
64 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
66 # Extract the arguments
67 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
68 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
69 comment = params[:text]
71 # Include in a transaction to ensure that there is always a note_comment for every note
74 @note = Note.create(:lat => lat, :lon => lon)
75 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
80 # Add a comment to the note
81 add_comment(@note, comment, "opened")
84 # Return a copy of the new note
85 respond_to do |format|
86 format.xml { render :action => :show }
87 format.json { render :action => :show }
92 # Add a comment to an existing note
95 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
97 # Check the arguments are sane
98 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
99 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
101 # Extract the arguments
102 id = params[:id].to_i
103 comment = params[:text]
105 # Find the note and check it is valid
106 @note = Note.find(id)
107 raise OSM::APINotFoundError unless @note
108 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
109 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
111 # Add a comment to the note
113 add_comment(@note, comment, "commented")
116 # Return a copy of the updated note
117 respond_to do |format|
118 format.xml { render :action => :show }
119 format.json { render :action => :show }
126 # Check the arguments are sane
127 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
129 # Extract the arguments
130 id = params[:id].to_i
131 comment = params[:text]
133 # Find the note and check it is valid
134 @note = Note.find_by(:id => id)
135 raise OSM::APINotFoundError unless @note
136 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
137 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
139 # Close the note and add a comment
143 add_comment(@note, comment, "closed")
146 # Return a copy of the updated note
147 respond_to do |format|
148 format.xml { render :action => :show }
149 format.json { render :action => :show }
156 # Check the arguments are sane
157 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
159 # Extract the arguments
160 id = params[:id].to_i
161 comment = params[:text]
163 # Find the note and check it is valid
164 @note = Note.find_by(:id => id)
165 raise OSM::APINotFoundError unless @note
166 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
167 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
169 # Reopen the note and add a comment
173 add_comment(@note, comment, "reopened")
176 # Return a copy of the updated note
177 respond_to do |format|
178 format.xml { render :action => :show }
179 format.json { render :action => :show }
184 # Get a feed of recent notes and comments
186 # Get any conditions that need to be applied
187 notes = closed_condition(Note.all)
191 bbox = BoundingBox.from_bbox_params(params)
193 bbox.check_boundaries
194 bbox.check_size(Settings.max_note_request_area)
196 notes = notes.bbox(bbox)
197 @min_lon = bbox.min_lon
198 @min_lat = bbox.min_lat
199 @max_lon = bbox.max_lon
200 @max_lat = bbox.max_lat
203 # Find the comments we want to return
204 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
207 respond_to do |format|
215 # Check the arguments are sane
216 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
218 # Find the note and check it is valid
219 @note = Note.find(params[:id])
220 raise OSM::APINotFoundError unless @note
221 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
224 respond_to do |format|
233 # Delete (hide) a note
235 # Check the arguments are sane
236 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
238 # Extract the arguments
239 id = params[:id].to_i
240 comment = params[:text]
242 # Find the note and check it is valid
243 @note = Note.find(id)
244 raise OSM::APINotFoundError unless @note
245 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
247 # Mark the note as hidden
249 @note.status = "hidden"
252 add_comment(@note, comment, "hidden", :notify => false)
255 # Return a copy of the updated note
256 respond_to do |format|
257 format.xml { render :action => :show }
258 format.json { render :action => :show }
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 <= 10000
351 raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
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]
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.days)))
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?