diff --git a/app/models/issue.rb b/app/models/issue.rb index 6451146179..fcf824d1d5 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -109,6 +109,7 @@ class Issue < ActiveRecord::Base before_validation :default_assign, on: :create before_validation :clear_disabled_fields + before_create :add_auto_watcher before_save :close_duplicates, :update_done_ratio_from_issue_status, :force_updated_on_change, :update_closed_on after_save do |issue| @@ -2020,6 +2021,16 @@ class Issue < ActiveRecord::Base end end + def add_auto_watcher + if author && + author.allowed_to?(:add_issue_watchers, project) && + author.pref.auto_watch_on?('issue_created') && + !Watcher.any_watched?(Array.wrap(self), author) && + !self.watcher_user_ids.include?(author.id) + self.set_watcher(author, true) + end + end + def send_notification if notify? && Setting.notified_events.include?('issue_added') Mailer.deliver_issue_add(self) diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 61c7090933..21992f9454 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -44,7 +44,7 @@ class UserPreference < ActiveRecord::Base TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional'] DEFAULT_TOOLBAR_LANGUAGE_OPTIONS = %w[c cpp csharp css diff go groovy html java javascript objc perl php python r ruby sass scala shell sql swift xml yaml] - AUTO_WATCH_ON_OPTIONS = ['issue_contributed_to'] + AUTO_WATCH_ON_OPTIONS = %w[issue_created issue_contributed_to] def initialize(attributes=nil, *args) super diff --git a/config/locales/en.yml b/config/locales/en.yml index 64d09c8cfd..34f936b735 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -962,6 +962,7 @@ en: label_optional_description: Optional description label_add_another_file: Add another file label_auto_watch_on: Auto watch + label_auto_watch_on_issue_created: Issues I created label_auto_watch_on_issue_contributed_to: Issues I contributed to label_preferences: Preferences label_chronological_order: In chronological order diff --git a/db/migrate/20230126213739_add_issue_created_to_auto_watch_on_user_preferences.rb b/db/migrate/20230126213739_add_issue_created_to_auto_watch_on_user_preferences.rb new file mode 100644 index 0000000000..1c5a5f3e30 --- /dev/null +++ b/db/migrate/20230126213739_add_issue_created_to_auto_watch_on_user_preferences.rb @@ -0,0 +1,10 @@ +class AddIssueCreatedToAutoWatchOnUserPreferences < ActiveRecord::Migration[6.1] + def up + UserPreference.find_each do |pref| + if pref.auto_watch_on?('issue_contributed_to') + pref.auto_watch_on << 'issue_created' + pref.save + end + end + end +end diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index 14dca00e36..5795d2bb1a 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -3445,6 +3445,40 @@ class IssueTest < ActiveSupport::TestCase assert_equal [5], issue2.filter_projects_scope('').ids.sort end + def test_create_should_add_watcher + user = User.first + user.pref.auto_watch_on=['issue_created'] + user.pref.save + issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher') + + assert_difference 'Watcher.count', 1 do + assert_equal true, issue.save + end + end + + def test_create_should_add_author_watcher_only_once + user = User.first + user.pref.auto_watch_on=['issue_created'] + user.pref.save + issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher') + issue.watcher_user_ids = [user.id] + + assert_difference 'Watcher.count', 1 do + assert_equal true, issue.save + end + end + + def test_create_should_not_add_watcher + user = User.first + user.pref.auto_watch_on=[] + user.pref.save + issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_not_add_watcher') + + assert_no_difference 'Watcher.count' do + assert_equal true, issue.save + end + end + def test_like_should_escape_query issue = Issue.generate!(:subject => "asdf") r = Issue.like('as_f') diff --git a/test/unit/user_preference_test.rb b/test/unit/user_preference_test.rb index 67157d5a22..6a74dde0a9 100644 --- a/test/unit/user_preference_test.rb +++ b/test/unit/user_preference_test.rb @@ -59,7 +59,7 @@ class UserPreferenceTest < ActiveSupport::TestCase def test_auto_watch_on_should_default_to_setting preference = UserPreference.new - assert_equal ['issue_contributed_to'], preference.auto_watch_on + assert_equal %w[issue_created issue_contributed_to], preference.auto_watch_on end def test_create