Project

General

Profile

Feature #38238 » 38238-3.patch

Felix Schäfer, 2023-02-11 21:42

View differences:

app/models/issue.rb
121 121
  # Should be after_create but would be called before previous after_save callbacks
122 122
  after_save :after_create_from_copy, :create_parent_issue_journal
123 123
  after_destroy :update_parent_attributes, :create_parent_issue_journal
124
  # add_auto_watcher needs to run before sending notifications, thus it needs
125
  # to be added after send_notification (after_ callbacks are run in inverse order)
126
  # https://api.rubyonrails.org/v5.2.3/classes/ActiveSupport/Callbacks/ClassMethods.html#method-i-set_callback
124 127
  after_create_commit :send_notification
128
  after_create_commit :add_auto_watcher
125 129

  
126 130
  # Returns a SQL conditions string used to find all issues visible by the specified user
127 131
  def self.visible_condition(user, options={})
......
2020 2024
    end
2021 2025
  end
2022 2026

  
2027
  def add_auto_watcher
2028
    if author &&
2029
        author.allowed_to?(:add_issue_watchers, project) &&
2030
        author.pref.auto_watch_on?('issue_created') &&
2031
        self.watcher_user_ids.exclude?(author.id)
2032
      self.set_watcher(author, true)
2033
    end
2034
  end
2035

  
2023 2036
  def send_notification
2024 2037
    if notify? && Setting.notified_events.include?('issue_added')
2025 2038
      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
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
(4-4/4)