diff --git a/app/models/user.rb b/app/models/user.rb index cc1841a61b..01aad3c7c2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -76,6 +76,7 @@ class User < Principal ['all', :label_user_mail_option_all], ['selected', :label_user_mail_option_selected], ['only_my_events', :label_user_mail_option_only_my_events], + ['only_bookmarked_projects', :label_user_mail_option_only_bookmarked_projects], ['only_assigned', :label_user_mail_option_only_assigned], ['only_owner', :label_user_mail_option_only_owner], ['none', :label_user_mail_option_none] @@ -509,6 +510,11 @@ class User < Principal members.update_all(:mail_notification => false) members.where(:project_id => ids).update_all(:mail_notification => true) if ids.any? end + if saved_change_to_mail_notification? && mail_notification == 'only_bookmarked_projects' + ids = self.bookmarked_project_ids + self.members.update_all(mail_notification: false) + self.members.where(project_id: ids).update_all(mail_notification: true) if ids.any? + end end private :update_notified_project_ids @@ -827,7 +833,7 @@ class User < Principal case object when Issue case mail_notification - when 'selected', 'only_my_events' + when 'selected', 'only_my_events', 'only_bookmarked_projects' # user receives notifications for created/assigned issues on unselected projects object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.previous_assignee) when 'only_assigned' diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 930effb673..f96491f12d 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -26,6 +26,7 @@ class UserPreference < ActiveRecord::Base serialize :others before_save :set_others_hash, :clear_unused_block_settings + after_save :update_notified_project_ids safe_attributes( 'hide_mail', @@ -193,4 +194,16 @@ class UserPreference < ActiveRecord::Base my_page_settings.keep_if {|block, settings| blocks.include?(block)} end private :clear_unused_block_settings + + def update_notified_project_ids + return if self.user.mail_notification != 'only_bookmarked_projects' || self.saved_change_to_others.blank? + + saved_change_to_bookmarked_project_ids = self.saved_change_to_others.compact.pluck(:bookmarked_project_ids) + if saved_change_to_bookmarked_project_ids[0] != saved_change_to_bookmarked_project_ids[1] + ids = self.user.bookmarked_project_ids + self.user.members.update_all(mail_notification: false) + self.user.members.where(project_id: ids).update_all(mail_notification: true) if ids.any? + end + end + private :update_notified_project_ids end diff --git a/config/locales/en.yml b/config/locales/en.yml index 2ac0affb83..a727bdecc5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -935,6 +935,7 @@ en: label_user_mail_option_only_my_events: "Only for things I watch or I'm involved in" 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_option_only_bookmarked_projects: "For any event on the my bookmarked projects" 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 %{prio} or higher" label_registration_activation_by_email: account activation by email diff --git a/config/locales/ja.yml b/config/locales/ja.yml index d72a082a00..a3ca4d261e 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -768,6 +768,7 @@ ja: label_user_mail_option_only_my_events: "ウォッチ中または自分が関係しているもの" label_user_mail_option_only_assigned: "ウォッチ中または自分が担当しているもの" label_user_mail_option_only_owner: "ウォッチ中または自分が作成したもの" + label_user_mail_option_only_bookmarked_projects: "ブックマークしているプロジェクトのすべての通知" label_user_mail_no_self_notified: 自分自身による変更の通知は不要 label_registration_activation_by_email: メールでアカウントを有効化 label_registration_manual_activation: 手動でアカウントを有効化 diff --git a/test/functional/my_controller_test.rb b/test/functional/my_controller_test.rb index 2bf8b3516f..a8544558ce 100644 --- a/test/functional/my_controller_test.rb +++ b/test/functional/my_controller_test.rb @@ -510,6 +510,29 @@ class MyControllerTest < Redmine::ControllerTest assert [mail.bcc, mail.cc].flatten.include?('foobar@example.com') end + def test_update_account_mail_notification_to_only_bookmarked_projects_should_update_members + user = User.find(2) + jump_box = Redmine::ProjectJumpBox.new user + jump_box.bookmark_project user.members.order(:id).first.project + jump_box.delete_project_bookmark user.members.order(:id).second.project + jump_box.delete_project_bookmark user.members.order(:id).third.project + + assert_equal 'all', user.mail_notification + assert_equal [true, true, true], user.members.order(:id).pluck(:mail_notification) + put( + :account, + params: { + user: { + mail_notification: 'only_bookmarked_projects' + + } + } + ) + + assert_equal 'only_bookmarked_projects', user.reload.mail_notification + assert_equal [true, false, false], user.members.order(:id).pluck(:mail_notification) + end + def test_my_account_notify_about_high_priority_issues_preference # normally, preference should be shown get :account diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb index 2c3cb2968e..4b32b69f92 100644 --- a/test/functional/projects_controller_test.rb +++ b/test/functional/projects_controller_test.rb @@ -1305,6 +1305,19 @@ class ProjectsControllerTest < Redmine::ControllerTest refute jb.bookmark?(Project.find('ecookbook')) end + def test_bookmark_should_change_bookmark_and_update_members_if_mail_notification_is_only_bookmarked_projects + user = User.find(2) + @request.session[:user_id] = user.id + user.update(mail_notification: 'only_bookmarked_projects') + assert_equal [false, false, false], user.members.order(:id).pluck(:mail_notification) + + post(:bookmark, :params => {:id => 'ecookbook'}) + assert_equal [true, false, false], user.members.order(:id).pluck(:mail_notification) + + delete(:bookmark, :params => {:id => 'ecookbook'}) + assert_equal [false, false, false], user.members.order(:id).pluck(:mail_notification) + end + def test_index_jump_without_project_id_should_redirect_to_active_tab get(:index, :params => {:jump => 'issues'}) assert_redirected_to '/issues' diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index 2c23feb833..6d37456a95 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -1020,6 +1020,9 @@ class ProjectTest < ActiveSupport::TestCase only_my_events_user = User.generate!(:mail_notification => 'only_my_events') Member.create!(:project => project, :roles => [role], :principal => only_my_events_user) + only_bookmarked_projects_user = User.generate!(:mail_notification => 'only_bookmarked_projects') + Member.create!(:project => project, :roles => [role], :principal => only_bookmarked_projects_user) + only_assigned_user = User.generate!(:mail_notification => 'only_assigned') Member.create!(:project => project, :roles => [role], :principal => only_assigned_user) @@ -1034,6 +1037,8 @@ class ProjectTest < ActiveSupport::TestCase "should not include users with the 'none' notification option" assert !project.notified_users.include?(only_my_events_user), "should not include users with the 'only_my_events' notification option" + assert !project.notified_users.include?(only_bookmarked_projects_user), + "should not include users with the 'only_bookmarked_projects' notification option" assert !project.notified_users.include?(only_assigned_user), "should not include users with the 'only_assigned' notification option" assert !project.notified_users.include?(only_owned_user), diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index c395bb310e..b52d721bea 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -1036,15 +1036,15 @@ class UserTest < ActiveSupport::TestCase def test_valid_notification_options # without memberships - assert_equal 5, User.find(7).valid_notification_options.size + assert_equal 6, User.find(7).valid_notification_options.size # with memberships - assert_equal 6, User.find(2).valid_notification_options.size + assert_equal 7, User.find(2).valid_notification_options.size end def test_valid_notification_options_class_method - assert_equal 5, User.valid_notification_options.size - assert_equal 5, User.valid_notification_options(User.find(7)).size - assert_equal 6, User.valid_notification_options(User.find(2)).size + assert_equal 6, User.valid_notification_options.size + assert_equal 6, User.valid_notification_options(User.find(7)).size + assert_equal 7, User.valid_notification_options(User.find(2)).size end def test_notified_project_ids_setter_should_coerce_to_unique_integer_array @@ -1243,8 +1243,8 @@ class UserTest < ActiveSupport::TestCase issue = Issue.generate!(:project => project, :assigned_to => assignee, :author => author) tests = { - author => %w(all only_my_events only_owner selected), - assignee => %w(all only_my_events only_assigned selected), + author => %w(all only_my_events only_owner selected only_bookmarked_projects), + assignee => %w(all only_my_events only_assigned selected only_bookmarked_projects), member => %w(all) }