]> git.openstreetmap.org Git - rails.git/blob - app/controllers/notes_controller.rb
2089c2289ecaafc6cd704571643df657b346727b
[rails.git] / app / controllers / notes_controller.rb
1 class NotesController < ApplicationController
2
3   layout 'site', :only => [:mine]
4
5   before_filter :check_api_readable
6   before_filter :authorize_web, :only => [:create, :comment, :close, :destroy, :mine]
7   before_filter :check_api_writable, :only => [:create, :comment, :close, :destroy]
8   before_filter :set_locale, :only => [:mine]
9   after_filter :compress_output
10   around_filter :api_call_handle_error, :api_call_timeout
11
12   ##
13   # Return a list of notes in a given area
14   def index
15     # Figure out the bbox - we prefer a bbox argument but also
16     # support the old, deprecated, method with four arguments
17     if params[:bbox]
18       bbox = BoundingBox.from_bbox_params(params)
19     else
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]
24
25       bbox = BoundingBox.from_lrbt_params(params)
26     end
27
28     # Get any conditions that need to be applied
29     notes = closed_condition(Note.scoped)
30
31     # Check that the boundaries are valid
32     bbox.check_boundaries
33
34     # Check the the bounding box is not too big
35     bbox.check_size(MAX_NOTE_REQUEST_AREA)
36
37     # Find the notes we want to return
38     @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
39
40     # Render the result
41     respond_to do |format|
42       format.rss
43       format.xml
44       format.json
45       format.gpx
46     end
47   end
48
49   ##
50   # Create a new note
51   def create
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]
56
57     # Extract the arguments
58     lon = params[:lon].to_f
59     lat = params[:lat].to_f
60     comment = params[:text]
61     name = params[:name]
62
63     # Include in a transaction to ensure that there is always a note_comment for every note
64     Note.transaction do
65       # Create the note
66       @note = Note.create(:lat => lat, :lon => lon)
67       raise OSM::APIBadUserInput.new("The note is outside this world") unless @note.in_world?
68
69       #TODO: move this into a helper function
70       begin
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))) 
73                 
74         if result = response.get_text("reversegeocode/result") 
75           @note.nearby_place = result.to_s 
76         else 
77           @note.nearby_place = "unknown"
78         end
79       rescue Exception => err
80         @note.nearby_place = "unknown"
81       end
82
83       # Save the note
84       @note.save!
85
86       # Add a comment to the note
87       add_comment(@note, comment, name, "opened")
88     end
89
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 }
94     end
95   end
96
97   ##
98   # Add a comment to an existing note
99   def comment
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]
103
104     # Extract the arguments
105     id = params[:id].to_i
106     comment = params[:text]
107     name = params[:name] or "NoName"
108
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?
113
114     # Add a comment to the note
115     Note.transaction do
116       add_comment(@note, comment, name, "commented")
117     end
118
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 }
123     end
124   end
125
126   ##
127   # Close a note
128   def close
129     # Check the arguments are sane
130     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
131
132     # Extract the arguments
133     id = params[:id].to_i
134     comment = params[:text]
135     name = params[:name]
136
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?
141
142     # Close the note and add a comment
143     Note.transaction do
144       @note.close
145
146       add_comment(@note, comment, name, "closed")
147     end
148
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 }
153     end
154   end 
155
156   ##
157   # Get a feed of recent notes and comments
158   def feed
159     # Get any conditions that need to be applied
160     notes = closed_condition(Note.scoped)
161
162     # Process any bbox
163     if params[:bbox]
164       bbox = BoundingBox.from_bbox_params(params)
165
166       bbox.check_boundaries
167       bbox.check_size(MAX_NOTE_REQUEST_AREA)
168
169       notes = notes.bbox(bbox)
170     end
171
172     # Find the comments we want to return
173     @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
174
175     # Render the result
176     respond_to do |format|
177       format.rss
178     end
179   end
180
181   ##
182   # Read a note
183   def show
184     # Check the arguments are sane
185     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
186
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?
191     
192     # Render the result
193     respond_to do |format|
194       format.xml
195       format.rss
196       format.json
197       format.gpx
198     end
199   end
200
201   ##
202   # Delete (hide) a note
203   def destroy
204     # Check the arguments are sane
205     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
206
207     # Extract the arguments
208     id = params[:id].to_i
209     name = params[:name]
210
211     # Find the note and check it is valid
212     note = Note.find(id)
213     raise OSM::APINotFoundError unless note
214     raise OSM::APIAlreadyDeletedError unless note.visible?
215
216     # Mark the note as hidden
217     Note.transaction do
218       note.status = "hidden"
219       note.save
220
221       add_comment(note, nil, name, "hidden")
222     end
223
224     # Render the result
225     render :text => "ok\n", :content_type => "text/html" 
226   end
227
228   ##
229   # Return a list of notes matching a given string
230   def search
231     # Check the arguments are sane
232     raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
233
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])
237
238     # Find the notes we want to return
239     @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
240
241     # Render the result
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 }
247     end
248   end
249
250   ##
251   # Display a list of notes by a specified user
252   def mine
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 
259         @page_size = 10
260         @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
261       else
262         @title = t 'user.no_such_user.title' 
263         @not_found_user = params[:display_name] 
264
265         render :template => 'user/no_such_user', :status => :not_found 
266       end 
267     end
268   end
269
270 private 
271   #------------------------------------------------------------ 
272   # utility functions below. 
273   #------------------------------------------------------------   
274  
275   ##
276   # Render an OK response
277   def render_ok
278     if params[:format] == "js"
279       render :text => "osbResponse();", :content_type => "text/javascript" 
280     else
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
283     end
284   end
285
286   ##
287   # Get the maximum number of results to return
288   def result_limit
289     if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
290       params[:limit].to_i
291     else
292       100
293     end
294   end
295
296   ##
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)
300     if params[:closed]
301       closed_since = params[:closed].to_i
302     else
303       closed_since = 7
304     end
305         
306     if closed_since < 0
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}'))")
310     else
311       notes = notes.where("status = 'open'")
312     end
313
314     return notes
315   end
316
317   ##
318   # Add a comment to a note
319   def add_comment(note, text, name, event)
320     name = "NoName" if name.nil?
321
322     attributes = { :visible => true, :event => event, :body => text }
323
324     if @user  
325       attributes[:author_id] = @user.id
326       attributes[:author_name] = @user.display_name
327     else  
328       attributes[:author_ip] = request.remote_ip
329       attributes[:author_name] = name + " (a)"
330     end
331
332     comment = note.comments.create(attributes, :without_protection => true)
333
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
337       end
338     end
339   end
340 end