]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces_controller.rb
Make profiles/images#update handle being given no image
[rails.git] / app / controllers / traces_controller.rb
1 # frozen_string_literal: true
2
3 class TracesController < ApplicationController
4   include UserMethods
5   include PaginationMethods
6
7   layout :site_layout
8
9   before_action :authorize_web
10   before_action :set_locale
11   before_action :check_database_readable
12
13   authorize_resource
14
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]
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.find_by(:display_name => display_name)
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_gps_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     traces = traces.visible
63
64     @params = params.permit(:display_name, :tag, :before, :after)
65
66     @traces = get_page_items(traces, :includes => [:user, :tags])
67
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
71   end
72
73   def show
74     @trace = Trace.visible.find(params.expect(:id))
75
76     if @trace.public? || @trace.user == current_user
77       @title = t ".title", :name => @trace.name
78     else
79       flash[:error] = t ".trace_not_found"
80       redirect_to :action => "index"
81     end
82   rescue ActiveRecord::RecordNotFound
83     flash[:error] = t ".trace_not_found"
84     redirect_to :action => "index"
85   end
86
87   def new
88     @title = t ".upload_trace"
89     @trace = Trace.new(:visibility => current_user.default_trace_visibility)
90   end
91
92   def edit
93     @trace = Trace.visible.find(params.expect(:id))
94
95     if current_user.nil? || @trace.user != current_user
96       head :forbidden
97     else
98       @title = t ".title", :name => @trace.name
99     end
100   rescue ActiveRecord::RecordNotFound
101     head :not_found
102   end
103
104   def create
105     @title = t ".upload_trace"
106
107     logger.info(params[:trace][:gpx_file].class.name)
108
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])
112
113       if @trace.id
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
116
117         @trace.schedule_import
118         redirect_to :action => :index, :display_name => current_user.display_name
119       else
120         flash.now[:error] = t(".upload_failed") if @trace.valid?
121
122         render :action => "new"
123       end
124     else
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)
131       @trace.valid?
132       @trace.errors.add(:gpx_file, "can't be blank")
133
134       render :action => "new"
135     end
136   end
137
138   def update
139     @trace = Trace.visible.find(params.expect(:id))
140
141     if current_user.nil? || @trace.user != current_user
142       head :forbidden
143     elsif @trace.update(trace_params)
144       flash[:notice] = t ".updated"
145       redirect_to :action => "show", :display_name => current_user.display_name
146     else
147       @title = t "traces.edit.title", :name => @trace.name
148       render :action => "edit"
149     end
150   rescue ActiveRecord::RecordNotFound
151     head :not_found
152   end
153
154   def destroy
155     trace = Trace.visible.find(params.expect(:id))
156
157     if current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
158       head :forbidden
159     else
160       trace.visible = false
161       trace.save
162       flash[:notice] = t ".scheduled_for_deletion"
163       trace.schedule_destruction
164       redirect_to :action => :index, :display_name => trace.user.display_name
165     end
166   rescue ActiveRecord::RecordNotFound
167     head :not_found
168   end
169
170   def mine
171     redirect_to :action => :index, :display_name => current_user.display_name
172   end
173
174   private
175
176   def do_create(file, tags, description, visibility)
177     # Sanitise the user's filename
178     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
179
180     # Create the trace object
181     trace = Trace.new(
182       :name => name,
183       :tagstring => tags,
184       :description => description,
185       :visibility => visibility,
186       :inserted => false,
187       :user => current_user,
188       :timestamp => Time.now.utc,
189       :file => file
190     )
191
192     # Save the trace object
193     if trace.save
194       # Finally save the user's preferred privacy level
195       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
196         pref.v = visibility
197         pref.save
198       else
199         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
200       end
201     end
202
203     trace
204   end
205
206   def offline_warning
207     flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
208   end
209
210   def offline_redirect
211     render :action => :offline if Settings.status == "gpx_offline"
212   end
213
214   def trace_params
215     params.expect(:trace => [:description, :tagstring, :visibility])
216   end
217 end