Project

General

Profile

Defect #17096 » mail-17096-20140608.diff

Toshi MARUYAMA, 2014-06-08 11:47

View differences:

app/models/mailer.rb
34 34
                    'Issue-Author' => issue.author.login
35 35
    redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
36 36
    message_id issue
37
    references issue
38 37
    @author = issue.author
39 38
    @issue = issue
40 39
    @users = to_users + cc_users
......
398 397
    end
399 398

  
400 399
    if @message_id_object
401
      headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
400
      headers[:message_id] = "<#{self.class.message_id_for(@message_id_object, @users, @issue)}>"
402 401
    end
403 402
    if @references_objects
404
      headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
403
      headers[:references] =
404
        @references_objects.collect {|o| "<#{self.class.references_for(o, @users, @issue)}>"}.join(' ')
405 405
    end
406 406

  
407 407
    m = if block_given?
......
454 454
    h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
455 455
  end
456 456

  
457
  def self.token_for(object, rand=true)
457
  def self.token_for(object, users, issue)
458 458
    timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
459 459
    hash = [
460 460
      "redmine",
461 461
      "#{object.class.name.demodulize.underscore}-#{object.id}",
462 462
      timestamp.strftime("%Y%m%d%H%M%S")
463 463
    ]
464
    if rand
465
      hash << Redmine::Utils.random_hex(8)
464
    if users && users.present? && issue
465
      ids = issue.visible_custom_field_values(users.first).map(&:custom_field_id).sort
466
      if ids.present?
467
        hash << Digest::SHA1.hexdigest(ids.join(","))[0, 8]
468
      end
466 469
    end
467 470
    host = Setting.mail_from.to_s.strip.gsub(%r{^.*@|>}, '')
468 471
    host = "#{::Socket.gethostname}.redmine" if host.empty?
......
470 473
  end
471 474

  
472 475
  # Returns a Message-Id for the given object
473
  def self.message_id_for(object)
474
    token_for(object, true)
476
  def self.message_id_for(object, users, issue)
477
    token_for(object, users, issue)
475 478
  end
476 479

  
477 480
  # Returns a uniq token for a given object referenced by all notifications
478 481
  # related to this object
479
  def self.references_for(object)
480
    token_for(object, false)
482
  def self.references_for(object, users, issue)
483
    token_for(object, users, issue)
481 484
  end
482 485

  
483 486
  def message_id(object)
test/unit/mailer_test.rb
245 245
    Mailer.deliver_issue_add(issue)
246 246
    mail = last_email
247 247
    assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
248
    assert_include "redmine.issue-2.20060719190421@example.net", mail.references
249 248
  end
250 249

  
251 250
  def test_issue_edit_message_id
......
255 254
    Mailer.deliver_issue_edit(journal)
256 255
    mail = last_email
257 256
    assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
258
    assert_include "redmine.issue-2.20060719190421@example.net", mail.references
257
    assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.references
259 258
    assert_select_email do
260 259
      # link to the update
261 260
      assert_select "a[href=?]",
......
267 266
    message = Message.find(1)
268 267
    Mailer.message_posted(message).deliver
269 268
    mail = last_email
270
    assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
269
    assert_include "redmine.message-1.20070512151532@example.net", mail.message_id
271 270
    assert_include "redmine.message-1.20070512151532@example.net", mail.references
272 271
    assert_select_email do
273 272
      # link to the message
......
281 280
    message = Message.find(3)
282 281
    Mailer.message_posted(message).deliver
283 282
    mail = last_email
284
    assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
283
    assert_include "redmine.message-3.20070512151802@example.net", mail.message_id
285 284
    assert_include "redmine.message-1.20070512151532@example.net", mail.references
286 285
    assert_select_email do
287 286
      # link to the reply
......
652 651

  
653 652
  def test_token_for_should_strip_trailing_gt_from_address_with_full_name
654 653
    with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
655
      assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!)
654
      assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/,
655
                   Mailer.token_for(Issue.generate!, [User.find(1)], Issue.find(1))
656 656
    end
657 657
  end
658 658

  
(2-2/4)