notification_service.rb 12.5 KB
Newer Older
1 2
# NotificationService class
#
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
3
# Used for notifying users with emails about different events
4 5 6 7 8 9 10
#
# Ex.
#   NotificationService.new.new_issue(issue, current_user)
#
class NotificationService
  # Always notify user about ssh key added
  # only if ssh key is not deploy key
11 12 13
  #
  # This is security email so it will be sent
  # even if user disabled notifications
14 15
  def new_key(key)
    if key.user
16
      mailer.new_ssh_key_email(key.id).deliver_later
17 18 19
    end
  end

20 21 22
  # Always notify user about email added to profile
  def new_email(email)
    if email.user
23
      mailer.new_email_email(email.id).deliver_later
24 25 26
    end
  end

27
  # When create an issue we should send an email to:
28
  #
29
  #  * issue assignee if their notification level is not Disabled
30
  #  * project team members with notification level higher then Participating
31
  #  * watchers of the issue's labels
32
  #  * users with custom level checked with "new issue"
33 34
  #
  def new_issue(issue, current_user)
35
    new_resource_email(issue, issue.project, :new_issue_email)
36 37
  end

38 39 40 41 42 43 44 45 46 47 48 49 50 51
  # When issue text is updated, we should send an email to:
  #
  #  * newly mentioned project team members with notification level higher than Participating
  #
  def new_mentions_in_issue(issue, new_mentioned_users, current_user)
    new_mentions_in_resource_email(
      issue,
      issue.project,
      new_mentioned_users,
      current_user,
      :new_mention_in_issue_email
    )
  end

52
  # When we close an issue we should send an email to:
53
  #
54 55
  #  * issue author if their notification level is not Disabled
  #  * issue assignee if their notification level is not Disabled
56
  #  * project team members with notification level higher then Participating
57
  #  * users with custom level checked with "close issue"
58 59
  #
  def close_issue(issue, current_user)
60
    close_resource_email(issue, issue.project, current_user, :closed_issue_email)
61 62
  end

63
  # When we reassign an issue we should send an email to:
64
  #
65 66
  #  * issue old assignee if their notification level is not Disabled
  #  * issue new assignee if their notification level is not Disabled
67
  #  * users with custom level checked with "reassign issue"
68
  #
69 70 71 72 73 74 75 76
  def reassigned_issue(issue, current_user, previous_assignees = [])
    recipients = NotificationRecipientService.new(issue.project).build_recipients(
      issue,
      current_user,
      action: "reassign",
      previous_assignee: previous_assignees
    )

77 78
    previous_assignee_ids = previous_assignees.map(&:id)

79 80 81 82 83
    recipients.each do |recipient|
      mailer.send(
        :reassigned_issue_email,
        recipient.id,
        issue.id,
84
        previous_assignee_ids,
85 86 87
        current_user.id
      ).deliver_later
    end
88 89
  end

90
  # When we add labels to an issue we should send an email to:
91
  #
92 93 94
  #  * watchers of the issue's labels
  #
  def relabeled_issue(issue, added_labels, current_user)
95
    relabeled_resource_email(issue, issue.project, added_labels, current_user, :relabeled_issue_email)
96 97
  end

98
  # When create a merge request we should send an email to:
99
  #
100
  #  * mr assignee if their notification level is not Disabled
101 102
  #  * project team members with notification level higher then Participating
  #  * watchers of the mr's labels
103
  #  * users with custom level checked with "new merge request"
104 105
  #
  def new_merge_request(merge_request, current_user)
106
    new_resource_email(merge_request, merge_request.target_project, :new_merge_request_email)
107
  end
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122
  # When merge request text is updated, we should send an email to:
  #
  #  * newly mentioned project team members with notification level higher than Participating
  #
  def new_mentions_in_merge_request(merge_request, new_mentioned_users, current_user)
    new_mentions_in_resource_email(
      merge_request,
      merge_request.target_project,
      new_mentioned_users,
      current_user,
      :new_mention_in_merge_request_email
    )
  end

123
  # When we reassign a merge_request we should send an email to:
124
  #
125 126
  #  * merge_request old assignee if their notification level is not Disabled
  #  * merge_request assignee if their notification level is not Disabled
127
  #  * users with custom level checked with "reassign merge request"
128 129
  #
  def reassigned_merge_request(merge_request, current_user)
130
    reassign_resource_email(merge_request, merge_request.target_project, current_user, :reassigned_merge_request_email)
131
  end
132

133
  # When we add labels to a merge request we should send an email to:
134
  #
