Project

General

Profile

Feature #35189 » feature-35189-v3.patch

Go MAEDA, 2023-11-02 07:54

View differences:

app/models/user.rb
75 75
  MAIL_NOTIFICATION_OPTIONS = [
76 76
    ['all', :label_user_mail_option_all],
77 77
    ['selected', :label_user_mail_option_selected],
78
    ['bookmarked', :label_user_mail_option_bookmarked],
78 79
    ['only_my_events', :label_user_mail_option_only_my_events],
79 80
    ['only_assigned', :label_user_mail_option_only_assigned],
80 81
    ['only_owner', :label_user_mail_option_only_owner],
......
497 498

  
498 499
  # Updates per project notifications (after_save callback)
499 500
  def update_notified_project_ids
501
    update_member_notifications = lambda do |ids|
502
      members.update_all(mail_notification: false)
503
      members.where(project_id: ids).update_all(mail_notification: true) if ids.present?
504
    end
505

  
500 506
    if @notified_projects_ids_changed
501
      ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : [])
502
      members.update_all(:mail_notification => false)
503
      members.where(:project_id => ids).update_all(:mail_notification => true) if ids.any?
507
      selected_ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : [])
508
      update_member_notifications.call(selected_ids)
509
    end
510
    if saved_change_to_mail_notification? && mail_notification == 'bookmarked'
511
      bookmarked_ids = self.bookmarked_project_ids
512
      update_member_notifications.call(bookmarked_ids)
504 513
    end
505 514
  end
506 515
  private :update_notified_project_ids
......
834 843
      case object
835 844
      when Issue
836 845
        case mail_notification
837
        when 'selected', 'only_my_events'
846
        when 'selected', 'only_my_events', 'bookmarked'
838 847
          # user receives notifications for created/assigned issues on unselected projects
839 848
          object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.previous_assignee)
840 849
        when 'only_assigned'
app/models/user_preference.rb
26 26
  serialize :others
27 27

  
28 28
  before_save :set_others_hash, :clear_unused_block_settings
29
  after_save :update_notified_project_ids
