Class: NotificationRecipients::Builder::Base

Inherits:
Object
  • Object
show all
Defined in:
app/services/notification_recipients/builder/base.rb

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



6
7
8
# File 'app/services/notification_recipients/builder/base.rb', line 6

def initialize(*)
  raise 'abstract'
end

Instance Method Details

#acting_userObject



18
19
20
# File 'app/services/notification_recipients/builder/base.rb', line 18

def acting_user
  current_user
end

#add_recipients(users, type, reason) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord



44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/notification_recipients/builder/base.rb', line 44

def add_recipients(users, type, reason)
  if users.is_a?(ActiveRecord::Relation)
    users = users.includes(:notification_settings)
      .allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/421821')
  end

  users = Array(users).compact
  preload_users_namespace_bans(users)

  recipients.concat(users.map { |u| make_recipient(u, type, reason) })
end

#build!Object



10
11
12
# File 'app/services/notification_recipients/builder/base.rb', line 10

def build!
  raise 'abstract'
end

#custom_actionObject



85
86
87
# File 'app/services/notification_recipients/builder/base.rb', line 85

def custom_action
  nil
end

#filter!Object



14
15
16
# File 'app/services/notification_recipients/builder/base.rb', line 14

def filter!
  recipients.select!(&:notifiable?)
end

#groupObject



35
36
37
# File 'app/services/notification_recipients/builder/base.rb', line 35

def group
  project&.group || target.try(:group)
end

#make_recipient(user, type, reason) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord



63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/notification_recipients/builder/base.rb', line 63

def make_recipient(user, type, reason)
  NotificationRecipient.new(
    user, type,
    reason: reason,
    project: project,
    group: group,
    custom_action: custom_action,
    target: recipients_target,
    acting_user: acting_user
  )
end

#notification_recipientsObject



75
76
77
78
79
80
81
82
83
# File 'app/services/notification_recipients/builder/base.rb', line 75

def notification_recipients
  @notification_recipients ||=
    begin
      build!
      filter!
      recipients = self.recipients.sort_by { |r| NotificationReason.priority(r.reason) }.uniq(&:user)
      recipients.freeze
    end
end

#projectObject



31
32
33
# File 'app/services/notification_recipients/builder/base.rb', line 31

def project
  target.project
end

#recipientsObject



39
40
41
# File 'app/services/notification_recipients/builder/base.rb', line 39

def recipients
  @recipients ||= []
end

#recipients_targetObject

override if needed



27
28
29
# File 'app/services/notification_recipients/builder/base.rb', line 27

def recipients_target
  target
end

#targetObject



22
23
24
# File 'app/services/notification_recipients/builder/base.rb', line 22

def target
  raise 'abstract'
end

#user_scopeObject

rubocop: disable CodeReuse/ActiveRecord



58
59
60
# File 'app/services/notification_recipients/builder/base.rb', line 58

def user_scope
  User.includes(:notification_settings)
end