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       # Get note's author info (for logged in users - user_id, for logged out users - IP address)
 
  93       note_author_info = author_info
 
  95       # Include in a transaction to ensure that there is always a note_comment for every note
 
  98         @note = Note.create(:lat => lat, :lon => lon, :description => description, :user_id => note_author_info[:user_id], :user_ip => note_author_info[:user_ip])
 
  99         raise OSM::APIBadUserInput, "The note is outside this world" unless @note.in_world?
 
 104         # Add opening comment (description) to the note
 
 105         add_comment(@note, description, "opened")
 
 108       # Return a copy of the new note
 
 109       respond_to do |format|
 
 110         format.xml { render :action => :show }
 
 111         format.json { render :action => :show }
 
 116     # Delete (hide) a note
 
 118       # Check the arguments are sane
 
 119       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
 
 121       # Extract the arguments
 
 122       id = params[:id].to_i
 
 123       comment = params[:text]
 
 125       # Find the note and check it is valid
 
 127         @note = Note.lock.find(id)
 
 128         raise OSM::APINotFoundError unless @note
 
 129         raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
 
 131         # Mark the note as hidden
 
 132         @note.status = "hidden"
 
 135         add_comment(@note, comment, "hidden", :notify => false)
 
 138       # Return a copy of the updated note
 
 139       respond_to do |format|
 
 140         format.xml { render :action => :show }
 
 141         format.json { render :action => :show }
 
 146     # Add a comment to an existing note
 
 148       # Check the arguments are sane
 
 149       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
 
 150       raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
 
 152       # Extract the arguments
 
 153       id = params[:id].to_i
 
 154       comment = params[:text]
 
 156       # Find the note and check it is valid
 
 158         @note = Note.lock.find(id)
 
 159         raise OSM::APINotFoundError unless @note
 
 160         raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
 
 161         raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
 
 163         # Add a comment to the note
 
 164         add_comment(@note, comment, "commented")
 
 167       # Return a copy of the updated note
 
 168       respond_to do |format|
 
 169         format.xml { render :action => :show }
 
 170         format.json { render :action => :show }
 
 177       # Check the arguments are sane
 
 178       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
 
 180       # Extract the arguments
 
 181       id = params[:id].to_i
 
 182       comment = params[:text]
 
 184       # Find the note and check it is valid
 
 186         @note = Note.lock.find_by(:id => id)
 
 187         raise OSM::APINotFoundError unless @note
 
 188         raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
 
 189         raise OSM::APINoteAlreadyClosedError, @note if @note.closed?
 
 191         # Close the note and add a comment
 
 194         add_comment(@note, comment, "closed")
 
 197       # Return a copy of the updated note
 
 198       respond_to do |format|
 
 199         format.xml { render :action => :show }
 
 200         format.json { render :action => :show }
 
 207       # Check the arguments are sane
 
 208       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
 
 210       # Extract the arguments
 
 211       id = params[:id].to_i
 
 212       comment = params[:text]
 
 214       # Find the note and check it is valid
 
 216         @note = Note.lock.find_by(:id => id)
 
 217         raise OSM::APINotFoundError unless @note
 
 218         raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
 
 219         raise OSM::APINoteAlreadyOpenError, @note unless @note.closed? || !@note.visible?
 
 221         # Reopen the note and add a comment
 
 224         add_comment(@note, comment, "reopened")
 
 227       # Return a copy of the updated note
 
 228       respond_to do |format|
 
 229         format.xml { render :action => :show }
 
 230         format.json { render :action => :show }
 
 235     # Get a feed of recent notes and comments
 
 237       # Get any conditions that need to be applied
 
 238       notes = closed_condition(Note.all)
 
 239       notes = bbox_condition(notes)
 
 241       # Find the comments we want to return
 
 242       @comments = NoteComment.where(:note => notes)
 
 243                              .order(:created_at => :desc)
 
 244       @comments = query_limit(@comments)
 
 245       @comments = @comments.preload(:author, :note => { :comments => :author })
 
 248       respond_to do |format|
 
 254     # Return a list of notes matching a given string
 
 256       # Get the initial set of notes
 
 257       @notes = closed_condition(Note.all)
 
 258       @notes = bbox_condition(@notes)
 
 260       # Add any user filter
 
 261       user = query_conditions_user_value
 
 262       @notes = @notes.joins(:comments).where(:note_comments => { :author_id => user }) if user
 
 264       # Add any text filter
 
 266         @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?) OR to_tsvector('english', notes.description) @@ plainto_tsquery('english', ?)", params[:q], params[:q])
 
 269       # Add any date filter
 
 270       time_filter_property = if params[:sort] == "updated_at"
 
 275       @notes = query_conditions_time(@notes, time_filter_property)
 
 277       # Choose the sort order
 
 278       @notes = if params[:sort] == "created_at"
 
 279                  if params[:order] == "oldest"
 
 280                    @notes.order(:created_at)
 
 282                    @notes.order(:created_at => :desc)
 
 285                  if params[:order] == "oldest"
 
 286                    @notes.order(:updated_at)
 
 288                    @notes.order(:updated_at => :desc)
 
 292       # Find the notes we want to return
 
 293       @notes = query_limit(@notes.distinct)
 
 294       @notes = @notes.preload(:comments)
 
 297       respond_to do |format|
 
 298         format.rss { render :action => :index }
 
 299         format.xml { render :action => :index }
 
 300         format.json { render :action => :index }
 
 301         format.gpx { render :action => :index }
 
 307     #------------------------------------------------------------
 
 308     # utility functions below.
 
 309     #------------------------------------------------------------
 
 312     # Generate a condition to choose which notes we want based
 
 313     # on their status and the user's request parameters
 
 314     def closed_condition(notes)
 
 315       closed_since = if params[:closed]
 
 316                        params[:closed].to_i.days
 
 318                        Note::DEFAULT_FRESHLY_CLOSED_LIMIT
 
 321       if closed_since.negative?
 
 322         notes.where.not(:status => "hidden")
 
 323       elsif closed_since.positive?
 
 324         notes.where(:status => "open")
 
 325              .or(notes.where(:status => "closed")
 
 326                       .where(notes.arel_table[:closed_at].gt(Time.now.utc - closed_since)))
 
 328         notes.where(:status => "open")
 
 333     # Generate a condition to choose which notes we want based
 
 334     # on the user's bounding box request parameters
 
 335     def bbox_condition(notes)
 
 337         bbox = BoundingBox.from_bbox_params(params)
 
 339         bbox.check_boundaries
 
 340         bbox.check_size(Settings.max_note_request_area)
 
 342         @min_lon = bbox.min_lon
 
 343         @min_lat = bbox.min_lat
 
 344         @max_lon = bbox.max_lon
 
 345         @max_lat = bbox.max_lat
 
 354     # Get author's information (for logged in users - user_id, for logged out users - IP address)
 
 357         { :user_id => current_user.id }
 
 359         { :user_ip => request.remote_ip }
 
 364     # Add a comment to a note
 
 365     def add_comment(note, text, event, notify: true)
 
 366       attributes = { :visible => true, :event => event, :body => text }
 
 368       # Get note comment's author info (for logged in users - user_id, for logged out users - IP address)
 
 369       note_comment_author_info = author_info
 
 371       if note_comment_author_info[:user_ip].nil?
 
 372         attributes[:author_id] = note_comment_author_info[:user_id]
 
 374         attributes[:author_ip] = note_comment_author_info[:user_ip]
 
 377       comment = note.comments.create!(attributes)
 
 380         note.subscribers.visible.each do |user|
 
 381           UserMailer.note_comment_notification(comment, user).deliver_later if current_user != user
 
 385       NoteSubscription.find_or_create_by(:note => note, :user => current_user) if current_user