debian-mirror-gitlab/app/services/projects/participants_service.rb

60 lines
1.5 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Projects
class ParticipantsService < BaseService
2016-06-02 11:05:42 +05:30
def execute(noteable_type, noteable_id)
@noteable_type = noteable_type
@noteable_id = noteable_id
2015-04-26 12:48:37 +05:30
project_members = sorted(project.team.members)
2016-06-02 11:05:42 +05:30
participants = target_owner + participants_in_target + all_members + groups + project_members
2014-09-02 18:07:02 +05:30
participants.uniq
end
2016-06-02 11:05:42 +05:30
def target
@target ||=
case @noteable_type
2015-09-11 14:41:01 +05:30
when "Issue"
2016-06-02 11:05:42 +05:30
project.issues.find_by_iid(@noteable_id)
2015-09-11 14:41:01 +05:30
when "MergeRequest"
2016-06-02 11:05:42 +05:30
project.merge_requests.find_by_iid(@noteable_id)
2015-09-11 14:41:01 +05:30
when "Commit"
2016-06-02 11:05:42 +05:30
project.commit(@noteable_id)
else
nil
2015-09-11 14:41:01 +05:30
end
2016-06-02 11:05:42 +05:30
end
def target_owner
return [] unless target && target.author.present?
[{
name: target.author.name,
username: target.author.username
}]
end
def participants_in_target
2015-09-11 14:41:01 +05:30
return [] unless target
users = target.participants(current_user)
2014-09-02 18:07:02 +05:30
sorted(users)
end
def sorted(users)
2016-06-02 11:05:42 +05:30
users.uniq.to_a.compact.sort_by(&:username).map do |user|
2015-04-26 12:48:37 +05:30
{ username: user.username, name: user.name }
end
end
def groups
2016-06-02 11:05:42 +05:30
current_user.authorized_groups.sort_by(&:path).map do |group|
2015-04-26 12:48:37 +05:30
count = group.users.count
2015-09-11 14:41:01 +05:30
{ username: group.path, name: group.name, count: count }
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
def all_members
2015-04-26 12:48:37 +05:30
count = project.team.members.flatten.count
2015-09-11 14:41:01 +05:30
[{ username: "all", name: "All Project and Group Members", count: count }]
2014-09-02 18:07:02 +05:30
end
end
end