]> git.openstreetmap.org Git - rails.git/blob - app/controllers/notes_controller.rb
fe3615d7a0965e4290a21b5eb47fc00c9203ac63
[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     # Send an OK response
91     render_ok
92   end
93
94   ##
95   # Add a comment to an existing note
96   def comment
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]
100
101     # Extract the arguments
102     id = params[:id].to_i
103     comment = params[:text]
104     name = params[:name] or "NoName"
105
106     # Find the note and check it is valid
107     note = Note.find(id)
108     raise OSM::APINotFoundError unless note
109     raise OSM::APIAlreadyDeletedError unless note.visible?
110
111     # Add a comment to the note
112     Note.transaction do
113       add_comment(note, comment, name, "commented")
114     end
115
116     # Send an OK response
117     render_ok
118   end
119
120   ##
121   # Close a note
122   def close
123     # Check the arguments are sane
124     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
125
126     # Extract the arguments
127     id = params[:id].to_i
128     name = params[:name]
129
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?
134
135     # Close the note and add a comment
136     Note.transaction do
137       note.close
138
139       add_comment(note, nil, name, "closed")
140     end
141
142     # Send an OK response
143     render_ok
144   end 
145
146   ##
147   # Get a feed of recent notes and comments
148   def feed
149     # Get any conditions that need to be applied
150     notes = closed_condition(Note.scoped)
151
152     # Process any bbox
153     if params[:bbox]
154       bbox = BoundingBox.from_bbox_params(params)
155
156       bbox.check_boundaries
157       bbox.check_size(MAX_NOTE_REQUEST_AREA)
158
159       notes = notes.bbox(bbox)
160     end
161
162     # Find the comments we want to return
163     @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
164
165     # Render the result
166     respond_to do |format|
167       format.rss
168     end
169   end
170
171   ##
172   # Read a note
173   def show
174     # Check the arguments are sane
175     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
176
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?
181     
182     # Render the result
183     respond_to do |format|
184       format.xml
185       format.rss
186       format.json
187       format.gpx
188     end
189   end
190
191   ##
192   # Delete (hide) a note
193   def destroy
194     # Check the arguments are sane
195     raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
196
197     # Extract the arguments
198     id = params[:id].to_i
199     name = params[:name]
200
201     # Find the note and check it is valid
202     note = Note.find(id)
203     raise OSM::APINotFoundError unless note
204     raise OSM::APIAlreadyDeletedError unless note.visible?
205
206     # Mark the note as hidden
207     Note.transaction do
208       note.status = "hidden"
209       note.save
210
211       add_comment(note, nil, name, "hidden")
212     end
213
214     # Render the result
215     render :text => "ok\n", :content_type => "text/html" 
216   end
217
218   ##
219   # Return a list of notes matching a given string
220   def search
221     # Check the arguments are sane
222     raise OSM::APIBadUserInput.new("No query string was given") unless params[:q]
223
224     # Get any conditions that need to be applied
225     @notes = closed_condition(Note.scoped)
226     @notes = @notes.joins(:comments).where("note_comments.body ~ ?", params[:q])
227
228     # Find the notes we want to return
229     @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
230
231     # Render the result
232     respond_to do |format|
233       format.rss { render :action => :index }
234       format.xml { render :action => :index }
235       format.json { render :action => :index }
236       format.gpx { render :action => :index }
237     end
238   end
239
240   ##
241   # Display a list of notes by a specified user
242   def mine
243     if params[:display_name] 
244       if @this_user = User.active.find_by_display_name(params[:display_name])
245         @title =  t 'note.mine.title', :user => @this_user.display_name 
246         @heading =  t 'note.mine.heading', :user => @this_user.display_name 
247         @description = t 'note.mine.description', :user => render_to_string(:partial => "user", :object => @this_user)
248         @page = (params[:page] || 1).to_i 
249         @page_size = 10
250         @notes = @this_user.notes.order("updated_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author)
251       else
252         @title = t 'user.no_such_user.title' 
253         @not_found_user = params[:display_name] 
254
255         render :template => 'user/no_such_user', :status => :not_found 
256       end 
257     end
258   end
259
260 private 
261   #------------------------------------------------------------ 
262   # utility functions below. 
263   #------------------------------------------------------------   
264  
265   ##
266   # Render an OK response
267   def render_ok
268     if params[:format] == "js"
269       render :text => "osbResponse();", :content_type => "text/javascript" 
270     else
271       render :text => "ok " + @note.id.to_s + "\n", :content_type => "text/plain" if @note
272       render :text => "ok\n", :content_type => "text/plain" unless @note
273     end
274   end
275
276   ##
277   # Get the maximum number of results to return
278   def result_limit
279     if params[:limit] and params[:limit].to_i > 0 and params[:limit].to_i < 10000
280       params[:limit].to_i
281     else
282       100
283     end
284   end
285
286   ##
287   # Generate a condition to choose which bugs we want based
288   # on their status and the user's request parameters
289   def closed_condition(notes)
290     if params[:closed]
291       closed_since = params[:closed].to_i
292     else
293       closed_since = 7
294     end
295         
296     if closed_since < 0
297       notes = notes.where("status != 'hidden'")
298     elsif closed_since > 0
299       notes = notes.where("(status = 'open' OR (status = 'closed' AND closed_at > '#{Time.now - closed_since.days}'))")
300     else
301       notes = notes.where("status = 'open'")
302     end
303
304     return notes
305   end
306
307   ##
308   # Add a comment to a note
309   def add_comment(note, text, name, event)
310     name = "NoName" if name.nil?
311
312     attributes = { :visible => true, :event => event, :body => text }
313
314     if @user  
315       attributes[:author_id] = @user.id
316       attributes[:author_name] = @user.display_name
317     else  
318       attributes[:author_ip] = request.remote_ip
319       attributes[:author_name] = name + " (a)"
320     end
321
322     note.comments.create(attributes, :without_protection => true)
323
324     note.comments.map { |c| c.author }.uniq.each do |user|
325       if user and user != @user
326         Notifier.deliver_note_comment_notification(comment, user)
327       end
328     end
329   end
330 end