]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces_controller.rb
Merge remote-tracking branch 'upstream/pull/4705'
[rails.git] / app / controllers / traces_controller.rb
1 class TracesController < ApplicationController
2   include UserMethods
3   include PaginationMethods
4
5   layout "site", :except => :georss
6
7   before_action :authorize_web
8   before_action :set_locale
9   before_action :check_database_readable
10
11   authorize_resource
12
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, :data]
16
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
19   def index
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)
24       if target_user.nil?
25         render_unknown_user display_name
26         return
27       end
28     end
29
30     # set title
31     @title = if target_user.nil?
32                t ".public_traces"
33              elsif current_user && current_user == target_user
34                t ".my_gps_traces"
35              else
36                t ".public_traces_from", :user => target_user.display_name
37              end
38
39     @title += t ".tagged_with", :tags => params[:tag] if params[:tag]
40
41     # four main cases:
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
47                if current_user
48                  Trace.visible_to(current_user) # 1
49                else
50                  Trace.visible_to_all # 2
51                end
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)
54              else
55                target_user.traces.visible_to_all # 4
56              end
57
58     traces = traces.tagged(params[:tag]) if params[:tag]
59
60     traces = traces.visible
61
62     @params = params.permit(:display_name, :tag, :before, :after)
63
64     @traces, @newer_traces_id, @older_traces_id = get_page_items(traces, :includes => [:user, :tags])
65
66     # final helper vars for view
67     @target_user = target_user
68   end
69
70   def show
71     @trace = Trace.visible.find(params[:id])
72
73     if @trace.public? || @trace.user == current_user
74       @title = t ".title", :name => @trace.name
75     else
76       flash[:error] = t ".trace_not_found"
77       redirect_to :action => "index"
78     end
79   rescue ActiveRecord::RecordNotFound
80     flash[:error] = t ".trace_not_found"
81     redirect_to :action => "index"
82   end
83
84   def new
85     @title = t ".upload_trace"
86     @trace = Trace.new(:visibility => default_visibility)
87   end
88
89   def edit
90     @trace = Trace.visible.find(params[:id])
91
92     if current_user.nil? || @trace.user != current_user
93       head :forbidden
94     else
95       @title = t ".title", :name => @trace.name
96     end
97   rescue ActiveRecord::RecordNotFound
98     head :not_found
99   end
100
101   def create
102     @title = t ".upload_trace"
103
104     logger.info(params[:trace][:gpx_file].class.name)
105
106     if params[:trace][:gpx_file].respond_to?(:read)
107       @trace = do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
108                          params[:trace][:description], params[:trace][:visibility])
109
110       if @trace.id
111         flash[:notice] = t ".trace_uploaded"
112         flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4
113
114         @trace.schedule_import
115         redirect_to :action => :index, :display_name => current_user.display_name
116       else
117         flash[:error] = t(".upload_failed") if @trace.valid?
118
119         render :action => "new"
120       end
121     else
122       @trace = Trace.new(:name => "Dummy",
123                          :tagstring => params[:trace][:tagstring],
124                          :description => params[:trace][:description],
125                          :visibility => params[:trace][:visibility],
126                          :inserted => false, :user => current_user,
127                          :timestamp => Time.now.utc)
128       @trace.valid?
129       @trace.errors.add(:gpx_file, "can't be blank")
130
131       render :action => "new"
132     end
133   end
134
135   def update
136     @trace = Trace.visible.find(params[:id])
137
138     if current_user.nil? || @trace.user != current_user
139       head :forbidden
140     elsif @trace.update(trace_params)
141       flash[:notice] = t ".updated"
142       redirect_to :action => "show", :display_name => current_user.display_name
143     else
144       @title = t ".title", :name => @trace.name
145       render :action => "edit"
146     end
147   rescue ActiveRecord::RecordNotFound
148     head :not_found
149   end
150
151   def destroy
152     trace = Trace.visible.find(params[:id])
153
154     if current_user.nil? || (trace.user != current_user && !current_user.administrator? && !current_user.moderator?)
155       head :forbidden
156     else
157       trace.visible = false
158       trace.save
159       flash[:notice] = t ".scheduled_for_deletion"
160       trace.schedule_destruction
161       redirect_to :action => :index, :display_name => trace.user.display_name
162     end
163   rescue ActiveRecord::RecordNotFound
164     head :not_found
165   end
166
167   def mine
168     redirect_to :action => :index, :display_name => current_user.display_name
169   end
170
171   def data
172     trace = Trace.visible.find(params[:id])
173
174     if trace.public? || (current_user && current_user == trace.user)
175       if Acl.no_trace_download(request.remote_ip)
176         head :forbidden
177       elsif request.format == Mime[:xml]
178         send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
179       elsif request.format == Mime[:gpx]
180         send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
181       elsif trace.file.attached?
182         redirect_to rails_blob_path(trace.file, :disposition => "attachment")
183       else
184         send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
185       end
186     else
187       head :not_found
188     end
189   rescue ActiveRecord::RecordNotFound
190     head :not_found
191   end
192
193   def georss
194     @traces = Trace.visible_to_all.visible
195
196     @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] }) if params[:display_name]
197
198     @traces = @traces.tagged(params[:tag]) if params[:tag]
199     @traces = @traces.order("timestamp DESC")
200     @traces = @traces.limit(20)
201     @traces = @traces.includes(:user)
202   end
203
204   private
205
206   def do_create(file, tags, description, visibility)
207     # Sanitise the user's filename
208     name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
209
210     # Create the trace object
211     trace = Trace.new(
212       :name => name,
213       :tagstring => tags,
214       :description => description,
215       :visibility => visibility,
216       :inserted => false,
217       :user => current_user,
218       :timestamp => Time.now.utc,
219       :file => file
220     )
221
222     # Save the trace object
223     if trace.save
224       # Finally save the user's preferred privacy level
225       if pref = current_user.preferences.find_by(:k => "gps.trace.visibility")
226         pref.v = visibility
227         pref.save
228       else
229         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
230       end
231     end
232
233     trace
234   end
235
236   def offline_warning
237     flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
238   end
239
240   def offline_redirect
241     render :action => :offline if Settings.status == "gpx_offline"
242   end
243
244   def default_visibility
245     visibility = current_user.preferences.find_by(:k => "gps.trace.visibility")
246
247     if visibility
248       visibility.v
249     elsif current_user.preferences.find_by(:k => "gps.trace.public", :v => "default").nil?
250       "private"
251     else
252       "public"
253     end
254   end
255
256   def trace_params
257     params.require(:trace).permit(:description, :tagstring, :visibility)
258   end
259 end