Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AO3-6764 Add kudo mailer preview #4875

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions app/mailers/kudo_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ def batch_kudo_notification(user_id, user_kudos)
user = User.find(user_id)
kudos_hash = JSON.parse(user_kudos)

I18n.with_locale(user.preference.locale.iso) do
kudos_hash.each_pair do |commentable_info, kudo_givers_hash|
# Parse the key to extract the type and id of the commentable so we can
# weed out any commentables that no longer exist.
commentable_type, commentable_id = commentable_info.split("_")
commentable = commentable_type.constantize.find_by(id: commentable_id)
next unless commentable
kudos_hash.each_pair do |commentable_info, _kudo_givers_hash|
# Parse the key to extract the type and id of the commentable so we can
# weed out any commentables that no longer exist.
commentable_type, commentable_id = commentable_info.split("_")
commentable = commentable_type.constantize.find_by(id: commentable_id)
next unless commentable

@commentables << commentable
end
mail(
to: user.email,
subject: default_i18n_subject(app_name: ArchiveConfig.APP_SHORT_NAME)
)
@commentables << commentable
end
mail(
to: user.email,
subject: default_i18n_subject(app_name: ArchiveConfig.APP_SHORT_NAME)
)
end
end
4 changes: 3 additions & 1 deletion app/models/redis_mail_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def self.deliver_kudos
# queue the notification for delivery
begin
# don't die if we hit one deleted user
KudoMailer.batch_kudo_notification(author_id, user_kudos.to_json).deliver_later
I18n.with_locale(User.find(author_id).preference.locale.iso) do
KudoMailer.batch_kudo_notification(author_id, user_kudos.to_json).deliver_later
end
rescue
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/views/kudo_mailer/batch_kudo_notification.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# kudos are present.
givers = names.dup
givers << t(".guest", count: guest_count) unless guest_count.zero?
givers_list = givers.map { |k| style_bold(k) }.to_sentence.html_safe
givers_list = to_sentence(givers.map { |k| style_bold(k) })
%>

<% if kudo_count == 1 && guest_count == 1 %>
<%= t(".html.single_guest.left_kudos", giver_list: style_bold(t(".html.single_guest.giver_list")).html_safe, commentable_link: commentable_link.html_safe).html_safe %>
<%= t(".single_guest.html", giver: style_bold(t(".single_guest.giver")), commentable_link: commentable_link) %>
<% else %>
<%= t(".html.left_kudos", givers_list: givers_list, commentable_link: commentable_link.html_safe, count: kudo_count).html_safe %>
<%= t(".left_kudos.html", givers_list: givers_list, commentable_link: commentable_link, count: kudo_count) %>
<% end %>

<% if (index < @commentables.length - 1) %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/kudo_mailer/batch_kudo_notification.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# present.
givers = names.dup
givers << t(".guest", count: guest_count) unless guest_count.zero?
givers_list = givers.to_sentence
givers_list = to_sentence(givers)
%>

<% if kudo_count == 1 && guest_count == 1 %>
<%= t(".text.single_guest", commentable_title: commentable_title, commentable_url: commentable_url) %>
<%= t(".single_guest.text", commentable_title: commentable_title, commentable_url: commentable_url) %>
<% else %>
<%= t(".text.left_kudos", givers_list: givers_list, commentable_title: commentable_title, commentable_url: commentable_url, count: kudo_count) %>
<%= t(".left_kudos.text", givers_list: givers_list, commentable_title: commentable_title, commentable_url: commentable_url, count: kudo_count) %>
<% end %>

<% if (index < @commentables.length - 1) %>
Expand Down
17 changes: 8 additions & 9 deletions config/locales/mailers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ en:
guest:
one: a guest
other: "%{count} guests"
html:
left_kudos:
left_kudos:
html:
one: "%{givers_list} left kudos on %{commentable_link}."
other: "%{givers_list} left kudos on %{commentable_link}."
single_guest:
giver_list: A guest
left_kudos: "%{giver_list} left kudos on %{commentable_link}."
subject: "[%{app_name}] You've got kudos!"
text:
left_kudos:
text:
one: '%{givers_list} left kudos on "%{commentable_title}" (%{commentable_url}).'
other: '%{givers_list} left kudos on "%{commentable_title}" (%{commentable_url}).'
single_guest: A guest left kudos on "%{commentable_title}" (%{commentable_url}).
single_guest:
giver: A guest
html: "%{giver} left kudos on %{commentable_link}."
text: A guest left kudos on "%{commentable_title}" (%{commentable_url}).
subject: "[%{app_name}] You've got kudos!"
mailer:
general:
closing:
Expand Down
15 changes: 15 additions & 0 deletions features/comments_and_kudos/kudos.feature
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ Feature: Kudos
And the email should not contain "0 guests"
And the email should not contain "translation missing"

