diff --git a/app/models/issue.rb b/app/models/issue.rb index 6451146179..4edf4fa1b8 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -121,7 +121,11 @@ class Issue < ActiveRecord::Base # Should be after_create but would be called before previous after_save callbacks after_save :after_create_from_copy, :create_parent_issue_journal after_destroy :update_parent_attributes, :create_parent_issue_journal + # add_auto_watcher needs to run before sending notifications, thus it needs + # to be added after send_notification (after_ callbacks are run in inverse order) + # https://api.rubyonrails.org/v5.2.3/classes/ActiveSupport/Callbacks/ClassMethods.html#method-i-set_callback after_create_commit :send_notification + after_create_commit :add_auto_watcher # Returns a SQL conditions string used to find all issues visible by the specified user def self.visible_condition(user, options={}) @@ -2020,6 +2024,15 @@ 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') && + self.watcher_user_ids.exclude?(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/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