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]
63 # Include in a transaction to ensure that there is always a note_comment for every note
66 @note = Note.create(:lat => lat, :lon => lon)
67 raise OSM::APIBadUserInput.new("The note is outside this world") unless @note.in_world?
72 # Add a comment to the note
73 add_comment(@note, comment, name, "opened")
76 # Return a copy of the new note
77 respond_to do |format|
78 format.xml { render :action => :show }
79 format.json { render :action => :show }
84 # Add a comment to an existing note
86 # Check the arguments are sane
87 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
88 raise OSM::APIBadUserInput.new("No text was given") unless params[:text]
90 # Extract the arguments
92 comment = params[:text]
93 name = params[:name] or "NoName"
95 # Find the note and check it is valid
97 raise OSM::APINotFoundError unless @note
98 raise OSM::APIAlreadyDeletedError unless @note.visible?
100 # Add a comment to the note
102 add_comment(@note, comment, name, "commented")
105 # Return a copy of the updated note
106 respond_to do |format|
107 format.xml { render :action => :show }
108 format.json { render :action => :show }
115 # Check the arguments are sane
116 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
118 # Extract the arguments
119 id = params[:id].to_i
120 comment = params[:text]
123 # Find the note and check it is valid
124 @note = Note.find_by_id(id)
125 raise OSM::APINotFoundError unless @note
126 raise OSM::APIAlreadyDeletedError unless @note.visible?
128 # Close the note and add a comment
132 add_comment(@note, comment, name, "closed")
135 # Return a copy of the updated note
136 respond_to do |format|
137 format.xml { render :action => :show }
138 format.json { render :action => :show }
143 # Get a feed of recent notes and comments
145 # Get any conditions that need to be applied
146 notes = closed_condition(Note.scoped)
150 bbox = BoundingBox.from_bbox_params(params)
152 bbox.check_boundaries
153 bbox.check_size(MAX_NOTE_REQUEST_AREA)
155 notes = notes.bbox(bbox)
158 # Find the comments we want to return
159 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
162 respond_to do |format|
170 # Check the arguments are sane
171 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
173 # Find the note and check it is valid
174 @note = Note.find(params[:id])
175 raise OSM::APINotFoundError unless @note
176 raise OSM::APIAlreadyDeletedError unless @note.visible?
179 respond_to do |format|
188 # Delete (hide) a note
190 # Check the arguments are sane
191 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
193 # Extract the arguments
194 id = params[:id].to_i
197 # Find the note and check it is valid
199 raise OSM::APINotFoundError unless note
200 raise OSM::APIAlreadyDeletedError unless note.visible?
202 # Mark the note as hidden
204 note.status = "hidden"
207 add_comment(note, nil, name, "hidden")
211 render :text => "ok\n", :content_type => "text/html"
215 # Return a list of notes matching a given string
217 # Check the arguments are sane
218 raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
220 # Get any conditions that need to be applied
221 @notes = closed_condition(Note.scoped)
222 @notes = @notes.joins(:comments).where("note_comments.body ~ ?", params[:q])
224 # Find the notes we want to return
225 @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
228 respond_to do |format|
229 format.rss { render :action => :index }
230 format.xml { render :action => :index }
231 format.json { render :action => :index }
232 format.gpx { render :action => :index }
237 # Display a list of notes by a specified user
239 if params[:display_name]
240 if @this_user = User.active.find_by_display_name(params[:display_name])
241 @title = t 'note.mine.title', :user => @this_user.display_name
242 @heading = t 'note.mine.heading', :user => @this_user.display_name
243 @description = t 'note.mine.description', :user => render_to_string(:partial => "user", :object => @this_user)
244 @page = (params[:page] || 1).to_i
246 @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
248 @title = t 'user.no_such_user.title'
249 @not_found_user = params[:display_name]
251 render :template => 'user/no_such_user', :status => :not_found
257 #------------------------------------------------------------
258 # utility functions below.
259 #------------------------------------------------------------
262 # Render an OK response
264 if params[:format] == "js"
265 render :text => "osbResponse();", :content_type => "text/javascript"
267 render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
268 render :text => "ok\n", :content_type => "text/plain" unless @note
273 # Get the maximum number of results to return
275 if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
283 # Generate a condition to choose which bugs we want based
284 # on their status and the user's request parameters
285 def closed_condition(notes)
287 closed_since = params[:closed].to_i
293 notes = notes.where("status != 'hidden'")
294 elsif closed_since > 0
295 notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
297 notes = notes.where("status = 'open'")
304 # Add a comment to a note
305 def add_comment(note, text, name, event)
306 name = "NoName" if name.nil?
308 attributes = { :visible => true, :event => event, :body => text }
311 attributes[:author_id] = @user.id
312 attributes[:author_name] = @user.display_name
314 attributes[:author_ip] = request.remote_ip
315 attributes[:author_name] = name + " (a)"
318 comment = note.comments.create(attributes, :without_protection => true)
320 note.comments.map { |c| c.author }.uniq.each do |user|
321 if user and user != @user
322 Notifier.note_comment_notification(comment, user).deliver