29 30

  
30 31
  safe_attributes(
31 32
    'hide_mail',
......
210 211
    my_page_settings.keep_if {|block, settings| blocks.include?(block)}
211 212
  end
212 213
  private :clear_unused_block_settings
214

  
215
  def update_notified_project_ids
216
    return if self.user.mail_notification != 'bookmarked' || self.saved_change_to_others.blank?
217

  
218
    saved_change_to_bookmarked_project_ids = self.saved_change_to_others.compact.pluck(:bookmarked_project_ids)
219
    if saved_change_to_bookmarked_project_ids[0] != saved_change_to_bookmarked_project_ids[1]
220
      ids = self.user.bookmarked_project_ids
221
      self.user.members.update_all(mail_notification: false)
222
      self.user.members.where(project_id: ids).update_all(mail_notification: true) if ids.any?
223
    end
224
  end
225
  private :update_notified_project_ids
213 226
end
config/locales/en.yml
942 942
  label_search_titles_only: Search titles only
943 943
  label_user_mail_option_all: "For any event on all my projects"
944 944
  label_user_mail_option_selected: "For any event on the selected projects only..."
945
  label_user_mail_option_bookmarked: "For any event on the my bookmarked projects"
945 946
  label_user_mail_option_none: "No events"
946 947
  label_user_mail_option_only_my_events: "Only for things I watch or I'm involved in"
947 948
  label_user_mail_option_only_assigned: "Only for things I watch or I am assigned to"
config/locales/ja.yml
756 756
  label_search_titles_only: タイトルのみ
757 757
  label_user_mail_option_all: "参加しているプロジェクトのすべての通知"
758 758
  label_user_mail_option_selected: "選択したプロジェクトのすべての通知..."
759
  label_user_mail_option_bookmarked: "ブックマークしているプロジェクトのすべての通知"
759 760
  label_user_mail_option_none: "通知しない"
760 761
  label_user_mail_option_only_my_events: "ウォッチ中または自分が関係しているもの"
761 762
  label_user_mail_option_only_assigned: "ウォッチ中または自分が担当しているもの"
test/functional/my_controller_test.rb
516 516
    assert mail.to.include?('foobar@example.com')
517 517
  end
518 518

  
519
  def test_update_account_mail_notification_to_bookmarked_should_update_members
520
    user = User.find(2)
521
    jump_box = Redmine::ProjectJumpBox.new user
522
    jump_box.bookmark_project user.members.order(:id).first.project
523
    jump_box.delete_project_bookmark user.members.order(:id).second.project
524
    jump_box.delete_project_bookmark user.members.order(:id).third.project
525

  
526
    assert_equal 'all', user.mail_notification
527
    assert_equal [true, true, true], user.members.order(:id).pluck(:mail_notification)
528
    put(
529
      :account,
530
      params: {
531
        user: {
532
          mail_notification: 'bookmarked'
533

  
534
        }
535
      }
536
    )
537

  
538
    assert_equal 'bookmarked', user.reload.mail_notification
539
    assert_equal [true, false, false], user.members.order(:id).pluck(:mail_notification)
540
  end
541

  
519 542
  def test_my_account_notify_about_high_priority_issues_preference
520 543
    # normally, preference should be shown
521 544
    get :account
test/functional/projects_controller_test.rb
1499 1499
    refute jb.bookmark?(Project.find('ecookbook'))
1500 1500
  end
1501 1501

  
1502
  def test_bookmark_should_change_bookmark_and_update_members_if_mail_notification_is_bookmarked
1503
    user = User.find(2)
1504
    @request.session[:user_id] = user.id
1505
    user.update(mail_notification: 'bookmarked')
1506
    assert_equal [false, false, false], user.members.order(:id).pluck(:mail_notification)
1507

  
1508
    post(:bookmark, :params => {:id => 'ecookbook'})
1509
    assert_equal [true, false, false], user.members.order(:id).pluck(:mail_notification)
1510

  
1511
    delete(:bookmark, :params => {:id => 'ecookbook'})
1512
    assert_equal [false, false, false], user.members.order(:id).pluck(:mail_notification)
1513
  end
1514

  
1502 1515
  def test_index_jump_without_project_id_should_redirect_to_active_tab
1503 1516
    get(:index, :params => {:jump => 'issues'})
1504 1517
    assert_redirected_to '/issues'
test/unit/project_test.rb
1045 1045
    only_my_events_user = User.generate!(:mail_notification => 'only_my_events')
1046 1046
    Member.create!(:project => project, :roles => [role], :principal => only_my_events_user)
1047 1047

  
1048
    bookmarked_user = User.generate!(:mail_notification => 'bookmarked')
1049
    Member.create!(:project => project, :roles => [role], :principal => bookmarked_user)
1050

  
1048 1051
    only_assigned_user = User.generate!(:mail_notification => 'only_assigned')
1049 1052
    Member.create!(:project => project, :roles => [role], :principal => only_assigned_user)
1050 1053

  
......
1059 1062
           "should not include users with the 'none' notification option"
1060 1063
    assert !project.notified_users.include?(only_my_events_user),
1061 1064
           "should not include users with the 'only_my_events' notification option"
1065
    assert !project.notified_users.include?(bookmarked_user),
1066
           "should not include users with the 'bookmarked' notification option"
1062 1067
    assert !project.notified_users.include?(only_assigned_user),
1063 1068
           "should not include users with the 'only_assigned' notification option"
1064 1069
    assert !project.notified_users.include?(only_owned_user),
test/unit/user_test.rb
1043 1043

  
1044 1044
  def test_valid_notification_options
1045 1045
    # without memberships
1046
    assert_equal 5, User.find(7).valid_notification_options.size
1046
    assert_equal 6, User.find(7).valid_notification_options.size
1047 1047
    # with memberships
1048
    assert_equal 6, User.find(2).valid_notification_options.size
1048
    assert_equal 7, User.find(2).valid_notification_options.size
1049 1049
  end
1050 1050

  
1051 1051
  def test_valid_notification_options_class_method
1052
    assert_equal 5, User.valid_notification_options.size
1053
    assert_equal 5, User.valid_notification_options(User.find(7)).size
1054
    assert_equal 6, User.valid_notification_options(User.find(2)).size
1052
    assert_equal 6, User.valid_notification_options.size
1053
    assert_equal 6, User.valid_notification_options(User.find(7)).size
1054
    assert_equal 7, User.valid_notification_options(User.find(2)).size
1055 1055
  end
1056 1056

  
1057 1057
  def test_notified_project_ids_setter_should_coerce_to_unique_integer_array
......
1250 1250
    issue = Issue.generate!(:project => project, :assigned_to => assignee, :author => author)
1251 1251

  
1252 1252
    tests = {
1253
      author => %w(all only_my_events only_owner selected),
1254
      assignee => %w(all only_my_events only_assigned selected),
1253
      author => %w(all only_my_events only_owner selected bookmarked),
1254
      assignee => %w(all only_my_events only_assigned selected bookmarked),
1255 1255
      member => %w(all)
1256 1256
    }
1257 1257

  
(4-4/5)