notification_service.rb 19.3 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
  # When we reassign a merge_request we should send an email to:
93
  #
94 95
  #  * merge_request old assignee if their notification level is not Disabled
  #  * merge_request assignee if their notification level is not Disabled
96
  #  * users with custom level checked with "reassign merge request"
97 98
  #
  def reassigned_merge_request(merge_request, current_user)
99
    reassign_resource_email(merge_request, merge_request.target_project, current_user, :reassigned_merge_request_email)
100
  end
101

102
  # When we add labels to a merge request we should send an email to:
103
  #
104 105 106
  #  * watchers of the mr's labels
  #
  def relabeled_merge_request(merge_request, added_labels, current_user)
107
    relabeled_resource_email(merge_request, added_labels, current_user, :relabeled_merge_request_email)
108 109
  end

110
  def close_mr(merge_request, current_user)
111
    close_resource_email(merge_request, merge_request.target_project, current_user, :closed_merge_request_email)
112 113
  end

114
  def reopen_issue(issue, current_user)
115
    reopen_resource_email(issue, issue.project, current_user, :issue_status_changed_email, 'reopened')
116 117
  end

118
  def merge_mr(merge_request, current_user)
V
Valery Sizov 已提交
119 120 121 122
    close_resource_email(
      merge_request,
      merge_request.target_project,
      current_user,
123
      :merged_merge_request_email
V
Valery Sizov 已提交
124
    )
125 126
  end

M
Marin Jankovski 已提交
127
  def reopen_mr(merge_request, current_user)
V
Valery Sizov 已提交
128 129 130
    reopen_resource_email(
      merge_request,
      merge_request.target_project,
131
      current_user,
132
      :merge_request_status_email,
V
Valery Sizov 已提交
133 134
      'reopened'
    )
M
Marin Jankovski 已提交
135 136
  end

137
  # Notify new user with email after creation
138
  def new_user(user, token = nil)
J
Johannes Schleifenbaum 已提交
139
    # Don't email omniauth created users
V
Valery Sizov 已提交
140
    mailer.new_user_email(user.id, token).deliver_later unless user.identities.any?
141 142 143 144 145 146 147
  end

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

150
    # ignore gitlab service messages
151
    return true if note.note.start_with?('Status changed to closed')
Z
Z.J. van de Weg 已提交
152
    return true if note.cross_reference? && note.system?
153

154
    target = note.noteable
155

156
    recipients = []
157

158 159 160 161 162
    mentioned_users = note.mentioned_users
    mentioned_users.select! do |user|
      user.can?(:read_project, note.project)
    end

D
Douwe Maan 已提交
163
    # Add all users participating in the thread (author, assignee, comment authors)
164
    participants =
165
      if target.respond_to?(:participants)
166
        target.participants(note.author)
D
Douwe Maan 已提交
167
      else
168
        mentioned_users
D
Douwe Maan 已提交
169
      end
170

D
Douwe Maan 已提交
171
    recipients = recipients.concat(participants)
172 173

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

176
    # Merge project with custom notification
177
    recipients = add_custom_notifications(recipients, note.project, :new_note)
178

D
Douwe Maan 已提交
179
    # Reject users with Mention notification level, except those mentioned in _this_ note.
180 181
    recipients = reject_mention_users(recipients - mentioned_users, note.project)
    recipients = recipients + mentioned_users
182

183
    recipients = reject_muted_users(recipients, note.project)
184

V
Valery Sizov 已提交
185 186
    recipients = add_subscribed_users(recipients, note.noteable)
    recipients = reject_unsubscribed_users(recipients, note.noteable)
187
    recipients = reject_users_without_access(recipients, note.noteable)
V
Valery Sizov 已提交
188

189
    recipients.delete(note.author)
190
    recipients = recipients.uniq
191 192 193

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

195
    recipients.each do |recipient|
V
Valery Sizov 已提交
196
      mailer.send(notify_method, recipient.id, note.id).deliver_later
197 198
    end
  end
199

R
Rémy Coutable 已提交
200 201 202
  # Members
  def new_access_request(member)
    mailer.member_access_requested_email(member.real_source_type, member.id).deliver_later
