Class: NotificationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/notification_service.rb

Overview

NotificationService class

Used for notifying users with emails about different events

Ex.

NotificationService.new.new_issue(issue, current_user)

Instance Method Summary collapse

Instance Method Details

#accept_group_invite(group_member) ⇒ Object


206
207
208
# File 'app/services/notification_service.rb', line 206

def accept_group_invite(group_member)
  mailer.group_invite_accepted_email(group_member.id).deliver_later
end

#accept_project_invite(project_member) ⇒ Object


181
182
183
# File 'app/services/notification_service.rb', line 181

def accept_project_invite(project_member)
  mailer.project_invite_accepted_email(project_member.id).deliver_later
end

#close_issue(issue, current_user) ⇒ Object

When we close an issue we should send an email to:

* issue author if their notification level is not Disabled
* issue assignee if their notification level is not Disabled
* project team members with notification level higher then Participating

43
44
45
# File 'app/services/notification_service.rb', line 43

def close_issue(issue, current_user)
  close_resource_email(issue, issue.project, current_user, 'closed_issue_email')
end

#close_mr(merge_request, current_user) ⇒ Object


91
92
93
# File 'app/services/notification_service.rb', line 91

def close_mr(merge_request, current_user)
  close_resource_email(merge_request, merge_request.target_project, current_user, 'closed_merge_request_email')
end

#decline_group_invite(group_member) ⇒ Object


210
211
212
213
214
215
216
217
# File 'app/services/notification_service.rb', line 210

def decline_group_invite(group_member)
  mailer.group_invite_declined_email(
    group_member.group.id,
    group_member.invite_email,
    group_member.access_level,
    group_member.created_by_id
  ).deliver_later
end

#decline_project_invite(project_member) ⇒ Object


185
186
187
188
189
190
191
192
# File 'app/services/notification_service.rb', line 185

def decline_project_invite(project_member)
  mailer.project_invite_declined_email(
    project_member.project.id,
    project_member.invite_email,
    project_member.access_level,
    project_member.created_by_id
  ).deliver_later
end

#invite_group_member(group_member, token) ⇒ Object


202
203
204
# File 'app/services/notification_service.rb', line 202

def invite_group_member(group_member, token)
  mailer.group_member_invited_email(group_member.id, token).deliver_later
end

#invite_project_member(project_member, token) ⇒ Object


177
178
179
# File 'app/services/notification_service.rb', line 177

def invite_project_member(project_member, token)
  mailer.project_member_invited_email(project_member.id, token).deliver_later
end

#issue_moved(issue, new_issue, current_user) ⇒ Object


240
241
242
243
244
245
246
247
248
# File 'app/services/notification_service.rb', line 240

def issue_moved(issue, new_issue, current_user)
  recipients = build_recipients(issue, issue.project, current_user)

  recipients.map do |recipient|
    email = mailer.issue_moved_email(recipient, issue, new_issue, current_user)
    email.deliver_later
    email
  end
end

#merge_mr(merge_request, current_user) ⇒ Object


99
100
101
102
103
104
105
106
# File 'app/services/notification_service.rb', line 99

def merge_mr(merge_request, current_user)
  close_resource_email(
    merge_request,
    merge_request.target_project,
    current_user,
    'merged_merge_request_email'
  )
end

#new_email(email) ⇒ Object

Always notify user about email added to profile


21
22
23
24
25
# File 'app/services/notification_service.rb', line 21

def new_email(email)
  if email.user
    mailer.new_email_email(email.id).deliver_later
  end
end

#new_group_member(group_member) ⇒ Object


219
220
221
# File 'app/services/notification_service.rb', line 219

def new_group_member(group_member)
  mailer.group_access_granted_email(group_member.id).deliver_later
end

#new_issue(issue, current_user) ⇒ Object

When create an issue we should send an email to:

* issue assignee if their notification level is not Disabled
* project team members with notification level higher then Participating
* watchers of the issue's labels

33
34
35
# File 'app/services/notification_service.rb', line 33

def new_issue(issue, current_user)
  new_resource_email(issue, issue.project, 'new_issue_email')
end

#new_key(key) ⇒ Object

Always notify user about ssh key added only if ssh key is not deploy key

This is security email so it will be sent even if user disabled notifications


14
15
16
17
18
# File 'app/services/notification_service.rb', line 14

def new_key(key)
  if key.user
    mailer.new_ssh_key_email(key.id).deliver_later
  end
end

#new_merge_request(merge_request, current_user) ⇒ Object

When create a merge request we should send an email to:

* mr assignee if their notification level is not Disabled
* project team members with notification level higher then Participating
* watchers of the mr's labels

70
71
72
# File 'app/services/notification_service.rb', line 70

def new_merge_request(merge_request, current_user)
  new_resource_email(merge_request, merge_request.target_project, 'new_merge_request_email')
end

#new_note(note) ⇒ Object

Notify users on new note in system

TODO: split on methods and refactor


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/services/notification_service.rb', line 128

