]> source.dussan.org Git - redmine.git/commitdiff
Receive e-mail replies to news and news comments (#38274).
authorGo MAEDA <maeda@farend.jp>
Sun, 26 Mar 2023 05:35:50 +0000 (05:35 +0000)
committerGo MAEDA <maeda@farend.jp>
Sun, 26 Mar 2023 05:35:50 +0000 (05:35 +0000)
Patch by Felix Schäfer.

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

app/models/mail_handler.rb
test/fixtures/mail_handler/news_comment_reply.eml [new file with mode: 0644]
test/fixtures/mail_handler/news_reply.eml [new file with mode: 0644]
test/unit/mail_handler_test.rb

index 8a4084769072814e8d279136a63985a6e49fca51..67cb016d13c021fdb57ac3a99dbb6d1e3696cd23 100644 (file)
@@ -306,6 +306,41 @@ class MailHandler < ActionMailer::Base
     end
   end
 
+  # Receives a reply to a news entry
+  def receive_news_reply(news_id)
+    news = News.find_by_id(news_id)
+    if news.nil?
+      raise MissingContainer, "reply to nonexistant news [#{news_id}]"
+    end
+
+    # Never receive emails to projects where adding news comments is not possible
+    project = news.project
+    raise NotAllowedInProject, "not possible to add news comments to project [#{project.name}]" unless project.allows_to?(:comment_news)
+
+    unless handler_options[:no_permission_check]
+      unless news.commentable?(user)
+        raise InsufficientPermissions, "not allowed to comment on news item [#{news.id} #{news.title}]"
+      end
+    end
+
+    comment = news.comments.new
+    comment.author = user
+    comment.comments = cleaned_up_text_body
+    comment.save!
+    comment
+  end
+
+  # Receives a reply to a comment to a news entry
+  def receive_comment_reply(comment_id)
+    comment = Comment.find_by_id(comment_id)
+
+    if comment && comment.commented_type == 'News'
+      receive_news_reply(comment.commented.id)
+    else
+      raise MissingContainer, "reply to nonexistant comment [#{comment_id}]"
+    end
+  end
+
   def add_attachments(obj)
     if email.attachments && email.attachments.any?
       email.attachments.each do |attachment|
diff --git a/test/fixtures/mail_handler/news_comment_reply.eml b/test/fixtures/mail_handler/news_comment_reply.eml
new file mode 100644 (file)
index 0000000..2ce1660
--- /dev/null
@@ -0,0 +1,15 @@
+Message-ID: <4974C93E.3071105@somenet.foo>
+Date: Mon, 19 Jan 2023 19:41:02 +0100
+From: "John Smith" <jsmith@somenet.foo>
+User-Agent: Thunderbird 2.0.0.19 (Windows/20081209)
+MIME-Version: 1.0
+To: redmine@somenet.foo
+Subject: News comment reply via email
+References: <redmine.comment-1.20230214171800@somenet.foo>
+In-Reply-To: <redmine.comment-1.20230214171800@somenet.foo>
+Content-Type: text/plain; charset=UTF-8; format=flowed
+Content-Transfer-Encoding: 7bit
+
+This is a reply to a comment.
+
+
diff --git a/test/fixtures/mail_handler/news_reply.eml b/test/fixtures/mail_handler/news_reply.eml
new file mode 100644 (file)
index 0000000..880ebf3
--- /dev/null
@@ -0,0 +1,15 @@
+Message-ID: <4974C93E.3071005@somenet.foo>
+Date: Mon, 19 Jan 2023 19:41:02 +0100
+From: "John Smith" <jsmith@somenet.foo>
+User-Agent: Thunderbird 2.0.0.19 (Windows/20081209)
+MIME-Version: 1.0
+To: redmine@somenet.foo
+Subject: News comment via email
+References: <redmine.news-1.20230214171800@somenet.foo>
+In-Reply-To: <redmine.news-1.20230214171800@somenet.foo>
+Content-Type: text/plain; charset=UTF-8; format=flowed
+Content-Transfer-Encoding: 7bit
+
+This is a reply to a news.
+
+
index a038d59b19ba84b6558ca3f3bf3c7b492352bf6a..c4771f55613565b1f620dc9c04c8ee81abb4aa83 100644 (file)
@@ -28,7 +28,7 @@ class MailHandlerTest < ActiveSupport::TestCase
            :workflows, :trackers, :projects_trackers,
            :versions, :enumerations, :issue_categories,
            :custom_fields, :custom_fields_trackers, :custom_fields_projects, :custom_values,
-           :boards, :messages, :watchers
+           :boards, :messages, :watchers, :news, :comments
 
   FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
 
@@ -1174,6 +1174,40 @@ class MailHandlerTest < ActiveSupport::TestCase
     end
   end
 
+  def test_reply_to_a_news
+    m = submit_email('news_reply.eml')
+    assert m.is_a?(Comment)
+    assert !m.new_record?
+    m.reload
+    assert_equal News.find(1), m.commented
+    assert_equal "This is a reply to a news.", m.content
+  end
+
+  def test_reply_to_a_news_comment
+    m = submit_email('news_comment_reply.eml')
+    assert m.is_a?(Comment)
+    assert !m.new_record?
+    m.reload
+    assert_equal News.find(1), m.commented
+    assert_equal "This is a reply to a comment.", m.content
+  end
+
+  def test_reply_to_a_nonexistant_news
+    News.find(1).destroy
+    assert_no_difference('Comment.count') do
+      assert_not submit_email('news_reply.eml')
+      assert_not submit_email('news_comment_reply.eml')
+    end
+  end
+
+  def test_reply_to_a_news_without_permission
+    Role.all.each {|r| r.remove_permission! :comment_news}
+    assert_no_difference('Comment.count') do
+      assert_not submit_email('news_reply.eml')
+      assert_not submit_email('news_comment_reply.eml')
+    end
+  end
+
   def test_should_convert_tags_of_html_only_emails
     with_settings :text_formatting => 'textile' do
       issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})