summaryrefslogtreecommitdiff
path: root/app/services/projects/participants_service.rb
blob: 1616a8a4062393da6497f60ef801b0c6750a902a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

module Projects
  class ParticipantsService < BaseService
    include Users::ParticipableService

    def execute(noteable)
      @noteable = noteable

      participants =
        noteable_owner +
        participants_in_noteable +
        all_members +
        groups +
        project_members

      render_participants_as_hash(participants.uniq)
    end

    def project_members
      @project_members ||= sorted(get_project_members)
    end

    def get_project_members
      members = Member.from_union([project_members_through_ancestral_groups,
                                   project_members_through_invited_groups,
                                   individual_project_members])

      User.id_in(members.select(:user_id))
    end

    def all_members
      [{ username: "all", name: "All Project and Group Members", count: project_members.count }]
    end

    private

    def project_members_through_invited_groups
      groups_with_ancestors = if ::Feature.enabled?(:linear_participants_service_ancestor_scopes, current_user, default_enabled: :yaml)
                                visible_groups.self_and_ancestors
                              else
                                Gitlab::ObjectHierarchy
                                  .new(visible_groups)
                                  .base_and_ancestors
                              end

      GroupMember
        .active_without_invites_and_requests
        .with_source_id(groups_with_ancestors.pluck_primary_key)
    end

    def visible_groups
      visible_groups = project.invited_groups

      unless project_owner?
        visible_groups = visible_groups.public_or_visible_to_user(current_user)
      end

      visible_groups
    end

    def project_members_through_ancestral_groups
      project.group.present? ? project.group.members_with_parents : Member.none
    end

    def individual_project_members
      project.project_members
    end

    def project_owner?
      if project.group.present?
        project.group.owners.include?(current_user)
      else
        project.namespace.owner == current_user
      end
    end
  end
end