2 class NotesController < ApiController
3 before_action :check_api_readable
4 before_action :setup_user_auth, :only => [:create, :comment, :show]
5 before_action :authorize, :only => [:close, :reopen, :destroy, :comment]
9 before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
10 before_action :set_locale
11 around_action :api_call_handle_error, :api_call_timeout
14 # Return a list of notes in a given area
16 # Figure out the bbox - we prefer a bbox argument but also
17 # support the old, deprecated, method with four arguments
19 bbox = BoundingBox.from_bbox_params(params)
21 raise OSM::APIBadUserInput, "No l was given" unless params[:l]
22 raise OSM::APIBadUserInput, "No r was given" unless params[:r]
23 raise OSM::APIBadUserInput, "No b was given" unless params[:b]
24 raise OSM::APIBadUserInput, "No t was given" unless params[:t]
26 bbox = BoundingBox.from_lrbt_params(params)
29 # Get any conditions that need to be applied
30 notes = closed_condition(Note.all)
32 # Check that the boundaries are valid
35 # Check the the bounding box is not too big
36 bbox.check_size(Settings.max_note_request_area)
38 # Find the notes we want to return
39 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
42 respond_to do |format|
54 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
56 # Check the arguments are sane
57 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
58 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
59 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
61 # Extract the arguments
62 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
63 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
64 comment = params[:text]
66 # Include in a transaction to ensure that there is always a note_comment for every note
69 @note = Note.create(:lat => lat, :lon => lon)
70 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
75 # Add a comment to the note
76 add_comment(@note, comment, "opened")
79 # Return a copy of the new note
80 respond_to do |format|
81 format.xml { render :action => :show }
82 format.json { render :action => :show }
87 # Add a comment to an existing note
90 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
92 # Check the arguments are sane
93 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
94 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
96 # Extract the arguments
98 comment = params[:text]
100 # Find the note and check it is valid
101 @note = Note.find(id)
102 raise OSM::APINotFoundError unless @note
103 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
104 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
106 # Add a comment to the note
108 add_comment(@note, comment, "commented")
111 # Return a copy of the updated note
112 respond_to do |format|
113 format.xml { render :action => :show }
114 format.json { render :action => :show }
121 # Check the arguments are sane
122 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
124 # Extract the arguments
125 id = params[:id].to_i
126 comment = params[:text]
128 # Find the note and check it is valid
129 @note = Note.find_by(:id => id)
130 raise OSM::APINotFoundError unless @note
131 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
132 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
134 # Close the note and add a comment
138 add_comment(@note, comment, "closed")
141 # Return a copy of the updated note
142 respond_to do |format|
143 format.xml { render :action => :show }
144 format.json { render :action => :show }
151 # Check the arguments are sane
152 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
154 # Extract the arguments
155 id = params[:id].to_i
156 comment = params[:text]
158 # Find the note and check it is valid
159 @note = Note.find_by(:id => id)
160 raise OSM::APINotFoundError unless @note
161 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
162 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
164 # Reopen the note and add a comment
168 add_comment(@note, comment, "reopened")
171 # Return a copy of the updated note
172 respond_to do |format|
173 format.xml { render :action => :show }
174 format.json { render :action => :show }
179 # Get a feed of recent notes and comments
181 # Get any conditions that need to be applied
182 notes = closed_condition(Note.all)
186 bbox = BoundingBox.from_bbox_params(params)
188 bbox.check_boundaries
189 bbox.check_size(Settings.max_note_request_area)
191 notes = notes.bbox(bbox)
194 # Find the comments we want to return
195 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
198 respond_to do |format|
206 # Check the arguments are sane
207 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
209 # Find the note and check it is valid
210 @note = Note.find(params[:id])
211 raise OSM::APINotFoundError unless @note
212 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
215 respond_to do |format|
224 # Delete (hide) a note
226 # Check the arguments are sane
227 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
229 # Extract the arguments
230 id = params[:id].to_i
231 comment = params[:text]
233 # Find the note and check it is valid
234 @note = Note.find(id)
235 raise OSM::APINotFoundError unless @note
236 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
238 # Mark the note as hidden
240 @note.status = "hidden"
243 add_comment(@note, comment, "hidden", false)
246 # Return a copy of the updated note
247 respond_to do |format|
248 format.xml { render :action => :show }
249 format.json { render :action => :show }
254 # Return a list of notes matching a given string
256 # Get the initial set of notes
257 @notes = closed_condition(Note.all)
259 # Add any user filter
260 if params[:display_name] || params[:user]
261 if params[:display_name]
262 @user = User.find_by(:display_name => params[:display_name])
264 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
266 @user = User.find_by(:id => params[:user])
268 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
271 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
274 # Add any text filter
275 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
277 # Add any date filter
280 from = Time.parse(params[:from])
282 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
287 Time.parse(params[:to])
292 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
295 @notes = if params[:sort] == "updated_at"
296 @notes.where(:updated_at => from..to)
298 @notes.where(:created_at => from..to)
302 # Choose the sort order
303 @notes = if params[:sort] == "created_at"
304 if params[:order] == "oldest"
305 @notes.order("created_at ASC")
307 @notes.order("created_at DESC")
310 if params[:order] == "oldest"
311 @notes.order("updated_at ASC")
313 @notes.order("updated_at DESC")
317 # Find the notes we want to return
318 @notes = @notes.distinct.limit(result_limit).preload(:comments)
321 respond_to do |format|
322 format.rss { render :action => :index }
323 format.xml { render :action => :index }
324 format.json { render :action => :index }
325 format.gpx { render :action => :index }
331 #------------------------------------------------------------
332 # utility functions below.
333 #------------------------------------------------------------
336 # Get the maximum number of results to return
339 if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
342 raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
350 # Generate a condition to choose which notes we want based
351 # on their status and the user's request parameters
352 def closed_condition(notes)
353 closed_since = if params[:closed]
359 if closed_since.negative?
360 notes.where.not(:status => "hidden")
361 elsif closed_since.positive?
362 notes.where(:status => "open")
363 .or(notes.where(:status => "closed")
364 .where(notes.arel_table[:closed_at].gt(Time.now - closed_since.days)))
366 notes.where(:status => "open")
371 # Add a comment to a note
372 def add_comment(note, text, event, notify = true)
373 attributes = { :visible => true, :event => event, :body => text }
376 attributes[:author_id] = current_user.id
378 attributes[:author_ip] = request.remote_ip
381 comment = note.comments.create!(attributes)
383 note.comments.map(&:author).uniq.each do |user|
384 UserMailer.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?