2015-04-23 15:27:45 +00:00
|
|
|
# == Participable concern
|
|
|
|
#
|
|
|
|
# Contains functionality related to objects that can have participants, such as
|
|
|
|
# an author, an assignee and people mentioned in its description or comments.
|
|
|
|
#
|
|
|
|
# Used by Issue, Note, MergeRequest, Snippet and Commit.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# class Issue < ActiveRecord::Base
|
|
|
|
# include Participable
|
|
|
|
#
|
|
|
|
# # ...
|
|
|
|
#
|
2015-10-14 14:20:11 +00:00
|
|
|
# participant :author, :assignee, :notes, ->(current_user) { mentioned_users(current_user) }
|
2015-04-23 15:27:45 +00:00
|
|
|
# end
|
2015-06-24 10:49:34 +00:00
|
|
|
#
|
2015-04-23 15:27:45 +00:00
|
|
|
# issue = Issue.last
|
|
|
|
# users = issue.participants
|
|
|
|
# # `users` will contain the issue's author, its assignee,
|
|
|
|
# # all users returned by its #mentioned_users method,
|
|
|
|
# # as well as all participants to all of the issue's notes,
|
|
|
|
# # since Note implements Participable as well.
|
|
|
|
#
|
2015-04-21 13:23:20 +00:00
|
|
|
module Participable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def participant(*attrs)
|
2015-10-14 07:17:05 +00:00
|
|
|
participant_attrs.concat(attrs)
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def participant_attrs
|
|
|
|
@participant_attrs ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-24 10:49:34 +00:00
|
|
|
# Be aware that this method makes a lot of sql queries.
|
|
|
|
# Save result into variable if you are going to reuse it inside same request
|
2015-12-21 14:36:08 +00:00
|
|
|
def participants(current_user = self.author)
|
2015-12-15 14:51:16 +00:00
|
|
|
participants =
|
|
|
|
Gitlab::ReferenceExtractor.lazily do
|
|
|
|
self.class.participant_attrs.flat_map do |attr|
|
|
|
|
value =
|
|
|
|
if attr.respond_to?(:call)
|
|
|
|
instance_exec(current_user, &attr)
|
|
|
|
else
|
|
|
|
send(attr)
|
|
|
|
end
|
2015-04-21 13:23:20 +00:00
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
participants_for(value, current_user)
|
|
|
|
end.compact.uniq
|
|
|
|
end
|
2015-10-14 07:17:05 +00:00
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
unless Gitlab::ReferenceExtractor.lazy?
|
2015-10-15 09:26:58 +00:00
|
|
|
participants.select! do |user|
|
|
|
|
user.can?(:read_project, project)
|
2015-05-15 10:45:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
participants
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2015-06-24 10:49:34 +00:00
|
|
|
|
2015-10-12 09:54:46 +00:00
|
|
|
def participants_for(value, current_user = nil)
|
2015-04-21 15:41:18 +00:00
|
|
|
case value
|
2015-12-15 14:51:16 +00:00
|
|
|
when User, Banzai::LazyReference
|
2015-04-21 15:41:18 +00:00
|
|
|
[value]
|
2015-04-23 15:27:03 +00:00
|
|
|
when Enumerable, ActiveRecord::Relation
|
2015-10-12 09:54:46 +00:00
|
|
|
value.flat_map { |v| participants_for(v, current_user) }
|
2015-04-21 15:41:18 +00:00
|
|
|
when Participable
|
2015-12-15 14:51:16 +00:00
|
|
|
value.participants(current_user)
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|
2015-04-21 15:41:18 +00:00
|
|
|
end
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|