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