2015-04-23 11:27:45 -04: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 10:20:11 -04:00
|
|
|
# participant :author, :assignee, :notes, ->(current_user) { mentioned_users(current_user) }
|
2015-04-23 11:27:45 -04:00
|
|
|
# end
|
2015-06-24 06:49:34 -04:00
|
|
|
#
|
2015-04-23 11:27:45 -04: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 09:23:20 -04:00
|
|
|
module Participable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def participant(*attrs)
|
2015-10-14 03:17:05 -04:00
|
|
|
participant_attrs.concat(attrs)
|
2015-04-21 09:23:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def participant_attrs
|
|
|
|
@participant_attrs ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-24 06:49:34 -04: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-10-15 05:26:58 -04:00
|
|
|
def participants(current_user = self.author, load_lazy_references: true)
|
2015-05-15 06:45:45 -04:00
|
|
|
participants = self.class.participant_attrs.flat_map do |attr|
|
2015-06-24 06:49:34 -04:00
|
|
|
value =
|
2015-10-14 10:20:11 -04:00
|
|
|
if attr.respond_to?(:call)
|
|
|
|
instance_exec(current_user, &attr)
|
2015-04-21 09:23:20 -04:00
|
|
|
else
|
2015-10-14 10:20:11 -04:00
|
|
|
send(attr)
|
2015-04-21 09:23:20 -04:00
|
|
|
end
|
|
|
|
|
2015-10-12 05:54:46 -04:00
|
|
|
participants_for(value, current_user)
|
2015-04-21 09:23:20 -04:00
|
|
|
end.compact.uniq
|
2015-05-15 06:45:45 -04:00
|
|
|
|
2015-10-14 03:17:05 -04:00
|
|
|
if load_lazy_references
|
|
|
|
participants = Gitlab::Markdown::ReferenceFilter::LazyReference.load(participants).uniq
|
|
|
|
|
2015-10-15 05:26:58 -04:00
|
|
|
participants.select! do |user|
|
|
|
|
user.can?(:read_project, project)
|
2015-05-15 06:45:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
participants
|
2015-04-21 09:23:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2015-06-24 06:49:34 -04:00
|
|
|
|
2015-10-12 05:54:46 -04:00
|
|
|
def participants_for(value, current_user = nil)
|
2015-04-21 11:41:18 -04:00
|
|
|
case value
|
2015-10-14 03:17:05 -04:00
|
|
|
when User, Gitlab::Markdown::ReferenceFilter::LazyReference
|
2015-04-21 11:41:18 -04:00
|
|
|
[value]
|
2015-04-23 11:27:03 -04:00
|
|
|
when Enumerable, ActiveRecord::Relation
|
2015-10-12 05:54:46 -04:00
|
|
|
value.flat_map { |v| participants_for(v, current_user) }
|
2015-04-21 11:41:18 -04:00
|
|
|
when Participable
|
2015-10-15 05:26:58 -04:00
|
|
|
value.participants(current_user, load_lazy_references: false)
|
2015-04-21 09:23:20 -04:00
|
|
|
end
|
2015-04-21 11:41:18 -04:00
|
|
|
end
|
2015-04-21 09:23:20 -04:00
|
|
|
end
|