Project

General

Profile

Defect #17096 » 17096-fix-emails-threading-v2.patch

Go MAEDA, 2019-03-03 10:01

View differences:

app/models/mailer.rb (作業コピー)
137 137
    redmine_headers 'Project' => document.project.identifier
138 138
    @author = author
139 139
    @document = document
140
    @user = user
140 141
    @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
141 142
    mail :to => user,
142 143
      :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
......
172 173
    end
173 174
    redmine_headers 'Project' => container.project.identifier
174 175
    @attachments = attachments
176
    @user = user
175 177
    @added_to = added_to
176 178
    @added_to_url = added_to_url
177 179
    mail :to => user,
......
203 205
    message_id news
204 206
    references news
205 207
    @news = news
208
    @user = user
206 209
    @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
207 210
    mail :to => user,
208 211
      :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
......
228 231
    references news
229 232
    @news = news
230 233
    @comment = comment
234
    @user = user
231 235
    @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
232 236
    mail :to => user,
233 237
     :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
......
253 257
    message_id message
254 258
    references message.root
255 259
    @message = message
260
    @user = user
256 261
    @message_url = url_for(message.event_url)
257 262
    mail :to => user,
258 263
      :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
......
279 284
    @author = wiki_content.author
280 285
    message_id wiki_content
281 286
    @wiki_content = wiki_content
287
    @user = user
282 288
    @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
283 289
                                      :project_id => wiki_content.project,
284 290
                                      :id => wiki_content.page.title)
......
304 310
    @author = wiki_content.author
305 311
    message_id wiki_content
306 312
    @wiki_content = wiki_content
313
    @user = user
307 314
    @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
308 315
                                      :project_id => wiki_content.project,
309 316
                                      :id => wiki_content.page.title)
......
662 669
    end
663 670

  
664 671
    if @message_id_object
665
      headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
672
      headers[:message_id] = "<#{self.class.message_id_for(@message_id_object, @user)}>"
666 673
    end
667 674
    if @references_objects
668
      headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
675
      headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o, @user)}>"}.join(' ')
669 676
    end
670 677

  
671 678
    if block_given?
......
719 726
    h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
720 727
  end
721 728

  
722
  def self.token_for(object, rand=true)
729
  def self.token_for(object, user)
723 730
    timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
724 731
    hash = [
725 732
      "redmine",
726 733
      "#{object.class.name.demodulize.underscore}-#{object.id}",
727 734
      timestamp.utc.strftime("%Y%m%d%H%M%S")
728 735
    ]
729
    if rand
730
      hash << Redmine::Utils.random_hex(8)
731
    end
736
    hash << user.id if user
732 737
    host = Setting.mail_from.to_s.strip.gsub(%r{^.*@|>}, '')
733 738
    host = "#{::Socket.gethostname}.redmine" if host.empty?
734 739
    "#{hash.join('.')}@#{host}"
735 740
  end
736 741

  
737 742
  # Returns a Message-Id for the given object
738
  def self.message_id_for(object)
739
    token_for(object, true)
743
  def self.message_id_for(object, user)
744
    token_for(object, user)
740 745
  end
741 746

  
742 747
  # Returns a uniq token for a given object referenced by all notifications
743 748
  # related to this object
744
  def self.references_for(object)
745
    token_for(object, false)
749
  def self.references_for(object, user)
750
    token_for(object, user)
746 751
  end
747 752

  
748 753
  def message_id(object)
......
754 759
    @references_objects << object
755 760
  end
756 761
end
762

  
test/unit/mailer_test.rb (作業コピー)
296 296
    issue = Issue.find(2)
297 297
    Mailer.deliver_issue_add(issue)
298 298
    mail = last_email
299
    assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
300
    assert_include "redmine.issue-2.20060719190421@example.net", mail.references
299
    uid = destination_user(mail).id
300
    assert_include "redmine.issue-2.20060719190421.#{uid}@example.net", mail.message_id
301
    assert_include "redmine.issue-2.20060719190421.#{uid}@example.net", mail.references
301 302
  end
302 303

  
303 304
  def test_issue_edit_message_id
......
306 307

  
307 308
    Mailer.deliver_issue_edit(journal)
308 309
    mail = last_email
309
    assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
310
    assert_include "redmine.issue-2.20060719190421@example.net", mail.references
310
    uid = destination_user(mail).id
311
    assert_match /^redmine\.journal-3\.\d+\.#{uid}@example\.net/, mail.message_id
312
    assert_include "redmine.issue-2.20060719190421.#{uid}@example.net", mail.references
311 313
    assert_select_email do
312 314
      # link to the update
313 315
      assert_select "a[href=?]",
......
319 321
    message = Message.find(1)
320 322
    Mailer.deliver_message_posted(message)
321 323
    mail = last_email
322
    assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
323
    assert_include "redmine.message-1.20070512151532@example.net", mail.references
324
    uid = destination_user(mail).id
325
    assert_include "redmine.message-1.20070512151532.#{uid}@example.net", mail.message_id
326
    assert_include "redmine.message-1.20070512151532.#{uid}@example.net", mail.references
324 327
    assert_select_email do
325 328
      # link to the message
326 329
      assert_select "a[href=?]",
......
333 336
    message = Message.find(3)
334 337
    Mailer.deliver_message_posted(message)
335 338
    mail = last_email
336
    assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
337
    assert_include "redmine.message-1.20070512151532@example.net", mail.references
339
    uid = destination_user(mail).id
340
    assert_include "redmine.message-3.20070512151802.#{uid}@example.net", mail.message_id
341
    assert_include "redmine.message-1.20070512151532.#{uid}@example.net", mail.references
338 342
    assert_select_email do
339 343
      # link to the reply
340 344
      assert_select "a[href=?]",
......
348 352
    issue = Issue.find(3)
349 353
    %w(UTC Paris Tokyo).each do |zone|
350 354
      Time.zone = zone
351
      assert_match /^redmine\.issue-3\.20060719190727\.[a-f0-9]+@example\.net/, Mailer.token_for(issue)
355
      assert_match /^redmine\.issue-3\.20060719190727\.1@example\.net/, Mailer.token_for(issue, User.find(1))
352 356
    end
353 357
  ensure
354 358
    Time.zone = zone_was
......
803 807

  
804 808
  def test_token_for_should_strip_trailing_gt_from_address_with_full_name
805 809
    with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
806
      assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!)
810
      assert_match /\Aredmine.issue-\d+\.\d+\.3@redmine.org\z/,
811
        Mailer.token_for(Issue.generate!, User.find(3))
807 812
    end
808 813
  end
809 814

  
......
955 960
  def html_part
956 961
    last_email.parts.detect {|part| part.content_type.include?('text/html')}
957 962
  end
963

  
964
  def destination_user(mail)
965
    EmailAddress.where(:address => [mail.to, mail.cc, mail.bcc].flatten).map(&:user).first
966
  end
958 967
end
(4-4/4)