2 class NotesController < ApiController
3 layout "site", :only => [:mine]
5 before_action :check_api_readable
6 before_action :setup_user_auth, :only => [:create, :comment, :show]
7 before_action :authorize, :only => [:close, :reopen, :destroy]
8 before_action :api_deny_access_handler
12 before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
13 before_action :set_locale
14 around_action :api_call_handle_error, :api_call_timeout
17 # Return a list of notes in a given area
19 # Figure out the bbox - we prefer a bbox argument but also
20 # support the old, deprecated, method with four arguments
22 bbox = BoundingBox.from_bbox_params(params)
24 raise OSM::APIBadUserInput, "No l was given" unless params[:l]
25 raise OSM::APIBadUserInput, "No r was given" unless params[:r]
26 raise OSM::APIBadUserInput, "No b was given" unless params[:b]
27 raise OSM::APIBadUserInput, "No t was given" unless params[:t]
29 bbox = BoundingBox.from_lrbt_params(params)
32 # Get any conditions that need to be applied
33 notes = closed_condition(Note.all)
35 # Check that the boundaries are valid
38 # Check the the bounding box is not too big
39 bbox.check_size(Settings.max_note_request_area)
41 # Find the notes we want to return
42 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
45 respond_to do |format|
57 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
59 # Check the arguments are sane
60 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
61 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
62 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
64 # Extract the arguments
65 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
66 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
67 comment = params[:text]
69 # Include in a transaction to ensure that there is always a note_comment for every note
72 @note = Note.create(:lat => lat, :lon => lon)
73 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
78 # Add a comment to the note
79 add_comment(@note, comment, "opened")
82 # Return a copy of the new note
83 respond_to do |format|
84 format.xml { render :action => :show }
85 format.json { render :action => :show }
90 # Add a comment to an existing note
93 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
95 # Check the arguments are sane
96 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
97 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
99 # Extract the arguments
100 id = params[:id].to_i
101 comment = params[:text]
103 # Find the note and check it is valid
104 @note = Note.find(id)
105 raise OSM::APINotFoundError unless @note
106 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
107 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
109 # Add a comment to the note
111 add_comment(@note, comment, "commented")
114 # Return a copy of the updated note
115 respond_to do |format|
116 format.xml { render :action => :show }
117 format.json { render :action => :show }
124 # Check the arguments are sane
125 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
127 # Extract the arguments
128 id = params[:id].to_i
129 comment = params[:text]
131 # Find the note and check it is valid
132 @note = Note.find_by(:id => id)
133 raise OSM::APINotFoundError unless @note
134 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
135 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
137 # Close the note and add a comment
141 add_comment(@note, comment, "closed")
144 # Return a copy of the updated note
145 respond_to do |format|
146 format.xml { render :action => :show }
147 format.json { render :action => :show }
154 # Check the arguments are sane
155 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
157 # Extract the arguments
158 id = params[:id].to_i
159 comment = params[:text]
161 # Find the note and check it is valid
162 @note = Note.find_by(:id => id)
163 raise OSM::APINotFoundError unless @note
164 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
165 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
167 # Reopen the note and add a comment
171 add_comment(@note, comment, "reopened")
174 # Return a copy of the updated note
175 respond_to do |format|
176 format.xml { render :action => :show }
177 format.json { render :action => :show }
182 # Get a feed of recent notes and comments
184 # Get any conditions that need to be applied
185 notes = closed_condition(Note.all)
189 bbox = BoundingBox.from_bbox_params(params)
191 bbox.check_boundaries
192 bbox.check_size(Settings.max_note_request_area)
194 notes = notes.bbox(bbox)
197 # Find the comments we want to return
198 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
201 respond_to do |format|
209 # Check the arguments are sane
210 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
212 # Find the note and check it is valid
213 @note = Note.find(params[:id])
214 raise OSM::APINotFoundError unless @note
215 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
218 respond_to do |format|
227 # Delete (hide) a note
229 # Check the arguments are sane
230 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
232 # Extract the arguments
233 id = params[:id].to_i
234 comment = params[:text]
236 # Find the note and check it is valid
237 @note = Note.find(id)
238 raise OSM::APINotFoundError unless @note
239 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
241 # Mark the note as hidden
243 @note.status = "hidden"
246 add_comment(@note, comment, "hidden", false)
249 # Return a copy of the updated note
250 respond_to do |format|
251 format.xml { render :action => :show }
252 format.json { render :action => :show }
257 # Return a list of notes matching a given string
259 # Get the initial set of notes
260 @notes = closed_condition(Note.all)
262 # Add any user filter
263 if params[:display_name] || params[:user]
264 if params[:display_name]
265 @user = User.find_by(:display_name => params[:display_name])
267 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
269 @user = User.find_by(:id => params[:user])
271 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
274 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
277 # Add any text filter
278 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
280 # Add any date filter
283 from = Time.parse(params[:from])
285 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
290 Time.parse(params[:to])
295 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
298 @notes = @notes.where(:created_at => from..to)
301 # Find the notes we want to return
302 @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
305 respond_to do |format|
306 format.rss { render :action => :index }
307 format.xml { render :action => :index }
308 format.json { render :action => :index }
309 format.gpx { render :action => :index }
315 #------------------------------------------------------------
316 # utility functions below.
317 #------------------------------------------------------------
320 # Get the maximum number of results to return
323 if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
326 raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
334 # Generate a condition to choose which notes we want based
335 # on their status and the user's request parameters
336 def closed_condition(notes)
337 closed_since = if params[:closed]
343 if closed_since.negative?
344 notes.where.not(:status => "hidden")
345 elsif closed_since.positive?
346 notes.where(:status => "open")
347 .or(notes.where(:status => "closed")
348 .where(notes.arel_table[:closed_at].gt(Time.now - closed_since.days)))
350 notes.where(:status => "open")
355 # Add a comment to a note
356 def add_comment(note, text, event, notify = true)
357 attributes = { :visible => true, :event => event, :body => text }
360 attributes[:author_id] = current_user.id
362 attributes[:author_ip] = request.remote_ip
365 comment = note.comments.create!(attributes)
367 note.comments.map(&:author).uniq.each do |user|
368 Notifier.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?