1 # frozen_string_literal: true
4 class NotesController < ApiController
7 before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
8 before_action :setup_user_auth, :only => [:create, :show]
9 before_action :authorize, :only => [:close, :reopen, :destroy, :comment]
13 before_action :set_locale
14 before_action :set_request_formats, :except => [:feed]
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)
23 elsif params[:l] && params[:r] && params[:b] && params[:t]
24 bbox = BoundingBox.from_lrbt_params(params)
26 raise OSM::APIBadUserInput, "The parameter bbox is required"
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)
37 @min_lon = bbox.min_lon
38 @min_lat = bbox.min_lat
39 @max_lon = bbox.max_lon
40 @max_lat = bbox.max_lat
42 # Find the notes we want to return
43 notes = notes.bbox(bbox).order(:updated_at => :desc)
44 notes = query_limit(notes)
45 @notes = notes.preload(:comments)
48 respond_to do |format|
59 # Check the arguments are sane
60 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
62 # Find the note and check it is valid
63 @note = Note.find(params[:id])
64 raise OSM::APINotFoundError unless @note
65 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
68 respond_to do |format|
80 raise OSM::APIAccessDenied if current_user.nil? && Acl.no_note_comment?(request.remote_ip)
82 # Check the arguments are sane
83 raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
84 raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
85 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
87 # Extract the arguments
88 lon = OSM.parse_float(params[:lon], OSM::APIBadUserInput, "lon was not a number")
89 lat = OSM.parse_float(params[:lat], OSM::APIBadUserInput, "lat was not a number")
90 description = params[:text]
92 raise OSM::APIModerationZoneError if current_user.nil? && ModerationZone.falls_within_any?(:lon => lon, :lat => lat)
94 # Get note's author info (for logged in users - user_id, for logged out users - IP address)
95 note_author_info = author_info
97 # Include in a transaction to ensure that there is always a note_comment for every note
100 @note = Note.create(:lat => lat, :lon => lon, :description => description, :user_id => note_author_info[:user_id], :user_ip => note_author_info[:user_ip])
101 raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
106 # Add opening comment (description) to the note
107 add_comment(@note, description, "opened")
110 # Return a copy of the new note
111 respond_to do |format|
112 format.xml { render :action => :show }
113 format.json { render :action => :show }
118 # Delete (hide) a note
120 # Check the arguments are sane
121 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
123 # Extract the arguments
124 id = params[:id].to_i
125 comment = params[:text]
127 # Find the note and check it is valid
129 @note = Note.lock.find(id)
130 raise OSM::APINotFoundError unless @note
131 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
133 # Mark the note as hidden
134 @note.status = "hidden"
137 add_comment(@note, comment, "hidden", :notify => false)
140 # Return a copy of the updated note
141 respond_to do |format|
142 format.xml { render :action => :show }
143 format.json { render :action => :show }
148 # Add a comment to an existing note
150 # Check the arguments are sane
151 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
152 raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
154 # Extract the arguments
155 id = params[:id].to_i
156 comment = params[:text]
158 # Find the note and check it is valid
160 @note = Note.lock.find(id)
161 raise OSM::APINotFoundError unless @note
162 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
163 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
165 # Add a comment to the note
166 add_comment(@note, comment, "commented")
169 # Return a copy of the updated note
170 respond_to do |format|
171 format.xml { render :action => :show }
172 format.json { render :action => :show }
179 # Check the arguments are sane
180 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
182 # Extract the arguments
183 id = params[:id].to_i
184 comment = params[:text]
186 # Find the note and check it is valid
188 @note = Note.lock.find_by(:id => id)
189 raise OSM::APINotFoundError unless @note
190 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
191 raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
193 # Close the note and add a comment
196 add_comment(@note, comment, "closed")
199 # Return a copy of the updated note
200 respond_to do |format|
201 format.xml { render :action => :show }
202 format.json { render :action => :show }
209 # Check the arguments are sane
210 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
212 # Extract the arguments
213 id = params[:id].to_i
214 comment = params[:text]
216 # Find the note and check it is valid
218 @note = Note.lock.find_by(:id => id)
219 raise OSM::APINotFoundError unless @note
220 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
221 raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
223 # Reopen the note and add a comment
226 add_comment(@note, comment, "reopened")
229 # Return a copy of the updated note
230 respond_to do |format|
231 format.xml { render :action => :show }
232 format.json { render :action => :show }
237 # Get a feed of recent notes and comments
239 # Get any conditions that need to be applied
240 notes = closed_condition(Note.all)
241 notes = bbox_condition(notes)
243 # Find the comments we want to return
244 @comments = NoteComment.where(:note => notes)
245 .order(:created_at => :desc)
246 @comments = query_limit(@comments)
247 @comments = @comments.preload(:author, :note => { :comments => :author })
250 respond_to do |format|
256 # Return a list of notes matching a given string
258 # Get the initial set of notes
259 @notes = closed_condition(Note.all)
260 @notes = bbox_condition(@notes)
262 # Add any user filter
263 user = query_conditions_user_value
264 @notes = @notes.joins(:comments).where(:note_comments => { :author_id => user }) if user
266 # Add any text filter
268 matched_notes = @notes.where("to_tsvector('english', notes.description) @@ plainto_tsquery('english', ?)", params[:q])
269 matched_note_comments = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q])
271 @notes = matched_notes.union_all(matched_note_comments)
274 # Add any date filter
275 time_filter_property = if params[:sort] == "updated_at"
280 @notes = query_conditions_time(@notes, time_filter_property)
282 # Choose the sort order
283 @notes = if params[:sort] == "created_at"
284 if params[:order] == "oldest"
285 @notes.order(:created_at)
287 @notes.order(:created_at => :desc)
290 if params[:order] == "oldest"
291 @notes.order(:updated_at)
293 @notes.order(:updated_at => :desc)
297 # Find the notes we want to return
298 @notes = query_limit(@notes.distinct)
299 @notes = @notes.preload(:comments)
302 respond_to do |format|
303 format.rss { render :action => :index }
304 format.xml { render :action => :index }
305 format.json { render :action => :index }
306 format.gpx { render :action => :index }
312 #------------------------------------------------------------
313 # utility functions below.
314 #------------------------------------------------------------
317 # Generate a condition to choose which notes we want based
318 # on their status and the user's request parameters
319 def closed_condition(notes)
320 closed_since = if params[:closed]
321 params[:closed].to_i.days
323 Note::DEFAULT_FRESHLY_CLOSED_LIMIT
326 if closed_since.negative?
327 notes.where.not(:status => "hidden")
328 elsif closed_since.positive?
329 notes.where(:status => "open")
330 .or(notes.where(:status => "closed")
331 .where(notes.arel_table[:closed_at].gt(Time.now.utc - closed_since)))
333 notes.where(:status => "open")
338 # Generate a condition to choose which notes we want based
339 # on the user's bounding box request parameters
340 def bbox_condition(notes)
342 bbox = BoundingBox.from_bbox_params(params)
344 bbox.check_boundaries
345 bbox.check_size(Settings.max_note_request_area)
347 @min_lon = bbox.min_lon
348 @min_lat = bbox.min_lat
349 @max_lon = bbox.max_lon
350 @max_lat = bbox.max_lat
359 # Get author's information (for logged in users - user_id, for logged out users - IP address)
362 { :user_id => current_user.id }
364 { :user_ip => request.remote_ip }
369 # Add a comment to a note
370 def add_comment(note, text, event, notify: true)
371 attributes = { :visible => true, :event => event, :body => text }
373 # Get note comment's author info (for logged in users - user_id, for logged out users - IP address)
374 note_comment_author_info = author_info
376 if note_comment_author_info[:user_ip].nil?
377 attributes[:author_id] = note_comment_author_info[:user_id]
379 attributes[:author_ip] = note_comment_author_info[:user_ip]
382 comment = note.comments.create!(attributes)
384 NoteCommentNotifier.with(:record => comment).deliver_later if notify
386 NoteSubscription.find_or_create_by(:note => note, :user => current_user) if current_user