]> git.openstreetmap.org Git - rails.git/blob - app/controllers/notes_controller.rb
Merge remote-tracking branch 'upstream/pull/1955'
[rails.git] / app / controllers / notes_controller.rb
1 class NotesController < ApplicationController
2   layout "site", :only => [:mine]
3
4   skip_before_action :verify_authenticity_token, :except => [:mine]
5   before_action :check_api_readable
6   before_action :authorize_web, :only => [:mine]
7   before_action :setup_user_auth, :only => [:create, :comment, :show]
8   before_action :authorize, :only => [:close, :reopen, :destroy]
9   before_action :require_moderator, :only => [:destroy]
10   before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
11   before_action :require_allow_write_notes, :only => [:create, :comment, :close, :reopen, :destroy]
12   before_action :set_locale
13   around_action :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       raise OSM::APIBadUserInput, "No l was given" unless params[:l]
24       raise OSM::APIBadUserInput, "No r was given" unless params[:r]
25       raise OSM::APIBadUserInput, "No b was given" unless params[:b]
26       raise OSM::APIBadUserInput, "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     raise OSM::APIAccessDenied if Acl.no_note_comment(request.remote_ip)
57
58     # Check the arguments are sane
59     raise OSM::APIBadUserInput, "No lat was given" unless params[:lat]
60     raise OSM::APIBadUserInput, "No lon was given" unless params[:lon]
61     raise OSM::APIBadUserInput, "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       raise OSM::APIBadUserInput, "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     raise OSM::APIAccessDenied if Acl.no_note_comment(request.remote_ip)
93
94     # Check the arguments are sane
95     raise OSM::APIBadUserInput, "No id was given" unless params[:id]
96     raise OSM::APIBadUserInput, "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     raise OSM::APINotFoundError unless @note
105     raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
106     raise OSM::APINoteAlreadyClosedError, @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     raise OSM::APIBadUserInput, "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     raise OSM::APINotFoundError unless @note
133     raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible?
134     raise OSM::APINoteAlreadyClosedError, @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     raise OSM::APIBadUserInput, "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     raise OSM::APINotFoundError unless @note
163     raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user.moderator?
164     raise OSM::APINoteAlreadyOpenError, @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     raise OSM::APIBadUserInput, "No id was given" unless params[:id]
210
211     # Find the note and check it is valid
212     @note = Note.find(params[:id])
213     raise OSM::APINotFoundError unless @note
214     raise OSM::APIAlreadyDeletedError.new("note", @note.id) unless @note.visible? || current_user&.moderator?
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     raise OSM::APIBadUserInput, "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     raise OSM::APINotFoundError unless @note
238     raise 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     # Filter either by the name or the id of the user
259     if params[:display_name]
260       @user = User.find_by(:display_name => params[:display_name])
261       raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless @user
262     elsif params[:id]
263       @user = User.find_by(:id => params[:id])
264       raise OSM::APIBadUserInput, "User #{params[:id]} not known" unless @user
265     end
266
267     @notes = closed_condition(Note.all)
268
269     @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user }) if @user
270
271     # Filter by a given string
272     @notes = @notes.joins(:comments).where("to_tsvector('english', note_comments.body) @@ plainto_tsquery('english', ?)", params[:q]) if params[:q]
273
274     # Filter by a given start date and an optional end date
275     if params[:from]
276       begin
277         from = Time.parse(params[:from])
278       rescue ArgumentError
279         raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
280       end
281
282       begin
283         to = if params[:to]
284                Time.parse(params[:to])
285              else
286                Time.now
287              end
288       rescue ArgumentError
289         raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
290       end
291       @notes = @notes.where(:created_at => from..to)
292     end
293
294     # Find the notes we want to return
295     @notes = @notes.order("updated_at DESC").limit(result_limit).preload(:comments)
296
297     # Render the result
298     respond_to do |format|
299       format.rss { render :action => :index }
300       format.xml { render :action => :index }
301       format.json { render :action => :index }
302       format.gpx { render :action => :index }
303     end
304   end
305
306   ##
307   # Display a list of notes by a specified user
308   def mine
309     if params[:display_name]
310       if @user = User.active.find_by(:display_name => params[:display_name])
311         @params = params.permit(:display_name)
312         @title = t "notes.mine.title", :user => @user.display_name
313         @heading = t "notes.mine.heading", :user => @user.display_name
314         @description = t "notes.mine.subheading", :user => render_to_string(:partial => "user", :object => @user)
315         @page = (params[:page] || 1).to_i
316         @page_size = 10
317         @notes = @user.notes
318         @notes = @notes.visible unless current_user&.moderator?
319         @notes = @notes.order("updated_at DESC, id").distinct.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author).to_a
320       else
321         @title = t "users.no_such_user.title"
322         @not_found_user = params[:display_name]
323
324         render :template => "users/no_such_user", :status => :not_found
325       end
326     end
327   end
328
329   private
330
331   #------------------------------------------------------------
332   # utility functions below.
333   #------------------------------------------------------------
334
335   ##
336   # Get the maximum number of results to return
337   def result_limit
338     if params[:limit]
339       if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
340         params[:limit].to_i
341       else
342         raise OSM::APIBadUserInput, "Note limit must be between 1 and 10000"
343       end
344     else
345       100
346     end
347   end
348
349   ##
350   # Generate a condition to choose which notes we want based
351   # on their status and the user's request parameters
352   def closed_condition(notes)
353     closed_since = if params[:closed]
354                      params[:closed].to_i
355                    else
356                      7
357                    end
358
359     if closed_since.negative?
360       notes.where.not(:status => "hidden")
361     elsif closed_since.positive?
362       notes.where(:status => "open")
363            .or(notes.where(:status => "closed")
364                     .where(notes.arel_table[:closed_at].gt(Time.now - closed_since.days)))
365     else
366       notes.where(:status => "open")
367     end
368   end
369
370   ##
371   # Add a comment to a note
372   def add_comment(note, text, event, notify = true)
373     attributes = { :visible => true, :event => event, :body => text }
374
375     if current_user
376       attributes[:author_id] = current_user.id
377     else
378       attributes[:author_ip] = request.remote_ip
379     end
380
381     comment = note.comments.create!(attributes)
382
383     note.comments.map(&:author).uniq.each do |user|
384       Notifier.note_comment_notification(comment, user).deliver_now if notify && user && user != current_user && user.visible?
385     end
386   end
387 end