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.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# class Issue < ActiveRecord::Base
|
|
|
|
# include Participable
|
|
|
|
#
|
|
|
|
# # ...
|
|
|
|
#
|
2016-05-26 11:38:28 +00:00
|
|
|
# participant :author
|
|
|
|
# participant :assignee
|
|
|
|
# participant :notes
|
|
|
|
#
|
|
|
|
# participant -> (current_user, ext) do
|
|
|
|
# ext.analyze('...')
|
|
|
|
# end
|
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
|
2015-04-21 13:23:20 +00:00
|
|
|
module Participable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ClassMethods
|
2016-05-26 11:38:28 +00:00
|
|
|
# Adds a list of participant attributes. Attributes can either be symbols or
|
|
|
|
# Procs.
|
|
|
|
#
|
|
|
|
# When using a Proc instead of a Symbol the Proc will be given two
|
|
|
|
# arguments:
|
|
|
|
#
|
|
|
|
# 1. The current user (as an instance of User)
|
|
|
|
# 2. An instance of `Gitlab::ReferenceExtractor`
|
|
|
|
#
|
|
|
|
# It is expected that a Proc populates the given reference extractor
|
|
|
|
# instance with data. The return value of the Proc is ignored.
|
|
|
|
#
|
|
|
|
# attr - The name of the attribute or a Proc
|
|
|
|
def participant(attr)
|
|
|
|
participant_attrs << attr
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def participant_attrs
|
|
|
|
@participant_attrs ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
# Returns the users participating in a discussion.
|
|
|
|
#
|
|
|
|
# This method processes attributes of objects in breadth-first order.
|
|
|
|
#
|
|
|
|
# Returns an Array of User instances.
|
|
|
|
def participants(current_user = nil)
|
|
|
|
current_user ||= author
|
|
|
|
ext = Gitlab::ReferenceExtractor.new(project, current_user)
|
|
|
|
participants = Set.new
|
|
|
|
process = [self]
|
2015-04-21 13:23:20 +00:00
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
until process.empty?
|
|
|
|
source = process.pop
|
2015-10-14 07:17:05 +00:00
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
case source
|
|
|
|
when User
|
|
|
|
participants << source
|
|
|
|
when Participable
|
|
|
|
source.class.participant_attrs.each do |attr|
|
|
|
|
if attr.respond_to?(:call)
|
|
|
|
source.instance_exec(current_user, ext, &attr)
|
|
|
|
else
|
|
|
|
process << source.__send__(attr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when Enumerable, ActiveRecord::Relation
|
|
|
|
# This uses reverse_each so we can use "pop" to get the next value to
|
|
|
|
# process (in order). Using unshift instead of pop would require
|
|
|
|
# moving all Array values one index to the left (which can be
|
|
|
|
# expensive).
|
|
|
|
source.reverse_each { |obj| process << obj }
|
2015-05-15 10:45:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
participants.merge(ext.users)
|
2015-06-24 10:49:34 +00:00
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
Ability.users_that_can_read_project(participants.to_a, project)
|
2015-04-21 15:41:18 +00:00
|
|
|
end
|
2015-04-21 13:23:20 +00:00
|
|
|
end
|