notification_service.rb 19.8 KB
Newer Older
1 2
# NotificationService class
#
J
Johannes Schleifenbaum 已提交
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
V
Valery Sizov 已提交
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
V
Valery Sizov 已提交
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
  #
  def reassigned_issue(issue, current_user)
70
    reassign_resource_email(issue, issue.project, current_user, :reassigned_issue_email)
71 72
  end

73
  # When we add labels to an issue we should send an email to:
74
  #
75 76 77
  #  * watchers of the issue's labels
  #
  def relabeled_issue(issue, added_labels, current_user)
78
    relabeled_resource_email(issue, added_labels, current_user, :relabeled_issue_email)
79 80
  end

81
  # When create a merge request we should send an email to:
82
  #
83
  #  * mr assignee if their notification level is not Disabled
84 85
  #  * project team members with notification level higher then Participating
  #  * watchers of the mr's labels
86
  #  * users with custom level checked with "new merge request"
87 88
  #
  def new_merge_request(merge_request, current_user)
89
    new_resource_email(merge_request, merge_request.target_project, :new_merge_request_email)
90
  end
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105
  # 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

106
  # When we reassign a merge_request we should send an email to:
107
  #
108 109
  #  * merge_request old assignee if their notification level is not Disabled
  #  * merge_request assignee if their notification level is not Disabled
110
  #  * users with custom level checked with "reassign merge request"
111 112
  #
  def reassigned_merge_request(merge_request, current_user)
113
    reassign_resource_email(merge_request, merge_request.target_project, current_user, :reassigned_merge_request_email)
114
  end
115

116
  # When we add labels to a merge request we should send an email to:
117
  #
118 119 120
  #  * watchers of the mr's labels
  #
  def relabeled_merge_request(merge_request, added_labels, current_user)
121
    relabeled_resource_email(merge_request, added_labels, current_user, :relabeled_merge_request_email)
122 123
  end

124
  def close_mr(merge_request, current_user)
125
    close_resource_email(merge_request, merge_request.target_project, current_user, :closed_merge_request_email)
126 127
  end

128
  def reopen_issue(issue, current_user)
129
    reopen_resource_email(issue, issue.project, current_user, :issue_status_changed_email, 'reopened')
130 131
  end

132
  def merge_mr(merge_request, current_user)
V
Valery Sizov 已提交
133 134 135 136
    close_resource_email(
      merge_request,
      merge_request.target_project,
      current_user,
137
      :merged_merge_request_email
V
Valery Sizov 已提交
138
    )
139 140
  end

M
Marin Jankovski 已提交
141
  def reopen_mr(merge_request, current_user)
V
Valery Sizov 已提交
142 143 144
    reopen_resource_email(
      merge_request,
      merge_request.target_project,
145
      current_user,
146
      :merge_request_status_email,
V
Valery Sizov 已提交
147 148
      'reopened'
    )
M
Marin Jankovski 已提交
149 150
  end

151
  # Notify new user with email after creation
152
  def new_user(user, token = nil)
J
Johannes Schleifenbaum 已提交
153
    # Don't email omniauth created users
V
Valery Sizov 已提交
154
    mailer.new_user_email(user.id, token).deliver_later unless user.identities.any?
155 156 157 158 159 160 161
  end

  # Notify users on new note in system
  #
  # TODO: split on methods and refactor
  #
  def new_note(note)
162 163
    return true unless note.noteable_type.present?

164
    # ignore gitlab service messages
165
    return true if note.note.start_with?('Status changed to closed')
Z
Z.J. van de Weg 已提交
166
    return true if note.cross_reference? && note.system?
167

168
    target = note.noteable
169

170
    recipients = []
171

172 173 174 175 176
    mentioned_users = note.mentioned_users
    mentioned_users.select! do |user|
      user.can?(:read_project, note.project)
    end

D
Douwe Maan 已提交
177
    # Add all users participating in the thread (author, assignee, comment authors)
178
    participants =
179
      if target.respond_to?(:participants)
180
        target.participants(note.author)