203 204
  end

R
Rémy Coutable 已提交
205 206
  def decline_access_request(member)
    mailer.member_access_denied_email(member.real_source_type, member.source_id, member.user_id).deliver_later
207 208
  end

R
Rémy Coutable 已提交
209
  # Project invite
D
Douwe Maan 已提交
210
  def invite_project_member(project_member, token)
R
Rémy Coutable 已提交
211
    mailer.member_invited_email(project_member.real_source_type, project_member.id, token).deliver_later
D
Douwe Maan 已提交
212 213 214
  end

  def accept_project_invite(project_member)
R
Rémy Coutable 已提交
215
    mailer.member_invite_accepted_email(project_member.real_source_type, project_member.id).deliver_later
D
Douwe Maan 已提交
216 217
  end

D
Douwe Maan 已提交
218
  def decline_project_invite(project_member)
219
    mailer.member_invite_declined_email(
R
Rémy Coutable 已提交
220
      project_member.real_source_type,
V
Valery Sizov 已提交
221 222 223 224 225
      project_member.project.id,
      project_member.invite_email,
      project_member.access_level,
      project_member.created_by_id
    ).deliver_later
D
Douwe Maan 已提交
226 227
  end

228
  def new_project_member(project_member)
R
Rémy Coutable 已提交
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)
R
Rémy Coutable 已提交
233
    mailer.member_access_granted_email(project_member.real_source_type, project_member.id).deliver_later
234
  end
235

R
Rémy Coutable 已提交
236
  # Group invite
D
Douwe Maan 已提交
237
  def invite_group_member(group_member, token)
R
Rémy Coutable 已提交
238
    mailer.member_invited_email(group_member.real_source_type, group_member.id, token).deliver_later
D
Douwe Maan 已提交
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
D
Douwe Maan 已提交
243 244
  end

D
Douwe Maan 已提交
245
  def decline_group_invite(group_member)
246
    mailer.member_invite_declined_email(
R
Rémy Coutable 已提交
247
      group_member.real_source_type,
V
Valery Sizov 已提交
248 249 250 251 252
      group_member.group.id,
      group_member.invite_email,
      group_member.access_level,
      group_member.created_by_id
    ).deliver_later
D
Douwe Maan 已提交
253 254
  end

255
  def new_group_member(group_member)
R
Rémy Coutable 已提交
256
    mailer.member_access_granted_email(group_member.real_source_type, group_member.id).deliver_later
257 258
  end

259
  def update_group_member(group_member)
R
Rémy Coutable 已提交
260
    mailer.member_access_granted_email(group_member.real_source_type, group_member.id).deliver_later
261 262
  end

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

    recipients.each do |recipient|
V
Valery Sizov 已提交
268 269 270 271 272
      mailer.project_was_moved_email(
        project.id,
        recipient.id,
        old_path_with_namespace
      ).deliver_later
273 274 275
    end
  end

276 277 278 279 280 281 282 283 284 285
  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

286 287 288 289 290 291 292 293
  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

294 295
  protected

296
  # Get project/group users with CUSTOM notification level
297
  def add_custom_notifications(recipients, project, action)
298 299
    user_ids = []

300
    # Users with a notification setting on group or project
301 302 303
    user_ids += notification_settings_for(project, :custom, action)
    user_ids += notification_settings_for(project.group, :custom, action)

304 305 306 307 308 309 310
    # 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)

311 312 313
    recipients.concat(User.find(user_ids))
  end

314 315
  # Get project users with WATCH notification level
  def project_watchers(project)
316
    project_members = notification_settings_for(project)
317

318
    users_with_project_level_global = notification_settings_for(project, :global)
319
    users_with_group_level_global   = notification_settings_for(project.group, :global)
320

321
    users = users_with_global_level_watch([users_with_project_level_global, users_with_group_level_global].flatten.uniq)
322

323
    users_with_project_setting = select_project_member_setting(project, users_with_project_level_global, users)
324
    users_with_group_setting = select_group_member_setting(project, project_members, users_with_group_level_global, users)
325

326
    User.where(id: users_with_project_setting.concat(users_with_group_setting).uniq).to_a
