notification_service.rb 15.6 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 33
  #
  def new_issue(issue, current_user)
I
Izaak Alpert 已提交
34
    new_resource_email(issue, issue.project, 'new_issue_email')
35 36
  end

37
  # When we close an issue we should send an email to:
38
  #
39 40
  #  * issue author if their notification level is not Disabled
  #  * issue assignee if their notification level is not Disabled
41 42 43
  #  * project team members with notification level higher then Participating
  #
  def close_issue(issue, current_user)
I
Izaak Alpert 已提交
44
    close_resource_email(issue, issue.project, current_user, 'closed_issue_email')
45 46
  end

47
  # When we reassign an issue we should send an email to:
48
  #
49 50
  #  * issue old assignee if their notification level is not Disabled
  #  * issue new assignee if their notification level is not Disabled
51 52
  #
  def reassigned_issue(issue, current_user)
I
Izaak Alpert 已提交
53
    reassign_resource_email(issue, issue.project, current_user, 'reassigned_issue_email')
54 55
  end

56
  # When we add labels to an issue we should send an email to:
57
  #
58 59 60 61
  #  * watchers of the issue's labels
  #
  def relabeled_issue(issue, added_labels, current_user)
    relabeled_resource_email(issue, added_labels, current_user, 'relabeled_issue_email')
62 63
  end

64
  # When create a merge request we should send an email to:
65
  #
66
  #  * mr assignee if their notification level is not Disabled
67 68
  #  * project team members with notification level higher then Participating
  #  * watchers of the mr's labels
69 70
  #
  def new_merge_request(merge_request, current_user)
I
Izaak Alpert 已提交
71
    new_resource_email(merge_request, merge_request.target_project, 'new_merge_request_email')
72
  end
73

74
  # When we reassign a merge_request we should send an email to:
75
  #
76 77
  #  * merge_request old assignee if their notification level is not Disabled
  #  * merge_request assignee if their notification level is not Disabled
78 79
  #
  def reassigned_merge_request(merge_request, current_user)
I
Izaak Alpert 已提交
80
    reassign_resource_email(merge_request, merge_request.target_project, current_user, 'reassigned_merge_request_email')
81
  end
82

83
  # When we add labels to a merge request we should send an email to:
84
  #
85 86 87 88
  #  * watchers of the mr's labels
  #
  def relabeled_merge_request(merge_request, added_labels, current_user)
    relabeled_resource_email(merge_request, added_labels, current_user, 'relabeled_merge_request_email')
89 90
  end

91
  def close_mr(merge_request, current_user)
I
Izaak Alpert 已提交
92
    close_resource_email(merge_request, merge_request.target_project, current_user, 'closed_merge_request_email')
93 94
  end

95 96 97 98
  def reopen_issue(issue, current_user)
    reopen_resource_email(issue, issue.project, current_user, 'issue_status_changed_email', 'reopened')
  end

99
  def merge_mr(merge_request, current_user)
V
Valery Sizov 已提交
100 101 102 103 104 105
    close_resource_email(
      merge_request,
      merge_request.target_project,
      current_user,
      'merged_merge_request_email'
    )
106 107
  end

M
Marin Jankovski 已提交
108
  def reopen_mr(merge_request, current_user)
V
Valery Sizov 已提交
109 110 111
    reopen_resource_email(
      merge_request,
      merge_request.target_project,
112 113
      current_user,
      'merge_request_status_email',
V
Valery Sizov 已提交
114 115
      'reopened'
    )
M
Marin Jankovski 已提交
116 117
  end

118
  # Notify new user with email after creation
119
  def new_user(user, token = nil)
J
Johannes Schleifenbaum 已提交
120
    # Don't email omniauth created users
V
Valery Sizov 已提交
121
    mailer.new_user_email(user.id, token).deliver_later unless user.identities.any?
122 123 124 125 126 127 128
  end

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

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

135
    target = note.noteable
136

137
    recipients = []
138

139 140 141 142 143
    mentioned_users = note.mentioned_users
    mentioned_users.select! do |user|
      user.can?(:read_project, note.project)
    end