D
Douwe Maan 已提交
181
      else
182
        mentioned_users
D
Douwe Maan 已提交
183
      end
184

D
Douwe Maan 已提交
185
    recipients = recipients.concat(participants)
186 187

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

190
    # Merge project with custom notification
191
    recipients = add_custom_notifications(recipients, note.project, :new_note)
192

D
Douwe Maan 已提交
193
    # Reject users with Mention notification level, except those mentioned in _this_ note.
194 195
    recipients = reject_mention_users(recipients - mentioned_users, note.project)
    recipients = recipients + mentioned_users
196

197
    recipients = reject_muted_users(recipients, note.project)
198

V
Valery Sizov 已提交
199 200
    recipients = add_subscribed_users(recipients, note.noteable)
    recipients = reject_unsubscribed_users(recipients, note.noteable)
201
    recipients = reject_users_without_access(recipients, note.noteable)
V
Valery Sizov 已提交
202

203
    recipients.delete(note.author)
204
    recipients = recipients.uniq
205 206 207

    # build notify method like 'note_commit_email'
    notify_method = "note_#{note.noteable_type.underscore}_email".to_sym
208

209
    recipients.each do |recipient|
V
Valery Sizov 已提交
210
      mailer.send(notify_method, recipient.id, note.id).deliver_later
211 212
    end
  end
213

R
Rémy Coutable 已提交
214 215 216
  # Members
  def new_access_request(member)
    mailer.member_access_requested_email(member.real_source_type, member.id).deliver_later
217 218
  end

R
Rémy Coutable 已提交
219 220
  def decline_access_request(member)
    mailer.member_access_denied_email(member.real_source_type, member.source_id, member.user_id).deliver_later
221 222
  end

R
Rémy Coutable 已提交
223
  # Project invite
D
Douwe Maan 已提交
224
  def invite_project_member(project_member, token)
R
Rémy Coutable 已提交
225
    mailer.member_invited_email(project_member.real_source_type, project_member.id, token).deliver_later
D
Douwe Maan 已提交
226 227 228
  end

  def accept_project_invite(project_member)
R
Rémy Coutable 已提交
229
    mailer.member_invite_accepted_email(project_member.real_source_type, project_member.id).deliver_later
D
Douwe Maan 已提交
230 231
  end

D
Douwe Maan 已提交
232
  def decline_project_invite(project_member)
233
    mailer.member_invite_declined_email(
R
Rémy Coutable 已提交
234
      project_member.real_source_type,
V
Valery Sizov 已提交
235 236 237 238 239
      project_member.project.id,
      project_member.invite_email,
      project_member.access_level,
      project_member.created_by_id
    ).deliver_later
D
Douwe Maan 已提交
240 241
  end

242
  def new_project_member(project_member)
R
Rémy Coutable 已提交
243
    mailer.member_access_granted_email(project_member.real_source_type, project_member.id).deliver_later
244 245
  end

246
  def update_project_member(project_member)
R
Rémy Coutable 已提交
247
    mailer.member_access_granted_email(project_member.real_source_type, project_member.id).deliver_later
248
  end
249

R
Rémy Coutable 已提交
250
  # Group invite
D
Douwe Maan 已提交
251
  def invite_group_member(group_member, token)
R
Rémy Coutable 已提交
252
    mailer.member_invited_email(group_member.real_source_type, group_member.id, token).deliver_later
D
Douwe Maan 已提交
253 254 255
  end

  def accept_group_invite(group_member)
256
    mailer.member_invite_accepted_email(group_member.real_source_type, group_member.id).deliver_later
D
Douwe Maan 已提交
257 258
  end

D
Douwe Maan 已提交
259
  def decline_group_invite(group_member)
260
    mailer.member_invite_declined_email(
R
Rémy Coutable 已提交
261
      group_member.real_source_type,
V
Valery Sizov 已提交
262 263 264 265 266
      group_member.group.id,
      group_member.invite_email,
      group_member.access_level,
      group_member.created_by_id
    ).deliver_later
