From fdfb153c17b46a41725365604eb3a9b58f46c343 Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Wed, 21 Apr 2021 11:04:07 +0800 Subject: [PATCH] auto watch issues a user contributes to original patch by Takenori TAKAKI, https://www.redmine.org/issues/4347#note-20 --- app/helpers/users_helper.rb | 4 ++++ app/models/journal.rb | 10 ++++++++++ app/models/user_preference.rb | 12 +++++++++++- app/views/my/account.html.erb | 5 +++++ app/views/users/_auto_watch_on.html.erb | 5 +++++ app/views/users/_form.html.erb | 5 +++++ config/locales/en.yml | 2 ++ test/unit/journal_test.rb | 22 ++++++++++++++++++++++ test/unit/user_preference_test.rb | 5 +++++ 9 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 app/views/users/_auto_watch_on.html.erb diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 63e0f75fc..8f0f6c6fd 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -42,6 +42,10 @@ module UsersHelper [l('label_last_tab_visited'), 'last_tab_visited']] end + def auto_watch_on_options + UserPreference::AUTO_WATCH_ON_OPTIONS.map {|o| [l("label_auto_watch_on_#{o}"), o]}.to_h + end + def change_status_link(user) url = {:controller => 'users', :action => 'update', :id => user, :page => params[:page], :status => params[:status], :tab => nil} diff --git a/app/models/journal.rb b/app/models/journal.rb index a608703d7..a4b446ab1 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -59,6 +59,7 @@ class Journal < ActiveRecord::Base end ) before_create :split_private_notes + before_create :add_watcher after_create_commit :send_notification scope :visible, (lambda do |*args| @@ -324,6 +325,15 @@ class Journal < ActiveRecord::Base true end + def add_watcher + if user && + user.allowed_to?(:add_issue_watchers, project) && + user.pref.auto_watch_on?('add_note') && + !Watcher.any_watched?(Array.wrap(journalized), user) + journalized.set_watcher(user, true) + end + end + def send_notification if notify? && ( diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 930effb67..c5d5847ff 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -37,10 +37,13 @@ class UserPreference < ActiveRecord::Base 'textarea_font', 'recently_used_projects', 'history_default_tab', - '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 = ['add_note'] def initialize(attributes=nil, *args) super @@ -54,6 +57,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 @@ -116,6 +122,10 @@ class UserPreference < ActiveRecord::Base self[:toolbar_language_options] = languages.join(',') 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 diff --git a/app/views/my/account.html.erb b/app/views/my/account.html.erb index c54183a8c..acfed2067 100644 --- a/app/views/my/account.html.erb +++ b/app/views/my/account.html.erb @@ -58,6 +58,11 @@ <%= render :partial => 'users/mail_notifications' %> +
+ <%=l(:label_auto_watch_on)%> + <%= render :partial => 'users/auto_watch_on' %> +
+
<%=l(:label_preferences)%> <%= render :partial => 'users/preferences' %> diff --git a/app/views/users/_auto_watch_on.html.erb b/app/views/users/_auto_watch_on.html.erb new file mode 100644 index 000000000..28d1fa21f --- /dev/null +++ b/app/views/users/_auto_watch_on.html.erb @@ -0,0 +1,5 @@ +<%= labelled_fields_for :pref, @user.pref do |pref_fields| %> + <%= pref_fields.collection_check_boxes :auto_watch_on, auto_watch_on_options, :last, :first, :checked => @user.pref.auto_watch_on do |b| %> +

<%= b.check_box %> <%= b.label %>

+ <% end %> +<% end %> diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb index 4b030d920..83d544feb 100644 --- a/app/views/users/_form.html.erb +++ b/app/views/users/_form.html.erb @@ -66,6 +66,11 @@ <%= render :partial => 'users/mail_notifications' %>
+
+ <%=l(:label_auto_watch_on)%> + <%= render :partial => 'users/auto_watch_on' %> +
+
<%=l(:label_preferences)%> <%= render :partial => 'users/preferences' %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 9d779a2fe..bda29e3e9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -951,6 +951,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_add_note: Issues I contributed to label_preferences: Preferences label_chronological_order: In chronological order label_reverse_chronological_order: In reverse chronological order diff --git a/test/unit/journal_test.rb b/test/unit/journal_test.rb index 096240734..0a2692f10 100644 --- a/test/unit/journal_test.rb +++ b/test/unit/journal_test.rb @@ -120,6 +120,28 @@ class JournalTest < ActiveSupport::TestCase end end + def test_create_should_add_wacher + user = User.first + user.pref.auto_watch_on=['add_note'] + 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 diff --git a/test/unit/user_preference_test.rb b/test/unit/user_preference_test.rb index fbe0031e1..3bd8e6bb4 100644 --- a/test/unit/user_preference_test.rb +++ b/test/unit/user_preference_test.rb @@ -57,6 +57,11 @@ class UserPreferenceTest < ActiveSupport::TestCase end end + def test_auto_watch_on_should_default_to_setting + preference = UserPreference.new + assert_equal ['add_note'], preference.auto_watch_on + end + def test_create user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") user.login = "newuser" -- 2.20.1