Added `In-Reply-To` and `References` email headers. Additionally removed username from

email replies and new posts to keep the subjects collapsable.
This commit is contained in:
Robin Ward 2013-07-08 11:48:40 -04:00
parent 4ddd50fe17
commit 013ad0fdda
3 changed files with 51 additions and 30 deletions

View File

@ -934,7 +934,7 @@ en:
Please visit this link to view the topic: %{base_url}%{url}
user_replied:
subject_template: "[%{site_name}] %{username} replied to your post in '%{topic_title}'"
subject_template: "[%{site_name}] new reply to your post in '%{topic_title}'"
text_body_template: |
%{username} replied to your post in '%{topic_title}' on %{site_name}:
@ -967,7 +967,7 @@ en:
%{respond_instructions}
user_posted:
subject_template: "[%{site_name}] %{username} posted in '%{topic_title}'"
subject_template: "[%{site_name}] new post in '%{topic_title}'"
text_body_template: |
%{username} posted in '%{topic_title}' on %{site_name}:

View File

@ -47,11 +47,24 @@ module Email
user_id: @user.try(:id))
@message.header['List-Id'] = Email::Sender.list_id_for(SiteSetting.title, Discourse.base_url)
host = Email::Sender.host_for(Discourse.base_url)
add_header_to_log('X-Discourse-Reply-Key', email_log, :reply_key)
add_header_to_log('X-Discourse-Post-Id', email_log, :post_id)
add_header_to_log('X-Discourse-Topic-Id', email_log, :topic_id)
@message.header['List-Id'] = Email::Sender.list_id_for(SiteSetting.title, host)
topic_id = header_value('X-Discourse-Topic-Id')
post_id = header_value('X-Discourse-Post-Id')
reply_key = header_value('X-Discourse-Reply-Key')
if topic_id.present?
email_log.topic_id = topic_id
topic_identitfier = "<topic/#{topic_id}@#{host}>"
@message.header['In-Reply-To'] = topic_identitfier
@message.header['References'] = topic_identitfier
end
email_log.post_id = post_id if post_id.present?
email_log.reply_key = reply_key if reply_key.present?
# Remove headers we don't need anymore
@message.header['X-Discourse-Topic-Id'] = nil
@ -66,8 +79,7 @@ module Email
end
def self.list_id_for(site_name, base_url)
def self.host_for(base_url)
host = "localhost"
if base_url.present?
begin
@ -76,18 +88,19 @@ module Email
rescue URI::InvalidURIError
end
end
host
end
def self.list_id_for(site_name, host)
"\"#{site_name.gsub(/\"/, "'")}\" <discourse.forum.#{Slug.for(site_name)}.#{host}>"
end
private
def add_header_to_log(name, email_log, email_log_field)
def header_value(name)
header = @message.header[name]
return unless header
val = header.value
email_log[email_log_field] = val if val.present?
return nil unless header
header.value
end
end

View File

@ -20,32 +20,37 @@ describe Email::Sender do
Email::Sender.new(message, :hello).send
end
context "host_for" do
it "defaults to localhost" do
Email::Sender.host_for(nil).should == "localhost"
end
it "returns localhost for a weird host" do
Email::Sender.host_for("this is not a real host").should == "localhost"
end
it "parses hosts from urls" do
Email::Sender.host_for("http://meta.discourse.org").should == "meta.discourse.org"
end
it "downcases hosts" do
Email::Sender.host_for("http://ForumSite.com").should == "forumsite.com"
end
end
context "list_id_for" do
it "joins the host and forum name" do
Email::Sender.list_id_for("myforum", "http://mysite.com").should == '"myforum" <discourse.forum.myforum.mysite.com>'
end
it "uses localhost when no host is present" do
Email::Sender.list_id_for("myforum", nil).should == '"myforum" <discourse.forum.myforum.localhost>'
end
it "uses localhost with a weird host" do
Email::Sender.list_id_for("Fun", "this is not a real host").should == '"Fun" <discourse.forum.fun.localhost>'
Email::Sender.list_id_for("myforum", "mysite.com").should == '"myforum" <discourse.forum.myforum.mysite.com>'
end
it "removes double quotes from names" do
Email::Sender.list_id_for('Quoted "Forum"', 'http://quoted.com').should == '"Quoted \'Forum\'" <discourse.forum.quoted-forum.quoted.com>'
Email::Sender.list_id_for('Quoted "Forum"', 'quoted.com').should == '"Quoted \'Forum\'" <discourse.forum.quoted-forum.quoted.com>'
end
it "converts the site name to lower case and removes spaces" do
Email::Sender.list_id_for("Robin's cool Forum!", "http://robin.com").should == '"Robin\'s cool Forum!" <discourse.forum.robins-cool-forum.robin.com>'
Email::Sender.list_id_for("Robin's cool Forum!", "robin.com").should == '"Robin\'s cool Forum!" <discourse.forum.robins-cool-forum.robin.com>'
end
it "downcases host names" do
Email::Sender.list_id_for("cool", "http://ForumSite.com").should == '"cool" <discourse.forum.cool.forumsite.com>'
end
end
context 'with a valid message' do
@ -92,6 +97,9 @@ describe Email::Sender do
When { email_sender.send }
Then { expect(email_log.post_id).to eq(3344) }
Then { expect(email_log.topic_id).to eq(5577) }
Then { expect(message.header['In-Reply-To']).to be_present }
Then { expect(message.header['References']).to be_present }
end
context "email log with a reply key" do