Scenario: Translated kudos email

Given a locale with translated emails
And the user "myname1" enables translated emails
And all emails have been delivered
And the kudos queue is cleared
And I am logged in as "myname2"
And I leave kudos on "Awesome Story"
When kudos are sent
Then 1 email should be delivered to "[email protected]"
And the email should have "You've got kudos!" in the subject
And the email to "myname1" should contain "myname2"
And the email to "myname1" should contain "Awesome Story"
And the email to "myname1" should be translated

Scenario: Blocked users should not see a kudos button on their blocker's works
Given the work "Aftermath" by "creator"
And the user "creator" has blocked the user "pest"
Expand Down
49 changes: 34 additions & 15 deletions features/step_definitions/email_custom_steps.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
Given /^(?:a clear email queue|no emails have been sent|the email queue is clear)$/ do
Given "the email queue is clear" do
reset_mailer
end

Then /^"([^\"]*)" should be emailed$/ do |user|
Given "a locale with translated emails" do
language = Language.find_or_create_by(short: "new", name: "New")
Locale.create(iso: "new", name: "New", language: language, email_enabled: true)
Copy link
Contributor Author

@Bilka2 Bilka2 Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a completely new language and locale to prevent the existing translations from interfering with the tests. Otherwise a test might break when a Phrase export changes a translation.

# These keys are used in every email
I18n.backend.store_translations(:new, { mailer: { general: { footer: { general: { about: { html: "Translated footer", text: "Translated footer" } } } } } })
end

Given "the user {string} enables translated emails" do |user|
user = User.find_by(login: user)
$rollout.activate_user(:set_locale_preference, user)
user.preference.update!(locale: Locale.find_by(iso: "new"))
end

Then "the email to {string} should be translated" do |user|
step(%{the email to "#{user}" should contain "Translated footer"})
step(%{the email to "#{user}" should not contain "fan-run and fan-supported archive"}) # untranslated English text
step(%{the email to "#{user}" should not contain "translation missing"}) # missing translations in the target language fall back to English
end

Then "{string} should be emailed" do |user|
@user = User.find_by(login: user)
emails("to: \"#{email_for(@user.email)}\"").size.should > 0
expect(emails("to: \"#{email_for(@user.email)}\"").size).to be_positive
end

Then /^"([^\"]*)" should not be emailed$/ do |user|
Then "{string} should not be emailed" do |user|
@user = User.find_by(login: user)
emails("to: \"#{email_for(@user.email)}\"").size.should == 0
expect(emails("to: \"#{email_for(@user.email)}\"")).to be_empty
end

Then /^the email to "([^\"]*)" should contain "([^\"]*)"$/ do |user, text|
Then "the email to {string} should contain {string}" do |user, text|
@user = User.find_by(login: user)
email = emails("to: \"#{email_for(@user.email)}\"").first
if email.multipart?
email.text_part.body.should =~ /#{text}/
email.html_part.body.should =~ /#{text}/
expect(email.text_part.body).to match(text)
expect(email.html_part.body).to match(text)
else
email.body.should =~ /#{text}/
expect(email.body).to match(text)
end
end

Then /^the email to "([^\"]*)" should not contain "([^\"]*)"$/ do |user, text|
Then "the email to {string} should not contain {string}" do |user, text|
@user = User.find_by(login: user)
email = emails("to: \"#{email_for(@user.email)}\"").first
if email.multipart?
email.text_part.body.should_not =~ /#{text}/
email.html_part.body.should_not =~ /#{text}/
expect(email.text_part.body).not_to match(text)
expect(email.html_part.body).not_to match(text)
else
email.body.should_not =~ /#{text}/
expect(email.body).not_to match(text)
end
end

