summaryrefslogtreecommitdiff
path: root/app/services/projects/participants_service.rb
blob: ba88ba73c3506b204b12e63fdfb8c7896d399026 (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
module Projects
  class ParticipantsService < BaseService
    def execute(note_type, note_id)
      @target = get_target(note_type, note_id)
      participating =
        if note_type && note_id
          participants_in(note_type, note_id)
        else
          []
        end
      project_members = sorted(project.team.members)
      participants = target_owner + participating + all_members + groups + project_members
      participants.uniq
    end

    def get_target(type, id)
      case type
      when "Issue"
        project.issues.find_by_iid(id)
      when "MergeRequest"
        project.merge_requests.find_by_iid(id)
      when "Commit"
        project.commit(id)
      end
    end

    def target_owner
      [{
        name: @target.author.name,
        username: @target.author.username
      }]
    end

    def participants_in(type, id)
      return [] unless @target

      users = @target.participants(current_user)
      sorted(users)
    end

    def sorted(users)
      users.uniq.to_a.compact.sort_by(&:username).map do |user|
        { username: user.username, name: user.name }
      end
    end

    def groups
      current_user.authorized_groups.sort_by(&:path).map do |group|
        count = group.users.count
        { username: group.path, name: group.name, count: count }
      end
    end

    def all_members
      count = project.team.members.flatten.count
      [{ username: "all", name: "All Project and Group Members", count: count }]
    end
  end
end