1 class NotesController < ApplicationController
 
   3   layout 'site', :only => [:mine]
 
   5   before_filter :check_api_readable
 
   6   before_filter :authorize_web, :only => [:mine]
 
   7   before_filter :setup_user_auth, :only => [:create, :comment]
 
   8   before_filter :authorize, :only => [:close, :reopen, :destroy]
 
   9   before_filter :require_moderator, :only => [:destroy]
 
  10   before_filter :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
 
  11   before_filter :require_allow_write_notes, :only => [:create, :comment, :close, :reopen, :destroy]
 
  12   before_filter :set_locale
 
  13   after_filter :compress_output
 
  14   around_filter :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.new("No l was given") unless params[:l]
 
  25       raise OSM::APIBadUserInput.new("No r was given") unless params[:r]
 
  26       raise OSM::APIBadUserInput.new("No b was given") unless params[:b]
 
  27       raise OSM::APIBadUserInput.new("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(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 Acl.no_note_comment(request.remote_ip)
 
  59     # Check the arguments are sane
 
  60     raise OSM::APIBadUserInput.new("No lat was given") unless params[:lat]
 
  61     raise OSM::APIBadUserInput.new("No lon was given") unless params[:lon]
 
  62     raise OSM::APIBadUserInput.new("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.new("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 Acl.no_note_comment(request.remote_ip)
 
  95     # Check the arguments are sane
 
  96     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
 
  97     raise OSM::APIBadUserInput.new("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.new(@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.new("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.new(@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.new("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? or @user.moderator?
 
 165     raise OSM::APINoteAlreadyOpenError.new(@note) unless @note.closed? or not @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(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.new("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?
 
 218     respond_to do |format|
 
 227   # Delete (hide) a note
 
 229     # Check the arguments are sane
 
 230     raise OSM::APIBadUserInput.new("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     # Check the arguments are sane
 
 260     raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
 
 262     # Get any conditions that need to be applied
 
 263     @notes = closed_condition(Note.all)
 
 264     @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q])
 
 266     # Find the notes we want to return
 
 267     @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
 
 270     respond_to do |format|
 
 271       format.rss { render :action => :index }
 
 272       format.xml { render :action => :index }
 
 273       format.json { render :action => :index }
 
 274       format.gpx { render :action => :index }
 
 279   # Display a list of notes by a specified user
 
 281     if params[:display_name] 
 
 282       if @this_user = User.active.find_by_display_name(params[:display_name])
 
 283         @title =  t 'note.mine.title', :user => @this_user.display_name 
 
 284         @heading =  t 'note.mine.heading', :user => @this_user.display_name 
 
 285         @description = t 'note.mine.subheading', :user => render_to_string(:partial => "user", :object => @this_user)
 
 286         @page = (params[:page] || 1).to_i 
 
 288         @notes = @this_user.notes.order("updated_at DESC, id").uniq.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author).to_a
 
 290         @title = t 'user.no_such_user.title' 
 
 291         @not_found_user = params[:display_name] 
 
 293         render :template => 'user/no_such_user', :status => :not_found 
 
 299   #------------------------------------------------------------ 
 
 300   # utility functions below. 
 
 301   #------------------------------------------------------------   
 
 304   # Render an OK response
 
 306     if params[:format] == "js"
 
 307       render :text => "osbResponse();", :content_type => "text/javascript" 
 
 309       render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
 
 310       render :text => "ok\n", :content_type => "text/plain" unless @note
 
 315   # Get the maximum number of results to return
 
 318       if params[:limit].to_i > 0 and params[:limit].to_i <= 10000
 
 321         raise OSM::APIBadUserInput.new("Note limit must be between 1 and 10000")
 
 329   # Generate a condition to choose which bugs we want based
 
 330   # on their status and the user's request parameters
 
 331   def closed_condition(notes)
 
 333       closed_since = params[:closed].to_i
 
 339       notes = notes.where("status != 'hidden'")
 
 340     elsif closed_since > 0
 
 341       notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
 
 343       notes = notes.where("status = 'open'")
 
 350   # Add a comment to a note
 
 351   def add_comment(note, text, event, notify = true)
 
 352     attributes = { :visible => true, :event => event, :body => text }
 
 355       attributes[:author_id] = @user.id
 
 357       attributes[:author_ip] = request.remote_ip
 
 360     comment = note.comments.create(attributes)
 
 362     note.comments.map { |c| c.author }.uniq.each do |user|
 
 363       if notify and user and user != @user
 
 364         Notifier.note_comment_notification(comment, user).deliver