summaryrefslogtreecommitdiff
path: root/app/services/projects/participants_service.rb
blob: 6040391fd947dedeb3fff66000eb0defab11042e (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
module Projects
  class ParticipantsService < BaseService
    attr_reader :noteable

    def execute(noteable)
      @noteable = noteable

      project_members = sorted(project.team.members)
      participants = noteable_owner + participants_in_noteable + all_members + groups + project_members
      participants.uniq
    end

    def noteable_owner
      return [] unless noteable && noteable.author.present?

      [{
        name: noteable.author.name,
        username: noteable.author.username,
        avatar_url: noteable.author.avatar_url
      }]
    end

    def participants_in_noteable
      return [] unless noteable

      users = noteable.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, avatar_url: user.avatar_url }
      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, avatar_url: group.avatar.url }
      end
    end

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