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