def new_note(note)
  return true unless note.noteable_type.present?

  # ignore gitlab service messages
  return true if note.note.start_with?('Status changed to closed')
  return true if note.cross_reference? && note.system == true
  return true if note.is_award

  target = note.noteable

  recipients = []

  mentioned_users = note.mentioned_users
  mentioned_users.select! do |user|
    user.can?(:read_project, note.project)
  end

  # Add all users participating in the thread (author, assignee, comment authors)
  participants =
    if target.respond_to?(:participants)
      target.participants(note.author)
    else
      mentioned_users
    end
  recipients = recipients.concat(participants)

  # Merge project watchers
  recipients = add_project_watchers(recipients, note.project)

  # Reject users with Mention notification level, except those mentioned in _this_ note.
  recipients = reject_mention_users(recipients - mentioned_users, note.project)
  recipients = recipients + mentioned_users

  recipients = reject_muted_users(recipients, note.project)

  recipients = add_subscribed_users(recipients, note.noteable)
  recipients = reject_unsubscribed_users(recipients, note.noteable)
  recipients = reject_users_without_access(recipients, note.noteable)

  recipients.delete(note.author)
  recipients = recipients.uniq

  # build notify method like 'note_commit_email'
  notify_method = "note_#{note.noteable_type.underscore}_email".to_sym
  recipients.each do |recipient|
    mailer.send(notify_method, recipient.id, note.id).deliver_later
  end
end

#new_project_member(project_member) ⇒ Object


194
195
196
# File 'app/services/notification_service.rb', line 194

def new_project_member(project_member)
  mailer.project_access_granted_email(project_member.id).deliver_later
end

#new_user(user, token = nil) ⇒ Object

Notify new user with email after creation


119
120
121
122
# File 'app/services/notification_service.rb', line 119

def new_user(user, token = nil)
  # Don't email omniauth created users
  mailer.new_user_email(user.id, token).deliver_later unless user.identities.any?
end

#project_was_moved(project, old_path_with_namespace) ⇒ Object


227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/services/notification_service.rb', line 227

def project_was_moved(project, old_path_with_namespace)
  recipients = project.team.members
  recipients = reject_muted_users(recipients, project)

  recipients.each do |recipient|
    mailer.project_was_moved_email(
      project.id,
      recipient.id,
      old_path_with_namespace
    ).deliver_later
  end
end

#reassigned_issue(issue, current_user) ⇒ Object

When we reassign an issue we should send an email to:

* issue old assignee if their notification level is not Disabled
* issue new assignee if their notification level is not Disabled

52
53
54
# File 'app/services/notification_service.rb', line 52

def reassigned_issue(issue, current_user)
  reassign_resource_email(issue, issue.project, current_user, 'reassigned_issue_email')
end

#reassigned_merge_request(merge_request, current_user) ⇒ Object

When we reassign a merge_request we should send an email to:

* merge_request old assignee if their notification level is not Disabled
* merge_request assignee if their notification level is not Disabled

79
80
81
# File 'app/services/notification_service.rb', line 79

def reassigned_merge_request(merge_request, current_user)
  reassign_resource_email(merge_request, merge_request.target_project, current_user, 'reassigned_merge_request_email')
end

#relabeled_issue(issue, added_labels, current_user) ⇒ Object

When we add labels to an issue we should send an email to:

* watchers of the issue's labels

60
61
62
# File 'app/services/notification_service.rb', line 60

def relabeled_issue(issue, added_labels, current_user)
  relabeled_resource_email(issue, added_labels, current_user, 'relabeled_issue_email')
end

#relabeled_merge_request(merge_request, added_labels, current_user) ⇒ Object

When we add labels to a merge request we should send an email to:

* watchers of the mr's labels

87
88
89
# File 'app/services/notification_service.rb', line 87

def relabeled_merge_request(merge_request, added_labels, current_user)
  relabeled_resource_email(merge_request, added_labels, current_user, 'relabeled_merge_request_email')
end

#reopen_issue(issue, current_user) ⇒ Object


95
96
97
# File 'app/services/notification_service.rb', line 95

def reopen_issue(issue, current_user)
  reopen_resource_email(issue, issue.project, current_user, 'issue_status_changed_email', 'reopened')
end

#reopen_mr(merge_request, current_user) ⇒ Object


108
109
110
111
112
113
114
115
116
# File 'app/services/notification_service.rb', line 108

def reopen_mr(merge_request, current_user)
  reopen_resource_email(
    merge_request,
    merge_request.target_project,
    current_user,
    'merge_request_status_email',
    'reopened'
  )
end

#update_group_member(group_member) ⇒ Object


223
224
225
# File 'app/services/notification_service.rb', line 223

def update_group_member(group_member)
  mailer.group_access_granted_email(group_member.id).deliver_later
end

#update_project_member(project_member) ⇒ Object


198
199
200
# File 'app/services/notification_service.rb', line 198

def update_project_member(project_member)
  mailer.project_access_granted_email(project_member.id).deliver_later
end