135 136 137
  #  * watchers of the mr's labels
  #
  def relabeled_merge_request(merge_request, added_labels, current_user)
138
    relabeled_resource_email(merge_request, merge_request.target_project, added_labels, current_user, :relabeled_merge_request_email)
139 140
  end

141
  def close_mr(merge_request, current_user)
142
    close_resource_email(merge_request, merge_request.target_project, current_user, :closed_merge_request_email)
143 144
  end

145
  def reopen_issue(issue, current_user)
146
    reopen_resource_email(issue, issue.project, current_user, :issue_status_changed_email, 'reopened')
147 148
  end

149
  def merge_mr(merge_request, current_user)
150 151 152 153
    close_resource_email(
      merge_request,
      merge_request.target_project,
      current_user,
154
      :merged_merge_request_email,
155
      skip_current_user: !merge_request.merge_when_pipeline_succeeds?
156
    )
157 158
  end

159
  def reopen_mr(merge_request, current_user)
160 161 162
    reopen_resource_email(
      merge_request,
      merge_request.target_project,
163
      current_user,
164
      :merge_request_status_email,
165 166
      'reopened'
    )
167 168
  end

169
  def resolve_all_discussions(merge_request, current_user)
170 171 172 173
    recipients = NotificationRecipientService.new(merge_request.target_project).build_recipients(
      merge_request,
      current_user,
      action: "resolve_all_discussions")
174 175 176 177 178 179

    recipients.each do |recipient|
      mailer.resolved_all_discussions_email(recipient.id, merge_request.id, current_user.id).deliver_later
    end
  end

180
  # Notify new user with email after creation
181
  def new_user(user, token = nil)
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
182
    # Don't email omniauth created users
183
    mailer.new_user_email(user.id, token).deliver_later unless user.identities.any?
184 185 186 187
  end

  # Notify users on new note in system
  def new_note(note)
188 189
    return true unless note.noteable_type.present?

190
    # ignore gitlab service messages
Z.J. van de Weg's avatar
Z.J. van de Weg committed
191
    return true if note.cross_reference? && note.system?
192

Jarka Kadlecova's avatar
Jarka Kadlecova committed
193
    notify_method = "note_#{note.to_ability_name}_email".to_sym
194

195
    recipients = NotificationRecipientService.new(note.project).build_new_note_recipients(note)
196
    recipients.each do |recipient|
197
      mailer.send(notify_method, recipient.id, note.id).deliver_later
198 199
    end
  end
200

201 202 203
  # Members
  def new_access_request(member)
    mailer.member_access_requested_email(member.real_source_type, member.id).deliver_later
204 205
  end

206 207
  def decline_access_request(member)
    mailer.member_access_denied_email(member.real_source_type, member.source_id, member.user_id).deliver_later
208 209
  end

210
  # Project invite
211
  def invite_project_member(project_member, token)
212
    mailer.member_invited_email(project_member.real_source_type, project_member.id, token).deliver_later
213 214 215
  end

  def accept_project_invite(project_member)
216
    mailer.member_invite_accepted_email(project_member.real_source_type, project_member.id).deliver_later
217 218
  end

Douwe Maan's avatar
Douwe Maan committed
219
  def decline_project_invite(project_member)
220
    mailer.member_invite_declined_email(
221
      project_member.real_source_type,
222 223 224 225
      project_member.project.id,
      project_member.invite_email,
      project_member.created_by_id
    ).deliver_later
Douwe Maan's avatar
Douwe Maan committed
226 227
  end

228
  def new_project_member(project_member)
229
    mailer.member_access_granted_email(project_member.real_source_type, project_member.id).deliver_later
230 231
  end

232
  def update_project_member(project_member)
233
    mailer.member_access_granted_email(project_member.real_source_type, project_member.id).deliver_later
234
  end
235

236
  # Group invite
237
  def invite_group_member(group_member, token)
238
    mailer.member_invited_email(group_member.real_source_type, group_member.id, token).deliver_later
239 240 241
  end

  def accept_group_invite(group_member)
242
    mailer.member_invite_accepted_email(group_member.real_source_type, group_member.id).deliver_later
243 244
  end

Douwe Maan's avatar
Douwe Maan committed
245
  def decline_group_invite(group_member)
246
    mailer.member_invite_declined_email(
247
      group_member.real_source_type,
248 249 250 251
      group_member.group.id,
      group_member.invite_email,
      group_member.created_by_id
    ).deliver_later
Douwe Maan's avatar
Douwe Maan committed
252 253
  end

254
  def new_group_member(group_member)
255
    mailer.member_access_granted_email(group_member.real_source_type, group_member.id).deliver_later
