]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces_controller.rb
Suppress changeset pagination for inactive users
[rails.git] / app / controllers / traces_controller.rb
1 class TracesController < ApplicationController
2   layout "site", :except => :georss
3
4   skip_before_action :verify_authenticity_token, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
5   before_action :authorize_web
6   before_action :set_locale
7   before_action :require_user, :only => [:mine, :new, :create, :edit, :delete]
8   before_action :authorize, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
9   before_action :check_database_readable, :except => [:api_read, :api_data]
10   before_action :check_database_writable, :only => [:new, :create, :edit, :delete, :api_create, :api_update, :api_delete]
11   before_action :check_api_readable, :only => [:api_read, :api_data]
12   before_action :check_api_writable, :only => [:api_create, :api_update, :api_delete]
13   before_action :require_allow_read_gpx, :only => [:api_read, :api_data]
14   before_action :require_allow_write_gpx, :only => [:api_create, :api_update, :api_delete]
15   before_action :offline_warning, :only => [:mine, :show]
16   before_action :offline_redirect, :only => [:new, :create, :edit, :delete, :data, :api_create, :api_delete, :api_data]
17   around_action :api_call_handle_error, :only => [:api_create, :api_read, :api_update, :api_delete, :api_data]
18
19   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
20   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
21   def index
22     # from display name, pick up user id if one user's traces only
23     display_name = params[:display_name]
24     if display_name.present?
25       target_user = User.active.where(:display_name => display_name).first
26       if target_user.nil?
27         render_unknown_user display_name
28         return
29       end
30     end
31
32     # set title
33     @title = if target_user.nil?
34                t ".public_traces"
35              elsif current_user && current_user == target_user
36                t ".my_traces"
37              else
38                t ".public_traces_from", :user => target_user.display_name
39              end
40
41     @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
42
43     # four main cases:
44     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
45     # 2 - all traces, not logged in = all public traces
46     # 3 - user's traces, logged in as same user = all user's traces
47     # 4 - user's traces, not logged in as that user = all user's public traces
48     @traces = if target_user.nil? # all traces
49                 if current_user
50                   Trace.visible_to(current_user) # 1
51                 else
52                   Trace.visible_to_all # 2
53                 end
54               elsif current_user && current_user == target_user
55                 current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
56               else
57                 target_user.traces.visible_to_all # 4
58               end
59
60     @traces = @traces.tagged(params[:tag]) if params[:tag]
61
62     @params = params.permit(:display_name, :tag)
63
64     @page = (params[:page] || 1).to_i
65     @page_size = 20
66
67     @traces = @traces.visible
68     @traces = @traces.order(:id => :desc)
69     @traces = @traces.offset((@page - 1) * @page_size)
70     @traces = @traces.limit(@page_size)
71     @traces = @traces.includes(:user, :tags)
72
73     # put together SET of tags across traces, for related links
74     tagset = {}
75     @traces.each do |trace|
76       trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
77       trace.tags.each do |tag|
78         tagset[tag.tag] = tag.tag
79       end
80     end
81
82     # final helper vars for view
83     @target_user = target_user
84     @display_name = target_user.display_name if target_user
85     @all_tags = tagset.values
86   end
87
88   def mine
89     redirect_to :action => :index, :display_name => current_user.display_name
90   end
91
92   def show
93     @trace = Trace.find(params[:id])
94
95     if @trace&.visible? &&
96        (@trace&.public? || @trace&.user == current_user)
97       @title = t ".title", :name => @trace.name
98     else
99       flash[:error] = t ".trace_not_found"
100       redirect_to :action => "index"
101     end
102   rescue ActiveRecord::RecordNotFound
103     flash[:error] = t ".trace_not_found"
104     redirect_to :action => "index"
105   end
106
107   def new
108     @title = t ".upload_trace"
109     @trace = Trace.new(:visibility => default_visibility)
110   end
111
112   def create
113     @title = t ".upload_trace"
114
115     logger.info(params[:trace][:gpx_file].class.name)
116
117     if params[:trace][:gpx_file].respond_to?(:read)
118       begin
119         @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
120                            params[:trace][:description], params[:trace][:visibility])
121       rescue StandardError => ex
122         logger.debug ex
123       end
124
125       if @trace.id
126         flash[:notice] = t ".trace_uploaded"
127         flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
128
129         redirect_to :action => :index, :display_name => current_user.display_name
130       else
131         flash[:error] = t("traces.create.upload_failed") if @trace.valid?
132
133         render :action => "new"
134       end
135     else
136       @trace = Trace.new(:name => "Dummy",
137                          :tagstring => params[:trace][:tagstring],
138                          :description => params[:trace][:description],
139                          :visibility => params[:trace][:visibility],
140                          :inserted => false, :user => current_user,
141                          :timestamp => Time.now.getutc)
142       @trace.valid?
143       @trace.errors.add(:gpx_file, "can't be blank")
144
145       render :action => "new"
146     end
147   end
148
149   def data
150     trace = Trace.find(params[:id])
151
152     if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
153       if Acl.no_trace_download(request.remote_ip)
154         head :forbidden
155       elsif request.format == Mime[:xml]
156         send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
157       elsif request.format == Mime[:gpx]
158         send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
159       else
160         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
161       end
162     else
163       head :not_found
164     end
165   rescue ActiveRecord::RecordNotFound
166     head :not_found
167   end
168
169   def edit
170     @trace = Trace.find(params[:id])
171
172     if !@trace.visible?
173       head :not_found
174     elsif current_user.nil? || @trace.user != current_user
175       head :forbidden
176     else
177       @title = t ".title", :name => @trace.name
178     end
179   rescue ActiveRecord::RecordNotFound
180     head :not_found
181   end
182
183   def update
184     @trace = Trace.find(params[:id])
185
186     if !@trace.visible?
187       head :not_found
188     elsif current_user.nil? || @trace.user != current_user
189       head :forbidden
190     elsif @trace.update(trace_params)
191       flash[:notice] = t ".updated"
192       redirect_to :action => "show", :display_name => current_user.display_name
193     else
194       @title = t ".title", :name => @trace.name
195       render :action => "edit"
196     end
197   rescue ActiveRecord::RecordNotFound
198     head :not_found
199   end
200
201   def delete
202     trace = Trace.find(params[:id])
203
204     if !trace.visible?
205       head :not_found
206     elsif current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
207       head :forbidden
208     else
209       trace.visible = false
210       trace.save
211       flash[:notice] = t ".scheduled_for_deletion"
212       redirect_to :action => :index, :display_name => trace.user.display_name
213     end
214   rescue ActiveRecord::RecordNotFound
215     head :not_found
216   end
217
218   def georss
219     @traces = Trace.visible_to_all.visible
220
221     @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
222
223     @traces = @traces.tagged(params[:tag]) if params[:tag]
224     @traces = @traces.order("timestamp DESC")
225     @traces = @traces.limit(20)
226     @traces = @traces.includes(:user)
227   end
228
229   def picture
230     trace = Trace.find(params[:id])
231
232     if trace.visible? && trace.inserted?
233       if trace.public? || (current_user && current_user == trace.user)
234         expires_in 7.days, :private => !trace.public?, :public => trace.public?
235         send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
236       else
237         head :forbidden
238       end
239     else
240       head :not_found
241     end
242   rescue ActiveRecord::RecordNotFound
243     head :not_found
244   end
245
246   def icon
247     trace = Trace.find(params[:id])
248
249     if trace.visible? && trace.inserted?
250       if trace.public? || (current_user && current_user == trace.user)
251         expires_in 7.days, :private => !trace.public?, :public => trace.public?
252         send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
253       else
254         head :forbidden
255       end
256     else
257       head :not_found
258     end
259   rescue ActiveRecord::RecordNotFound
260     head :not_found
261   end
262
263   def api_read
264     trace = Trace.visible.find(params[:id])
265
266     if trace.public? || trace.user == current_user
267       render :xml => trace.to_xml.to_s
268     else
269       head :forbidden
270     end
271   end
272
273   def api_update
274     trace = Trace.visible.find(params[:id])
275
276     if trace.user == current_user
277       trace.update_from_xml(request.raw_post)
278       trace.save!
279
280       head :ok
281     else
282       head :forbidden
283     end
284   end
285
286   def api_delete
287     trace = Trace.visible.find(params[:id])
288
289     if trace.user == current_user
290       trace.visible = false
291       trace.save!
292
293       head :ok
294     else
295       head :forbidden
296     end
297   end
298
299   def api_data
300     trace = Trace.visible.find(params[:id])
301
302     if trace.public? || trace.user == current_user
303       if request.format == Mime[:xml]
304         send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
305       elsif request.format == Mime[:gpx]
306         send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
307       else
308         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
309       end
310     else
311       head :forbidden
312     end
313   end
314
315   def api_create
316     tags = params[:tags] || ""
317     description = params[:description] || ""
318     visibility = params[:visibility]
319
320     if visibility.nil?
321       visibility = if params[:public]&.to_i&.nonzero?
322                      "public"
323                    else
324                      "private"
325                    end
326     end
327
328     if params[:file].respond_to?(:read)
329       trace = do_create(params[:file], tags, description, visibility)
330
331       if trace.id
332         render :plain => trace.id.to_s
333       elsif trace.valid?
334         head :internal_server_error
335       else
336         head :bad_request
337       end
338     else
339       head :bad_request
340     end
341   end
342
343   private
344
345   def do_create(file, tags, description, visibility)
346     # Sanitise the user's filename
347     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
348
349     # Get a temporary filename...
350     filename = "/tmp/#{rand}"
351
352     # ...and save the uploaded file to that location
353     File.open(filename, "wb") { |f| f.write(file.read) }
354
355     # Create the trace object, falsely marked as already
356     # inserted to stop the import daemon trying to load it
357     trace = Trace.new(
358       :name => name,
359       :tagstring => tags,
360       :description => description,
361       :visibility => visibility,
362       :inserted => true,
363       :user => current_user,
364       :timestamp => Time.now.getutc
365     )
366
367     if trace.valid?
368       Trace.transaction do
369         begin
370           # Save the trace object
371           trace.save!
372
373           # Rename the temporary file to the final name
374           FileUtils.mv(filename, trace.trace_name)
375         rescue StandardError
376           # Remove the file as we have failed to update the database
377           FileUtils.rm_f(filename)
378
379           # Pass the exception on
380           raise
381         end
382
383         begin
384           # Clear the inserted flag to make the import daemon load the trace
385           trace.inserted = false
386           trace.save!
387         rescue StandardError
388           # Remove the file as we have failed to update the database
389           FileUtils.rm_f(trace.trace_name)
390
391           # Pass the exception on
392           raise
393         end
394       end
395     end
396
397     # Finally save the user's preferred privacy level
398     if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
399       pref.v = visibility
400       pref.save
401     else
402       current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
403     end
404
405     trace
406   end
407
408   def offline_warning
409     flash.now[:warning] = t "traces.offline_warning.message" if STATUS == :gpx_offline
410   end
411
412   def offline_redirect
413     redirect_to :action => :offline if STATUS == :gpx_offline
414   end
415
416   def default_visibility
417     visibility = current_user.preferences.where(:k => "gps.trace.visibility").first
418
419     if visibility
420       visibility.v
421     elsif current_user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
422       "private"
423     else
424       "public"
425     end
426   end
427
428   def trace_params
429     params.require(:trace).permit(:description, :tagstring, :visibility)
430   end
431 end