FIX: respect user timezone in emails about silencing and suspending (#16918)

This commit is contained in:
Andrei Prigorshnev 2022-05-27 13:58:54 +04:00 committed by GitHub
parent 0c590963c3
commit 25e4095c9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 2 deletions

View File

@ -161,12 +161,13 @@ class UserNotifications < ActionMailer::Base
reason: user_history.details
)
else
silenced_till = user.silenced_till.in_time_zone(user.user_option.timezone)
build_email(
user.email,
template: "user_notifications.account_silenced",
locale: user_locale(user),
reason: user_history.details,
silenced_till: I18n.l(user.silenced_till, format: :long)
silenced_till: I18n.l(silenced_till, format: :long)
)
end
end
@ -184,12 +185,13 @@ class UserNotifications < ActionMailer::Base
reason: user_history.details
)
else
suspended_till = user.suspended_till.in_time_zone(user.user_option.timezone)
build_email(
user.email,
template: "user_notifications.account_suspended",
locale: user_locale(user),
reason: user_history.details,
suspended_till: I18n.l(user.suspended_till, format: :long)
suspended_till: I18n.l(suspended_till, format: :long)
)
end
end

View File

@ -0,0 +1,4 @@
# frozen_string_literal: true
Fabricator(:user_history) do
end

View File

@ -1184,4 +1184,68 @@ describe UserNotifications do
"[admin](http://test.localhost/u/admin)")
end
end
describe ".account_silenced" do
fab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:silence_user]) }
it "adds the silenced_till date in user's timezone" do
user.user_option.timezone = "Asia/Tbilisi" # GMT+4
user.silenced_till = DateTime.parse("May 25, 2020, 12:00pm")
mail = UserNotifications.account_silenced(user, { user_history: user_history })
expect(mail.body).to include("May 25, 2020, 4:00pm")
end
context "user doesn't have timezone set" do
before do
user.user_option.timezone = nil
end
it "doesn't raise error" do
expect { UserNotifications.account_silenced(user) }.not_to raise_error
end
it "adds the silenced_till date in UTC" do
date = "May 25, 2020, 12:00pm"
user.silenced_till = DateTime.parse(date)
mail = UserNotifications.account_silenced(user, { user_history: user_history })
expect(mail.body).to include(date)
end
end
end
describe ".account_suspended" do
fab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:suspend_user]) }
it "adds the suspended_till date in user's timezone" do
user.user_option.timezone = "Asia/Tbilisi" # GMT+4
user.suspended_till = DateTime.parse("May 25, 2020, 12:00pm")
mail = UserNotifications.account_suspended(user, { user_history: user_history })
expect(mail.body).to include("May 25, 2020, 4:00pm")
end
context "user doesn't have timezone set" do
before do
user.user_option.timezone = nil
end
it "doesn't raise error" do
expect { UserNotifications.account_suspended(user) }.not_to raise_error
end
it "adds the suspended_till date in UTC" do
date = "May 25, 2020, 12:00pm"
user.suspended_till = DateTime.parse(date)
mail = UserNotifications.account_suspended(user, { user_history: user_history })
expect(mail.body).to include(date)
end
end
end
end