Class: NotificationRecipients::Builder::Base

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

Constant Summary collapse

BATCH_SIZE =
100

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



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

def initialize(*)
  raise 'abstract'
end

Instance Method Details

#acting_userObject



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

def acting_user
  current_user
end

#add_recipients(users, type, reason) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord



51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/notification_recipients/builder/base.rb', line 51

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



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

def build!
  raise 'abstract'
end

#custom_actionObject



92
93
94
# File 'app/services/notification_recipients/builder/base.rb', line 92

def custom_action
  nil
end

#filter!Object



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

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

#groupObject



41
42
43
# File 'app/services/notification_recipients/builder/base.rb', line 41

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

#make_recipient(user, type, reason) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord



70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/notification_recipients/builder/base.rb', line 70

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



82
83
84
85
86
87
88
89
90
# File 'app/services/notification_recipients/builder/base.rb', line 82

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



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

def project
  target.project
end

#recipientsObject



46
47
48
# File 'app/services/notification_recipients/builder/base.rb', line 46

def recipients
  @recipients ||= []
end

#recipients_targetObject

override if needed



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

def recipients_target
  target
end

#targetObject



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

def target
  raise 'abstract'
end

#user_scopeObject

rubocop: disable CodeReuse/ActiveRecord



65
66
67
# File 'app/services/notification_recipients/builder/base.rb', line 65

def user_scope
  User.includes(:notification_settings)
end