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, :destroy]
9 before_filter :require_moderator, :only => [:destroy]
10 before_filter :check_api_writable, :only => [:create, :comment, :close, :destroy]
11 before_filter :require_allow_write_notes, :only => [:create, :comment, :close, :destroy]
12 before_filter :set_locale, :only => [:mine]
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.scoped)
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|
56 # Check the arguments are sane
57 raise OSM::APIBadUserInput.new("No lat was given") unless params[:lat]
58 raise OSM::APIBadUserInput.new("No lon was given") unless params[:lon]
59 raise OSM::APIBadUserInput.new("No text was given") if params[:text].blank?
61 # Extract the arguments
62 lon = params[:lon].to_f
63 lat = params[:lat].to_f
64 comment = params[:text]
66 # Include in a transaction to ensure that there is always a note_comment for every note
69 @note = Note.create(:lat => lat, :lon => lon)
70 raise OSM::APIBadUserInput.new("The note is outside this world") unless @note.in_world?
75 # Add a comment to the note
76 add_comment(@note, comment, "opened")
79 # Return a copy of the new note
80 respond_to do |format|
81 format.xml { render :action => :show }
82 format.json { render :action => :show }
87 # Add a comment to an existing note
89 # Check the arguments are sane
90 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
91 raise OSM::APIBadUserInput.new("No text was given") if params[:text].blank?
93 # Extract the arguments
95 comment = params[:text]
97 # Find the note and check it is valid
99 raise OSM::APINotFoundError unless @note
100 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
101 raise OSM::APINoteAlreadyClosedError.new(@note) if @note.closed?
103 # Add a comment to the note
105 add_comment(@note, comment, "commented")
108 # Return a copy of the updated note
109 respond_to do |format|
110 format.xml { render :action => :show }
111 format.json { render :action => :show }
118 # Check the arguments are sane
119 raise OSM::APIBadUserInput.new("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
126 @note = Note.find_by_id(id)
127 raise OSM::APINotFoundError unless @note
128 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
129 raise OSM::APINoteAlreadyClosedError.new(@note) if @note.closed?
131 # Close the note and add a comment
135 add_comment(@note, comment, "closed")
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 # Get a feed of recent notes and comments
148 # Get any conditions that need to be applied
149 notes = closed_condition(Note.scoped)
153 bbox = BoundingBox.from_bbox_params(params)
155 bbox.check_boundaries
156 bbox.check_size(MAX_NOTE_REQUEST_AREA)
158 notes = notes.bbox(bbox)
161 # Find the comments we want to return
162 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
165 respond_to do |format|
173 # Check the arguments are sane
174 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
176 # Find the note and check it is valid
177 @note = Note.find(params[:id])
178 raise OSM::APINotFoundError unless @note
179 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
182 respond_to do |format|
191 # Delete (hide) a note
193 # Check the arguments are sane
194 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
196 # Extract the arguments
197 id = params[:id].to_i
198 comment = params[:text]
200 # Find the note and check it is valid
202 raise OSM::APINotFoundError unless note
203 raise OSM::APIAlreadyDeletedError.new("note", note.id) unless note.visible?
205 # Mark the note as hidden
207 note.status = "hidden"
210 add_comment(note, comment, "hidden")
214 render :text => "ok\n", :content_type => "text/html"
218 # Return a list of notes matching a given string
220 # Check the arguments are sane
221 raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
223 # Get any conditions that need to be applied
224 @notes = closed_condition(Note.scoped)
225 @notes = @notes.joins(:comments).where("note_comments.body ~ ?", params[:q])
227 # Find the notes we want to return
228 @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
231 respond_to do |format|
232 format.rss { render :action => :index }
233 format.xml { render :action => :index }
234 format.json { render :action => :index }
235 format.gpx { render :action => :index }
240 # Display a list of notes by a specified user
242 if params[:display_name]
243 if @this_user = User.active.find_by_display_name(params[:display_name])
244 @title = t 'note.mine.title', :user => @this_user.display_name
245 @heading = t 'note.mine.heading', :user => @this_user.display_name
246 @description = t 'note.mine.subheading', :user => render_to_string(:partial => "user", :object => @this_user)
247 @page = (params[:page] || 1).to_i
249 @notes = @this_user.notes.order("updated_at DESC, id").uniq.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
251 @title = t 'user.no_such_user.title'
252 @not_found_user = params[:display_name]
254 render :template => 'user/no_such_user', :status => :not_found
260 #------------------------------------------------------------
261 # utility functions below.
262 #------------------------------------------------------------
265 # Render an OK response
267 if params[:format] == "js"
268 render :text => "osbResponse();", :content_type => "text/javascript"
270 render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
271 render :text => "ok\n", :content_type => "text/plain" unless @note
276 # Get the maximum number of results to return
278 if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
286 # Generate a condition to choose which bugs we want based
287 # on their status and the user's request parameters
288 def closed_condition(notes)
290 closed_since = params[:closed].to_i
296 notes = notes.where("status != 'hidden'")
297 elsif closed_since > 0
298 notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
300 notes = notes.where("status = 'open'")
307 # Add a comment to a note
308 def add_comment(note, text, event)
309 attributes = { :visible => true, :event => event, :body => text }
312 attributes[:author_id] = @user.id
314 attributes[:author_ip] = request.remote_ip
317 comment = note.comments.create(attributes, :without_protection => true)
319 note.comments.map { |c| c.author }.uniq.each do |user|
320 if user and user != @user
321 Notifier.note_comment_notification(comment, user).deliver