users_controller.rb 2.34 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 8
    @contributed_projects = Project.
      where(id: authorized_projects_ids & @user.contributed_projects_ids).
9 10 11
      in_group_namespace.
      includes(:namespace).
      reject(&:forked?)
12

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

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

19
    @title = @user.name
20
    @title_url = user_path(@user)
21

22 23 24
    respond_to do |format|
      format.html
      format.atom { render layout: false }
25 26 27 28
      format.json do
        load_events
        pager_json("events/_events", @events.count)
      end
29 30 31 32
    end
  end

  def calendar
33
    projects = Project.where(id: authorized_projects_ids & @user.contributed_projects_ids)
34

35
    calendar = Gitlab::CommitsCalendar.new(projects, @user)
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  def calendar_activities
    projects = Project.where(id: authorized_projects_ids & @user.contributed_projects_ids)

    date = Date.parse(params[:date]) rescue nil
    if date
      @calendar_activities = Gitlab::CommitsCalendar.get_commits_for_date(projects, @user, date)
    else
      @calendar_activities = {}
    end

    # get the total number of unique commits
    @commit_count = @calendar_activities.values.flatten.map(&:id).uniq.count

    @calendar_date = date

    render 'calendar_activities', layout: false
  end

61 62 63 64 65 66 67
  def determine_layout
    if current_user
      'navless'
    else
      'public_users'
    end
  end
68 69 70

  private

71
  def set_user
72 73 74 75 76 77
    @user = User.find_by_username!(params[:username])

    unless current_user || @user.public_profile?
      return authenticate_user!
    end
  end
78 79 80 81 82 83

  def authorized_projects_ids
    # Projects user can view
    @authorized_projects_ids ||=
      ProjectsFinder.new.execute(current_user).pluck(:id)
  end
84 85 86 87 88 89 90 91 92

  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
93
end