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