1 class NotesController < ApplicationController
2 layout "site", :only => [:mine]
4 skip_before_action :verify_authenticity_token, :except => [:mine]
5 before_action :check_api_readable
6 before_action :authorize_web, :only => [:mine]
7 before_action :setup_user_auth, :only => [:create, :comment, :show]
8 before_action :authorize, :only => [:close, :reopen, :destroy]
9 before_action :api_deny_access_handler, :except => [:mine]
13 before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
14 before_action :set_locale
15 around_action :api_call_handle_error, :api_call_timeout
18 # Return a list of notes in a given area
20 # Figure out the bbox - we prefer a bbox argument but also
21 # support the old, deprecated, method with four arguments
23 bbox = BoundingBox.from_bbox_params(params)
25 raise OSM::APIBadUserInput, "No l was given" unless params[:l]
26 raise OSM::APIBadUserInput, "No r was given" unless params[:r]
27 raise OSM::APIBadUserInput, "No b was given" unless params[:b]
28 raise OSM::APIBadUserInput, "No t was given" unless params[:t]
30 bbox = BoundingBox.from_lrbt_params(params)
33 # Get any conditions that need to be applied
34 notes = closed_condition(Note.all)
36 # Check that the boundaries are valid
39 # Check the the bounding box is not too big
40 bbox.check_size(MAX_NOTE_REQUEST_AREA)
42 # Find the notes we want to return
43 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
46 respond_to do |format|
58 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
60 # Check the arguments are sane
61 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
62 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
63 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
65 # Extract the arguments
66 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
67 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
68 comment = params[:text]
70 # Include in a transaction to ensure that there is always a note_comment for every note
73 @note = Note.create(:lat => lat, :lon => lon)
74 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
79 # Add a comment to the note
80 add_comment(@note, comment, "opened")
83 # Return a copy of the new note
84 respond_to do |format|
85 format.xml { render :action => :show }
86 format.json { render :action => :show }
91 # Add a comment to an existing note
94 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment(request.remote_ip)
96 # Check the arguments are sane
97 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
98 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
100 # Extract the arguments
101 id = params[:id].to_i
102 comment = params[:text]
104 # Find the note and check it is valid
105 @note = Note.find(id)
106 raise OSM::APINotFoundError unless @note
107 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
108 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
110 # Add a comment to the note
112 add_comment(@note, comment, "commented")
115 # Return a copy of the updated note
116 respond_to do |format|
117 format.xml { render :action => :show }
118 format.json { render :action => :show }
125 # Check the arguments are sane
126 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
128 # Extract the arguments
129 id = params[:id].to_i
130 comment = params[:text]
132 # Find the note and check it is valid
133 @note = Note.find_by(:id => id)
134 raise OSM::APINotFoundError unless @note
135 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
136 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
138 # Close the note and add a comment
142 add_comment(@note, comment, "closed")
145 # Return a copy of the updated note
146 respond_to do |format|
147 format.xml { render :action => :show }
148 format.json { render :action => :show }
155 # Check the arguments are sane
156 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
158 # Extract the arguments
159 id = params[:id].to_i
160 comment = params[:text]
162 # Find the note and check it is valid
163 @note = Note.find_by(:id => id)
164 raise OSM::APINotFoundError unless @note
165 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
166 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
168 # Reopen the note and add a comment
172 add_comment(@note, comment, "reopened")
175 # Return a copy of the updated note
176 respond_to do |format|
177 format.xml { render :action => :show }
178 format.json { render :action => :show }
183 # Get a feed of recent notes and comments
185 # Get any conditions that need to be applied
186 notes = closed_condition(Note.all)
190 bbox = BoundingBox.from_bbox_params(params)
192 bbox.check_boundaries
193 bbox.check_size(MAX_NOTE_REQUEST_AREA)
195 notes = notes.bbox(bbox)
198 # Find the comments we want to return
199 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
202 respond_to do |format|
210 # Check the arguments are sane
211 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
213 # Find the note and check it is valid
214 @note = Note.find(params[:id])
215 raise OSM::APINotFoundError unless @note
216 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
219 respond_to do |format|
228 # Delete (hide) a note
230 # Check the arguments are sane
231 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
233 # Extract the arguments
234 id = params[:id].to_i
235 comment = params[:text]
237 # Find the note and check it is valid
238 @note = Note.find(id)
239 raise OSM::APINotFoundError unless @note
240 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
242 # Mark the note as hidden
244 @note.status = "hidden"
247 add_comment(@note, comment, "hidden", false)
250 # Return a copy of the updated note
251 respond_to do |format|
252 format.xml { render :action => :show }
253 format.json { render :action => :show }
258 # Return a list of notes matching a given string
260 # Get the initial set of notes
261 @notes = closed_condition(Note.all)
263 # Add any user filter
264 if params[:display_name] || params[:user]
265 if params[:display_name]
266 @user = User.find_by(:display_name => params[:display_name])
268 raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
270 @user = User.find_by(:id => params[:user])
272 raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless @user
275 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
278 # Add any text filter
279 @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
281 # Add any date filter
284 from = Time.parse(params[:from])
286 raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
291 Time.parse(params[:to])
296 raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
299 @notes = @notes.where(:created_at => from..to)
302 # Find the notes we want to return
303 @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
306 respond_to do |format|
307 format.rss { render :action => :index }
308 format.xml { render :action => :index }
309 format.json { render :action => :index }
310 format.gpx { render :action => :index }
315 # Display a list of notes by a specified user
317 if params[:display_name]
318 if @user = User.active.find_by(:display_name => params[:display_name])
319 @params = params.permit(:display_name)
320 @title = t "notes.mine.title", :user => @user.display_name
321 @heading = t "notes.mine.heading", :user => @user.display_name
322 @description = t "notes.mine.subheading", :user => render_to_string(:partial => "user", :object => @user)
323 @page = (params[:page] || 1).to_i
326 @notes = @notes.visible unless current_user&.moderator?
327 @notes = @notes.order("updated_at DESC, id").distinct.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author).to_a
329 @title = t "users.no_such_user.title"
330 @not_found_user = params[:display_name]
332 render :template => "users/no_such_user", :status => :not_found
339 #------------------------------------------------------------
340 # utility functions below.
341 #------------------------------------------------------------
344 # Get the maximum number of results to return
347 if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
350 raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
358 # Generate a condition to choose which notes we want based
359 # on their status and the user's request parameters
360 def closed_condition(notes)
361 closed_since = if params[:closed]
367 if closed_since.negative?
368 notes.where.not(:status => "hidden")
369 elsif closed_since.positive?
370 notes.where(:status => "open")
371 .or(notes.where(:status => "closed")
372 .where(notes.arel_table[:closed_at].gt(Time.now - closed_since.days)))
374 notes.where(:status => "open")
379 # Add a comment to a note
380 def add_comment(note, text, event, notify = true)
381 attributes = { :visible => true, :event => event, :body => text }
384 attributes[:author_id] = current_user.id
386 attributes[:author_ip] = request.remote_ip
389 comment = note.comments.create!(attributes)
391 note.comments.map(&:author).uniq.each do |user|
392 Notifier.note_comment_notification(comment, user).deliver_later if notify && user && user != current_user && user.visible?