1 class TracesController < ApplicationController
 
   3   include PaginationMethods
 
   7   before_action :authorize_web
 
   8   before_action :set_locale
 
   9   before_action :check_database_readable
 
  13   before_action :check_database_writable, :only => [:new, :create, :edit, :destroy]
 
  14   before_action :offline_warning, :only => [:mine, :show]
 
  15   before_action :offline_redirect, :only => [:new, :create, :edit, :destroy]
 
  17   # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.).
 
  18   #  target_user - if set, specifies the user to fetch traces for.  if not set will fetch all traces
 
  20     # from display name, pick up user id if one user's traces only
 
  21     display_name = params[:display_name]
 
  22     if display_name.present?
 
  23       target_user = User.active.find_by(:display_name => display_name)
 
  25         render_unknown_user display_name
 
  31     @title = if target_user.nil?
 
  33              elsif current_user && current_user == target_user
 
  36                t ".public_traces_from", :user => target_user.display_name
 
  39     @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
 
  42     # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
 
  43     # 2 - all traces, not logged in = all public traces
 
  44     # 3 - user's traces, logged in as same user = all user's traces
 
  45     # 4 - user's traces, not logged in as that user = all user's public traces
 
  46     traces = if target_user.nil? # all traces
 
  48                  Trace.visible_to(current_user) # 1
 
  50                  Trace.visible_to_all # 2
 
  52              elsif current_user && current_user == target_user
 
  53                current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
 
  55                target_user.traces.visible_to_all # 4
 
  58     traces = traces.tagged(params[:tag]) if params[:tag]
 
  60     traces = traces.visible
 
  62     @params = params.permit(:display_name, :tag, :before, :after)
 
  64     @traces, @newer_traces_id, @older_traces_id = get_page_items(traces, :includes => [:user, :tags])
 
  66     # final helper vars for view
 
  67     @target_user = target_user
 
  69     render :partial => "page" if turbo_frame_request_id == "pagination"
 
  73     @trace = Trace.visible.find(params[:id])
 
  75     if @trace.public? || @trace.user == current_user
 
  76       @title = t ".title", :name => @trace.name
 
  78       flash[:error] = t ".trace_not_found"
 
  79       redirect_to :action => "index"
 
  81   rescue ActiveRecord::RecordNotFound
 
  82     flash[:error] = t ".trace_not_found"
 
  83     redirect_to :action => "index"
 
  87     @title = t ".upload_trace"
 
  88     @trace = Trace.new(:visibility => default_visibility)
 
  92     @trace = Trace.visible.find(params[:id])
 
  94     if current_user.nil? || @trace.user != current_user
 
  97       @title = t ".title", :name => @trace.name
 
  99   rescue ActiveRecord::RecordNotFound
 
 104     @title = t ".upload_trace"
 
 106     logger.info(params[:trace][:gpx_file].class.name)
 
 108     if params[:trace][:gpx_file].respond_to?(:read)
 
 109       @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
 
 110                          params[:trace][:description], params[:trace][:visibility])
 
 113         flash[:notice] = t ".trace_uploaded"
 
 114         flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
 
 116         @trace.schedule_import
 
 117         redirect_to :action => :index, :display_name => current_user.display_name
 
 119         flash.now[:error] = t(".upload_failed") if @trace.valid?
 
 121         render :action => "new"
 
 124       @trace = Trace.new(:name => "Dummy",
 
 125                          :tagstring => params[:trace][:tagstring],
 
 126                          :description => params[:trace][:description],
 
 127                          :visibility => params[:trace][:visibility],
 
 128                          :inserted => false, :user => current_user,
 
 129                          :timestamp => Time.now.utc)
 
 131       @trace.errors.add(:gpx_file, "can't be blank")
 
 133       render :action => "new"
 
 138     @trace = Trace.visible.find(params[:id])
 
 140     if current_user.nil? || @trace.user != current_user
 
 142     elsif @trace.update(trace_params)
 
 143       flash[:notice] = t ".updated"
 
 144       redirect_to :action => "show", :display_name => current_user.display_name
 
 146       @title = t "traces.edit.title", :name => @trace.name
 
 147       render :action => "edit"
 
 149   rescue ActiveRecord::RecordNotFound
 
 154     trace = Trace.visible.find(params[:id])
 
 156     if current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
 
 159       trace.visible = false
 
 161       flash[:notice] = t ".scheduled_for_deletion"
 
 162       trace.schedule_destruction
 
 163       redirect_to :action => :index, :display_name => trace.user.display_name
 
 165   rescue ActiveRecord::RecordNotFound
 
 170     redirect_to :action => :index, :display_name => current_user.display_name
 
 175   def do_create(file, tags, description, visibility)
 
 176     # Sanitise the user's filename
 
 177     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
 
 179     # Create the trace object
 
 183       :description => description,
 
 184       :visibility => visibility,
 
 186       :user => current_user,
 
 187       :timestamp => Time.now.utc,
 
 191     # Save the trace object
 
 193       # Finally save the user's preferred privacy level
 
 194       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
 
 198         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
 
 206     flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
 
 210     render :action => :offline if Settings.status == "gpx_offline"
 
 213   def default_visibility
 
 214     visibility = current_user.preferences.find_by(:k => "gps.trace.visibility")
 
 218     elsif current_user.preferences.find_by(:k => "gps.trace.public", :v => "default").nil?
 
 226     params.expect(:trace => [:description, :tagstring, :visibility])