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