]> source.dussan.org Git - redmine.git/commitdiff
Enable users to receive email notifications about high issues (only) (#32628).
authorGo MAEDA <maeda@farend.jp>
Tue, 21 Jan 2020 04:23:26 +0000 (04:23 +0000)
committerGo MAEDA <maeda@farend.jp>
Tue, 21 Jan 2020 04:23:26 +0000 (04:23 +0000)
Patch by Jan Schulz-Hofen.

git-svn-id: http://svn.redmine.org/redmine/trunk@19449 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
app/models/user.rb
app/models/user_preference.rb
app/views/users/_mail_notifications.html.erb
config/locales/en.yml
test/functional/my_controller_test.rb
test/unit/issue_test.rb

index efb55fafbbfdec7713756f0b3fe872c2eafb04f0..63bc8cabe06fce6ac7e513f7fb621f927cce46da 100644 (file)
@@ -1042,6 +1042,7 @@ class Issue < ActiveRecord::Base
     notified = notified.select {|u| u.active? && u.notify_about?(self)}
 
     notified += project.notified_users
+    notified += project.users.preload(:preference).select(&:notify_about_high_priority_issues?) if priority.high?
     notified.uniq!
     # Remove users that can not view the issue
     notified.reject! {|user| !visible?(user)}
index e291bd98b82232242becc245f647458d9744cec7..a837697a4504d742cb6e2aa6850a602a8103038d 100644 (file)
@@ -811,6 +811,10 @@ class User < Principal
     end
   end
 
+  def notify_about_high_priority_issues?
+    self.pref.notify_about_high_priority_issues
+  end
+
   def self.current=(user)
     RequestStore.store[:current_user] = user
   end
index 3f0d9743c8bb5b3560e48f555406bf2aa1023bb2..f4317025ffb9599aa2712eb8bd3e72422734f550 100644 (file)
@@ -33,6 +33,7 @@ class UserPreference < ActiveRecord::Base
     'comments_sorting',
     'warn_on_leaving_unsaved',
     'no_self_notified',
+    'notify_about_high_priority_issues',
     'textarea_font',
     'recently_used_projects',
     'history_default_tab',
@@ -89,6 +90,9 @@ class UserPreference < ActiveRecord::Base
   def no_self_notified; (self[:no_self_notified] == true || self[:no_self_notified] == '1'); end
   def no_self_notified=(value); self[:no_self_notified]=value; end
 
+  def notify_about_high_priority_issues; (self[:notify_about_high_priority_issues] == true || self[:notify_about_high_priority_issues] == '1'); end
+  def notify_about_high_priority_issues=(value); self[:notify_about_high_priority_issues]=value; end
+
   def activity_scope; Array(self[:activity_scope]) ; end
   def activity_scope=(value); self[:activity_scope]=value ; end
 
index 85842de56a729b3f8b23950a55d4899782ce0f20..abf890e46afd07b836e0ec146149d0ec4b0a8c34 100644 (file)
@@ -10,7 +10,7 @@
 <%= content_tag 'fieldset', :id => 'notified-projects', :style => (@user.mail_notification == 'selected' ? '' : 'display:none;') do %>
   <legend><%= toggle_checkboxes_link("#notified-projects input[type=checkbox]") %><%=l(:label_project_plural)%></legend>
   <%= render_project_nested_lists(@user.projects) do |project|
-        content_tag('label', 
+        content_tag('label',
           check_box_tag(
             'user[notified_project_ids][]',
              project.id,
 <% end %>
 
 <%= fields_for :pref, @user.pref do |pref_fields| %>
+
+<% if IssuePriority.default_or_middle and high_priority = IssuePriority.where(['position > ?', IssuePriority.default_or_middle.position]).first %>
+<p>
+  <%= pref_fields.check_box :notify_about_high_priority_issues %>
+  <label for="pref_notify_about_high_priority_issues"><%= t(:label_user_mail_notify_about_high_priority_issues_html, prio: high_priority.name.downcase) %></label>
+</p>
+<% end %>
 <p>
   <%= pref_fields.check_box :no_self_notified %>
   <label for="pref_no_self_notified"><%= l(:label_user_mail_no_self_notified) %></label>
index 3df365c090a59460f7dd787e0bf400045990337c..3f0fd2d77df5b29b2620faa2de47e4e52a6dad0f 100644 (file)
@@ -909,6 +909,7 @@ en:
   label_user_mail_option_only_assigned: "Only for things I watch or I am assigned to"
   label_user_mail_option_only_owner: "Only for things I watch or I am the owner of"
   label_user_mail_no_self_notified: "I don't want to be notified of changes that I make myself"
+  label_user_mail_notify_about_high_priority_issues_html: "Also notify me about issues with a priority of <em>%{prio}</em> or higher"
   label_registration_activation_by_email: account activation by email
   label_registration_manual_activation: manual account activation
   label_registration_automatic_activation: automatic account activation
index d5796b5518c98b9108dbe4d0608bdb1b4234f9ea..52dbcb85ae1572b04b1210a44d0e3cf1783bc268 100644 (file)
@@ -428,6 +428,43 @@ class MyControllerTest < Redmine::ControllerTest
     assert [mail.bcc, mail.cc].flatten.include?('foobar@example.com')
   end
 
+  def test_my_account_notify_about_high_priority_issues_preference
+
+    # normally, preference should be shown
+    get :account
+    assert_select 'label[for="pref_notify_about_high_priority_issues"]'
+
+    # preference should be persisted
+    put :account, :params => {
+        :pref => {
+          notify_about_high_priority_issues: '1'
+        }
+      }
+    assert User.find(2).notify_about_high_priority_issues?
+
+    # preference should be hidden if there aren't any priorities
+    Issue.destroy_all
+    IssuePriority.destroy_all
+    get :account
+    assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
+
+    # preference should be hidden if there isn't a "high" priority
+    a = IssuePriority.create! name: 'A'
+    get :account
+    assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
+
+    # preference should be shown if there are at least two priorities (one low, one high)
+    b = IssuePriority.create! name: 'B'
+    get :account
+    assert_select 'label[for="pref_notify_about_high_priority_issues"]'
+
+    # preference should be hidden if the highest priority is the default one,
+    # because that means that there is no "high" priority
+    b.update! is_default: true
+    get :account
+    assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
+  end
+
   def test_my_account_should_show_destroy_link
     get :account
     assert_select 'a[href="/my/account/destroy"]'
index 3203e2a3fa30799ad5d769ac49a17abf1a4cfcff..346208d00575930ed3e77aa1dd3a10b9b86f9101 100644 (file)
@@ -2928,6 +2928,47 @@ class IssueTest < ActiveSupport::TestCase
     assert !issue.recipients.include?(issue.assigned_to.mail)
   end
 
+  test "Issue#recipients should include users who want to be notified about high issues but only when issue has high priority" do
+    user = User.generate!
+    user.pref.update! notify_about_high_priority_issues: true
+    Member.create!(:project_id => 1, :principal => user, :role_ids => [1])
+
+    # creation with high prio
+    issue = Issue.generate!(priority: IssuePriority.find(6))
+    assert issue.recipients.include?(user.mail)
+
+    # creation with default prio
+    issue = Issue.generate!
+    assert !issue.recipients.include?(user.mail)
+
+    # update prio to high
+    issue.update! priority: IssuePriority.find(6)
+    assert issue.recipients.include?(user.mail)
+
+    # update prio to low
+    issue.update! priority: IssuePriority.find(4)
+    assert !issue.recipients.include?(user.mail)
+  end
+
+  test "Authors who don't want to be self-notified should not receive emails even when issue has high priority" do
+    user = User.generate!
+    user.pref.update! notify_about_high_priority_issues: true
+    user.pref.update! no_self_notified: true
+
+    project = Project.find(1)
+    project.memberships.destroy_all
+    Member.create!(:project_id => 1, :principal => user, :role_ids => [1])
+
+    ActionMailer::Base.deliveries.clear
+    Issue.create(author: user,
+                 priority: IssuePriority.find(6),
+                 subject: 'test create',
+                 project: project,
+                 tracker: Tracker.first,
+                 status: IssueStatus.first)
+    assert ActionMailer::Base.deliveries.empty?
+  end
+
   def test_last_journal_id_with_journals_should_return_the_journal_id
     assert_equal 2, Issue.find(1).last_journal_id
   end