327 328
  end

329 330
  def notification_settings_for(resource, notification_level = nil, action = nil)
    return [] unless resource
331

332
    if notification_level
333 334 335
      settings = resource.notification_settings.where(level: NotificationSetting.levels[notification_level])
      settings = settings.select { |setting| setting.events[action] } if action.present?
      settings.map(&:user_id)
336
    else
337
      resource.notification_settings.pluck(:user_id)
338
    end
339 340
  end

341
  def users_with_global_level_watch(ids)
342
    settings_with_global_level_of(:watch, ids).pluck(:user_id)
343
  end
344

345 346 347 348 349 350 351
  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)
352 353 354
    NotificationSetting.where(
      user_id: ids,
      source_type: nil,
355 356
      level: NotificationSetting.levels[level]
    )
357 358 359
  end

  # Build a list of users based on project notifcation settings
360
  def select_project_member_setting(project, global_setting, users_global_level_watch)
361
    users = notification_settings_for(project, :watch)
362

363 364 365 366
    # 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
367 368
      end
    end
369 370

    users
371 372
  end

S
Steven Burgart 已提交
373
  # Build a list of users based on group notification settings
374
  def select_group_member_setting(project, project_members, global_setting, users_global_level_watch)
375
    uids = notification_settings_for(project, :watch)
376 377 378 379 380 381

    # 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
382 383 384
      end
    end

385 386 387 388
    # 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
389 390 391
      end
    end

392
    users
393 394
  end

395
  def add_project_watchers(recipients, project)
396
    recipients.concat(project_watchers(project)).compact
397 398
  end

399 400
  # Remove users with disabled notifications from array
  # Also remove duplications and nil recipients
401
  def reject_muted_users(users, project = nil)
402
    reject_users(users, :disabled, project)
403 404
  end

405 406
  # Remove users with notification level 'Mentioned'
  def reject_mention_users(users, project = nil)
407
    reject_users(users, :mention, project)
408 409
  end

410
  # Reject users which has certain notification level
411 412
  #
  # Example:
413
  #   reject_users(users, :watch, project)
414
  #
415 416 417 418 419 420 421
  def reject_users(users, level, project = nil)
    level = level.to_s

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

422
    users = users.to_a.compact.uniq
423
    users = users.reject(&:blocked?)
424 425

    users.reject do |user|
426 427 428
      global_notification_setting = user.global_notification_setting

      next global_notification_setting.level == level unless project
429

430
      setting = user.notification_settings_for(project)
431

432
      if !setting && project.group
433
        setting = user.notification_settings_for(project.group)
434 435
      end

436
      # reject users who globally set mention notification and has no setting per project/group
437
      next global_notification_setting.level == level unless setting
438 439

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

442
      # reject users who have mention level in project and disabled in global settings
443
      setting.global? && global_notification_setting.level == level
444 445 446
    end
  end

V
Valery Sizov 已提交
447
  def reject_unsubscribed_users(recipients, target)
V
Valery Sizov 已提交
448
    return recipients unless target.respond_to? :subscriptions
449

V
Valery Sizov 已提交
450
    recipients.reject do |user|
451 452
      subscription = target.subscriptions.find_by_user_id(user.id)
      subscription && !subscription.subscribed
V
Valery Sizov 已提交
453 454 455
    end
  end

456 457 458 459 460 461 462 463
  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 已提交
464
  def add_subscribed_users(recipients, target)
465
    return recipients unless target.respond_to? :subscribers
V
Valery Sizov 已提交
466

467
    recipients + target.subscribers
V
Valery Sizov 已提交
468
  end
469

470
  def add_labels_subscribers(recipients, target, labels: nil)
471 472
    return recipients unless target.respond_to? :labels

473 474
    (labels || target.labels).each do |label|
      recipients += label.subscribers
475 476 477 478 479
    end

    recipients
  end

I
Izaak Alpert 已提交
480
  def new_resource_email(target, project, method)
481
    recipients = build_recipients(target, project, target.author, action: "new")
482 483

    recipients.each do |recipient|
V
Valery Sizov 已提交
484
      mailer.send(method, recipient.id, target.id).deliver_later
