]> source.dussan.org Git - redmine.git/commitdiff
Automatically add the user to the watchers list after contributing to an issue (...
authorMarius Balteanu <marius.balteanu@zitec.com>
Sat, 19 Mar 2022 10:14:29 +0000 (10:14 +0000)
committerMarius Balteanu <marius.balteanu@zitec.com>
Sat, 19 Mar 2022 10:14:29 +0000 (10:14 +0000)
Patch by Takenori TAKAKI and Jens Krämer.

git-svn-id: http://svn.redmine.org/redmine/trunk@21469 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/users_helper.rb
app/models/journal.rb
app/models/user_preference.rb
app/views/my/account.html.erb
app/views/users/_form.html.erb
config/locales/en.yml
test/unit/journal_test.rb
test/unit/user_preference_test.rb

index 9724a348739b29032709054ac5e8e089bfb52524..0dbb0f1bdf4627f54def20322dd9495a8ff55d06 100644 (file)
@@ -66,6 +66,10 @@ module UsersHelper
      [l('label_last_tab_visited'), 'last_tab_visited']]
   end
 
+  def auto_watch_on_options
+    UserPreference::AUTO_WATCH_ON_OPTIONS.index_by {|o| l("label_auto_watch_on_#{o}")}
+  end
+
   def change_status_link(user)
     url = {:controller => 'users', :action => 'update', :id => user, :page => params[:page], :status => params[:status], :tab => nil}
 
index 3d1feb906ccc05d831b59466366097380dbeccaf..01d5debcd796d45e5d761f707c15d99c662b1bf4 100644 (file)
@@ -60,6 +60,7 @@ class Journal < ActiveRecord::Base
   )
   acts_as_mentionable :attributes => ['notes']
   before_create :split_private_notes
+  before_create :add_watcher
   after_create_commit :send_notification
 
   scope :visible, (lambda do |*args|
@@ -327,6 +328,15 @@ class Journal < ActiveRecord::Base
     true
   end
 
+  def add_watcher
+    if user &&
+        user.allowed_to?(:add_issue_watchers, project) &&
+        user.pref.auto_watch_on?('issue_contributed_to') &&
+        !Watcher.any_watched?(Array.wrap(journalized), user)
+      journalized.set_watcher(user, true)
+    end
+  end
+
   def send_notification
     if notify? &&
         (
index a0fea2851db8425d5d5d50b48fd56a86da139082..7587368247a223fe8a12587fa0eef0f11c4ea6bf 100644 (file)
@@ -39,10 +39,12 @@ class UserPreference < ActiveRecord::Base
     'history_default_tab',
     'default_issue_query',
     'default_project_query',
-    'toolbar_language_options')
+    'toolbar_language_options',
+    'auto_watch_on')
 
   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']
 
   def initialize(attributes=nil, *args)
     super
@@ -56,6 +58,9 @@ class UserPreference < ActiveRecord::Base
       unless attributes && attributes.key?(:no_self_notified)
         self.no_self_notified = Setting.default_users_no_self_notified
       end
+      unless attributes && attributes.key?(:auto_watch_on)
+        self.auto_watch_on = AUTO_WATCH_ON_OPTIONS
+      end
     end
     self.others ||= {}
   end
@@ -124,6 +129,10 @@ class UserPreference < ActiveRecord::Base
   def default_project_query; self[:default_project_query] end
   def default_project_query=(value); self[:default_project_query]=value; end
 
+  def auto_watch_on; self[:auto_watch_on] || []; end
+  def auto_watch_on=(values); self[:auto_watch_on]=values; end
+  def auto_watch_on?(action); self.auto_watch_on.include?(action.to_s); end
+
   # Returns the names of groups that are displayed on user's page
   # Example:
   #   preferences.my_page_groups
index fc27dc61c07e5e2df5feddd91193b4c6386f9f89..4240832b72d106c2159e30dfd604e6f2119c0d46 100644 (file)
   <%= render :partial => 'users/mail_notifications' %>
 </fieldset>
 
+<fieldset class="box">
+  <legend><%=l(:label_auto_watch_on)%></legend>
+  <%= render :partial => 'users/auto_watch_on' %>
+</fieldset>
+
 <fieldset class="box tabular">
   <legend><%=l(:label_preferences)%></legend>
   <%= render :partial => 'users/preferences' %>
index 5505766b4fe8c3f1a08a98989e2587acd5304d32..6d6058c49e5db8db641871784e6f56ca1d1ef12e 100644 (file)
   <%= render :partial => 'users/mail_notifications' %>
 </fieldset>
 
+<fieldset class="box">
+  <legend><%=l(:label_auto_watch_on)%></legend>
+  <%= render :partial => 'users/auto_watch_on' %>
+</fieldset>
+
 <fieldset class="box tabular">
   <legend><%=l(:label_preferences)%></legend>
   <%= render :partial => 'users/preferences' %>
index 2378e56d5ba63de127fdbcefbcb0b3db17d3294b..62577376fa7591bda7b5ce33a725847fa5e82a03 100644 (file)
@@ -956,6 +956,8 @@ en:
   label_downloads_abbr: D/L
   label_optional_description: Optional description
   label_add_another_file: Add another file
+  label_auto_watch_on: Auto watch
+  label_auto_watch_on_issue_contributed_to: Issues I contributed to
   label_preferences: Preferences
   label_chronological_order: In chronological order
   label_reverse_chronological_order: In reverse chronological order
index f9c269291cf6f4b5e63c25c389ff8500be814561..cf86a7990734035bddddc422a4594a1e1bd9904f 100644 (file)
@@ -120,6 +120,28 @@ class JournalTest < ActiveSupport::TestCase
     end
   end
 
+  def test_create_should_add_wacher
+    user = User.first
+    user.pref.auto_watch_on=['issue_contributed_to']
+    user.save
+    journal = Journal.new(:journalized => Issue.first, :notes => 'notes', :user => user)
+
+    assert_difference 'Watcher.count', 1 do
+      assert_equal true, journal.save
+    end
+  end
+
+  def test_create_should_not_add_watcher
+    user = User.first
+    user.pref.auto_watch_on=[]
+    user.save
+    journal = Journal.new(:journalized => Issue.first, :notes => 'notes', :user => user)
+
+    assert_no_difference 'Watcher.count' do
+      assert_equal true, journal.save
+    end
+  end
+
   def test_visible_scope_for_anonymous
     # Anonymous user should see issues of public projects only
     journals = Journal.visible(User.anonymous).to_a
index 9ac213b5fae8f914e36787e37cc97e8f1537396c..4ca79400efa051959f3938a084d55dc7f6835c41 100644 (file)
@@ -57,6 +57,11 @@ class UserPreferenceTest < ActiveSupport::TestCase
     end
   end
 
+  def test_auto_watch_on_should_default_to_setting
+    preference = UserPreference.new
+    assert_equal ['issue_contributed_to'], preference.auto_watch_on
+  end
+
   def test_create
     user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
     user.login = "newuser"