users_controller.rb 2.84 KB
Newer Older
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
1
class UsersController < ApplicationController
2
  skip_before_action :authenticate_user!
Felipe's avatar
Felipe committed
3
  #TODO felipe_artur: Remove this "set_user" before action. It is not good to use before filters for loading database records.
Felipe's avatar
Felipe committed
4 5
  before_action :set_user, except: [:show]
  before_action :authorize_read_user, only: [:show]
6

Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
7
  def show
8 9
    respond_to do |format|
      format.html
10 11 12 13 14 15

      format.atom do
        load_events
        render layout: false
      end

16 17 18 19
      format.json do
        load_events
        pager_json("events/_events", @events.count)
      end
20 21 22
    end
  end

23 24 25
  def groups
    load_groups

Alfredo Sumaran's avatar
Alfredo Sumaran committed
26
    respond_to do |format|
27
      format.html { render 'show' }
Alfredo Sumaran's avatar
Alfredo Sumaran committed
28 29 30 31 32 33
      format.json do
        render json: {
          html: view_to_html_string("shared/groups/_list", groups: @groups)
        }
      end
    end
34 35
  end

36
  def projects
37 38
    load_projects

Alfredo Sumaran's avatar
Alfredo Sumaran committed
39
    respond_to do |format|
40
      format.html { render 'show' }
Alfredo Sumaran's avatar
Alfredo Sumaran committed
41 42 43 44 45 46
      format.json do
        render json: {
          html: view_to_html_string("shared/projects/_list", projects: @projects, remote: true)
        }
      end
    end
47 48
  end

49
  def contributed
50 51
    load_contributed_projects

Alfredo Sumaran's avatar
Alfredo Sumaran committed
52
    respond_to do |format|
53
      format.html { render 'show' }
Alfredo Sumaran's avatar
Alfredo Sumaran committed
54 55 56 57 58 59
      format.json do
        render json: {
          html: view_to_html_string("shared/projects/_list", projects: @contributed_projects)
        }
      end
    end
60 61
  end

62
  def calendar
63
    calendar = contributions_calendar
64
    @timestamps = calendar.timestamps
65 66
    @starting_year = calendar.starting_year
    @starting_month = calendar.starting_month
67

68
    render 'calendar', layout: false
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
69
  end
70

71
  def calendar_activities
72 73
    @calendar_date = Date.parse(params[:date]) rescue Date.today
    @events = contributions_calendar.events_by_date(@calendar_date)
74 75 76 77

    render 'calendar_activities', layout: false
  end

78
  private
79
  def authorize_read_user
Felipe's avatar
Felipe committed
80 81
    set_user
    render_404 unless can?(current_user, :read_user, @user)
82
  end
83

84
  def set_user
85 86
    @user = User.find_by_username!(params[:username])
  end
87

88
  def contributed_projects
89
    ContributedProjectsFinder.new(@user).execute(current_user)
90 91 92 93
  end

  def contributions_calendar
    @contributions_calendar ||= Gitlab::ContributionsCalendar.
94
      new(contributed_projects, @user)
95
  end
96

97 98 99
  def load_events
    # Get user activity feed for projects common for both users
    @events = @user.recent_events.
100 101 102 103 104
      merge(projects_for_current_user).
      references(:project).
      with_associations.
      limit_recent(20, params[:offset])
  end
105

106 107 108
  def load_projects
    @projects =
      PersonalProjectsFinder.new(@user).execute(current_user)
109
      .page(params[:page])
110 111 112 113 114 115 116
  end

  def load_contributed_projects
    @contributed_projects = contributed_projects.joined(@user)
  end

  def load_groups
117
    @groups = JoinedGroupsFinder.new(@user).execute(current_user)
118 119
  end

120 121
  def projects_for_current_user
    ProjectsFinder.new.execute(current_user)
122
  end
Dmytro Zaporozhets's avatar
Dmytro Zaporozhets committed
123
end