1 class NotesController < ApplicationController
 
   3   layout 'site', :only => [:mine]
 
   5   before_filter :check_api_readable
 
   6   before_filter :authorize_web, :only => [:create, :comment, :close, :destroy, :mine]
 
   7   before_filter :check_api_writable, :only => [:create, :comment, :close, :destroy]
 
   8   before_filter :set_locale, :only => [:mine]
 
   9   after_filter :compress_output
 
  10   around_filter :api_call_handle_error, :api_call_timeout
 
  13   # Return a list of notes in a given area
 
  15     # Figure out the bbox - we prefer a bbox argument but also
 
  16     # support the old, deprecated, method with four arguments
 
  18       bbox = BoundingBox.from_bbox_params(params)
 
  20       raise OSM::APIBadUserInput.new("No l was given") unless params[:l]
 
  21       raise OSM::APIBadUserInput.new("No r was given") unless params[:r]
 
  22       raise OSM::APIBadUserInput.new("No b was given") unless params[:b]
 
  23       raise OSM::APIBadUserInput.new("No t was given") unless params[:t]
 
  25       bbox = BoundingBox.from_lrbt_params(params)
 
  28     # Get any conditions that need to be applied
 
  29     notes = closed_condition(Note.scoped)
 
  31     # Check that the boundaries are valid
 
  34     # Check the the bounding box is not too big
 
  35     bbox.check_size(MAX_NOTE_REQUEST_AREA)
 
  37     # Find the notes we want to return
 
  38     @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
 
  41     respond_to do |format|
 
  52     # Check the arguments are sane
 
  53     raise OSM::APIBadUserInput.new("No lat was given") unless params[:lat]
 
  54     raise OSM::APIBadUserInput.new("No lon was given") unless params[:lon]
 
  55     raise OSM::APIBadUserInput.new("No text was given") unless params[:text]
 
  57     # Extract the arguments
 
  58     lon = params[:lon].to_f
 
  59     lat = params[:lat].to_f
 
  60     comment = params[:text]
 
  62     # Include in a transaction to ensure that there is always a note_comment for every note
 
  65       @note = Note.create(:lat => lat, :lon => lon)
 
  66       raise OSM::APIBadUserInput.new("The note is outside this world") unless @note.in_world?
 
  71       # Add a comment to the note
 
  72       add_comment(@note, comment, "opened")
 
  75     # Return a copy of the new note
 
  76     respond_to do |format|
 
  77       format.xml { render :action => :show }
 
  78       format.json { render :action => :show }
 
  83   # Add a comment to an existing note
 
  85     # Check the arguments are sane
 
  86     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
 
  87     raise OSM::APIBadUserInput.new("No text was given") unless params[:text]
 
  89     # Extract the arguments
 
  91     comment = params[:text]
 
  93     # Find the note and check it is valid
 
  95     raise OSM::APINotFoundError unless @note
 
  96     raise OSM::APIAlreadyDeletedError unless @note.visible?
 
  98     # Add a comment to the note
 
 100       add_comment(@note, comment, "commented")
 
 103     # Return a copy of the updated note
 
 104     respond_to do |format|
 
 105       format.xml { render :action => :show }
 
 106       format.json { render :action => :show }
 
 113     # Check the arguments are sane
 
 114     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
 
 116     # Extract the arguments
 
 117     id = params[:id].to_i
 
 118     comment = params[:text]
 
 120     # Find the note and check it is valid
 
 121     @note = Note.find_by_id(id)
 
 122     raise OSM::APINotFoundError unless @note
 
 123     raise OSM::APIAlreadyDeletedError unless @note.visible?
 
 125     # Close the note and add a comment
 
 129       add_comment(@note, comment, "closed")
 
 132     # Return a copy of the updated note
 
 133     respond_to do |format|
 
 134       format.xml { render :action => :show }
 
 135       format.json { render :action => :show }
 
 140   # Get a feed of recent notes and comments
 
 142     # Get any conditions that need to be applied
 
 143     notes = closed_condition(Note.scoped)
 
 147       bbox = BoundingBox.from_bbox_params(params)
 
 149       bbox.check_boundaries
 
 150       bbox.check_size(MAX_NOTE_REQUEST_AREA)
 
 152       notes = notes.bbox(bbox)
 
 155     # Find the comments we want to return
 
 156     @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
 
 159     respond_to do |format|
 
 167     # Check the arguments are sane
 
 168     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
 
 170     # Find the note and check it is valid
 
 171     @note = Note.find(params[:id])
 
 172     raise OSM::APINotFoundError unless @note
 
 173     raise OSM::APIAlreadyDeletedError unless @note.visible?
 
 176     respond_to do |format|
 
 185   # Delete (hide) a note
 
 187     # Check the arguments are sane
 
 188     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
 
 190     # Extract the arguments
 
 191     id = params[:id].to_i
 
 193     # Find the note and check it is valid
 
 195     raise OSM::APINotFoundError unless note
 
 196     raise OSM::APIAlreadyDeletedError unless note.visible?
 
 198     # Mark the note as hidden
 
 200       note.status = "hidden"
 
 203       add_comment(note, nil, "hidden")
 
 207     render :text => "ok\n", :content_type => "text/html" 
 
 211   # Return a list of notes matching a given string
 
 213     # Check the arguments are sane
 
 214     raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
 
 216     # Get any conditions that need to be applied
 
 217     @notes = closed_condition(Note.scoped)
 
 218     @notes = @notes.joins(:comments).where("note_comments.body ~ ?", params[:q])
 
 220     # Find the notes we want to return
 
 221     @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
 
 224     respond_to do |format|
 
 225       format.rss { render :action => :index }
 
 226       format.xml { render :action => :index }
 
 227       format.json { render :action => :index }
 
 228       format.gpx { render :action => :index }
 
 233   # Display a list of notes by a specified user
 
 235     if params[:display_name] 
 
 236       if @this_user = User.active.find_by_display_name(params[:display_name])
 
 237         @title =  t 'note.mine.title', :user => @this_user.display_name 
 
 238         @heading =  t 'note.mine.heading', :user => @this_user.display_name 
 
 239         @description = t 'note.mine.description', :user => render_to_string(:partial => "user", :object => @this_user)
 
 240         @page = (params[:page] || 1).to_i 
 
 242         @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
 
 244         @title = t 'user.no_such_user.title' 
 
 245         @not_found_user = params[:display_name] 
 
 247         render :template => 'user/no_such_user', :status => :not_found 
 
 253   #------------------------------------------------------------ 
 
 254   # utility functions below. 
 
 255   #------------------------------------------------------------   
 
 258   # Render an OK response
 
 260     if params[:format] == "js"
 
 261       render :text => "osbResponse();", :content_type => "text/javascript" 
 
 263       render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
 
 264       render :text => "ok\n", :content_type => "text/plain" unless @note
 
 269   # Get the maximum number of results to return
 
 271     if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
 
 279   # Generate a condition to choose which bugs we want based
 
 280   # on their status and the user's request parameters
 
 281   def closed_condition(notes)
 
 283       closed_since = params[:closed].to_i
 
 289       notes = notes.where("status != 'hidden'")
 
 290     elsif closed_since > 0
 
 291       notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
 
 293       notes = notes.where("status = 'open'")
 
 300   # Add a comment to a note
 
 301   def add_comment(note, text, event)
 
 302     attributes = { :visible => true, :event => event, :body => text }
 
 305       attributes[:author_id] = @user.id
 
 307       attributes[:author_ip] = request.remote_ip
 
 310     comment = note.comments.create(attributes, :without_protection => true)
 
 312     note.comments.map { |c| c.author }.uniq.each do |user|
 
 313       if user and user != @user
 
 314         Notifier.note_comment_notification(comment, user).deliver