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-6775 Create delete_work_notification mailer preview #5057

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 6 additions & 7 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,20 @@ def prompter_notification(work_id, collection_id = nil)
# Sends email to creators when a creation is deleted
# NOTE: this must be sent synchronously! otherwise the work will no longer be there to send
# TODO refactor to make it asynchronous by passing the content in the method
def delete_work_notification(user, work)
def delete_work_notification(user, work, deleter)
@user = user
@work = work
@deleter = deleter
download = Download.new(@work, mime_type: "text/html", include_draft_chapters: true)
html = DownloadWriter.new(download).generate_html
html = ::Mail::Encodings::Base64.encode(html)
attachments["#{download.file_name}.html"] = { content: html, encoding: "base64" }
attachments["#{download.file_name}.txt"] = { content: html, encoding: "base64" }

I18n.with_locale(@user.preference.locale.iso) do
mail(
to: user.email,
subject: t("user_mailer.delete_work_notification.subject", app_name: ArchiveConfig.APP_SHORT_NAME)
)
end
mail(
to: user.email,
subject: default_i18n_subject(app_name: ArchiveConfig.APP_SHORT_NAME)
)
end

# Sends email to creators when a creation is deleted by an admin
Expand Down
31 changes: 17 additions & 14 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,23 @@ def new_recipients_have_not_blocked_gift_giver

before_destroy :send_deleted_work_notification, prepend: true
def send_deleted_work_notification
if self.posted?
users = self.pseuds.collect(&:user).uniq
orphan_account = User.orphan_account
unless users.blank?
for user in users
next if user == orphan_account
# Check to see if this work is being deleted by an Admin
if User.current_user.is_a?(Admin)
# this has to use the synchronous version because the work is going to be destroyed
UserMailer.admin_deleted_work_notification(user, self).deliver_now
else
# this has to use the synchronous version because the work is going to be destroyed
UserMailer.delete_work_notification(user, self).deliver_now
end
return unless self.posted?

users = self.pseuds.collect(&:user).uniq
return if users.blank?

orphan_account = User.orphan_account
users.each do |user|
next if user == orphan_account

# Check to see if this work is being deleted by an Admin
if User.current_user.is_a?(Admin)
# this has to use the synchronous version because the work is going to be destroyed
UserMailer.admin_deleted_work_notification(user, self).deliver_now
else
I18n.with_locale(user.preference.locale.iso) do
# this has to use the synchronous version because the work is going to be destroyed
UserMailer.delete_work_notification(user, self, User.current_user).deliver_now
end
end
end
Expand Down
17 changes: 8 additions & 9 deletions app/views/user_mailer/delete_work_notification.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<% content_for :message do %>
<p><%= t("mailer.general.greeting.formal_html", name: style_bold(@user.login)) %></p>
<p><%= (@work.pseuds.count > 1 && @user != User.current_user) ?
t('.deleted_other.html',
title: style_creation_title(@work.title),
pseud: style_pseud_link(User.current_user.default_pseud)) :
t('.deleted_yourself.html',
title: style_creation_title(@work.title)) %></p>
<p><%= t('.questions.html', support: support_link(t '.support')) %></p>
<p><%= t('.attachment') %></p>
<p><%= t("mailer.general.greeting.formal_html", name: style_bold(@user.login)) %></p>
<% if @user == @deleter || !@deleter %>
<p><%= t(".deleted_yourself.html", title: style_creation_title(@work.title)) %></p>
<% else %>
<p><%= t(".deleted_other.html", title: style_creation_title(@work.title), pseud: style_pseud_link(@deleter.default_pseud)) %></p>
<% end %>
<p><%= t(".questions.html", support: support_link(t(".support"))) %></p>
<p><%= t(".attachment") %></p>
<% end %>
16 changes: 7 additions & 9 deletions app/views/user_mailer/delete_work_notification.text.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<% content_for :message do %>
<%= t("mailer.general.greeting.formal_html", name: @user.login) %>

<%= (@work.pseuds.count > 1 && @user != User.current_user) ?
t('.deleted_other.text',
title: @work.title,
pseud: text_pseud(User.current_user.default_pseud)) :
t('.deleted_yourself.text',
title: @work.title) %>
<% if @user == @deleter || !@deleter %>
<%= t(".deleted_yourself.text", title: @work.title) %>
<% else %>
<%= t(".deleted_other.text", title: @work.title, pseud: text_pseud(@deleter.default_pseud)) %>
<% end %>

<%= t('.questions.text', support: t('.support'), url: new_feedback_report_url) %>
<%= t(".questions.text", support: t(".support"), url: new_feedback_report_url) %>

<%= t('.attachment') %>
<%= t(".attachment") %>
<% end %>

7 changes: 7 additions & 0 deletions features/step_definitions/work_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@
FactoryBot.create(:work, title: title, authors: [user1.default_pseud, user2.default_pseud])
end

Given "the work {string} by {string}, {string} and {string}" do |title, login1, login2, login3|
user1 = ensure_user(login1)
user2 = ensure_user(login2)
user3 = ensure_user(login3)
FactoryBot.create(:work, title: title, authors: [user1.default_pseud, user2.default_pseud, user3.default_pseud])
end

Given "the work {string} by {string} and {string} with guest comments enabled" do |title, login1, login2|
user1 = ensure_user(login1)
user2 = ensure_user(login2)
Expand Down
18 changes: 18 additions & 0 deletions features/works/work_delete.feature
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,21 @@ Feature: Delete Works
When I am logged in as the author of "Over the Limit"
And I delete the work "Over the Limit"
Then I should see "Your work Over the Limit was deleted."

Scenario: Deleting a work sends translated deletion notification emails
Given a locale with translated emails
And the user "owner" exists and is activated
And the user "owner" enables translated emails
And the user "someone_else" exists and is activated
And the user "someone_else" enables translated emails
And the work "Many" by "owner", "someone_else" and "off"
And I am logged in as "owner"
When I delete the work "Many"
Then I should see "Your work Many was deleted."
And 3 emails should be delivered
And the email to "owner" should contain "was deleted at your request"
And the email to "owner" should be translated
And the email to "someone_else" should contain "was deleted at the request of"
And the email to "someone_else" should be translated
And the email to "off" should contain "was deleted at the request of"
And the email to "off" should be non-translated
2 changes: 1 addition & 1 deletion spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@
end

describe "delete_work_notification" do
subject(:email) { UserMailer.delete_work_notification(user, work) }
subject(:email) { UserMailer.delete_work_notification(user, work, user) }

let(:user) { create(:user) }
let(:work) { create(:work) }
Expand Down
13 changes: 13 additions & 0 deletions test/mailers/previews/user_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ def admin_hidden_work_notification
UserMailer.admin_hidden_work_notification(work, user.id)
end

def delete_work_notification_self
user = create(:user, :for_mailer_preview)
work = create(:work, authors: [user.default_pseud])
UserMailer.delete_work_notification(user, work, user)
end

def delete_work_notification_co_creator
first_creator = create(:user, :for_mailer_preview)
second_creator = create(:user, :for_mailer_preview)
work = create(:work, authors: [first_creator.default_pseud, second_creator.default_pseud])
UserMailer.delete_work_notification(first_creator, work, second_creator)
end

private

def creatorship_notification_data(creation_type)
Expand Down
Loading