From 40e9e1be6642b1faa91c0f8c0aaec8f7cd5dfab5 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 8 Jun 2016 18:04:25 +0530 Subject: [PATCH] FEATURE: user-friendly custom message --- app/mailers/invite_mailer.rb | 38 ++++++++++---------------- config/locales/client.en.yml | 37 ++----------------------- config/locales/server.en.yml | 42 ++++++++++++++++++++++++++++ spec/mailers/invite_mailer_spec.rb | 44 ++++++------------------------ 4 files changed, 68 insertions(+), 93 deletions(-) diff --git a/app/mailers/invite_mailer.rb b/app/mailers/invite_mailer.rb index 0af7acd9b..f94aca2eb 100644 --- a/app/mailers/invite_mailer.rb +++ b/app/mailers/invite_mailer.rb @@ -17,21 +17,6 @@ class InviteMailer < ActionMailer::Base invitee_name = "#{invite.invited_by.name} (#{invite.invited_by.username})" 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 first_topic.present? # get topic excerpt @@ -40,28 +25,35 @@ class InviteMailer < ActionMailer::Base topic_excerpt = first_topic.excerpt.gsub("\n", " ") end - html.gsub!("{topic_title}", first_topic.try(:title)) if html.present? && html =~ /{topic_title}/ - html.gsub!("{topic_excerpt}", topic_excerpt) if html.present? && html =~ /{topic_excerpt}/ + template = 'invite_mailer' + if custom_message.present? + template = 'custom_invite_mailer' + end build_email(invite.email, - template: 'invite_mailer', - html_override: html, + template: template, invitee_name: invitee_name, site_domain_name: Discourse.current_hostname, invite_link: "#{Discourse.base_url}/invites/#{invite.invite_key}", topic_title: first_topic.try(:title), topic_excerpt: topic_excerpt, site_description: SiteSetting.site_description, - site_title: SiteSetting.title) + site_title: SiteSetting.title, + user_custom_message: custom_message) else + template = 'invite_forum_mailer' + if custom_message.present? + template = 'custom_invite_forum_mailer' + end + build_email(invite.email, - template: 'invite_forum_mailer', - html_override: html, + template: template, invitee_name: invitee_name, site_domain_name: Discourse.current_hostname, invite_link: "#{Discourse.base_url}/invites/#{invite.invite_key}", site_description: SiteSetting.site_description, - site_title: SiteSetting.title) + site_title: SiteSetting.title, + user_custom_message: custom_message) end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 9cfe190f8..ce457927e 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3045,37 +3045,6 @@ en: invite: custom_message: "Make your invite a little bit more personal by writing a" custom_message_link: "custom message" - custom_message_placeholder: "Enter your custom message, use {invite_link} for specifying invite link." - custom_message_template_forum: | - Hello, - - 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. + custom_message_placeholder: "Enter your custom message" + custom_message_template_forum: "Hey, you should join this forum!" + custom_message_template_topic: "Hey, I thought you might enjoy this topic!" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 25ac3de9f..5f035a6fe 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1545,6 +1545,29 @@ en: 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: subject_template: "%{invitee_name} invited you to join %{site_domain_name}" text_body_template: | @@ -1560,6 +1583,25 @@ en: 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: subject_template: "Set password for your %{site_name} account" text_body_template: | diff --git a/spec/mailers/invite_mailer_spec.rb b/spec/mailers/invite_mailer_spec.rb index d8c2816f0..ff61f3998 100644 --- a/spec/mailers/invite_mailer_spec.rb +++ b/spec/mailers/invite_mailer_spec.rb @@ -38,7 +38,7 @@ describe InviteMailer do context "custom invite message" 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\nClick me.\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 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) end - it 'renders the html' do - expect(custom_invite_mail.html_part).to be_present + it 'renders the body' do + expect(custom_invite_mail.body).to be_present end 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 it 'renders the inviter email' do expect(custom_invite_mail.from).to eql([SiteSetting.notification_email]) end - it 'sanitizes HTML' do - expect(custom_invite_mail.html_part.to_s).to_not match("HACK!") - end - it 'renders invite link' do - expect(custom_invite_mail.html_part.to_s).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}") + expect(custom_invite_mail.body.encoded).to match("#{Discourse.base_url}/invites/#{invite.invite_key}") end end end @@ -133,22 +113,14 @@ describe InviteMailer do end context "custom invite message" do - let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hello,\n\nYou've been invited to a discussion\n\nClick me.\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.") } - - it 'renders the html' do - expect(custom_invite_mail.html_part).to be_present - end + let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hey, I thought you might enjoy this topic!") } it 'renders custom_message' do - expect(custom_invite_mail.html_part.to_s).to match("You've been invited to a discussion") - end - - it 'sanitizes HTML' do - expect(custom_invite_mail.html_part.to_s).to_not match("HACK!") + expect(custom_invite_mail.body.encoded).to match("Hey, I thought you might enjoy this topic!") end 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