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
V
Valery Sizov 已提交
134
    return true if note.is_award
135

136
    target = note.noteable
137

138
    recipients = []
139

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

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

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

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

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

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

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

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

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

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

D
Douwe Maan 已提交
185
  def decline_project_invite(project_member)
V
Valery Sizov 已提交
186 187 188 189 190 191
    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 已提交
192 193
  end

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

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

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

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

D
Douwe Maan 已提交
210
  def decline_group_invite(group_member)
V
Valery Sizov 已提交
211 212 213 214 215 216
    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 已提交
217 218
  end

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

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

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

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

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

250 251
  protected

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

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

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

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

266
  def project_member_notification(project, notification_level=nil)
267
    project_members = project.project_members
268

269 270 271 272 273
    if notification_level
      project_members.where(notification_level: notification_level).pluck(:user_id)
    else
      project_members.pluck(:user_id)
    end
274 275
  end

276
  def group_member_notification(project, notification_level)
277
    if project.group
278
      project.group.group_members.where(notification_level: notification_level).pluck(:user_id)
279 280
    else
      []
281
    end
282
  end
283

284 285 286 287 288 289 290 291
  def users_with_global_level_watch(ids)
    User.where(
      id: ids,
      notification_level: Notification::N_WATCH
    ).pluck(:id)
  end

  # Build a list of users based on project notifcation settings
292 293
  def select_project_member_setting(project, global_setting, users_global_level_watch)
    users = project_member_notification(project, Notification::N_WATCH)
294

295 296 297 298
    # 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
299 300
      end
    end
301 302

    users
303 304
  end

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

    # 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
314 315 316
      end
    end

317 318 319 320
    # 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
321 322 323
      end
    end

324
    users
325 326
  end

327 328 329 330
  def add_project_watchers(recipients, project)
    recipients.concat(project_watchers(project)).compact.uniq
  end

331 332
  # Remove users with disabled notifications from array
  # Also remove duplications and nil recipients
333
  def reject_muted_users(users, project = nil)
334
    reject_users(users, :disabled?, project)
335 336
  end

337 338
  # Remove users with notification level 'Mentioned'
  def reject_mention_users(users, project = nil)
339 340 341 342 343 344 345 346 347
    reject_users(users, :mention?, project)
  end

  # Reject users which method_name from notification object returns true.
  #
  # Example:
  #   reject_users(users, :watch?, project)
  #
  def reject_users(users, method_name, project = nil)
348
    users = users.to_a.compact.uniq
349
    users = users.reject(&:blocked?)
350 351

    users.reject do |user|
352
      next user.notification.send(method_name) unless project
353

D
Douwe Maan 已提交
354
      member = project.project_members.find_by(user_id: user.id)
355

D
Douwe Maan 已提交
356 357
      if !member && project.group
        member = project.group.group_members.find_by(user_id: user.id)
358 359 360
      end

      # reject users who globally set mention notification and has no membership
361
      next user.notification.send(method_name) unless member
362 363

      # reject users who set mention notification in project
364
      next true if member.notification.send(method_name)
365 366

      # reject users who have N_MENTION in project and disabled in global settings
367
      member.notification.global? && user.notification.send(method_name)
368 369 370
    end
  end

V
Valery Sizov 已提交
371
  def reject_unsubscribed_users(recipients, target)
V
Valery Sizov 已提交
372
    return recipients unless target.respond_to? :subscriptions
373

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

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

391
    recipients + target.subscribers
V
Valery Sizov 已提交
392
  end
393

394
  def add_labels_subscribers(recipients, target, labels: nil)
395 396
    return recipients unless target.respond_to? :labels

397 398
    (labels || target.labels).each do |label|
      recipients += label.subscribers
399 400 401 402 403
    end

    recipients
  end

I
Izaak Alpert 已提交
404
  def new_resource_email(target, project, method)
405
    recipients = build_recipients(target, project, target.author, action: :new)
406 407

    recipients.each do |recipient|
V
Valery Sizov 已提交
408
      mailer.send(method, recipient.id, target.id).deliver_later
409 410 411
    end
  end

I
Izaak Alpert 已提交
412
  def close_resource_email(target, project, current_user, method)
413
    recipients = build_recipients(target, project, current_user)
414 415

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

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

424
    recipients = build_recipients(target, project, current_user, action: :reassign, previous_assignee: previous_assignee)
425

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

437 438
  def relabeled_resource_email(target, labels, current_user, method)
    recipients = build_relabeled_recipients(target, current_user, labels: labels)
439 440 441
    label_names = labels.map(&:name)

    recipients.each do |recipient|
442
      mailer.send(method, recipient.id, target.id, label_names, current_user.id).deliver_later
443 444 445
    end
  end

446
  def reopen_resource_email(target, project, current_user, method, status)
447
    recipients = build_recipients(target, project, current_user)
448 449

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

454
  def build_recipients(target, project, current_user, action: nil, previous_assignee: nil)
455
    recipients = target.participants(current_user)
456

D
Fix.  
Douwe Maan 已提交
457
    recipients = add_project_watchers(recipients, project)
458
    recipients = reject_mention_users(recipients, project)
459

460 461 462 463
    # 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
464
      recipients << previous_assignee if previous_assignee
465 466 467 468
      recipients << target.assignee
    end

    recipients = reject_muted_users(recipients, project)
V
Valery Sizov 已提交
469
    recipients = add_subscribed_users(recipients, target)
470 471 472 473 474

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

V
Valery Sizov 已提交
475
    recipients = reject_unsubscribed_users(recipients, target)
476
    recipients = reject_users_without_access(recipients, target)
477

478
    recipients.delete(current_user)
479
    recipients.uniq
480 481
  end

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

D
Dmitriy Zaporozhets 已提交
490
  def mailer
V
Valery Sizov 已提交
491
    Notify
D
Dmitriy Zaporozhets 已提交
492
  end
493 494 495 496

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