D
Douwe Maan 已提交
144
    # Add all users participating in the thread (author, assignee, comment authors)
145
    participants =
146
      if target.respond_to?(:participants)
147
        target.participants(note.author)
D
Douwe Maan 已提交
148
      else
149
        mentioned_users
D
Douwe Maan 已提交
150 151
      end
    recipients = recipients.concat(participants)
152 153

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

D
Douwe Maan 已提交
156
    # Reject users with Mention notification level, except those mentioned in _this_ note.
157 158
    recipients = reject_mention_users(recipients - mentioned_users, note.project)
    recipients = recipients + mentioned_users
159

160
    recipients = reject_muted_users(recipients, note.project)
161

V
Valery Sizov 已提交
162 163
    recipients = add_subscribed_users(recipients, note.noteable)
    recipients = reject_unsubscribed_users(recipients, note.noteable)
164
    recipients = reject_users_without_access(recipients, note.noteable)
V
Valery Sizov 已提交
165

166
    recipients.delete(note.author)
167
    recipients = recipients.uniq
168 169 170 171

    # build notify method like 'note_commit_email'
    notify_method = "note_#{note.noteable_type.underscore}_email".to_sym
    recipients.each do |recipient|
V
Valery Sizov 已提交
172
      mailer.send(notify_method, recipient.id, note.id).deliver_later
173 174
    end
  end
175

D
Douwe Maan 已提交
176
  def invite_project_member(project_member, token)
V
Valery Sizov 已提交
177
    mailer.project_member_invited_email(project_member.id, token).deliver_later
D
Douwe Maan 已提交
178 179 180
  end

  def accept_project_invite(project_member)
V
Valery Sizov 已提交
181
    mailer.project_invite_accepted_email(project_member.id).deliver_later
D
Douwe Maan 已提交
182 183
  end

D
Douwe Maan 已提交
184
  def decline_project_invite(project_member)
V
Valery Sizov 已提交
185 186 187 188 189 190
    mailer.project_invite_declined_email(
      project_member.project.id,
      project_member.invite_email,
      project_member.access_level,
      project_member.created_by_id
    ).deliver_later
D
Douwe Maan 已提交
191 192
  end

193
  def new_project_member(project_member)
V
Valery Sizov 已提交
194
    mailer.project_access_granted_email(project_member.id).deliver_later
195 196
  end

197
  def update_project_member(project_member)
V
Valery Sizov 已提交
198
    mailer.project_access_granted_email(project_member.id).deliver_later
199
  end
200

D
Douwe Maan 已提交
201
  def invite_group_member(group_member, token)
V
Valery Sizov 已提交
202
    mailer.group_member_invited_email(group_member.id, token).deliver_later
D
Douwe Maan 已提交
203 204 205
  end

  def accept_group_invite(group_member)
V
Valery Sizov 已提交
206
    mailer.group_invite_accepted_email(group_member.id).deliver_later
D
Douwe Maan 已提交
207 208
  end

D
Douwe Maan 已提交
209
  def decline_group_invite(group_member)
V
Valery Sizov 已提交
210 211 212 213 214 215
    mailer.group_invite_declined_email(
      group_member.group.id,
      group_member.invite_email,
      group_member.access_level,
      group_member.created_by_id
    ).deliver_later
D
Douwe Maan 已提交
216 217
  end

218
  def new_group_member(group_member)
V
Valery Sizov 已提交
219
    mailer.group_access_granted_email(group_member.id).deliver_later
220 221
  end

222
  def update_group_member(group_member)
V
Valery Sizov 已提交
223
    mailer.group_access_granted_email(group_member.id).deliver_later
224 225
  end

226
  def project_was_moved(project, old_path_with_namespace)
227 228 229 230
    recipients = project.team.members
    recipients = reject_muted_users(recipients, project)

    recipients.each do |recipient|
V
Valery Sizov 已提交
231 232 233 234 235
      mailer.project_was_moved_email(
        project.id,
        recipient.id,
        old_path_with_namespace
      ).deliver_later
236 237 238
    end
  end

