Skip to content

Commit

Permalink
AO3-6493 Notify wrangling supervisors via email when a wrangler chang…
Browse files Browse the repository at this point in the history
…es their username (#4802)

* AO3-6493 Basic functionality

* AO3-6493 Only notify for wranglers

* AO3-6493 I18n emails

* AO3-6493 Configurable email address

* AO3-6493 Better email address config name

* AO3-6493 Come to me doggy

* AO3-6493 Reviewdog

* AO3-6493 Use placeholder email address

* AO3-6493 Rename TagWranglingAdminMailer to TagWranglingSupervisorMailer

* AO3-6493 Use standard informal greeting
  • Loading branch information
Bilka2 authored Aug 14, 2024
1 parent d53e9da commit ac53277
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/mailers/tag_wrangling_supervisor_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class TagWranglingSupervisorMailer < ApplicationMailer
default to: ArchiveConfig.TAG_WRANGLER_SUPERVISORS_ADDRESS

# Send an email to tag wrangling supervisors when a tag wrangler changes their username
def wrangler_username_change_notification(old_name, new_name)
@old_username = old_name
@new_username = new_name
mail(
subject: default_i18n_subject(app_name: ArchiveConfig.APP_SHORT_NAME)
)
end
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class User < ApplicationRecord

before_update :add_renamed_at, if: :will_save_change_to_login?
after_update :update_pseud_name
after_update :send_wrangler_username_change_notification, if: :is_tag_wrangler?
after_update :log_change_if_login_was_edited
after_update :log_email_change, if: :saved_change_to_email?

Expand Down Expand Up @@ -569,6 +570,12 @@ def log_change_if_login_was_edited
create_log_item(action: ArchiveConfig.ACTION_RENAME, note: "Old Username: #{login_before_last_save}; New Username: #{login}") if saved_change_to_login?
end

def send_wrangler_username_change_notification
return unless saved_change_to_login? && login_before_last_save.present?

TagWranglingSupervisorMailer.wrangler_username_change_notification(login_before_last_save, login).deliver_now
end

def log_email_change
current_admin = User.current_user if User.current_user.is_a?(Admin)
options = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% content_for :message do %>
<p><%= t("mailer.general.greeting.informal.addressed", name: style_bold(t("mailer.general.greeting.tag_wrangler_supervisors"))).html_safe %></p>

<p><%= t(".name_changed.html", old_username: style_bold(@old_username), new_username: style_bold(@new_username)) %></p>

<p>
<%= t("mailer.general.closing.informal") %><br />
<%= t("mailer.general.signature.app_short_name") %>
</p>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% content_for :message do %>
<%= t("mailer.general.greeting.informal.addressed", name: t("mailer.general.greeting.tag_wrangler_supervisors")) %>
<%= t(".name_changed.html", old_username: @old_username, new_username: @new_username) %>
<%= t("mailer.general.closing.informal") %>
<%= t("mailer.general.signature.app_short_name") %>
<% end %>
1 change: 1 addition & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ RETURN_ADDRESS: '[email protected]'
SPAM_ALERT_ADDRESS: '[email protected]'
SPAM_THRESHOLD: 15
ADMIN_ADDRESS: '[email protected]'
TAG_WRANGLER_SUPERVISORS_ADDRESS: '[email protected]'

# Because the default email addresses are fake,
# email delivery is turned off by default, even for production.
Expand Down
6 changes: 6 additions & 0 deletions config/locales/mailers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ en:
addressed: Hi, %{name}!
unaddressed: Hi!
introductory: Hello from the Archive of Our Own!
tag_wrangler_supervisors: Tag Wrangler Supervisors
metadata_label_indicator: ": "
signature:
abuse_team: The AO3 Policy & Abuse team
Expand All @@ -89,6 +90,11 @@ en:
guest_with_parens: "(Guest)"
official_with_parens: "(Official)"
registered_with_parens: "(Registered User)"
tag_wrangling_supervisor_mailer:
wrangler_username_change_notification:
name_changed:
html: The wrangler %{old_username} has changed their name to %{new_username}.
subject: "[%{app_name}] Wrangler name change"
user_mailer:
abuse_report:
copy:
Expand Down
22 changes: 22 additions & 0 deletions features/users/user_rename.feature
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,25 @@ Feature:
And I press "Change User Name"
Then I should get confirmation that I changed my username
And I should see "Hi, notforbidden"

Scenario: Tag wrangling supervisors are emailed about tag wrangler username changes
Given the user "before" exists and is activated
And I am logged in as "before" with password "password"
And all emails have been delivered
And I visit the change username page for before
And I fill in "New user name" with "after"
And I fill in "Password" with "password"
And I press "Change User Name"
Then 0 email should be delivered to "[email protected]"
When the user "wrangler_before" exists and has the role "tag_wrangler"
And I am logged in as "wrangler_before" with password "password"
And all emails have been delivered
And I visit the change username page for wrangler_before
And I fill in "New user name" with "wrangler_after"
And I fill in "Password" with "password"
And I press "Change User Name"
Then 1 email should be delivered to "[email protected]"
And the email should contain "The wrangler"
And the email should contain "wrangler_before"
And the email should contain "has changed their name"
And the email should contain "wrangler_after"
36 changes: 36 additions & 0 deletions spec/mailers/tag_wrangling_supervisor_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "spec_helper"

describe TagWranglingSupervisorMailer do
describe "#wrangler_username_change_notification" do
let(:email) { TagWranglingSupervisorMailer.wrangler_username_change_notification(old_name, new_name) }
let(:old_name) { "fast" }
let(:new_name) { "express" }

it_behaves_like "an email with a valid sender"
it_behaves_like "a multipart email"
it_behaves_like "a translated email"

it "has the correct subject line" do
subject = "[#{ArchiveConfig.APP_SHORT_NAME}] Wrangler name change"
expect(email).to have_subject(subject)
end

it "delivers to the correct address" do
expect(email).to deliver_to ArchiveConfig.TAG_WRANGLER_SUPERVISORS_ADDRESS
end

describe "HTML version" do
it "has the correct content" do
expect(email).to have_html_part_content("The wrangler <b")
expect(email).to have_html_part_content(">fast</b> has changed their name to <b")
expect(email).to have_html_part_content(">express</b>.")
end
end

describe "text version" do
it "has the correct content" do
expect(email).to have_text_part_content("The wrangler fast has changed their name to express.")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TagWranglingSupervisorMailerPreview < ApplicationMailerPreview
# Sent to tag wrangling supervisors when a tag wrangler changes their username
def wrangler_username_change_notification
TagWranglingSupervisorMailer.wrangler_username_change_notification("anakin", "vader")
end
end

0 comments on commit ac53277

Please sign in to comment.