Project

General

Profile

Feature #38238 » 38238-2.patch

Felix Schäfer, 2023-02-03 08:11

View differences:

app/models/issue.rb
109 109

  
110 110
  before_validation :default_assign, on: :create
111 111
  before_validation :clear_disabled_fields
112
  before_create :add_auto_watcher
112 113
  before_save :close_duplicates, :update_done_ratio_from_issue_status,
113 114
              :force_updated_on_change, :update_closed_on
114 115
  after_save do |issue|
......
2020 2021
    end
2021 2022
  end
2022 2023

  
2024
  def add_auto_watcher
2025
    if author &&
2026
        author.allowed_to?(:add_issue_watchers, project) &&
2027
        author.pref.auto_watch_on?('issue_created') &&
2028
        !Watcher.any_watched?(Array.wrap(self), author) &&
2029
        !self.watcher_user_ids.include?(author.id)
2030
      self.set_watcher(author, true)
2031
    end
2032
  end
2033

  
2023 2034
  def send_notification
2024 2035
    if notify? && Setting.notified_events.include?('issue_added')
2025 2036
      Mailer.deliver_issue_add(self)
app/models/user_preference.rb
44 44

  
45 45
  TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
46 46
  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]
47
  AUTO_WATCH_ON_OPTIONS = ['issue_contributed_to']
47
  AUTO_WATCH_ON_OPTIONS = %w[issue_created issue_contributed_to]
48 48

  
49 49
  def initialize(attributes=nil, *args)
50 50
    super
config/locales/en.yml
962 962
  label_optional_description: Optional description
963 963
  label_add_another_file: Add another file
964 964
  label_auto_watch_on: Auto watch
965
  label_auto_watch_on_issue_created: Issues I created
965 966
  label_auto_watch_on_issue_contributed_to: Issues I contributed to
966 967
  label_preferences: Preferences
967 968
  label_chronological_order: In chronological order
db/migrate/20230126213739_add_issue_created_to_auto_watch_on_user_preferences.rb
1
class AddIssueCreatedToAutoWatchOnUserPreferences < ActiveRecord::Migration[6.1]
2
  def up
3
    UserPreference.find_each do |pref|
4
      if pref.auto_watch_on?('issue_contributed_to')
5
        pref.auto_watch_on << 'issue_created'
6
        pref.save
7
      end
8
    end
9
  end
10
end
test/unit/issue_test.rb
3445 3445
    assert_equal [5], issue2.filter_projects_scope('').ids.sort
3446 3446
  end
3447 3447

  
3448
  def test_create_should_add_watcher
3449
    user = User.first
3450
    user.pref.auto_watch_on=['issue_created']
3451
    user.pref.save
3452
    issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher')
3453

  
3454
    assert_difference 'Watcher.count', 1 do
3455
      assert_equal true, issue.save
3456
    end
3457
  end
3458

  
3459
  def test_create_should_add_author_watcher_only_once
3460
    user = User.first
3461
    user.pref.auto_watch_on=['issue_created']
3462
    user.pref.save
3463
    issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_add_watcher')
3464
    issue.watcher_user_ids = [user.id]
3465

  
3466
    assert_difference 'Watcher.count', 1 do
3467
      assert_equal true, issue.save
3468
    end
3469
  end
3470

  
3471
  def test_create_should_not_add_watcher
3472
    user = User.first
3473
    user.pref.auto_watch_on=[]
3474
    user.pref.save
3475
    issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => user.id, :subject => 'test_create_should_not_add_watcher')
3476

  
3477
    assert_no_difference 'Watcher.count' do
3478
      assert_equal true, issue.save
3479
    end
3480
  end
3481

  
3448 3482
  def test_like_should_escape_query
3449 3483
    issue = Issue.generate!(:subject => "asdf")
3450 3484
    r = Issue.like('as_f')
test/unit/user_preference_test.rb
59 59

  
60 60
  def test_auto_watch_on_should_default_to_setting
61 61
    preference = UserPreference.new
62
    assert_equal ['issue_contributed_to'], preference.auto_watch_on
62
    assert_equal %w[issue_created issue_contributed_to], preference.auto_watch_on
63 63
  end
64 64

  
65 65
  def test_create
(2-2/4)