users_controller.rb 2.23 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

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

16
    @title = @user.name
17
    @title_url = user_path(@user)
18

19 20
    respond_to do |format|
      format.html
21 22 23 24 25 26

      format.atom do
        load_events
        render layout: false
      end

27 28 29 30
      format.json do
        load_events
        pager_json("events/_events", @events.count)
      end
31 32 33 34
    end
  end

  def calendar
35
    calendar = contributions_calendar
36
    @timestamps = calendar.timestamps
37 38
    @starting_year = calendar.starting_year
    @starting_month = calendar.starting_month
39

40
    render 'calendar', layout: false
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
41
  end
42

43
  def calendar_activities
44 45
    @calendar_date = Date.parse(params[:date]) rescue nil
    @events = []
46

47 48
    if @calendar_date
      @events = contributions_calendar.events_by_date(@calendar_date)
49 50 51 52 53
    end

    render 'calendar_activities', layout: false
  end

54 55 56 57 58 59 60
  def determine_layout
    if current_user
      'navless'
    else
      'public_users'
    end
  end
61 62 63

  private

64
  def set_user
65 66 67 68 69 70
    @user = User.find_by_username!(params[:username])

    unless current_user || @user.public_profile?
      return authenticate_user!
    end
  end
71 72 73 74 75 76

  def authorized_projects_ids
    # Projects user can view
    @authorized_projects_ids ||=
      ProjectsFinder.new.execute(current_user).pluck(:id)
  end
77 78 79

  def contributed_projects
    @contributed_projects = Project.
80 81
      where(id: authorized_projects_ids & @user.contributed_projects_ids).
      includes(:namespace)
82 83 84 85
  end

  def contributions_calendar
    @contributions_calendar ||= Gitlab::ContributionsCalendar.
86
      new(contributed_projects.reject(&:forked?), @user)
87
  end
88

89 90 91 92 93 94 95 96
  def load_events
    # Get user activity feed for projects common for both users
    @events = @user.recent_events.
      where(project_id: authorized_projects_ids).
      with_associations

    @events = @events.limit(20).offset(params[:offset] || 0)
  end
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
97
end