From 91da86a688c27d13c373861901efffc2ec1ddbf1 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 22 Jan 2016 20:01:05 +0000 Subject: [PATCH] Add watchers from To and Cc fields in issue replies (#7017). git-svn-id: http://svn.redmine.org/redmine/trunk@15092 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/mail_handler.rb | 9 +++++-- .../mail_handler/issue_update_with_cc.eml | 19 +++++++++++++++ test/unit/mail_handler_test.rb | 24 ++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/mail_handler/issue_update_with_cc.eml diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 0c3d34c41..dad13ebf8 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -240,6 +240,9 @@ class MailHandler < ActionMailer::Base issue.safe_attributes = issue_attributes_from_keywords(issue) issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} journal.notes = cleaned_up_text_body + + # add To and Cc as watchers before saving so the watchers can reply to Redmine + add_watchers(issue) add_attachments(issue) issue.save! if logger @@ -314,8 +317,10 @@ class MailHandler < ActionMailer::Base if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} unless addresses.empty? - User.active.having_mail(addresses).each do |w| - obj.add_watcher(w) + users = User.active.having_mail(addresses).to_a + users -= obj.watcher_users + users.each do |u| + obj.add_watcher(u) end end end diff --git a/test/fixtures/mail_handler/issue_update_with_cc.eml b/test/fixtures/mail_handler/issue_update_with_cc.eml new file mode 100644 index 000000000..644b084bd --- /dev/null +++ b/test/fixtures/mail_handler/issue_update_with_cc.eml @@ -0,0 +1,19 @@ +Return-Path: +Received: from osiris ([127.0.0.1]) + by OSIRIS + with hMailServer ; Sun, 22 Jun 2008 12:28:07 +0200 +Message-ID: <000501c8d452$a95cd7e0$0a00a8c0@osiris> +In-Reply-To: +From: "John Smith" +To: +Cc: +Subject: Re: update to issue 2 +Date: Sun, 22 Jun 2008 12:28:07 +0200 +MIME-Version: 1.0 +Content-Type: text/plain; + format=flowed; + charset="iso-8859-1"; + reply-type=original +Content-Transfer-Encoding: 7bit + +An update to the issue by the sender. diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index e2522db26..47e642e91 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -265,7 +265,7 @@ class MailHandlerTest < ActiveSupport::TestCase end end - def test_add_issue_with_cc + def test_add_issue_should_add_cc_as_watchers issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'}) assert issue.is_a?(Issue) assert !issue.new_record? @@ -813,6 +813,28 @@ class MailHandlerTest < ActiveSupport::TestCase assert_equal 'Normal', journal.issue.priority.name end + def test_update_issue_should_add_cc_as_watchers + Watcher.delete_all + issue = Issue.find(2) + + assert_difference 'Watcher.count' do + assert submit_email('issue_update_with_cc.eml') + end + issue.reload + assert_equal 1, issue.watcher_user_ids.size + assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo')) + end + + def test_update_issue_should_not_add_cc_as_watchers_if_already_watching + Watcher.delete_all + issue = Issue.find(2) + Watcher.create!(:watchable => issue, :user => User.find_by_mail('dlopper@somenet.foo')) + + assert_no_difference 'Watcher.count' do + assert submit_email('issue_update_with_cc.eml') + end + end + def test_replying_to_a_private_note_should_add_reply_as_private private_journal = Journal.create!(:notes => 'Private notes', :journalized => Issue.find(1), :private_notes => true, :user_id => 2) -- 2.39.5