users_controller.rb 2.01 KB
Newer Older
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
1
class UsersController < ApplicationController
2 3
  skip_before_filter :authenticate_user!
  before_filter :set_user
4
  layout :determine_layout
5

Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
6
  def show
7
    @contributed_projects = contributed_projects.joined(@user).
8
      reject(&:forked?)
9

10
    @projects = @user.personal_projects.
11
      where(id: authorized_projects_ids).includes(:namespace)
12 13 14 15 16 17

    # Collect only groups common for both users
    @groups = @user.groups & GroupsFinder.new.execute(current_user)

    # Get user activity feed for projects common for both users
    @events = @user.recent_events.
18
      where(project_id: authorized_projects_ids).
19
      with_associations.limit(30)
20

21
    @title = @user.name
22
    @title_url = user_path(@user)
23

24 25 26 27 28 29 30
    respond_to do |format|
      format.html
      format.atom { render layout: false }
    end
  end

  def calendar
31
    calendar = contributions_calendar
32
    @timestamps = calendar.timestamps
33 34
    @starting_year = calendar.starting_year
    @starting_month = calendar.starting_month
35

36
    render 'calendar', layout: false
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
37
  end
38

39
  def calendar_activities
40 41
    @calendar_date = Date.parse(params[:date]) rescue nil
    @events = []
42

43 44
    if @calendar_date
      @events = contributions_calendar.events_by_date(@calendar_date)
45 46 47 48 49
    end

    render 'calendar_activities', layout: false
  end

50 51 52 53 54 55 56
  def determine_layout
    if current_user
      'navless'
    else
      'public_users'
    end
  end
57 58 59

  private

60
  def set_user
61 62 63 64 65 66
    @user = User.find_by_username!(params[:username])

    unless current_user || @user.public_profile?
      return authenticate_user!
    end
  end
67 68 69 70 71 72

  def authorized_projects_ids
    # Projects user can view
    @authorized_projects_ids ||=
      ProjectsFinder.new.execute(current_user).pluck(:id)
  end
73 74 75

  def contributed_projects
    @contributed_projects = Project.
76 77
      where(id: authorized_projects_ids & @user.contributed_projects_ids).
      includes(:namespace)
78 79 80 81
  end

  def contributions_calendar
    @contributions_calendar ||= Gitlab::ContributionsCalendar.
82
      new(contributed_projects.reject(&:forked?), @user)
83
  end
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
84
end