BigW Consortium Gitlab

users_controller.rb 3.15 KB
Newer Older
1
class UsersController < ApplicationController
2
  skip_before_action :authenticate_user!
3
  before_action :user, except: [:exists]
Felipe Artur committed
4
  before_action :authorize_read_user!, only: [:show]
5

6
  def show
7 8
    respond_to do |format|
      format.html
9 10 11 12 13 14

      format.atom do
        load_events
        render layout: false
      end

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

22 23 24
  def groups
    load_groups

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

35
  def projects
36 37
    load_projects

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

48
  def contributed
49 50
    load_contributed_projects

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

61 62 63 64 65 66 67
  def snippets
    load_snippets

    respond_to do |format|
      format.html { render 'show' }
      format.json do
        render json: {
68
          html: view_to_html_string("snippets/_snippets", collection: @snippets, remote: true)
69 70 71 72 73
        }
      end
    end
  end

74
  def calendar
75
    calendar = contributions_calendar
76
    @activity_dates = calendar.activity_dates
77

78
    render 'calendar', layout: false
79
  end
80

81
  def calendar_activities
82 83
    @calendar_date = Date.parse(params[:date]) rescue Date.today
    @events = contributions_calendar.events_by_date(@calendar_date)
84 85 86 87

    render 'calendar_activities', layout: false
  end

88
  def exists
89
    render json: { exists: !!Namespace.find_by_path_or_name(params[:username]) }
90 91
  end

92
  private
Felipe Artur committed
93 94

  def authorize_read_user!
Felipe Artur committed
95
    render_404 unless can?(current_user, :read_user, user)
96
  end
97

Felipe Artur committed
98 99
  def user
    @user ||= User.find_by_username!(params[:username])
100
  end
101

102
  def contributed_projects
Felipe Artur committed
103
    ContributedProjectsFinder.new(user).execute(current_user)
104 105 106
  end

  def contributions_calendar
107
    @contributions_calendar ||= Gitlab::ContributionsCalendar.new(user, current_user)
108
  end
109

110 111
  def load_events
    # Get user activity feed for projects common for both users
Felipe Artur committed
112
    @events = user.recent_events.
113 114 115 116 117
      merge(projects_for_current_user).
      references(:project).
      with_associations.
      limit_recent(20, params[:offset])
  end
118

119 120
  def load_projects
    @projects =
Felipe Artur committed
121
      PersonalProjectsFinder.new(user).execute(current_user)
122
      .page(params[:page])
123 124 125
  end

  def load_contributed_projects
Felipe Artur committed
126
    @contributed_projects = contributed_projects.joined(user)
127 128 129
  end

  def load_groups
Felipe Artur committed
130
    @groups = JoinedGroupsFinder.new(user).execute(current_user)
131 132
  end

133 134 135 136 137 138 139 140 141
  def load_snippets
    @snippets = SnippetsFinder.new.execute(
      current_user,
      filter: :by_user,
      user: user,
      scope: params[:scope]
    ).page(params[:page])
  end

142 143
  def projects_for_current_user
    ProjectsFinder.new.execute(current_user)
144
  end
145
end