A platform for community discussion. Free, open, simple.
allow adding tags as a custom subject format for emails (#5846)
Maja Komel0942e2c795dc
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rbindex b1a46891357..6302918bdc2 100644--- a/app/mailers/user_notifications.rb+++ b/app/mailers/user_notifications.rb@@ -223,6 +223,7 @@ class UserNotifications < ActionMailer::Baseopts[:allow_reply_by_email] = trueopts[:use_site_subject] = trueopts[:show_category_in_subject] = true+ opts[:show_tags_in_subject] = truenotification_email(user, opts)end@@ -230,6 +231,7 @@ class UserNotifications < ActionMailer::Baseopts[:allow_reply_by_email] = trueopts[:use_site_subject] = trueopts[:show_category_in_subject] = true+ opts[:show_tags_in_subject] = truenotification_email(user, opts)end@@ -237,6 +239,7 @@ class UserNotifications < ActionMailer::Baseopts[:allow_reply_by_email] = trueopts[:use_site_subject] = trueopts[:show_category_in_subject] = true+ opts[:show_tags_in_subject] = truenotification_email(user, opts)end@@ -244,6 +247,7 @@ class UserNotifications < ActionMailer::Baseopts[:allow_reply_by_email] = trueopts[:use_site_subject] = trueopts[:show_category_in_subject] = true+ opts[:show_tags_in_subject] = truenotification_email(user, opts)end@@ -251,6 +255,7 @@ class UserNotifications < ActionMailer::Baseopts[:allow_reply_by_email] = trueopts[:use_site_subject] = trueopts[:show_category_in_subject] = true+ opts[:show_tags_in_subject] = truenotification_email(user, opts)end@@ -259,6 +264,7 @@ class UserNotifications < ActionMailer::Baseopts[:use_site_subject] = trueopts[:add_re_to_subject] = trueopts[:show_category_in_subject] = true+ opts[:show_tags_in_subject] = truenotification_email(user, opts)end@@ -267,6 +273,7 @@ class UserNotifications < ActionMailer::Baseopts[:use_site_subject] = trueopts[:add_re_to_subject] = trueopts[:show_category_in_subject] = false+ opts[:show_tags_in_subject] = falseopts[:show_group_in_subject] = true if SiteSetting.group_in_subject# We use the 'user_posted' event when you are emailed a post in a PM.@@ -285,6 +292,7 @@ class UserNotifications < ActionMailer::Baseopts[:allow_reply_by_email] = falseopts[:use_invite_template] = trueopts[:show_category_in_subject] = true+ opts[:show_tags_in_subject] = truenotification_email(user, opts)end@@ -299,6 +307,7 @@ class UserNotifications < ActionMailer::Baseuse_site_subject: true,add_re_to_subject: true,show_category_in_subject: true,+ show_tags_in_subject: true,notification_type: "posted",notification_data_hash: {original_username: post.user.username,@@ -382,6 +391,7 @@ class UserNotifications < ActionMailer::Baseuse_site_subject: opts[:use_site_subject],add_re_to_subject: opts[:add_re_to_subject],show_category_in_subject: opts[:show_category_in_subject],+ show_tags_in_subject: opts[:show_tags_in_subject],show_group_in_subject: opts[:show_group_in_subject],notification_type: notification_type,use_invite_template: opts[:use_invite_template],@@ -433,6 +443,12 @@ class UserNotifications < ActionMailer::Baseshow_category_in_subject = nilend+ # tag names+ if opts[:show_tags_in_subject] && post.topic_id+ tags = Topic.find_by(id: post.topic_id)&.tags.first(3)+ show_tags_in_subject = tags.any? ? tags.map { |t| "[#{t.name}]" }.join(" ") : nil+ end+if post.topic.private_message?subject_pm =if opts[:show_group_in_subject] && group = post.topic.allowed_groups&.first@@ -566,6 +582,7 @@ class UserNotifications < ActionMailer::Baseuse_site_subject: use_site_subject,add_re_to_subject: add_re_to_subject,show_category_in_subject: show_category_in_subject,+ show_tags_in_subject: show_tags_in_subject,private_reply: post.topic.private_message?,subject_pm: subject_pm,participants: participants,diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rbindex a6ec410ed01..0eb345b45f0 100644--- a/lib/email/message_builder.rb+++ b/lib/email/message_builder.rb@@ -66,6 +66,7 @@ module Emailsubject.gsub!("%{optional_re}", @opts[:add_re_to_subject] ? I18n.t('subject_re', @template_args) : '')subject.gsub!("%{optional_pm}", @opts[:private_reply] ? @template_args[:subject_pm] : '')subject.gsub!("%{optional_cat}", @template_args[:show_category_in_subject] ? "[#{@template_args[:show_category_in_subject]}] " : '')+ subject.gsub!("%{optional_tags}", @template_args[:show_tags_in_subject] ? "#{@template_args[:show_tags_in_subject]} " : '')subject.gsub!("%{topic_title}", @template_args[:topic_title]) if @template_args[:topic_title] # must be last for safetyelsesubject = @opts[:subject]diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rbindex 53ca30b5b5b..67033ff4018 100644--- a/spec/mailers/user_notifications_spec.rb+++ b/spec/mailers/user_notifications_spec.rb@@ -213,7 +213,9 @@ describe UserNotifications dodescribe '.user_replied' dolet(:response_by_user) { Fabricate(:user, name: "John Doe") }let(:category) { Fabricate(:category, name: 'India') }- let(:topic) { Fabricate(:topic, category: category) }+ let(:tag1) { Fabricate(:tag, name: 'Taggo') }+ let(:tag2) { Fabricate(:tag, name: 'Taggie') }+ let(:topic) { Fabricate(:topic, category: category, tags: [tag1, tag2]) }let(:post) { Fabricate(:post, topic: topic, raw: 'This is My super duper cool topic') }let(:response) { Fabricate(:post, reply_to_post_number: 1, topic: post.topic, user: response_by_user) }let(:user) { Fabricate(:user) }@@ -222,6 +224,7 @@ describe UserNotifications doit 'generates a correct email' do# Fabricator is not fabricating this ...+ SiteSetting.email_subject = "[%{site_name}] %{optional_pm}%{optional_cat}%{optional_tags}%{topic_title}"SiteSetting.enable_names = trueSiteSetting.display_name_on_posts = truemail = UserNotifications.user_replied(response.user,@@ -235,6 +238,10 @@ describe UserNotifications do# subject should include category nameexpect(mail.subject).to match(/India/)+ # subject should include tag names+ expect(mail.subject).to match(/Taggo/)+ expect(mail.subject).to match(/Taggie/)+mail_html = mail.html_part.to_sexpect(mail_html.scan(/My super duper cool topic/).count).to eq(1)