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
136 # Find the note and check it is valid
137 @note = Note.find_by_id(id)
138 raise OSM::APINotFoundError unless @note
139 raise OSM::APIAlreadyDeletedError unless @note.visible?
141 # Close the note and add a comment
145 add_comment(@note, nil, name, "closed")
148 # Return a copy of the updated note
149 respond_to do |format|
150 format.xml { render :action => :show }
151 format.json { render :action => :show }
156 # Get a feed of recent notes and comments
158 # Get any conditions that need to be applied
159 notes = closed_condition(Note.scoped)
163 bbox = BoundingBox.from_bbox_params(params)
165 bbox.check_boundaries
166 bbox.check_size(MAX_NOTE_REQUEST_AREA)
168 notes = notes.bbox(bbox)
171 # Find the comments we want to return
172 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
175 respond_to do |format|
183 # Check the arguments are sane
184 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
186 # Find the note and check it is valid
187 @note = Note.find(params[:id])
188 raise OSM::APINotFoundError unless @note
189 raise OSM::APIAlreadyDeletedError unless @note.visible?
192 respond_to do |format|
201 # Delete (hide) a note
203 # Check the arguments are sane
204 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
206 # Extract the arguments
207 id = params[:id].to_i
210 # Find the note and check it is valid
212 raise OSM::APINotFoundError unless note
213 raise OSM::APIAlreadyDeletedError unless note.visible?
215 # Mark the note as hidden
217 note.status = "hidden"
220 add_comment(note, nil, name, "hidden")
224 render :text => "ok\n", :content_type => "text/html"
228 # Return a list of notes matching a given string
230 # Check the arguments are sane
231 raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
233 # Get any conditions that need to be applied
234 @notes = closed_condition(Note.scoped)
235 @notes = @notes.joins(:comments).where("note_comments.body ~ ?", params[:q])
237 # Find the notes we want to return
238 @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
241 respond_to do |format|
242 format.rss { render :action => :index }
243 format.xml { render :action => :index }
244 format.json { render :action => :index }
245 format.gpx { render :action => :index }
250 # Display a list of notes by a specified user
252 if params[:display_name]
253 if @this_user = User.active.find_by_display_name(params[:display_name])
254 @title = t 'note.mine.title', :user => @this_user.display_name
255 @heading = t 'note.mine.heading', :user => @this_user.display_name
256 @description = t 'note.mine.description', :user => render_to_string(:partial => "user", :object => @this_user)
257 @page = (params[:page] || 1).to_i
259 @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
261 @title = t 'user.no_such_user.title'
262 @not_found_user = params[:display_name]
264 render :template => 'user/no_such_user', :status => :not_found
270 #------------------------------------------------------------
271 # utility functions below.
272 #------------------------------------------------------------
275 # Render an OK response
277 if params[:format] == "js"
278 render :text => "osbResponse();", :content_type => "text/javascript"
280 render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
281 render :text => "ok\n", :content_type => "text/plain" unless @note
286 # Get the maximum number of results to return
288 if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
296 # Generate a condition to choose which bugs we want based
297 # on their status and the user's request parameters
298 def closed_condition(notes)
300 closed_since = params[:closed].to_i
306 notes = notes.where("status != 'hidden'")
307 elsif closed_since > 0
308 notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
310 notes = notes.where("status = 'open'")
317 # Add a comment to a note
318 def add_comment(note, text, name, event)
319 name = "NoName" if name.nil?
321 attributes = { :visible => true, :event => event, :body => text }
324 attributes[:author_id] = @user.id
325 attributes[:author_name] = @user.display_name
327 attributes[:author_ip] = request.remote_ip
328 attributes[:author_name] = name + " (a)"
331 comment = note.comments.create(attributes, :without_protection => true)
333 note.comments.map { |c| c.author }.uniq.each do |user|
334 if user and user != @user
335 Notifier.note_comment_notification(comment, user).deliver