Project

General

Profile

Feature #32628 » 0004-Enable-users-to-receive-email-notifications-about-hi.patch

Jan from Planio www.plan.io, 2020-01-08 04:57

View differences:

app/models/issue.rb
1042 1042
    notified = notified.select {|u| u.active? && u.notify_about?(self)}
1043 1043

  
1044 1044
    notified += project.notified_users
1045
    notified += project.users.preload(:preference).select(&:notify_about_high_priority_issues?) if priority.high?
1045 1046
    notified.uniq!
1046 1047
    # Remove users that can not view the issue
1047 1048
    notified.reject! {|user| !visible?(user)}
app/models/user.rb
811 811
    end
812 812
  end
813 813

  
814
  def notify_about_high_priority_issues?
815
    self.pref.notify_about_high_priority_issues
816
  end
817

  
814 818
  def self.current=(user)
815 819
    RequestStore.store[:current_user] = user
816 820
  end
app/models/user_preference.rb
33 33
    'comments_sorting',
34 34
    'warn_on_leaving_unsaved',
35 35
    'no_self_notified',
36
    'notify_about_high_priority_issues',
36 37
    'textarea_font',
37 38
    'recently_used_projects',
38 39
    'history_default_tab')
......
51 52
      unless attributes && attributes.key?(:no_self_notified)
52 53
        self.no_self_notified = true
53 54
      end
55
      unless attributes && attributes.key?(:notify_about_high_priority_issues)
56
        self.notify_about_high_priority_issues = true
57
      end
54 58
    end
55 59
    self.others ||= {}
56 60
  end
......
87 91
  def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
88 92
  def no_self_notified=(value); self[:no_self_notified]=value; end
89 93

  
94
  def notify_about_high_priority_issues; (self[:notify_about_high_priority_issues] == true || self[:notify_about_high_priority_issues] == '1'); end
95
  def notify_about_high_priority_issues=(value); self[:notify_about_high_priority_issues]=value; end
96

  
90 97
  def activity_scope; Array(self[:activity_scope]) ; end
91 98
  def activity_scope=(value); self[:activity_scope]=value ; end
92 99

  
app/views/users/_mail_notifications.html.erb
10 10
<%= content_tag 'fieldset', :id => 'notified-projects', :style => (@user.mail_notification == 'selected' ? '' : 'display:none;') do %>
11 11
  <legend><%= toggle_checkboxes_link("#notified-projects input[type=checkbox]") %><%=l(:label_project_plural)%></legend>
12 12
  <%= render_project_nested_lists(@user.projects) do |project|
13
        content_tag('label', 
13
        content_tag('label',
14 14
          check_box_tag(
15 15
            'user[notified_project_ids][]',
16 16
             project.id,
......
24 24
<% end %>
25 25

  
26 26
<%= fields_for :pref, @user.pref do |pref_fields| %>
27

  
28
<% if IssuePriority.default_or_middle and high_priority = IssuePriority.where(['position > ?', IssuePriority.default_or_middle.position]).first %>
29
<p>
30
  <%= pref_fields.check_box :notify_about_high_priority_issues %>
31
  <label for="pref_notify_about_high_priority_issues"><%= t(:label_user_mail_notify_about_high_priority_issues_html, prio: high_priority.name.downcase) %></label>
32
</p>
33
<% end %>
27 34
<p>
28 35
  <%= pref_fields.check_box :no_self_notified %>
29 36
  <label for="pref_no_self_notified"><%= l(:label_user_mail_no_self_notified) %></label>
config/locales/en.yml
908 908
  label_user_mail_option_only_assigned: "Only for things I watch or I am assigned to"
909 909
  label_user_mail_option_only_owner: "Only for things I watch or I am the owner of"
910 910
  label_user_mail_no_self_notified: "I don't want to be notified of changes that I make myself"
911
  label_user_mail_notify_about_high_priority_issues_html: "Also notify me about issues with a priority of <em>%{prio}</em> or higher"
911 912
  label_registration_activation_by_email: account activation by email
912 913
  label_registration_manual_activation: manual account activation
913 914
  label_registration_automatic_activation: automatic account activation
test/functional/my_controller_test.rb
428 428
    assert [mail.bcc, mail.cc].flatten.include?('foobar@example.com')
429 429
  end
430 430

  
431
  def test_my_account_notify_about_high_priority_issues_preference
432

  
433
    # normally, preference should be shown
434
    get :account
435
    assert_select 'label[for="pref_notify_about_high_priority_issues"]'
436

  
437
    # preference should be persisted
438
    put :account, :params => {
439
        :pref => {
440
          notify_about_high_priority_issues: '1'
441
        }
442
      }
443
    assert User.find(2).notify_about_high_priority_issues?
444

  
445
    # preference should be hidden if there aren't any priorities
446
    Issue.destroy_all
447
    IssuePriority.destroy_all
448
    get :account
449
    assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
450

  
451
    # preference should be hidden if there isn't a "high" priority
452
    a = IssuePriority.create! name: 'A'
453
    get :account
454
    assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
455

  
456
    # preference should be shown if there are at least two priorities (one low, one high)
457
    b = IssuePriority.create! name: 'B'
458
    get :account
459
    assert_select 'label[for="pref_notify_about_high_priority_issues"]'
460

  
461
    # preference should be hidden if the highest priority is the default one,
462
    # because that means that there is no "high" priority
463
    b.update! is_default: true
464
    get :account
465
    assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
466
  end
467

  
431 468
  def test_my_account_should_show_destroy_link
432 469
    get :account
433 470
    assert_select 'a[href="/my/account/destroy"]'
test/unit/issue_test.rb
2928 2928
    assert !issue.recipients.include?(issue.assigned_to.mail)
2929 2929
  end
2930 2930

  
2931
  test "Issue#recipients should include users who want to be notified about high issues but only when issue has high priority" do
2932
    user = User.generate!
2933
    user.pref.update! notify_about_high_priority_issues: true
2934
    Member.create!(:project_id => 1, :principal => user, :role_ids => [1])
2935

  
2936
    # creation with high prio
2937
    issue = Issue.generate!(priority: IssuePriority.find(6))
2938
    assert issue.recipients.include?(user.mail)
2939

  
2940
    # creation with default prio
2941
    issue = Issue.generate!
2942
    assert !issue.recipients.include?(user.mail)
2943

  
2944
    # update prio to high
2945
    issue.update! priority: IssuePriority.find(6)
2946
    assert issue.recipients.include?(user.mail)
2947

  
2948
    # update prio to low
2949
    issue.update! priority: IssuePriority.find(4)
2950
    assert !issue.recipients.include?(user.mail)
2951
  end
2952

  
2953
  test "Authors who don't want to be self-notified should not receive emails even when issue has high priority" do
2954
    user = User.generate!
2955
    user.pref.update! notify_about_high_priority_issues: true
2956
    user.pref.update! no_self_notified: true
2957

  
2958
    project = Project.find(1)
2959
    project.memberships.destroy_all
2960
    Member.create!(:project_id => 1, :principal => user, :role_ids => [1])
2961

  
2962
    ActionMailer::Base.deliveries.clear
2963
    Issue.create(author: user,
2964
                 priority: IssuePriority.find(6),
2965
                 subject: 'test create',
2966
                 project: project,
2967
                 tracker: Tracker.first,
2968
                 status: IssueStatus.first)
2969
    assert ActionMailer::Base.deliveries.empty?
2970
  end
2971

  
2931 2972
  def test_last_journal_id_with_journals_should_return_the_journal_id
2932 2973
    assert_equal 2, Issue.find(1).last_journal_id
2933 2974
  end
(4-4/5)