gitlab-org--gitlab-foss/app/services/projects/participants_service.rb

65 lines
1.7 KiB
Ruby
Raw Normal View History

module Projects
class ParticipantsService < BaseService
attr_reader :noteable_type, :noteable_id
def execute
@noteable_type = params[:type]
@noteable_id = params[:type_id]
2015-03-27 11:58:23 +00:00
project_members = sorted(project.team.members)
2016-04-08 18:36:55 +00:00
participants = target_owner + participants_in_target + all_members + groups + project_members
participants.uniq
end
2016-04-08 18:36:55 +00:00
def target
@target ||=
case noteable_type
when 'Issue'
IssuesFinder.new(current_user, project_id: project.id, state: 'all').
execute.find_by(iid: noteable_id)
when 'MergeRequest'
MergeRequestsFinder.new(current_user, project_id: project.id, state: 'all').
execute.find_by(iid: noteable_id)
when 'Commit'
project.commit(noteable_id)
2016-04-08 18:36:55 +00:00
else
nil
end
end
def target_owner
2016-04-08 18:36:55 +00:00
return [] unless target && target.author.present?
2016-03-30 21:29:42 +00:00
[{
2016-04-08 18:36:55 +00:00
name: target.author.name,
username: target.author.username
}]
end
def participants_in_target
2016-04-08 18:36:55 +00:00
return [] unless target
2015-04-17 09:03:33 +00:00
2016-04-08 18:36:55 +00:00
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
2015-03-27 11:58:23 +00:00
count = project.team.members.flatten.count
[{ username: "all", name: "All Project and Group Members", count: count }]
end
end
end