mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
The EmailBuilder now creates the Reply by Email Key if necessary.
This commit is contained in:
parent
cf9b6beb13
commit
49c09898e2
8 changed files with 77 additions and 100 deletions
|
@ -107,13 +107,10 @@ class UserNotifications < ActionMailer::Base
|
|||
url: @post.url,
|
||||
username: username,
|
||||
add_unsubscribe_link: true,
|
||||
allow_reply_by_email: opts[:allow_reply_by_email],
|
||||
template: "user_notifications.user_#{notification_type}"
|
||||
}
|
||||
|
||||
if opts[:allow_reply_by_email] && SiteSetting.reply_by_email_enabled?
|
||||
email_opts[:allow_reply_by_email] = true
|
||||
end
|
||||
|
||||
# If we have a display name, change the from address
|
||||
if username.present?
|
||||
email_opts[:from_alias] = I18n.t(:via, username: username, site_name: SiteSetting.title)
|
||||
|
|
|
@ -3,13 +3,6 @@ class EmailLog < ActiveRecord::Base
|
|||
validates_presence_of :email_type
|
||||
validates_presence_of :to_address
|
||||
|
||||
before_create do
|
||||
# We only generate a reply
|
||||
if SiteSetting.reply_by_email_enabled?
|
||||
self.reply_key = SecureRandom.hex(16)
|
||||
end
|
||||
end
|
||||
|
||||
after_create do
|
||||
# Update last_emailed_at if the user_id is present
|
||||
User.update_all("last_emailed_at = CURRENT_TIMESTAMP", id: user_id) if user_id.present?
|
||||
|
|
|
@ -35,6 +35,10 @@ module Email
|
|||
body
|
||||
end
|
||||
|
||||
def allow_reply_by_email?
|
||||
SiteSetting.reply_by_email_enabled? && @opts[:allow_reply_by_email]
|
||||
end
|
||||
|
||||
def template_args
|
||||
@template_args ||= { site_name: SiteSetting.title,
|
||||
base_url: Discourse.base_url,
|
||||
|
@ -60,6 +64,9 @@ module Email
|
|||
if @opts[:add_unsubscribe_link]
|
||||
result['List-Unsubscribe'] = "<#{template_args[:user_preferences_url]}>" if @opts[:add_unsubscribe_link]
|
||||
end
|
||||
|
||||
result['Discourse-Reply-Key'] = SecureRandom.hex(16) if allow_reply_by_email?
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
|
|
|
@ -42,7 +42,13 @@ module Email
|
|||
to_address = @message.to
|
||||
to_address = to_address.first if to_address.is_a?(Array)
|
||||
|
||||
EmailLog.create!(email_type: @email_type, to_address: to_address, user_id: @user.try(:id))
|
||||
email_log = EmailLog.new(email_type: @email_type, to_address: to_address, user_id: @user.try(:id))
|
||||
|
||||
reply_key = @message.header['Discourse-Reply-Key'].to_s
|
||||
email_log.reply_key = reply_key if reply_key.present?
|
||||
email_log.save!
|
||||
email_log
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -25,6 +25,42 @@ describe Email::MessageBuilder do
|
|||
expect(builder.build_args[:charset]).to eq("UTF-8")
|
||||
end
|
||||
|
||||
context "reply by email" do
|
||||
|
||||
context "without allow_reply_by_email" do
|
||||
it "does not have a Discourse-Reply-Key" do
|
||||
expect(builder.header_args['Discourse-Reply-Key']).to be_blank
|
||||
end
|
||||
end
|
||||
|
||||
context "with allow_reply_by_email" do
|
||||
let(:reply_by_email_builder) { Email::MessageBuilder.new(to_address, allow_reply_by_email: true) }
|
||||
let(:reply_key) { reply_by_email_builder.header_args['Discourse-Reply-Key'] }
|
||||
|
||||
context "With the SiteSetting enabled" do
|
||||
before do
|
||||
SiteSetting.expects(:reply_by_email_enabled?).returns(true)
|
||||
end
|
||||
|
||||
it "has a Discourse-Reply-Key" do
|
||||
expect(reply_key).to be_present
|
||||
expect(reply_key.size).to eq(32)
|
||||
end
|
||||
end
|
||||
|
||||
context "With the SiteSetting disabled" do
|
||||
before do
|
||||
SiteSetting.expects(:reply_by_email_enabled?).returns(false)
|
||||
end
|
||||
|
||||
it "has no Discourse-Reply-Key" do
|
||||
expect(reply_key).to be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "unsubscribe link" do
|
||||
|
||||
context "with add_unsubscribe_link false" do
|
||||
|
|
|
@ -22,6 +22,8 @@ describe Email::Sender do
|
|||
|
||||
context 'with a valid message' do
|
||||
|
||||
let(:reply_key) { "abcd" * 8 }
|
||||
|
||||
let(:message) do
|
||||
message = Mail::Message.new to: 'eviltrout@test.domain',
|
||||
body: '**hello**'
|
||||
|
@ -37,49 +39,33 @@ describe Email::Sender do
|
|||
end
|
||||
|
||||
context 'email logs' do
|
||||
let(:email_log) { EmailLog.last }
|
||||
|
||||
before do
|
||||
email_sender.send
|
||||
@email_log = EmailLog.last
|
||||
end
|
||||
|
||||
it 'creates an email log' do
|
||||
@email_log.should be_present
|
||||
end
|
||||
|
||||
it 'has the correct type' do
|
||||
@email_log.email_type.should == 'valid_type'
|
||||
end
|
||||
|
||||
it 'has the correct to_address' do
|
||||
@email_log.to_address.should == 'eviltrout@test.domain'
|
||||
end
|
||||
|
||||
it 'has no user_id' do
|
||||
@email_log.user_id.should be_blank
|
||||
end
|
||||
|
||||
|
||||
When { email_sender.send }
|
||||
Then { expect(email_log).to be_present }
|
||||
Then { expect(email_log.email_type).to eq('valid_type') }
|
||||
Then { expect(email_log.to_address).to eq('eviltrout@test.domain') }
|
||||
Then { expect(email_log.reply_key).to be_blank }
|
||||
Then { expect(email_log.user_id).to be_blank }
|
||||
end
|
||||
|
||||
context "email log with a reply key" do
|
||||
before do
|
||||
message.header['Discourse-Reply-Key'] = reply_key
|
||||
end
|
||||
|
||||
let(:email_log) { EmailLog.last }
|
||||
When { email_sender.send }
|
||||
Then { expect(email_log.reply_key).to eq(reply_key) }
|
||||
end
|
||||
|
||||
|
||||
context 'email parts' do
|
||||
before { email_sender.send }
|
||||
|
||||
it 'makes the message multipart' do
|
||||
message.should be_multipart
|
||||
end
|
||||
|
||||
it 'sets the correct content type for the plain text part' do
|
||||
expect(message.text_part.content_type).to eq 'text/plain; charset=UTF-8'
|
||||
end
|
||||
|
||||
it 'sets the correct content type for the html part' do
|
||||
expect(message.html_part.content_type).to eq 'text/html; charset=UTF-8'
|
||||
end
|
||||
|
||||
it 'converts the html part to html' do
|
||||
expect(message.html_part.body.to_s).to match("<p><strong>hello</strong></p>")
|
||||
end
|
||||
When { email_sender.send }
|
||||
Then { expect(message).to be_multipart }
|
||||
Then { expect(message.text_part.content_type).to eq('text/plain; charset=UTF-8') }
|
||||
Then { expect(message.html_part.content_type).to eq('text/html; charset=UTF-8') }
|
||||
Then { expect(message.html_part.body.to_s).to match("<p><strong>hello</strong></p>") }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -50,27 +50,14 @@ describe UserNotifications do
|
|||
shared_examples "supports reply by email" do
|
||||
context "reply_by_email" do
|
||||
it "should have allow_reply_by_email set when that feature is enabled" do
|
||||
SiteSetting.stubs(:reply_by_email_enabled?).returns(true)
|
||||
expects_build_with(has_entry(:allow_reply_by_email, true))
|
||||
end
|
||||
|
||||
it "should have not allow_reply_by_email set when that feature is disabled" do
|
||||
SiteSetting.stubs(:reply_by_email_enabled?).returns(false)
|
||||
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "no reply by email" do
|
||||
context "reply_by_email" do
|
||||
|
||||
it "doesn't support reply by email, even when that option is enabled" do
|
||||
SiteSetting.stubs(:reply_by_email_enabled?).returns(true)
|
||||
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
|
||||
end
|
||||
|
||||
it "should have not allow_reply_by_email set when that feature is disabled" do
|
||||
SiteSetting.stubs(:reply_by_email_enabled?).returns(false)
|
||||
it "doesn't support reply by email" do
|
||||
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
|
||||
end
|
||||
end
|
||||
|
@ -131,7 +118,6 @@ describe UserNotifications do
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
describe "user quoted" do
|
||||
include_examples "notification email building" do
|
||||
let(:notification_type) { :quoted }
|
||||
|
|
|
@ -10,40 +10,6 @@ describe EmailLog do
|
|||
|
||||
context 'after_create' do
|
||||
|
||||
context "reply_key" do
|
||||
|
||||
context "with reply by email enabled" do
|
||||
before do
|
||||
SiteSetting.expects(:reply_by_email_enabled).returns(true)
|
||||
end
|
||||
|
||||
context 'generates a reply key' do
|
||||
let(:reply_key) { Fabricate(:email_log).reply_key }
|
||||
|
||||
it "has a reply key" do
|
||||
expect(reply_key).to be_present
|
||||
expect(reply_key.size).to eq(32)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with reply by email disabled" do
|
||||
before do
|
||||
SiteSetting.expects(:reply_by_email_enabled).returns(false)
|
||||
end
|
||||
|
||||
context 'generates a reply key' do
|
||||
let(:reply_key) { Fabricate(:email_log).reply_key }
|
||||
|
||||
it "has no reply key" do
|
||||
expect(reply_key).to be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
context 'with user' do
|
||||
let(:user) { Fabricate(:user) }
|
||||
|
||||
|
|
Loading…
Reference in a new issue