FEATURE: user-friendly custom message

This commit is contained in:
Arpit Jalan 2016-06-08 18:04:25 +05:30
parent 2605c87ed9
commit 40e9e1be66
4 changed files with 68 additions and 93 deletions

View File

@ -17,21 +17,6 @@ class InviteMailer < ActionMailer::Base
invitee_name = "#{invite.invited_by.name} (#{invite.invited_by.username})" invitee_name = "#{invite.invited_by.name} (#{invite.invited_by.username})"
end end
# custom message
html = nil
if custom_message.present? && custom_message =~ /{invite_link}/
custom_message.gsub!("{invite_link}", "#{Discourse.base_url}/invites/#{invite.invite_key}")
custom_message.gsub!("{site_title}", SiteSetting.title) if custom_message =~ /{site_title}/
custom_message.gsub!("{site_description}", SiteSetting.site_description) if custom_message =~ /{site_description}/
html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
template: 'email/invite',
format: :html,
locals: { message: PrettyText.cook(custom_message).html_safe,
classes: 'custom-invite-email' }
)
end
# If they were invited to a topic # If they were invited to a topic
if first_topic.present? if first_topic.present?
# get topic excerpt # get topic excerpt
@ -40,28 +25,35 @@ class InviteMailer < ActionMailer::Base
topic_excerpt = first_topic.excerpt.gsub("\n", " ") topic_excerpt = first_topic.excerpt.gsub("\n", " ")
end end
html.gsub!("{topic_title}", first_topic.try(:title)) if html.present? && html =~ /{topic_title}/ template = 'invite_mailer'
html.gsub!("{topic_excerpt}", topic_excerpt) if html.present? && html =~ /{topic_excerpt}/ if custom_message.present?
template = 'custom_invite_mailer'
end
build_email(invite.email, build_email(invite.email,
template: 'invite_mailer', template: template,
html_override: html,
invitee_name: invitee_name, invitee_name: invitee_name,
site_domain_name: Discourse.current_hostname, site_domain_name: Discourse.current_hostname,
invite_link: "#{Discourse.base_url}/invites/#{invite.invite_key}", invite_link: "#{Discourse.base_url}/invites/#{invite.invite_key}",
topic_title: first_topic.try(:title), topic_title: first_topic.try(:title),
topic_excerpt: topic_excerpt, topic_excerpt: topic_excerpt,
site_description: SiteSetting.site_description, site_description: SiteSetting.site_description,
site_title: SiteSetting.title) site_title: SiteSetting.title,
user_custom_message: custom_message)
else else
template = 'invite_forum_mailer'
if custom_message.present?
template = 'custom_invite_forum_mailer'
end
build_email(invite.email, build_email(invite.email,
template: 'invite_forum_mailer', template: template,
html_override: html,
invitee_name: invitee_name, invitee_name: invitee_name,
site_domain_name: Discourse.current_hostname, site_domain_name: Discourse.current_hostname,
invite_link: "#{Discourse.base_url}/invites/#{invite.invite_key}", invite_link: "#{Discourse.base_url}/invites/#{invite.invite_key}",
site_description: SiteSetting.site_description, site_description: SiteSetting.site_description,
site_title: SiteSetting.title) site_title: SiteSetting.title,
user_custom_message: custom_message)
end end
end end

View File

@ -3045,37 +3045,6 @@ en:
invite: invite:
custom_message: "Make your invite a little bit more personal by writing a" custom_message: "Make your invite a little bit more personal by writing a"
custom_message_link: "custom message" custom_message_link: "custom message"
custom_message_placeholder: "Enter your custom message, use {invite_link} for specifying invite link." custom_message_placeholder: "Enter your custom message"
custom_message_template_forum: | custom_message_template_forum: "Hey, you should join this forum!"
Hello, custom_message_template_topic: "Hey, I thought you might enjoy this topic!"
You've been invited to join
> **{site_title}**
>
> {site_description}
If you're interested, click the link below:
{invite_link}
This invitation is from a trusted user, so you won't need to log in.
custom_message_template_topic: |
Hello,
You've been invited to a discussion
> **{topic_title}**
>
> {topic_excerpt}
at
> {site_title} -- {site_description}
If you're interested, click the link below:
{invite_link}
This invitation is from a trusted user, so you can reply to the discussion immediately.

View File

