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 => notes).order("created_at DESC").limit(result_limit).include(: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 conditions = closed_condition
226 conditions = cond_merge conditions, ['note_comments.body ~ ?', params[:q]]
228 # Find the notes we want to return
229 @notes = Note.find(:all,
230 :conditions => conditions,
231 :order => "updated_at DESC",
232 :limit => result_limit,
234 :include => :comments)
237 respond_to do |format|
238 format.html { render :action => :list, :format => :rjs, :content_type => "text/javascript"}
239 format.rss { render :action => :list }
241 format.xml { render :action => :list }
242 format.json { render :action => :list }
243 format.gpx { render :action => :list }
248 if params[:display_name]
249 @user2 = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
252 if @user2.data_public? or @user2 == @user
253 conditions = ['note_comments.author_id = ?', @user2.id]
255 conditions = ['false']
257 else #if request.format == :html
258 @title = t 'user.no_such_user.title'
259 @not_found_user = params[:display_name]
260 render :template => 'user/no_such_user', :status => :not_found
266 user_link = render_to_string :partial => "user", :object => @user2
269 @title = t 'note.mine.title', :user => @user2.display_name
270 @heading = t 'note.mine.heading', :user => @user2.display_name
271 @description = t 'note.mine.description', :user => user_link
273 @page = (params[:page] || 1).to_i
276 @notes = Note.find(:all,
277 :include => [:comments, {:comments => :author}],
279 :order => "updated_at DESC",
280 :conditions => conditions,
281 :offset => (@page - 1) * @page_size,
282 :limit => @page_size).uniq
286 #------------------------------------------------------------
287 # utility functions below.
288 #------------------------------------------------------------
291 # Render an OK response
293 if params[:format] == "js"
294 render :text => "osbResponse();", :content_type => "text/javascript"
296 render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
297 render :text => "ok\n", :content_type => "text/plain" unless @note
302 # Get the maximum number of results to return
304 if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
312 # Generate a condition to choose which bugs we want based
313 # on their status and the user's request parameters
314 def closed_condition(notes)
316 closed_since = params[:closed].to_i
322 notes = notes.where("status != 'hidden'")
323 elsif closed_since > 0
324 notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
326 notes = notes.where("status = 'open'")
333 # Add a comment to a note
334 def add_comment(note, text, name, event)
335 name = "NoName" if name.nil?
337 attributes = { :visible => true, :event => event, :body => text }
340 attributes[:author_id] = @user.id
341 attributes[:author_name] = @user.display_name
343 attributes[:author_ip] = request.remote_ip
344 attributes[:author_name] = name + " (a)"
347 note.comments.create(attributes)
349 note.comments.map { |c| c.author }.uniq.each do |user|
350 if user and user != @user
351 Notifier.deliver_note_comment_notification(comment, user)