Then(/^"([^\"]*)" should receive (\d+) emails?$/) do |user, count|
Then "{string} should receive {int} email(s)" do |user, count|
@user = User.find_by(login: user)
emails("to: \"#{email_for(@user.email)}\"").size.should == count.to_i
expect(emails("to: \"#{email_for(@user.email)}\"").size).to eq(count.to_i)
end
40 changes: 25 additions & 15 deletions spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,25 @@
end
end

shared_examples "sends an English email" do
it "sends an English email" do
expect(I18n).to receive(:with_locale).with("en").and_call_original
email = UserMailer.admin_hidden_work_notification(work.id, user.id)
expect(email).to have_html_part_content("fan-run and fan-supported archive")
expect(email).not_to have_html_part_content("translated footer")
expect(email).to have_text_part_content("fan-run and fan-supported archive")
expect(email).not_to have_text_part_content("translated footer")
end
end

describe "obeys the set locale preference feature flag" do
Bilka2 marked this conversation as resolved.
Show resolved Hide resolved
let(:user) { create(:user) }
let(:work) { create(:work, authors: [user.default_pseud]) }
let(:locale) { create(:locale) }
let(:locale) { create(:locale, iso: "new", email_enabled: true) }

before do
I18n.backend.store_translations(:new, { mailer: { general: { footer: { general: { about: { html: "HTML translated footer", text: "Text translated footer" } } } } } })
end

context "when the set locale preference feature flag is on" do
before { $rollout.activate_user(:set_locale_preference, user) }
Expand All @@ -1217,18 +1232,19 @@
before { user.preference.update!(locale: locale) }

it "sends a localised email" do
expect(I18n).to receive(:with_locale).with(locale.iso)
expect(UserMailer.admin_hidden_work_notification(work.id, user.id)).to be_truthy
expect(I18n).to receive(:with_locale).with(locale.iso).and_call_original
email = UserMailer.admin_hidden_work_notification(work.id, user.id)
expect(email).to have_html_part_content("HTML translated footer")
expect(email).not_to have_html_part_content("fan-run and fan-supported archive")
expect(email).to have_text_part_content("Text translated footer")
expect(email).not_to have_text_part_content("fan-run and fan-supported archive")
end
end

context "and the user has the default locale set" do
before { user.preference.update!(locale: Locale.default) }

it "sends an English email" do
expect(I18n).to receive(:with_locale).with("en")
expect(UserMailer.admin_hidden_work_notification(work.id, user.id)).to be_truthy
end
include_examples "sends an English email"
end
end

Expand All @@ -1238,19 +1254,13 @@
context "and the user has non-default locale set" do
before { user.preference.update!(locale: locale) }

it "sends an English email" do
expect(I18n).to receive(:with_locale).with("en")
expect(UserMailer.admin_hidden_work_notification(work.id, user.id)).to be_truthy
end
include_examples "sends an English email"
end

context "and the user has the default locale set" do
before { user.preference.update!(locale: Locale.default) }

it "sends an English email" do
expect(I18n).to receive(:with_locale).with("en")
expect(UserMailer.admin_hidden_work_notification(work.id, user.id)).to be_truthy
end
include_examples "sends an English email"
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/mailers/previews/kudo_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class KudoMailerPreview < ApplicationMailerPreview
# Sends a kudos notification
def batch_kudo_notification
user = create(:user)
work = create(:work)
guest_count = params[:guest_count] || 1
user_count = params[:user_count].to_i || 1
names = Array.new(user_count) { "User#{Faker::Alphanumeric.alpha(number: 8)}" }
hash = { "Work_#{work.id}": { guest_count:, names: } }

Check warning on line 9 in test/mailers/previews/kudo_mailer_preview.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Space missing after colon. Raw Output: test/mailers/previews/kudo_mailer_preview.rb:9:46: C: Layout/SpaceAfterColon: Space missing after colon.
Copy link
Contributor Author

@Bilka2 Bilka2 Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax is new to Ruby 3.1 and the rubocop annotation is a false positive. Adding a space would make a different cop complain. This cop accepts the formatting starting with rubocop 1.24.0, we are on 1.22.3.

KudoMailer.batch_kudo_notification(user.id, hash.to_json)
end
end
Loading