1 class NotesController < ApplicationController
3 layout 'site', :only => [:mine]
5 before_filter :check_api_readable
6 before_filter :authorize_web, :only => [:mine]
7 before_filter :setup_user_auth, :only => [:create, :comment]
8 before_filter :authorize, :only => [:close, :destroy]
9 before_filter :check_api_writable, :only => [:create, :comment, :close, :destroy]
10 before_filter :set_locale, :only => [:mine]
11 after_filter :compress_output
12 around_filter :api_call_handle_error, :api_call_timeout
15 # Return a list of notes in a given area
17 # Figure out the bbox - we prefer a bbox argument but also
18 # support the old, deprecated, method with four arguments
20 bbox = BoundingBox.from_bbox_params(params)
22 raise OSM::APIBadUserInput.new("No l was given") unless params[:l]
23 raise OSM::APIBadUserInput.new("No r was given") unless params[:r]
24 raise OSM::APIBadUserInput.new("No b was given") unless params[:b]
25 raise OSM::APIBadUserInput.new("No t was given") unless params[:t]
27 bbox = BoundingBox.from_lrbt_params(params)
30 # Get any conditions that need to be applied
31 notes = closed_condition(Note.scoped)
33 # Check that the boundaries are valid
36 # Check the the bounding box is not too big
37 bbox.check_size(MAX_NOTE_REQUEST_AREA)
39 # Find the notes we want to return
40 @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
43 respond_to do |format|
54 # Check the arguments are sane
55 raise OSM::APIBadUserInput.new("No lat was given") unless params[:lat]
56 raise OSM::APIBadUserInput.new("No lon was given") unless params[:lon]
57 raise OSM::APIBadUserInput.new("No text was given") if params[:text].blank?
59 # Extract the arguments
60 lon = params[:lon].to_f
61 lat = params[:lat].to_f
62 comment = params[:text]
64 # Include in a transaction to ensure that there is always a note_comment for every note
67 @note = Note.create(:lat => lat, :lon => lon)
68 raise OSM::APIBadUserInput.new("The note is outside this world") unless @note.in_world?
73 # Add a comment to the note
74 add_comment(@note, comment, "opened")
77 # Return a copy of the new note
78 respond_to do |format|
79 format.xml { render :action => :show }
80 format.json { render :action => :show }
85 # Add a comment to an existing note
87 # Check the arguments are sane
88 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
89 raise OSM::APIBadUserInput.new("No text was given") if params[:text].blank?
91 # Extract the arguments
93 comment = params[:text]
95 # Find the note and check it is valid
97 raise OSM::APINotFoundError unless @note
98 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
99 raise OSM::APINoteAlreadyClosedError.new(@note) if @note.closed?
101 # Add a comment to the note
103 add_comment(@note, comment, "commented")
106 # Return a copy of the updated note
107 respond_to do |format|
108 format.xml { render :action => :show }
109 format.json { render :action => :show }
116 # Check the arguments are sane
117 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
119 # Extract the arguments
120 id = params[:id].to_i
121 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.new("note", @note.id) unless @note.visible?
127 raise OSM::APINoteAlreadyClosedError.new(@note) if @note.closed?
129 # Close the note and add a comment
133 add_comment(@note, comment, "closed")
136 # Return a copy of the updated note
137 respond_to do |format|
138 format.xml { render :action => :show }
139 format.json { render :action => :show }
144 # Get a feed of recent notes and comments
146 # Get any conditions that need to be applied
147 notes = closed_condition(Note.scoped)
151 bbox = BoundingBox.from_bbox_params(params)
153 bbox.check_boundaries
154 bbox.check_size(MAX_NOTE_REQUEST_AREA)
156 notes = notes.bbox(bbox)
159 # Find the comments we want to return
160 @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
163 respond_to do |format|
171 # Check the arguments are sane
172 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
174 # Find the note and check it is valid
175 @note = Note.find(params[:id])
176 raise OSM::APINotFoundError unless @note
177 raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
180 respond_to do |format|
189 # Delete (hide) a note
191 # Check the arguments are sane
192 raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
194 # Extract the arguments
195 id = params[:id].to_i
197 # Find the note and check it is valid
199 raise OSM::APINotFoundError unless note
200 raise OSM::APIAlreadyDeletedError.new("note", note.id) unless note.visible?
202 # Mark the note as hidden
204 note.status = "hidden"
207 add_comment(note, nil, "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.subheading', :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, id").uniq.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, event)
306 attributes = { :visible => true, :event => event, :body => text }
309 attributes[:author_id] = @user.id
311 attributes[:author_ip] = request.remote_ip
314 comment = note.comments.create(attributes, :without_protection => true)
316 note.comments.map { |c| c.author }.uniq.each do |user|
317 if user and user != @user
318 Notifier.note_comment_notification(comment, user).deliver