1 # frozen_string_literal: true
3 class TracesController < ApplicationController
5 include PaginationMethods
9 before_action :authorize_web
10 before_action :set_locale
11 before_action :check_database_readable
15 before_action :check_database_writable, :only => [:new, :create, :edit, :destroy]
16 before_action :offline_warning, :only => [:mine, :show]
17 before_action :offline_redirect, :only => [:new, :create, :edit, :destroy]
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
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.find_by(:display_name => display_name)
27 render_unknown_user display_name
33 @title = if target_user.nil?
35 elsif current_user && current_user == target_user
38 t ".public_traces_from", :user => target_user.display_name
41 @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
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
50 Trace.visible_to(current_user) # 1
52 Trace.visible_to_all # 2
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)
57 target_user.traces.visible_to_all # 4
60 traces = traces.tagged(params[:tag]) if params[:tag]
62 traces = traces.visible
64 @params = params.permit(:display_name, :tag, :before, :after)
66 @traces = get_page_items(traces, :includes => [:user, :tags])
68 # final helper vars for view
69 @target_user = target_user
70 @legacy_traces_exist = current_user.traces.visible.exists?(:visibility => Trace::LEGACY_VISIBILITIES) if current_user
74 @trace = Trace.visible.find(params.expect(:id))
76 if @trace.public? || @trace.user == current_user
77 @title = t ".title", :name => @trace.name
79 flash[:error] = t ".trace_not_found"
80 redirect_to :action => "index"
82 rescue ActiveRecord::RecordNotFound
83 flash[:error] = t ".trace_not_found"
84 redirect_to :action => "index"
88 @title = t ".upload_trace"
89 @trace = Trace.new(:visibility => current_user.default_trace_visibility)
93 @trace = Trace.visible.find(params.expect(:id))
95 if current_user.nil? || @trace.user != current_user
98 @title = t ".title", :name => @trace.name
100 rescue ActiveRecord::RecordNotFound
105 @title = t ".upload_trace"
107 logger.info(params[:trace][:gpx_file].class.name)
109 if params[:trace][:gpx_file].respond_to?(:read)
110 @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
111 params[:trace][:description], params[:trace][:visibility])
114 flash[:notice] = t ".trace_uploaded"
115 flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
117 @trace.schedule_import
118 redirect_to :action => :index, :display_name => current_user.display_name
120 flash.now[:error] = t(".upload_failed") if @trace.valid?
122 render :action => "new"
125 @trace = Trace.new(:name => "Dummy",
126 :tagstring => params[:trace][:tagstring],
127 :description => params[:trace][:description],
128 :visibility => params[:trace][:visibility],
129 :inserted => false, :user => current_user,
130 :timestamp => Time.now.utc)
132 @trace.errors.add(:gpx_file, "can't be blank")
134 render :action => "new"
139 @trace = Trace.visible.find(params.expect(:id))
141 if current_user.nil? || @trace.user != current_user
143 elsif @trace.update(trace_params)
144 flash[:notice] = t ".updated"
145 redirect_to :action => "show", :display_name => current_user.display_name
147 @title = t "traces.edit.title", :name => @trace.name
148 render :action => "edit"
150 rescue ActiveRecord::RecordNotFound
155 trace = Trace.visible.find(params.expect(:id))
157 if current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
160 trace.visible = false
162 flash[:notice] = t ".scheduled_for_deletion"
163 trace.schedule_destruction
164 redirect_to :action => :index, :display_name => trace.user.display_name
166 rescue ActiveRecord::RecordNotFound
171 redirect_to :action => :index, :display_name => current_user.display_name
176 def do_create(file, tags, description, visibility)
177 # Sanitise the user's filename
178 name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
180 # Create the trace object
184 :description => description,
185 :visibility => visibility,
187 :user => current_user,
188 :timestamp => Time.now.utc,
192 # Save the trace object
194 # Finally save the user's preferred privacy level
195 if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
199 current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
207 flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
211 render :action => :offline if Settings.status == "gpx_offline"
215 params.expect(:trace => [:description, :tagstring, :visibility])