Project

General

Profile

Feature #35189 » feature-35189-v3.patch

Go MAEDA, 2023-11-02 07:54

View differences:

app/models/user.rb
MAIL_NOTIFICATION_OPTIONS = [
['all', :label_user_mail_option_all],
['selected', :label_user_mail_option_selected],
['bookmarked', :label_user_mail_option_bookmarked],
['only_my_events', :label_user_mail_option_only_my_events],
['only_assigned', :label_user_mail_option_only_assigned],
['only_owner', :label_user_mail_option_only_owner],
......
# Updates per project notifications (after_save callback)
def update_notified_project_ids
update_member_notifications = lambda do |ids|
members.update_all(mail_notification: false)
members.where(project_id: ids).update_all(mail_notification: true) if ids.present?
end
if @notified_projects_ids_changed
ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : [])
members.update_all(:mail_notification => false)
members.where(:project_id => ids).update_all(:mail_notification => true) if ids.any?
selected_ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : [])
update_member_notifications.call(selected_ids)
end
if saved_change_to_mail_notification? && mail_notification == 'bookmarked'
bookmarked_ids = self.bookmarked_project_ids
update_member_notifications.call(bookmarked_ids)
end
end
private :update_notified_project_ids
......
case object
when Issue
case mail_notification
when 'selected', 'only_my_events'
when 'selected', 'only_my_events', 'bookmarked'
# 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'
app/models/user_preference.rb
serialize :others
before_save :set_others_hash, :clear_unused_block_settings
after_save :update_notified_project_ids
safe_attributes(
'hide_mail',
......
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 != 'bookmarked' || 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
config/locales/en.yml
label_search_titles_only: Search titles only
label_user_mail_option_all: "For any event on all my projects"
label_user_mail_option_selected: "For any event on the selected projects only..."
label_user_mail_option_bookmarked: "For any event on the my bookmarked projects"
label_user_mail_option_none: "No events"
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"
config/locales/ja.yml
label_search_titles_only: タイトルのみ
label_user_mail_option_all: "参加しているプロジェクトのすべての通知"
label_user_mail_option_selected: "選択したプロジェクトのすべての通知..."
label_user_mail_option_bookmarked: "ブックマークしているプロジェクトのすべての通知"
label_user_mail_option_none: "通知しない"
label_user_mail_option_only_my_events: "ウォッチ中または自分が関係しているもの"
label_user_mail_option_only_assigned: "ウォッチ中または自分が担当しているもの"
test/functional/my_controller_test.rb
assert mail.to.include?('foobar@example.com')
end
def test_update_account_mail_notification_to_bookmarked_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: 'bookmarked'
}
}
)
assert_equal 'bookmarked', 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
test/functional/projects_controller_test.rb
refute jb.bookmark?(Project.find('ecookbook'))
end
def test_bookmark_should_change_bookmark_and_update_members_if_mail_notification_is_bookmarked
user = User.find(2)
@request.session[:user_id] = user.id
user.update(mail_notification: 'bookmarked')
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'
test/unit/project_test.rb
only_my_events_user = User.generate!(:mail_notification => 'only_my_events')
Member.create!(:project => project, :roles => [role], :principal => only_my_events_user)
bookmarked_user = User.generate!(:mail_notification => 'bookmarked')
Member.create!(:project => project, :roles => [role], :principal => bookmarked_user)
only_assigned_user = User.generate!(:mail_notification => 'only_assigned')
Member.create!(:project => project, :roles => [role], :principal => only_assigned_user)
......
"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?(bookmarked_user),
"should not include users with the 'bookmarked' 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),
test/unit/user_test.rb
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
......
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 bookmarked),
assignee => %w(all only_my_events only_assigned selected bookmarked),
member => %w(all)
}
(4-4/5)