1 class NoteController < 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")
95 # Add a comment to an existing note
97 # Check the arguments are sane
98 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
99 raise OSM::APIBadUserInput.new("No text was given") unless params[:text]
101 # Extract the arguments
102 id = params[:id].to_i
103 comment = params[:text]
104 name = params[:name] or "NoName"
106 # Find the note and check it is valid
108 raise OSM::APINotFoundError unless note
109 raise OSM::APIAlreadyDeletedError unless note.visible?
111 # Add a comment to the note
113 add_comment(note, comment, name, "commented")
116 # Send an OK response
123 # Check the arguments are sane
124 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
126 # Extract the arguments
127 id = params[:id].to_i
130 # Find the note and check it is valid
131 note = Note.find_by_id(id)
132 raise OSM::APINotFoundError unless note
133 raise OSM::APIAlreadyDeletedError unless note.visible?
135 # Close the note and add a comment
139 add_comment(note, nil, name, "closed")
142 # Send an OK response
147 # Get a feed of recent notes and comments
149 # Get any conditions that need to be applied
150 notes = closed_condition(Note.scoped)
154 bbox = BoundingBox.from_bbox_params(params)
156 bbox.check_boundaries
157 bbox.check_size(MAX_NOTE_REQUEST_AREA)
159 notes = notes.bbox(bbox)
162 # Find the comments we want to return
163 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
166 respond_to do |format|
174 # Check the arguments are sane
175 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
177 # Find the note and check it is valid
178 @note = Note.find(params[:id])
179 raise OSM::APINotFoundError unless @note
180 raise OSM::APIAlreadyDeletedError unless @note.visible?
183 respond_to do |format|
192 # Delete (hide) a note
194 # Check the arguments are sane
195 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
197 # Extract the arguments
198 id = params[:id].to_i
201 # Find the note and check it is valid
203 raise OSM::APINotFoundError unless note
204 raise OSM::APIAlreadyDeletedError unless note.visible?
206 # Mark the note as hidden
208 note.status = "hidden"
211 add_comment(note, nil, name, "hidden")
215 render :text => "ok\n", :content_type => "text/html"
219 # Return a list of notes matching a given string
221 # Check the arguments are sane
222 raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
224 # Get any conditions that need to be applied
225 @notes = closed_condition(Note.scoped)
226 @notes = @notes.joins(:comments).where("note_comments.body ~ ?", params[:q])
228 # Find the notes we want to return
229 @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
232 respond_to do |format|
233 format.rss { render :action => :list }
234 format.xml { render :action => :list }
235 format.json { render :action => :list }
236 format.gpx { render :action => :list }
241 # Display a list of notes by a specified user
243 if params[:display_name]
244 if @this_user = User.active.find_by_display_name(params[:display_name])
245 @title = t 'note.mine.title', :user => @this_user.display_name
246 @heading = t 'note.mine.heading', :user => @this_user.display_name
247 @description = t 'note.mine.description', :user => render_to_string(:partial => "user", :object => @this_user)
248 @page = (params[:page] || 1).to_i
250 @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
252 @title = t 'user.no_such_user.title'
253 @not_found_user = params[:display_name]
255 render :template => 'user/no_such_user', :status => :not_found
261 #------------------------------------------------------------
262 # utility functions below.
263 #------------------------------------------------------------
266 # Render an OK response
268 if params[:format] == "js"
269 render :text => "osbResponse();", :content_type => "text/javascript"
271 render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
272 render :text => "ok\n", :content_type => "text/plain" unless @note
277 # Get the maximum number of results to return
279 if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
287 # Generate a condition to choose which bugs we want based
288 # on their status and the user's request parameters
289 def closed_condition(notes)
291 closed_since = params[:closed].to_i
297 notes = notes.where("status != 'hidden'")
298 elsif closed_since > 0
299 notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
301 notes = notes.where("status = 'open'")
308 # Add a comment to a note
309 def add_comment(note, text, name, event)
310 name = "NoName" if name.nil?
312 attributes = { :visible => true, :event => event, :body => text }
315 attributes[:author_id] = @user.id
316 attributes[:author_name] = @user.display_name
318 attributes[:author_ip] = request.remote_ip
319 attributes[:author_name] = name + " (a)"
322 note.comments.create(attributes, :without_protection => true)
324 note.comments.map { |c| c.author }.uniq.each do |user|
325 if user and user != @user
326 Notifier.deliver_note_comment_notification(comment, user)