2014-06-23 09:44:49 -04:00
|
|
|
module Projects
|
|
|
|
class ParticipantsService < BaseService
|
2015-02-04 11:10:39 -05:00
|
|
|
def initialize(project, user)
|
|
|
|
@project = project
|
|
|
|
@user = user
|
2014-06-23 09:44:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute(note_type, note_id)
|
2015-02-03 00:38:50 -05:00
|
|
|
participating =
|
|
|
|
if note_type && note_id
|
|
|
|
participants_in(note_type, note_id)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2015-03-13 11:22:03 -04:00
|
|
|
project_members = sorted(@project.team.members)
|
|
|
|
participants = all_members + groups + project_members + participating
|
2014-06-23 09:44:49 -04:00
|
|
|
participants.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def participants_in(type, id)
|
|
|
|
users = case type
|
|
|
|
when "Issue"
|
|
|
|
issue = @project.issues.find_by_iid(id)
|
2015-03-27 07:19:34 -04:00
|
|
|
issue ? issue.participants(user) : []
|
2014-06-23 09:44:49 -04:00
|
|
|
when "MergeRequest"
|
|
|
|
merge_request = @project.merge_requests.find_by_iid(id)
|
2015-03-27 07:19:34 -04:00
|
|
|
merge_request ? merge_request.participants(user) : []
|
2014-06-23 09:44:49 -04:00
|
|
|
when "Commit"
|
|
|
|
author_ids = Note.for_commit_id(id).pluck(:author_id).uniq
|
|
|
|
User.where(id: author_ids)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
sorted(users)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sorted(users)
|
2015-02-25 11:34:01 -05:00
|
|
|
users.uniq.to_a.compact.sort_by(&:username).map do |user|
|
|
|
|
{ username: user.username, name: user.name }
|
|
|
|
end
|
2014-06-23 09:44:49 -04:00
|
|
|
end
|
|
|
|
|
2015-02-04 11:10:39 -05:00
|
|
|
def groups
|
2015-02-25 11:34:01 -05:00
|
|
|
@user.authorized_groups.sort_by(&:path).map do |group|
|
|
|
|
count = group.users.count
|
|
|
|
{ username: group.path, name: "#{group.name} (#{count})" }
|
|
|
|
end
|
2015-02-04 11:10:39 -05:00
|
|
|
end
|
|
|
|
|
2014-06-23 09:44:49 -04:00
|
|
|
def all_members
|
2015-02-25 11:34:01 -05:00
|
|
|
count = @project.team.members.flatten.count
|
|
|
|
[{ username: "all", name: "All Project and Group Members (#{count})" }]
|
2014-06-23 09:44:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|