239 240 241 242 243 244 245 246 247 248
  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

249 250
  protected

251 252
  # Get project users with WATCH notification level
  def project_watchers(project)
253
    project_members = project_member_notification(project)
254

255 256
    users_with_project_level_global = project_member_notification(project, :global)
    users_with_group_level_global = group_member_notification(project, :global)
257
    users = users_with_global_level_watch([users_with_project_level_global, users_with_group_level_global].flatten.uniq)
258

259
    users_with_project_setting = select_project_member_setting(project, users_with_project_level_global, users)
260
    users_with_group_setting = select_group_member_setting(project, project_members, users_with_group_level_global, users)
261

262
    User.where(id: users_with_project_setting.concat(users_with_group_setting).uniq).to_a
263 264
  end

265
  def project_member_notification(project, notification_level=nil)
266
    if notification_level
267
      project.notification_settings.where(level: NotificationSetting.levels[notification_level]).pluck(:user_id)
268
    else
269
      project.notification_settings.pluck(:user_id)
270
    end
271 272
  end

273
  def group_member_notification(project, notification_level)
274
    if project.group
275
      project.group.notification_settings.where(level: NotificationSetting.levels[notification_level]).pluck(:user_id)
276 277
    else
      []
278
    end
279
  end
280

281 282 283
  def users_with_global_level_watch(ids)
    User.where(
      id: ids,
284
      notification_level: NotificationSetting.levels[:watch]
285 286 287 288
    ).pluck(:id)
  end

  # Build a list of users based on project notifcation settings
289
  def select_project_member_setting(project, global_setting, users_global_level_watch)
290
    users = project_member_notification(project, :watch)
291

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

    users
300 301
  end

S
Steven Burgart 已提交
302
  # Build a list of users based on group notification settings
303
  def select_group_member_setting(project, project_members, global_setting, users_global_level_watch)
304
    uids = group_member_notification(project, :watch)
305 306 307 308 309 310

    # 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
311 312 313
      end
    end

314 315 316 317
    # 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
318 319 320
      end
    end

321
    users
322 323
  end

324 325 326 327
  def add_project_watchers(recipients, project)
    recipients.concat(project_watchers(project)).compact.uniq
  end

328 329
  # Remove users with disabled notifications from array
  # Also remove duplications and nil recipients
330
  def reject_muted_users(users, project = nil)
331
    reject_users(users, :disabled, project)
332 333
  end

334 335
  # Remove users with notification level 'Mentioned'
  def reject_mention_users(users, project = nil)
336
    reject_users(users, :mention, project)
337 338
  end

339
  # Reject users which has certain notification level
340 341
  #
  # Example:
342
  #   reject_users(users, :watch, project)
343
  #
344 345 346 347 348 349 350
  def reject_users(users, level, project = nil)
    level = level.to_s

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

351
    users = users.to_a.compact.uniq
352
    users = users.reject(&:blocked?)
353 354

    users.reject do |user|
355
      next user.notification_level == level unless project
356

357
      setting = user.notification_settings_for(project)
358

359
      if !setting && project.group
360
        setting = user.notification_settings_for(project.group)
361 362
      end

363
      # reject users who globally set mention notification and has no setting per project/group
364
      next user.notification_level == level unless setting
365 366

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

369
      # reject users who have mention level in project and disabled in global settings
370
      setting.global? && user.notification_level == level
371 372 373
    end
  end

V
Valery Sizov 已提交
374
  def reject_unsubscribed_users(recipients, target)
V
Valery Sizov 已提交
375
    return recipients unless target.respond_to? :subscriptions
376

V
Valery Sizov 已提交
377
    recipients.reject do |user|
378 379
      subscription = target.subscriptions.find_by_user_id(user.id)
      subscription && !subscription.subscribed
V
Valery Sizov 已提交
380 381 382
    end
  end

383 384 385 386 387 388 389 390
  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 已提交
391
  def add_subscribed_users(recipients, target)
392
    return recipients unless target.respond_to? :subscribers
V
Valery Sizov 已提交
393

394
    recipients + target.subscribers