D
Douwe Maan 已提交
267 268
  end

269
  def new_group_member(group_member)
R
Rémy Coutable 已提交
270
    mailer.member_access_granted_email(group_member.real_source_type, group_member.id).deliver_later
271 272
  end

273
  def update_group_member(group_member)
R
Rémy Coutable 已提交
274
    mailer.member_access_granted_email(group_member.real_source_type, group_member.id).deliver_later
275 276
  end

277
  def project_was_moved(project, old_path_with_namespace)
278 279 280 281
    recipients = project.team.members
    recipients = reject_muted_users(recipients, project)

    recipients.each do |recipient|
V
Valery Sizov 已提交
282 283 284 285 286
      mailer.project_was_moved_email(
        project.id,
        recipient.id,
        old_path_with_namespace
      ).deliver_later
287 288 289
    end
  end

290 291 292 293 294 295 296 297 298 299
  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

300 301 302 303 304 305 306 307
  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

308 309
  protected

310
  # Get project/group users with CUSTOM notification level
311
  def add_custom_notifications(recipients, project, action)
312 313
    user_ids = []

314
    # Users with a notification setting on group or project
315 316 317
    user_ids += notification_settings_for(project, :custom, action)
    user_ids += notification_settings_for(project.group, :custom, action)

318 319 320 321 322 323 324
    # Users with global level custom
    users_with_project_level_global = notification_settings_for(project, :global)
    users_with_group_level_global   = notification_settings_for(project.group, :global)

    global_users_ids = users_with_project_level_global.concat(users_with_group_level_global)
    user_ids += users_with_global_level_custom(global_users_ids, action)

325 326 327
    recipients.concat(User.find(user_ids))
  end

328 329
  # Get project users with WATCH notification level
  def project_watchers(project)
330
    project_members = notification_settings_for(project)
331

332
    users_with_project_level_global = notification_settings_for(project, :global)
333
    users_with_group_level_global   = notification_settings_for(project.group, :global)
334

335
    users = users_with_global_level_watch([users_with_project_level_global, users_with_group_level_global].flatten.uniq)
336

337
    users_with_project_setting = select_project_member_setting(project, users_with_project_level_global, users)
338
    users_with_group_setting = select_group_member_setting(project, project_members, users_with_group_level_global, users)
339

340
    User.where(id: users_with_project_setting.concat(users_with_group_setting).uniq).to_a
341 342
  end

343 344
  def notification_settings_for(resource, notification_level = nil, action = nil)
    return [] unless resource
345

346
    if notification_level
347 348 349
      settings = resource.notification_settings.where(level: NotificationSetting.levels[notification_level])
      settings = settings.select { |setting| setting.events[action] } if action.present?
      settings.map(&:user_id)
350
    else
351
      resource.notification_settings.pluck(:user_id)
352
    end
353 354
  end

355
  def users_with_global_level_watch(ids)
356
    settings_with_global_level_of(:watch, ids).pluck(:user_id)
357
  end
358

359 360 361 362 363 364 365
  def users_with_global_level_custom(ids, action)
    settings = settings_with_global_level_of(:custom, ids)
    settings = settings.select { |setting| setting.events[action] }
    settings.map(&:user_id)
  end

  def settings_with_global_level_of(level, ids)
366 367 368
    NotificationSetting.where(
      user_id: ids,
      source_type: nil,
369 370
      level: NotificationSetting.levels[level]
    )
371 372 373
  end

  # Build a list of users based on project notifcation settings
374
  def select_project_member_setting(project, global_setting, users_global_level_watch)
375
    users = notification_settings_for(project, :watch)
376

377 378 379 380
    # If project setting is global, add to watch list if global setting is watch
    global_setting.each do |user_id|
      if users_global_level_watch.include?(user_id)
        users << user_id
381 382
      end
    end
383 384

    users
385 386
  end

S
Steven Burgart 已提交
387
  # Build a list of users based on group notification settings
388
  def select_group_member_setting(project, project_members, global_setting, users_global_level_watch)