256 257
  end

258
  def update_group_member(group_member)
259
    mailer.member_access_granted_email(group_member.real_source_type, group_member.id).deliver_later
260 261
  end

262
  def project_was_moved(project, old_path_with_namespace)
263
    recipients = project.team.members
264
    recipients = NotificationRecipientService.new(project).reject_muted_users(recipients)
265 266

    recipients.each do |recipient|
267 268 269 270 271
      mailer.project_was_moved_email(
        project.id,
        recipient.id,
        old_path_with_namespace
      ).deliver_later
272 273 274
    end
  end

275
  def issue_moved(issue, new_issue, current_user)
276
    recipients = NotificationRecipientService.new(issue.project).build_recipients(issue, current_user)
277 278 279 280 281 282 283 284

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

285 286 287 288 289 290 291 292
  def project_exported(project, current_user)
    mailer.project_was_exported_email(current_user, project).deliver_later
  end

  def project_not_exported(project, current_user, errors)
    mailer.project_was_not_exported_email(current_user, project, errors).deliver_later
  end

293 294 295 296 297
  def pipeline_finished(pipeline, recipients = nil)
    email_template = "pipeline_#{pipeline.status}_email"

    return unless mailer.respond_to?(email_template)

Sean McGivern's avatar
Sean McGivern committed
298
    recipients ||= NotificationRecipientService.new(pipeline.project).build_pipeline_recipients(
299
      pipeline,
300
      pipeline.user,
301
      action: pipeline.status
Sean McGivern's avatar
Sean McGivern committed
302
    ).map(&:notification_email)
303

304 305 306
    if recipients.any?
      mailer.public_send(email_template, pipeline, recipients).deliver_later
    end
307 308
  end

309 310
  protected

311
  def new_resource_email(target, project, method)
312
    recipients = NotificationRecipientService.new(project).build_recipients(target, target.author, action: "new")
313 314

    recipients.each do |recipient|
315
      mailer.send(method, recipient.id, target.id).deliver_later
316 317 318
    end
  end

319
  def new_mentions_in_resource_email(target, project, new_mentioned_users, current_user, method)
320
    recipients = NotificationRecipientService.new(project).build_recipients(target, current_user, action: "new")
321
    recipients = recipients & new_mentioned_users
322 323 324 325 326 327

    recipients.each do |recipient|
      mailer.send(method, recipient.id, target.id, current_user.id).deliver_later
    end
  end

328
  def close_resource_email(target, project, current_user, method, skip_current_user: true)
329
    action = method == :merged_merge_request_email ? "merge" : "close"
330

331
    recipients = NotificationRecipientService.new(project).build_recipients(
332 333 334 335 336
      target,
      current_user,
      action: action,
      skip_current_user: skip_current_user
    )
337 338

    recipients.each do |recipient|
339
      mailer.send(method, recipient.id, target.id, current_user.id).deliver_later
340 341 342
    end
  end

343
  def reassign_resource_email(target, project, current_user, method)
344
    previous_assignee_id = previous_record(target, 'assignee_id')
345 346
    previous_assignee = User.find_by(id: previous_assignee_id) if previous_assignee_id

347 348 349 350 351 352
    recipients = NotificationRecipientService.new(project).build_recipients(
      target,
      current_user,
      action: "reassign",
      previous_assignee: previous_assignee
    )
353

354
    recipients.each do |recipient|
355 356 357 358 359 360 361
      mailer.send(
        method,
        recipient.id,
        target.id,
        previous_assignee_id,
        current_user.id
      ).deliver_later
362 363
    end
  end
364

365
  def relabeled_resource_email(target, project, labels, current_user, method)
366
    recipients = NotificationRecipientService.new(project).build_relabeled_recipients(target, current_user, labels: labels)
367 368 369
    label_names = labels.map(&:name)

    recipients.each do |recipient|
370
      mailer.send(method, recipient.id, target.id, label_names, current_user.id).deliver_later
371 372 373
    end
  end

374
  def reopen_resource_email(target, project, current_user, method, status)
375
    recipients = NotificationRecipientService.new(project).build_recipients(target, current_user, action: "reopen")
376 377

    recipients.each do |recipient|
378
      mailer.send(method, recipient.id, target.id, status, current_user.id).deliver_later
379 380 381
    end
  end

382
  def mailer
383
    Notify
384
  end
385 386

  def previous_record(object, attribute)
387 388 389 390
    return unless object && attribute

    if object.previous_changes.include?(attribute)
      object.previous_changes[attribute].first
391 392
    end
  end
393
end