1 class NotesController < ApplicationController
3 layout 'site', :only => [:mine]
5 before_filter :check_api_readable
6 before_filter :authorize_web, :only => [:create, :close, :update, :delete, :mine]
7 before_filter :check_api_writable, :only => [:create, :close, :update, :delete]
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?
69 #TODO: move this into a helper function
71 url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16"
72 response = REXML::Document.new(Net::HTTP.get(URI.parse(url)))
74 if result = response.get_text("reversegeocode/result")
75 @note.nearby_place = result.to_s
77 @note.nearby_place = "unknown"
79 rescue Exception => err
80 @note.nearby_place = "unknown"
86 # Add a comment to the note
87 add_comment(@note, comment, name, "opened")
90 # Return a copy of the new note
91 respond_to do |format|
92 format.xml { render :action => :show }
93 format.json { render :action => :show }
98 # Add a comment to an existing note
100 # Check the arguments are sane
101 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
102 raise OSM::APIBadUserInput.new("No text was given") unless params[:text]
104 # Extract the arguments
105 id = params[:id].to_i
106 comment = params[:text]
107 name = params[:name] or "NoName"
109 # Find the note and check it is valid
110 @note = Note.find(id)
111 raise OSM::APINotFoundError unless @note
112 raise OSM::APIAlreadyDeletedError unless @note.visible?
114 # Add a comment to the note
116 add_comment(@note, comment, name, "commented")
119 # Return a copy of the updated note
120 respond_to do |format|
121 format.xml { render :action => :show }
122 format.json { render :action => :show }
129 # Check the arguments are sane
130 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
132 # Extract the arguments
133 id = params[:id].to_i
134 comment = params[:text]
137 # Find the note and check it is valid
138 @note = Note.find_by_id(id)
139 raise OSM::APINotFoundError unless @note
140 raise OSM::APIAlreadyDeletedError unless @note.visible?
142 # Close the note and add a comment
146 add_comment(@note, comment, name, "closed")
149 # Return a copy of the updated note
150 respond_to do |format|
151 format.xml { render :action => :show }
152 format.json { render :action => :show }
157 # Get a feed of recent notes and comments
159 # Get any conditions that need to be applied
160 notes = closed_condition(Note.scoped)
164 bbox = BoundingBox.from_bbox_params(params)
166 bbox.check_boundaries
167 bbox.check_size(MAX_NOTE_REQUEST_AREA)
169 notes = notes.bbox(bbox)
172 # Find the comments we want to return
173 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
176 respond_to do |format|
184 # Check the arguments are sane
185 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
187 # Find the note and check it is valid
188 @note = Note.find(params[:id])
189 raise OSM::APINotFoundError unless @note
190 raise OSM::APIAlreadyDeletedError unless @note.visible?
193 respond_to do |format|
202 # Delete (hide) a note
204 # Check the arguments are sane
205 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
207 # Extract the arguments
208 id = params[:id].to_i
211 # Find the note and check it is valid
213 raise OSM::APINotFoundError unless note
214 raise OSM::APIAlreadyDeletedError unless note.visible?
216 # Mark the note as hidden
218 note.status = "hidden"
221 add_comment(note, nil, name, "hidden")
225 render :text => "ok\n", :content_type => "text/html"
229 # Return a list of notes matching a given string
231 # Check the arguments are sane
232 raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
234 # Get any conditions that need to be applied
235 @notes = closed_condition(Note.scoped)
236 @notes = @notes.joins(:comments).where("note_comments.body ~ ?", params[:q])
238 # Find the notes we want to return
239 @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
242 respond_to do |format|
243 format.rss { render :action => :index }
244 format.xml { render :action => :index }
245 format.json { render :action => :index }
246 format.gpx { render :action => :index }
251 # Display a list of notes by a specified user
253 if params[:display_name]
254 if @this_user = User.active.find_by_display_name(params[:display_name])
255 @title = t 'note.mine.title', :user => @this_user.display_name
256 @heading = t 'note.mine.heading', :user => @this_user.display_name
257 @description = t 'note.mine.description', :user => render_to_string(:partial => "user", :object => @this_user)
258 @page = (params[:page] || 1).to_i
260 @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
262 @title = t 'user.no_such_user.title'
263 @not_found_user = params[:display_name]
265 render :template => 'user/no_such_user', :status => :not_found
271 #------------------------------------------------------------
272 # utility functions below.
273 #------------------------------------------------------------
276 # Render an OK response
278 if params[:format] == "js"
279 render :text => "osbResponse();", :content_type => "text/javascript"
281 render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
282 render :text => "ok\n", :content_type => "text/plain" unless @note
287 # Get the maximum number of results to return
289 if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
297 # Generate a condition to choose which bugs we want based
298 # on their status and the user's request parameters
299 def closed_condition(notes)
301 closed_since = params[:closed].to_i
307 notes = notes.where("status != 'hidden'")
308 elsif closed_since > 0
309 notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
311 notes = notes.where("status = 'open'")
318 # Add a comment to a note
319 def add_comment(note, text, name, event)
320 name = "NoName" if name.nil?
322 attributes = { :visible => true, :event => event, :body => text }
325 attributes[:author_id] = @user.id
326 attributes[:author_name] = @user.display_name
328 attributes[:author_ip] = request.remote_ip
329 attributes[:author_name] = name + " (a)"
332 comment = note.comments.create(attributes, :without_protection => true)
334 note.comments.map { |c| c.author }.uniq.each do |user|
335 if user and user != @user
336 Notifier.note_comment_notification(comment, user).deliver