V
Valery Sizov 已提交
395
  end
396

397
  def add_labels_subscribers(recipients, target, labels: nil)
398 399
    return recipients unless target.respond_to? :labels

400 401
    (labels || target.labels).each do |label|
      recipients += label.subscribers
402 403 404 405 406
    end

    recipients
  end

I
Izaak Alpert 已提交
407
  def new_resource_email(target, project, method)
408
    recipients = build_recipients(target, project, target.author, action: :new)
409 410

    recipients.each do |recipient|
V
Valery Sizov 已提交
411
      mailer.send(method, recipient.id, target.id).deliver_later
412 413 414
    end
  end

I
Izaak Alpert 已提交
415
  def close_resource_email(target, project, current_user, method)
416
    recipients = build_recipients(target, project, current_user)
417 418

    recipients.each do |recipient|
V
Valery Sizov 已提交
419
      mailer.send(method, recipient.id, target.id, current_user.id).deliver_later
420 421 422
    end
  end

I
Izaak Alpert 已提交
423
  def reassign_resource_email(target, project, current_user, method)
424
    previous_assignee_id = previous_record(target, 'assignee_id')
425 426
    previous_assignee = User.find_by(id: previous_assignee_id) if previous_assignee_id

427
    recipients = build_recipients(target, project, current_user, action: :reassign, previous_assignee: previous_assignee)
428

429
    recipients.each do |recipient|
V
Valery Sizov 已提交
430 431 432 433 434 435 436
      mailer.send(
        method,
        recipient.id,
        target.id,
        previous_assignee_id,
        current_user.id
      ).deliver_later
437 438
    end
  end
D
Dmitriy Zaporozhets 已提交
439

440 441
  def relabeled_resource_email(target, labels, current_user, method)
    recipients = build_relabeled_recipients(target, current_user, labels: labels)
442 443 444
    label_names = labels.map(&:name)

    recipients.each do |recipient|
445
      mailer.send(method, recipient.id, target.id, label_names, current_user.id).deliver_later
446 447 448
    end
  end

449
  def reopen_resource_email(target, project, current_user, method, status)
450
    recipients = build_recipients(target, project, current_user)
451 452

    recipients.each do |recipient|
V
Valery Sizov 已提交
453
      mailer.send(method, recipient.id, target.id, status, current_user.id).deliver_later
454 455 456
    end
  end

457
  def build_recipients(target, project, current_user, action: nil, previous_assignee: nil)
458
    recipients = target.participants(current_user)
459

D
Fix.  
Douwe Maan 已提交
460
    recipients = add_project_watchers(recipients, project)
461
    recipients = reject_mention_users(recipients, project)
462

463 464 465 466
    # 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
    if action == :reassign
467
      recipients << previous_assignee if previous_assignee
468 469 470 471
      recipients << target.assignee
    end

    recipients = reject_muted_users(recipients, project)
V
Valery Sizov 已提交
472
    recipients = add_subscribed_users(recipients, target)
473 474 475 476 477

    if action == :new
      recipients = add_labels_subscribers(recipients, target)
    end

V
Valery Sizov 已提交
478
    recipients = reject_unsubscribed_users(recipients, target)
479
    recipients = reject_users_without_access(recipients, target)
480

481
    recipients.delete(current_user)
482
    recipients.uniq
483 484
  end

485 486
  def build_relabeled_recipients(target, current_user, labels:)
    recipients = add_labels_subscribers([], target, labels: labels)
487
    recipients = reject_unsubscribed_users(recipients, target)
488
    recipients = reject_users_without_access(recipients, target)
489 490 491 492
    recipients.delete(current_user)
    recipients.uniq
  end

D
Dmitriy Zaporozhets 已提交
493
  def mailer
V
Valery Sizov 已提交
494
    Notify
D
Dmitriy Zaporozhets 已提交
495
  end
496 497 498 499

  def previous_record(object, attribute)
    if object && attribute
      if object.previous_changes.include?(attribute)
M
Marin Jankovski 已提交
500
        object.previous_changes[attribute].first
501 502 503
      end
    end
  end
504
end