users_controller.rb 1.95 KB
Newer Older
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
1
class UsersController < ApplicationController
2 3
  skip_before_action :authenticate_user!
  before_action :set_user
4

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

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

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

15 16
    respond_to do |format|
      format.html
17 18 19 20 21 22

      format.atom do
        load_events
        render layout: false
      end

23 24 25 26
      format.json do
        load_events
        pager_json("events/_events", @events.count)
      end
27 28 29 30
    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
  private

52
  def set_user
53 54
    @user = User.find_by_username!(params[:username])
  end
55 56 57 58 59 60

  def authorized_projects_ids
    # Projects user can view
    @authorized_projects_ids ||=
      ProjectsFinder.new.execute(current_user).pluck(:id)
  end
61 62 63

  def contributed_projects
    @contributed_projects = Project.
64 65
      where(id: authorized_projects_ids & @user.contributed_projects_ids).
      includes(:namespace)
66 67 68 69
  end

  def contributions_calendar
    @contributions_calendar ||= Gitlab::ContributionsCalendar.
70
      new(contributed_projects.reject(&:forked?), @user)
71
  end
72

73 74 75 76 77 78 79 80
  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
81
end