389
    uids = notification_settings_for(project, :watch)
390 391 392 393 394 395

    # Group setting is watch, add to users list if user is not project member
    users = []
    uids.each do |user_id|
      if project_members.exclude?(user_id)
        users << user_id
396 397 398
      end
    end

399 400 401 402
    # Group setting is global, add to users list if global setting is watch
    global_setting.each do |user_id|
      if project_members.exclude?(user_id) && users_global_level_watch.include?(user_id)
        users << user_id
403 404 405
      end
    end

406
    users
407 408
  end

409
  def add_project_watchers(recipients, project)
410
    recipients.concat(project_watchers(project)).compact
411 412
  end

413 414
  # Remove users with disabled notifications from array
  # Also remove duplications and nil recipients
415
  def reject_muted_users(users, project = nil)
416
    reject_users(users, :disabled, project)
417 418
  end

419 420
  # Remove users with notification level 'Mentioned'
  def reject_mention_users(users, project = nil)
421
    reject_users(users, :mention, project)
422 423
  end

424
  # Reject users which has certain notification level
425 426
  #
  # Example:
427
  #   reject_users(users, :watch, project)
428
  #
429 430 431 432 433 434 435
  def reject_users(users, level, project = nil)
    level = level.to_s

    unless NotificationSetting.levels.keys.include?(level)
      raise 'Invalid notification level'
    end

436
    users = users.to_a.compact.uniq
437
    users = users.reject(&:blocked?)
438 439

    users.reject do |user|
440 441 442
      global_notification_setting = user.global_notification_setting

      next global_notification_setting.level == level unless project
443

444
      setting = user.notification_settings_for(project)
445

446
      if !setting && project.group
447
        setting = user.notification_settings_for(project.group)
448 449
      end

450
      # reject users who globally set mention notification and has no setting per project/group
451
      next global_notification_setting.level == level unless setting
452 453

      # reject users who set mention notification in project
454
      next true if setting.level == level
455

456
      # reject users who have mention level in project and disabled in global settings
457
      setting.global? && global_notification_setting.level == level
458 459 460
    end
  end

V
Valery Sizov 已提交
461
  def reject_unsubscribed_users(recipients, target)
V
Valery Sizov 已提交
462
    return recipients unless target.respond_to? :subscriptions
463

V
Valery Sizov 已提交
464
    recipients.reject do |user|
465 466
      subscription = target.subscriptions.find_by_user_id(user.id)
      subscription && !subscription.subscribed
V
Valery Sizov 已提交
467 468 469
    end
  end

470 471 472 473 474 475 476 477
  def reject_users_without_access(recipients, target)
    return recipients unless target.is_a?(Issue)

    recipients.select do |user|
      user.can?(:read_issue, target)
    end
  end

V
Valery Sizov 已提交
478
  def add_subscribed_users(recipients, target)
479
    return recipients unless target.respond_to? :subscribers
V
Valery Sizov 已提交
480

481
    recipients + target.subscribers
V
Valery Sizov 已提交
482
  end
483

484
  def add_labels_subscribers(recipients, target, labels: nil)
485 486
    return recipients unless target.respond_to? :labels

487 488
    (labels || target.labels).each do |label|
      recipients += label.subscribers
489 490 491 492 493
    end

    recipients
  end

I
Izaak Alpert 已提交
494
  def new_resource_email(target, project, method)
495
    recipients = build_recipients(target, project, target.author, action: "new")
496 497

    recipients.each do |recipient|
V
Valery Sizov 已提交
498
      mailer.send(method, recipient.id, target.id).deliver_later
499 500 501
    end
  end

502
  def new_mentions_in_resource_email(target, project, new_mentioned_users, current_user, method)
503 504
    recipients = build_recipients(target, project, current_user, action: "new")
    recipients = recipients & new_mentioned_users
505 506 507 508 509 510

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

I
Izaak Alpert 已提交
511
  def close_resource_email(target, project, current_user, method)
512 513
    action = method == :merged_merge_request_email ? "merge" : "close"
    recipients = build_recipients(target, project, current_user, action: action)