485 486 487
    end
  end

488 489 490 491 492 493 494 495
  def new_mentions_in_resource_email(target, project, new_mentioned_users, current_user, method)
    recipients = build_recipients(target, project, current_user) & new_mentioned_users

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

I
Izaak Alpert 已提交
496
  def close_resource_email(target, project, current_user, method)
497 498
    action = method == :merged_merge_request_email ? "merge" : "close"
    recipients = build_recipients(target, project, current_user, action: action)
499 500

    recipients.each do |recipient|
V
Valery Sizov 已提交
501
      mailer.send(method, recipient.id, target.id, current_user.id).deliver_later
502 503 504
    end
  end

I
Izaak Alpert 已提交
505
  def reassign_resource_email(target, project, current_user, method)
506
    previous_assignee_id = previous_record(target, 'assignee_id')
507 508
    previous_assignee = User.find_by(id: previous_assignee_id) if previous_assignee_id

509
    recipients = build_recipients(target, project, current_user, action: "reassign", previous_assignee: previous_assignee)
510

511
    recipients.each do |recipient|
V
Valery Sizov 已提交
512 513 514 515 516 517 518
      mailer.send(
        method,
        recipient.id,
        target.id,
        previous_assignee_id,
        current_user.id
      ).deliver_later
519 520
    end
  end
D
Dmitriy Zaporozhets 已提交
521

522 523
  def relabeled_resource_email(target, labels, current_user, method)
    recipients = build_relabeled_recipients(target, current_user, labels: labels)
524 525 526
    label_names = labels.map(&:name)

    recipients.each do |recipient|
527
      mailer.send(method, recipient.id, target.id, label_names, current_user.id).deliver_later
528 529 530
    end
  end

531
  def reopen_resource_email(target, project, current_user, method, status)
532
    recipients = build_recipients(target, project, current_user, action: "reopen")
533 534

    recipients.each do |recipient|
V
Valery Sizov 已提交
535
      mailer.send(method, recipient.id, target.id, status, current_user.id).deliver_later
536 537 538
    end
  end

539
  def build_recipients(target, project, current_user, action: nil, previous_assignee: nil)
540 541
    custom_action = build_custom_key(action, target)

542
    recipients = target.participants(current_user)
D
Fix.  
Douwe Maan 已提交
543
    recipients = add_project_watchers(recipients, project)
544

545
    recipients = add_custom_notifications(recipients, project, custom_action)
546
    recipients = reject_mention_users(recipients, project)
547

548 549
    recipients = recipients.uniq

550 551 552
    # 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
553
    if [:reassign_merge_request, :reassign_issue].include?(custom_action)
554
      recipients << previous_assignee if previous_assignee
555 556 557 558
      recipients << target.assignee
    end

    recipients = reject_muted_users(recipients, project)
V
Valery Sizov 已提交
559
    recipients = add_subscribed_users(recipients, target)
560

561
    if [:new_issue, :new_merge_request].include?(custom_action)
562 563 564
      recipients = add_labels_subscribers(recipients, target)
    end

V
Valery Sizov 已提交
565
    recipients = reject_unsubscribed_users(recipients, target)
566
    recipients = reject_users_without_access(recipients, target)
567

568
    recipients.delete(current_user)
569
    recipients.uniq
570 571
  end

572 573
  def build_relabeled_recipients(target, current_user, labels:)
    recipients = add_labels_subscribers([], target, labels: labels)
574
    recipients = reject_unsubscribed_users(recipients, target)
575
    recipients = reject_users_without_access(recipients, target)
576 577 578 579
    recipients.delete(current_user)
    recipients.uniq
  end

D
Dmitriy Zaporozhets 已提交
580
  def mailer
V
Valery Sizov 已提交
581
    Notify
D
Dmitriy Zaporozhets 已提交
582
  end
583 584 585 586

  def previous_record(object, attribute)
    if object && attribute
      if object.previous_changes.include?(attribute)
M
Marin Jankovski 已提交
587
        object.previous_changes[attribute].first
588 589 590
      end
    end
  end
591 592 593 594 595 596

  # 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
597
end