+ # from display name, pick up user id if one user's traces only
+ display_name = params[:display_name]
+ if display_name.present?
+ target_user = User.active.where(:display_name => display_name).first
+ if target_user.nil?
+ render_unknown_user display_name
+ return
+ end
+ end
+
+ # set title
+ @title = if target_user.nil?
+ t "trace.list.public_traces"
+ elsif current_user && current_user == target_user
+ t "trace.list.your_traces"
+ else
+ t "trace.list.public_traces_from", :user => target_user.display_name
+ end
+
+ @title += t "trace.list.tagged_with", :tags => params[:tag] if params[:tag]
+
+ # four main cases:
+ # 1 - all traces, logged in = all public traces + all user's (i.e + all mine)
+ # 2 - all traces, not logged in = all public traces
+ # 3 - user's traces, logged in as same user = all user's traces
+ # 4 - user's traces, not logged in as that user = all user's public traces
+ @traces = if target_user.nil? # all traces
+ if current_user
+ Trace.visible_to(current_user) # 1
+ else
+ Trace.visible_to_all # 2
+ end
+ elsif current_user && current_user == target_user
+ current_user.traces # 3 (check vs user id, so no join + can't pick up non-public traces by changing name)
+ else
+ target_user.traces.visible_to_all # 4
+ end
+
+ @traces = @traces.tagged(params[:tag]) if params[:tag]
+
+ @params = params.permit(:display_name, :tag)
+
+ @page = (params[:page] || 1).to_i
+ @page_size = 20
+
+ @traces = @traces.visible
+ @traces = @traces.order(:id => :desc)
+ @traces = @traces.offset((@page - 1) * @page_size)
+ @traces = @traces.limit(@page_size)
+ @traces = @traces.includes(:user, :tags)
+
+ # put together SET of tags across traces, for related links
+ tagset = {}
+ @traces.each do |trace|
+ trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
+ trace.tags.each do |tag|
+ tagset[tag.tag] = tag.tag
+ end
+ end
+
+ # final helper vars for view
+ @target_user = target_user
+ @display_name = target_user.display_name if target_user
+ @all_tags = tagset.values