]> git.openstreetmap.org Git - rails.git/blob - app/controllers/notes_controller.rb
528907d16b9d1ab6c4c1cfeb25d79cd689e27c06
[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, :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
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     name = params[:name]
135
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?
140
141     # Close the note and add a comment
142     Note.transaction do
143       @note.close
144
145       add_comment(@note, nil, name, "closed")
146     end
147
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 }
152     end
153   end 
154
155   ##
156   # Get a feed of recent notes and comments
157   def feed
158     # Get any conditions that need to be applied
159     notes = closed_condition(Note.scoped)
160
161     # Process any bbox
162     if params[:bbox]
163       bbox = BoundingBox.from_bbox_params(params)
164
165       bbox.check_boundaries
166       bbox.check_size(MAX_NOTE_REQUEST_AREA)
167
168       notes = notes.bbox(bbox)
169     end
170
171     # Find the comments we want to return
172     @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
173
174     # Render the result
175     respond_to do |format|
176       format.rss
177     end
178   end
179
180   ##
181   # Read a note
182   def show
183     # Check the arguments are sane
184     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
185
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?
190     
191     # Render the result
192     respond_to do |format|
193       format.xml
194       format.rss
195       format.json
196       format.gpx
197     end
198   end
199
200   ##
201   # Delete (hide) a note
202   def destroy
203     # Check the arguments are sane
204     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
205
206     # Extract the arguments
207     id = params[:id].to_i
208     name = params[:name]
209
210     # Find the note and check it is valid
211     note = Note.find(id)
212     raise OSM::APINotFoundError unless note
213     raise OSM::APIAlreadyDeletedError unless note.visible?
214
215     # Mark the note as hidden
216     Note.transaction do
217       note.status = "hidden"
218       note.save
219
220       add_comment(note, nil, name, "hidden")
221     end
222
223     # Render the result
224     render :text => "ok\n", :content_type => "text/html" 
225   end
226
227   ##
228   # Return a list of notes matching a given string
229   def search
230     # Check the arguments are sane
231     raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
232
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])
236
237     # Find the notes we want to return
238     @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
239
240     # Render the result
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 }
246     end
247   end
248
249   ##
250   # Display a list of notes by a specified user
251   def mine
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 
258         @page_size = 10
259         @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
260       else
261         @title = t 'user.no_such_user.title' 
262         @not_found_user = params[:display_name] 
263
264         render :template => 'user/no_such_user', :status => :not_found 
265       end 
266     end
267   end
268
269 private 
270   #------------------------------------------------------------ 
271   # utility functions below. 
272   #------------------------------------------------------------   
273  
274   ##
275   # Render an OK response
276   def render_ok
277     if params[:format] == "js"
278       render :text => "osbResponse();", :content_type => "text/javascript" 
279     else
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
282     end
283   end
284
285   ##
286   # Get the maximum number of results to return
287   def result_limit
288     if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
289       params[:limit].to_i
290     else
291       100
292     end
293   end
294
295   ##
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)
299     if params[:closed]
300       closed_since = params[:closed].to_i
301     else
302       closed_since = 7
303     end
304         
305     if closed_since < 0
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}'))")
309     else
310       notes = notes.where("status = 'open'")
311     end
312
313     return notes
314   end
315
316   ##
317   # Add a comment to a note
318   def add_comment(note, text, name, event)
319     name = "NoName" if name.nil?
320
321     attributes = { :visible => true, :event => event, :body => text }
322
323     if @user  
324       attributes[:author_id] = @user.id
325       attributes[:author_name] = @user.display_name
326     else  
327       attributes[:author_ip] = request.remote_ip
328       attributes[:author_name] = name + " (a)"
329     end
330
331     comment = note.comments.create(attributes, :without_protection => true)
332
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
336       end
337     end
338   end
339 end