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