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)
39 # Find the notes we want to return
40 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
43 respond_to do |format|
55 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
57 # Check the arguments are sane
58 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
59 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
60 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
62 # Extract the arguments
63 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
64 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
65 comment = params[:text]
67 # Include in a transaction to ensure that there is always a note_comment for every note
70 @note = Note.create(:lat => lat, :lon => lon)
71 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
76 # Add a comment to the note
77 add_comment(@note, comment, "opened")
80 # Return a copy of the new note
81 respond_to do |format|
82 format.xml { render :action => :show }
83 format.json { render :action => :show }
88 # Add a comment to an existing note
91 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
93 # Check the arguments are sane
94 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
95 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
97 # Extract the arguments
99 comment = params[:text]
101 # Find the note and check it is valid
102 @note = Note.find(id)
103 raise OSM::APINotFoundError unless @note
104 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
105 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
107 # Add a comment to the note
109 add_comment(@note, comment, "commented")
112 # Return a copy of the updated note
113 respond_to do |format|
114 format.xml { render :action => :show }
115 format.json { render :action => :show }
122 # Check the arguments are sane
123 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
125 # Extract the arguments
126 id = params[:id].to_i
127 comment = params[:text]
129 # Find the note and check it is valid
130 @note = Note.find_by(:id => id)
131 raise OSM::APINotFoundError unless @note
132 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
133 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
135 # Close the note and add a comment
139 add_comment(@note, comment, "closed")
142 # Return a copy of the updated note
143 respond_to do |format|
144 format.xml { render :action => :show }
145 format.json { render :action => :show }
152 # Check the arguments are sane
153 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
155 # Extract the arguments
156 id = params[:id].to_i
157 comment = params[:text]
159 # Find the note and check it is valid
160 @note = Note.find_by(:id => id)
161 raise OSM::APINotFoundError unless @note
162 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
163 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
165 # Reopen the note and add a comment
169 add_comment(@note, comment, "reopened")
172 # Return a copy of the updated note
173 respond_to do |format|
174 format.xml { render :action => :show }
175 format.json { render :action => :show }
180 # Get a feed of recent notes and comments
182 # Get any conditions that need to be applied
183 notes = closed_condition(Note.all)
187 bbox = BoundingBox.from_bbox_params(params)
189 bbox.check_boundaries
190 bbox.check_size(Settings.max_note_request_area)
192 notes = notes.bbox(bbox)
195 # Find the comments we want to return
196 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
199 respond_to do |format|
207 # Check the arguments are sane
208 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
210 # Find the note and check it is valid
211 @note = Note.find(params[:id])
212 raise OSM::APINotFoundError unless @note
213 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
216 respond_to do |format|
225 # Delete (hide) a note
227 # Check the arguments are sane
228 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
230 # Extract the arguments
231 id = params[:id].to_i
232 comment = params[:text]
234 # Find the note and check it is valid
235 @note = Note.find(id)
236 raise OSM::APINotFoundError unless @note
237 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
239 # Mark the note as hidden
241 @note.status = "hidden"
244 add_comment(@note, comment, "hidden", :notify => false)
247 # Return a copy of the updated note
248 respond_to do |format|
249 format.xml { render :action => :show }
250 format.json { render :action => :show }
255 # Return a list of notes matching a given string
257 # Get the initial set of notes
258 @notes = closed_condition(Note.all)
260 # Add any user filter
261 if params[:display_name] || params[:user]
262 if params[:display_name]
263 @user = User.find_by(:display_name => params[:display_name])
265 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
267 @user = User.find_by(:id => params[:user])
269 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
272 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
275 # Add any text filter
276 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
278 # Add any date filter
281 from = Time.parse(params[:from]).utc
283 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
288 Time.parse(params[:to]).utc
293 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
296 @notes = if params[:sort] == "updated_at"
297 @notes.where(:updated_at => from..to)
299 @notes.where(:created_at => from..to)
303 # Choose the sort order
304 @notes = if params[:sort] == "created_at"
305 if params[:order] == "oldest"
306 @notes.order("created_at ASC")
308 @notes.order("created_at DESC")
311 if params[:order] == "oldest"
312 @notes.order("updated_at ASC")
314 @notes.order("updated_at DESC")
318 # Find the notes we want to return
319 @notes = @notes.distinct.limit(result_limit).preload(:comments)
322 respond_to do |format|
323 format.rss { render :action => :index }
324 format.xml { render :action => :index }
325 format.json { render :action => :index }
326 format.gpx { render :action => :index }
332 #------------------------------------------------------------
333 # utility functions below.
334 #------------------------------------------------------------
337 # Get the maximum number of results to return
340 if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
343 raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
351 # Generate a condition to choose which notes we want based
352 # on their status and the user's request parameters
353 def closed_condition(notes)
354 closed_since = if params[:closed]
360 if closed_since.negative?
361 notes.where.not(:status => "hidden")
362 elsif closed_since.positive?
363 notes.where(:status => "open")
364 .or(notes.where(:status => "closed")
365 .where(notes.arel_table[:closed_at].gt(Time.now.utc - closed_since.days)))
367 notes.where(:status => "open")
372 # Add a comment to a note
373 def add_comment(note, text, event, notify: true)
374 attributes = { :visible => true, :event => event, :body => text }
377 attributes[:author_id] = current_user.id
379 attributes[:author_ip] = request.remote_ip
382 comment = note.comments.create!(attributes)
384 note.comments.map(&:author).uniq.each do |user|
385 UserMailer.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?