A platform for community discussion. Free, open, simple.
DEV: Add spec for `Email::Sender` for upload links in plain text emails.
Guo Xiang Tan40e67971f95a
diff --git a/lib/email/sender.rb b/lib/email/sender.rbindex ffe18cb95a6..464431a3ecf 100644--- a/lib/email/sender.rb+++ b/lib/email/sender.rb@@ -72,7 +72,7 @@ module Email# These are the links we add when a user uploads a file or image.# Ideally we would parse general markdown into plain text, but that is almost an intractable problem.url_prefix = Discourse.base_url- @message.parts[0].body = @message.parts[0].body.to_s.gsub(/<a class="attachment" href="(\/uploads\/default\/[^"]+)">([^<]*)<\/a>/, '[\2](' + url_prefix + '\1)')+ @message.parts[0].body = @message.parts[0].body.to_s.gsub(/<a class="attachment" href="(\/uploads\/default\/[^"]+)">([^<]*)<\/a>/, '[\2|attachment](' + url_prefix + '\1)')@message.parts[0].body = @message.parts[0].body.to_s.gsub(/<img src="(\/uploads\/default\/[^"]+)"([^>]*)>/, '')@message.text_part.content_type = 'text/plain; charset=UTF-8'diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rbindex 5615078a927..a1117209be9 100644--- a/spec/jobs/user_email_spec.rb+++ b/spec/jobs/user_email_spec.rb@@ -147,6 +147,15 @@ describe Jobs::UserEmail doendit "sends an email by default for a PM to a user that's been recently seen" do+ upload = Fabricate(:upload)++ post.update!(raw: <<~RAW)+ This is a test post++ <a class="attachment" href="#{upload.url}">test</a>+ <img src="#{upload.url}"/>+ RAW+Jobs::UserEmail.new.execute(type: :user_private_message,user_id: user.id,@@ -154,9 +163,16 @@ describe Jobs::UserEmail donotification_id: notification.id)- expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(- user.email- )+ email = ActionMailer::Base.deliveries.first++ expect(email.to).to contain_exactly(user.email)++ expect(email.parts[0].body.to_s).to include(<<~MD)+ This is a test post++ [test|attachment](#{Discourse.base_url}#{upload.url})+ + MDendit "doesn't send a PM email to a user that's been recently seen and has email_messages_level set to never" do