514 515

    recipients.each do |recipient|
V
Valery Sizov 已提交
516
      mailer.send(method, recipient.id, target.id, current_user.id).deliver_later
517 518 519
    end
  end

I
Izaak Alpert 已提交
520
  def reassign_resource_email(target, project, current_user, method)
521
    previous_assignee_id = previous_record(target, 'assignee_id')
522 523
    previous_assignee = User.find_by(id: previous_assignee_id) if previous_assignee_id

524
    recipients = build_recipients(target, project, current_user, action: "reassign", previous_assignee: previous_assignee)
525

526
    recipients.each do |recipient|
V
Valery Sizov 已提交
527 528 529 530 531 532 533
      mailer.send(
        method,
        recipient.id,
        target.id,
        previous_assignee_id,
        current_user.id
      ).deliver_later
534 535
    end
  end
D
Dmitriy Zaporozhets 已提交
536

537 538
  def relabeled_resource_email(target, labels, current_user, method)
    recipients = build_relabeled_recipients(target, current_user, labels: labels)
539 540 541
    label_names = labels.map(&:name)

    recipients.each do |recipient|
542
      mailer.send(method, recipient.id, target.id, label_names, current_user.id).deliver_later
543 544 545
    end
  end

546
  def reopen_resource_email(target, project, current_user, method, status)
547
    recipients = build_recipients(target, project, current_user, action: "reopen")
548 549

    recipients.each do |recipient|
V
Valery Sizov 已提交
550
      mailer.send(method, recipient.id, target.id, status, current_user.id).deliver_later
551 552 553
    end
  end

554
  def build_recipients(target, project, current_user, action: nil, previous_assignee: nil)
555 556
    custom_action = build_custom_key(action, target)

557
    recipients = target.participants(current_user)
D
Fix.  
Douwe Maan 已提交
558
    recipients = add_project_watchers(recipients, project)
559

560
    recipients = add_custom_notifications(recipients, project, custom_action)
561
    recipients = reject_mention_users(recipients, project)
562

563 564
    recipients = recipients.uniq

565 566 567
    # Re-assign is considered as a mention of the new assignee so we add the
    # new assignee to the list of recipients after we rejected users with
    # the "on mention" notification level
568
    if [:reassign_merge_request, :reassign_issue].include?(custom_action)
569
      recipients << previous_assignee if previous_assignee
570 571 572 573
      recipients << target.assignee
    end

    recipients = reject_muted_users(recipients, project)
V
Valery Sizov 已提交
574
    recipients = add_subscribed_users(recipients, target)
575

576
    if [:new_issue, :new_merge_request].include?(custom_action)
577 578 579
      recipients = add_labels_subscribers(recipients, target)
    end

V
Valery Sizov 已提交
580
    recipients = reject_unsubscribed_users(recipients, target)
581
    recipients = reject_users_without_access(recipients, target)
582

583
    recipients.delete(current_user)
584
    recipients.uniq
585 586
  end

587 588
  def build_relabeled_recipients(target, current_user, labels:)
    recipients = add_labels_subscribers([], target, labels: labels)
589
    recipients = reject_unsubscribed_users(recipients, target)
590
    recipients = reject_users_without_access(recipients, target)
591 592 593 594
    recipients.delete(current_user)
    recipients.uniq
  end

D
Dmitriy Zaporozhets 已提交
595
  def mailer
V
Valery Sizov 已提交
596
    Notify
D
Dmitriy Zaporozhets 已提交
597
  end
598 599 600 601

  def previous_record(object, attribute)
    if object && attribute
      if object.previous_changes.include?(attribute)
M
Marin Jankovski 已提交
602
        object.previous_changes[attribute].first
603 604 605
      end
    end
  end
606 607 608 609 610 611

  # Build event key to search on custom notification level
  # Check NotificationSetting::EMAIL_EVENTS
  def build_custom_key(action, object)
    "#{action}_#{object.class.name.underscore}".to_sym
  end
612
end