@ -1545,6 +1545,29 @@ en:
This invitation is from a trusted user, so you can reply to the discussion immediately. This invitation is from a trusted user, so you can reply to the discussion immediately.
custom_invite_mailer:
subject_template: "%{invitee_name} invited you to '%{topic_title}' on %{site_domain_name}"
text_body_template: |
%{invitee_name} invited you to a discussion
> **%{topic_title}**
>
> %{topic_excerpt}
at
> %{site_title} -- %{site_description}
Message from %{invitee_name}:
%{user_custom_message}
If you're interested, click the link below:
%{invite_link}
This invitation is from a trusted user, so you can reply to the discussion immediately.
invite_forum_mailer: invite_forum_mailer:
subject_template: "%{invitee_name} invited you to join %{site_domain_name}" subject_template: "%{invitee_name} invited you to join %{site_domain_name}"
text_body_template: | text_body_template: |
@ -1560,6 +1583,25 @@ en:
This invitation is from a trusted user, so you won't need to log in. This invitation is from a trusted user, so you won't need to log in.
custom_invite_forum_mailer:
subject_template: "%{invitee_name} invited you to join %{site_domain_name}"
text_body_template: |
%{invitee_name} invited you to join
> **%{site_title}**
>
> %{site_description}
Message from %{invitee_name}:
%{user_custom_message}
If you're interested, click the link below:
%{invite_link}
This invitation is from a trusted user, so you won't need to log in.
invite_password_instructions: invite_password_instructions:
subject_template: "Set password for your %{site_name} account" subject_template: "Set password for your %{site_name} account"
text_body_template: | text_body_template: |

View File

@ -38,7 +38,7 @@ describe InviteMailer do
context "custom invite message" do context "custom invite message" do
context "custom message includes invite link" do context "custom message includes invite link" do
let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hello,\n\nYou've been invited you to join\n\n<a href=\"javascript:alert('HACK!')\">Click me.</a>\n\n> **{site_title}**\n>\n> {site_description}\n\nIf you're interested, click the link below:\n\n{invite_link}\n\nThis invitation is from a trusted user, so you won't need to log in.") } let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hey, you should join this forum!") }
it 'renders the invitee email' do it 'renders the invitee email' do
expect(custom_invite_mail.to).to eql([invite.email]) expect(custom_invite_mail.to).to eql([invite.email])
@ -52,40 +52,20 @@ describe InviteMailer do
expect(custom_invite_mail.subject).to match(Discourse.current_hostname) expect(custom_invite_mail.subject).to match(Discourse.current_hostname)
end end
it 'renders the html' do it 'renders the body' do
expect(custom_invite_mail.html_part).to be_present expect(custom_invite_mail.body).to be_present
end end
it 'renders custom_message' do it 'renders custom_message' do
expect(custom_invite_mail.html_part.to_s).to match("You've been invited you to join") expect(custom_invite_mail.body.encoded).to match("Hey, you should join this forum!")
end end
it 'renders the inviter email' do it 'renders the inviter email' do
expect(custom_invite_mail.from).to eql([SiteSetting.notification_email]) expect(custom_invite_mail.from).to eql([SiteSetting.notification_email])
end end
it 'sanitizes HTML' do
expect(custom_invite_mail.html_part.to_s).to_not match("HACK!")
end
it 'renders invite link' do it 'renders invite link' do
expect(custom_invite_mail.html_part.to_s).to match("#{Discourse.base_url}/invites/#{invite.invite_key}") expect(custom_invite_mail.body.encoded).to match("#{Discourse.base_url}/invites/#{invite.invite_key}")
end
end
context "custom message does not include invite link" do
let(:custom_invite_without_link) { InviteMailer.send_invite(invite, "Hello,\n\nYou've been invited to join\n\n> **{site_title}**\n>\n> {site_description}") }
it 'renders default body' do
expect(custom_invite_without_link.body).to be_present
end
it 'does not render html' do
expect(custom_invite_without_link.html_part).to eq(nil)
end
it 'renders invite link' do
expect(custom_invite_without_link.body.encoded).to match("#{Discourse.base_url}/invites/#{invite.invite_key}")
end end
end end
end end
@ -133,22 +113,14 @@ describe InviteMailer do
end end
context "custom invite message" do context "custom invite message" do
let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hello,\n\nYou've been invited to a discussion\n\n<a href=\"javascript:alert('HACK!')\">Click me.</a>\n\n> **{site_title}** -- {site_description}\n\nIf you're interested, click the link below:\n\n{invite_link}\n\nThis invitation is from a trusted user, so you can reply to the discussion immediately.") } let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hey, I thought you might enjoy this topic!") }
it 'renders the html' do
expect(custom_invite_mail.html_part).to be_present
end
it 'renders custom_message' do it 'renders custom_message' do
expect(custom_invite_mail.html_part.to_s).to match("You've been invited to a discussion") expect(custom_invite_mail.body.encoded).to match("Hey, I thought you might enjoy this topic!")
end
it 'sanitizes HTML' do
expect(custom_invite_mail.html_part.to_s).to_not match("HACK!")
end end
it 'renders invite link' do it 'renders invite link' do
expect(custom_invite_mail.html_part.to_s).to match("#{Discourse.base_url}/invites/#{invite.invite_key}") expect(custom_invite_mail.body.encoded).to match("#{Discourse.base_url}/invites/#{invite.invite_key}")
end end
end end
end end