users_controller.rb 3.27 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
class UsersController < ApplicationController
2 3
  include RoutableActions

4
  skip_before_action :authenticate_user!
5
  before_action :user, except: [:exists]
6
  before_action :authorize_read_user!, except: [:exists]
7

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

      format.atom do
        load_events
        render layout: false
      end

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

24 25 26
  def groups
    load_groups

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

37
  def projects
38 39
    load_projects

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

50
  def contributed
51 52
    load_contributed_projects

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

63 64 65 66 67 68 69
  def snippets
    load_snippets

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

76
  def calendar
77
    calendar = contributions_calendar
78
    @activity_dates = calendar.activity_dates
79

80
    render 'calendar', layout: false
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
81
  end
82

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

    render 'calendar_activities', layout: false
  end

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

94
  private
Felipe Artur's avatar
Felipe Artur committed
95 96

  def authorize_read_user!
97 98 99 100 101
    if can?(current_user, :read_user, user)
      ensure_canonical_path(user.namespace, params[:username])
    else
      render_404
    end
102
  end
103

Felipe Artur's avatar
Felipe Artur committed
104
  def user
105
    @user ||= User.find_by_full_path(params[:username], follow_redirects: true)
106
  end
107

108
  def contributed_projects
Felipe Artur's avatar
Felipe Artur committed
109
    ContributedProjectsFinder.new(user).execute(current_user)
110 111 112
  end

  def contributions_calendar
113
    @contributions_calendar ||= Gitlab::ContributionsCalendar.new(user, current_user)
114
  end
115

116 117
  def load_events
    # Get user activity feed for projects common for both users
Felipe Artur's avatar
Felipe Artur committed
118
    @events = user.recent_events.
119 120 121 122 123
      merge(projects_for_current_user).
      references(:project).
      with_associations.
      limit_recent(20, params[:offset])
  end
124

125 126
  def load_projects
    @projects =
Felipe Artur's avatar
Felipe Artur committed
127
      PersonalProjectsFinder.new(user).execute(current_user)
128
      .page(params[:page])
129 130 131
  end

  def load_contributed_projects
Felipe Artur's avatar
Felipe Artur committed
132
    @contributed_projects = contributed_projects.joined(user)
133 134 135
  end

  def load_groups
Felipe Artur's avatar
Felipe Artur committed
136
    @groups = JoinedGroupsFinder.new(user).execute(current_user)
137 138
  end

139 140 141 142 143 144 145 146 147
  def load_snippets
    @snippets = SnippetsFinder.new.execute(
      current_user,
      filter: :by_user,
      user: user,
      scope: params[:scope]
    ).page(params[:page])
  end

148
  def projects_for_current_user
149
    ProjectsFinder.new(current_user: current_user).execute
150
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
151
end