Skip to content

Commit d193bc7

Browse files
Bilka2weeklies
andauthored
AO3-6775 Create delete_work_notification mailer preview (#5057)
* AO3-6775 Add delete work previews * AO3-6775 Don't change User.current_user for preview * AO3-6775 Rubocop * AO3-6775 Fix tests * AO3-6775 Typo * AO3-6775 Fix it properly * AO3-6775 It was too obvious --------- Co-authored-by: weeklies <[email protected]>
1 parent 2e70edd commit d193bc7

File tree

8 files changed

+67
-36
lines changed

8 files changed

+67
-36
lines changed

app/mailers/user_mailer.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,20 @@ def prompter_notification(work_id, collection_id = nil)
317317
# Sends email to creators when a creation is deleted
318318
# NOTE: this must be sent synchronously! otherwise the work will no longer be there to send
319319
# TODO refactor to make it asynchronous by passing the content in the method
320-
def delete_work_notification(user, work)
320+
def delete_work_notification(user, work, deleter)
321321
@user = user
322322
@work = work
323+
@deleter = deleter
323324
download = Download.new(@work, mime_type: "text/html", include_draft_chapters: true)
324325
html = DownloadWriter.new(download).generate_html
325326
html = ::Mail::Encodings::Base64.encode(html)
326327
attachments["#{download.file_name}.html"] = { content: html, encoding: "base64" }
327328
attachments["#{download.file_name}.txt"] = { content: html, encoding: "base64" }
328329

329-
I18n.with_locale(@user.preference.locale.iso) do
330-
mail(
331-
to: user.email,
332-
subject: t("user_mailer.delete_work_notification.subject", app_name: ArchiveConfig.APP_SHORT_NAME)
333-
)
334-
end
330+
mail(
331+
to: user.email,
332+
subject: default_i18n_subject(app_name: ArchiveConfig.APP_SHORT_NAME)
333+
)
335334
end
336335

337336
# Sends email to creators when a creation is deleted by an admin

app/models/work.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,26 +250,23 @@ def new_recipients_have_not_blocked_gift_giver
250250

251251
before_destroy :send_deleted_work_notification, prepend: true
252252
def send_deleted_work_notification
253-
return unless self.posted?
254-
255-
users = self.pseuds.collect(&:user).uniq
256-
257-
return if users.blank?
253+
return unless self.posted? && users.present?
258254

259255
orphan_account = User.orphan_account
260-
261256
users.each do |user|
262257
next if user == orphan_account
263-
258+
264259
# Check to see if this work is being deleted by an Admin
265260
if User.current_user.is_a?(Admin)
266-
# this has to use the synchronous version because the work is going to be destroyed
267261
I18n.with_locale(user.preference.locale.iso) do
262+
# this has to use the synchronous version because the work is going to be destroyed
268263
UserMailer.admin_deleted_work_notification(user, self).deliver_now
269264
end
270265
else
271-
# this has to use the synchronous version because the work is going to be destroyed
272-
UserMailer.delete_work_notification(user, self).deliver_now
266+
I18n.with_locale(user.preference.locale.iso) do
267+
# this has to use the synchronous version because the work is going to be destroyed
268+
UserMailer.delete_work_notification(user, self, User.current_user).deliver_now
269+
end
273270
end
274271
end
275272
end
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<% content_for :message do %>
2-
<p><%= t("mailer.general.greeting.formal_html", name: style_bold(@user.login)) %></p>
3-
<p><%= (@work.pseuds.count > 1 && @user != User.current_user) ?
4-
t('.deleted_other.html',
5-
title: style_creation_title(@work.title),
6-
pseud: style_pseud_link(User.current_user.default_pseud)) :
7-
t('.deleted_yourself.html',
8-
title: style_creation_title(@work.title)) %></p>
9-
<p><%= t('.questions.html', support: support_link(t '.support')) %></p>
10-
<p><%= t('.attachment') %></p>
2+
<p><%= t("mailer.general.greeting.formal_html", name: style_bold(@user.login)) %></p>
3+
<% if @user == @deleter || !@deleter %>
4+
<p><%= t(".deleted_yourself.html", title: style_creation_title(@work.title)) %></p>
5+
<% else %>
6+
<p><%= t(".deleted_other.html", title: style_creation_title(@work.title), pseud: style_pseud_link(@deleter.default_pseud)) %></p>
7+
<% end %>
8+
<p><%= t(".questions.html", support: support_link(t(".support"))) %></p>
9+
<p><%= t(".attachment") %></p>
1110
<% end %>
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<% content_for :message do %>
22
<%= t("mailer.general.greeting.formal_html", name: @user.login) %>
33

4-
<%= (@work.pseuds.count > 1 && @user != User.current_user) ?
5-
t('.deleted_other.text',
6-
title: @work.title,
7-
pseud: text_pseud(User.current_user.default_pseud)) :
8-
t('.deleted_yourself.text',
9-
title: @work.title) %>
4+
<% if @user == @deleter || !@deleter %>
5+
<%= t(".deleted_yourself.text", title: @work.title) %>
6+
<% else %>
7+
<%= t(".deleted_other.text", title: @work.title, pseud: text_pseud(@deleter.default_pseud)) %>
8+
<% end %>
109

11-
<%= t('.questions.text', support: t('.support'), url: new_feedback_report_url) %>
10+
<%= t(".questions.text", support: t(".support"), url: new_feedback_report_url) %>
1211

13-
<%= t('.attachment') %>
12+
<%= t(".attachment") %>
1413
<% end %>
15-

features/step_definitions/work_steps.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@
225225
FactoryBot.create(:work, title: title, authors: [user1.default_pseud, user2.default_pseud])
226226
end
227227

228+
Given "the work {string} by {string}, {string} and {string}" do |title, login1, login2, login3|
229+
user1 = ensure_user(login1)
230+
user2 = ensure_user(login2)
231+
user3 = ensure_user(login3)
232+
FactoryBot.create(:work, title: title, authors: [user1.default_pseud, user2.default_pseud, user3.default_pseud])
233+
end
234+
228235
Given "the work {string} by {string} and {string} with guest comments enabled" do |title, login1, login2|
229236
user1 = ensure_user(login1)
230237
user2 = ensure_user(login2)

features/works/work_delete.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,21 @@ Feature: Delete Works
194194
When I am logged in as the author of "Over the Limit"
195195
And I delete the work "Over the Limit"
196196
Then I should see "Your work Over the Limit was deleted."
197+
198+
Scenario: Deleting a work sends translated deletion notification emails
199+
Given a locale with translated emails
200+
And the user "owner" exists and is activated
201+
And the user "owner" enables translated emails
202+
And the user "someone_else" exists and is activated
203+
And the user "someone_else" enables translated emails
204+
And the work "Many" by "owner", "someone_else" and "off"
205+
And I am logged in as "owner"
206+
When I delete the work "Many"
207+
Then I should see "Your work Many was deleted."
208+
And 3 emails should be delivered
209+
And the email to "owner" should contain "was deleted at your request"
210+
And the email to "owner" should be translated
211+
And the email to "someone_else" should contain "was deleted at the request of"
212+
And the email to "someone_else" should be translated
213+
And the email to "off" should contain "was deleted at the request of"
214+
And the email to "off" should be non-translated

spec/mailers/user_mailer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@
11011101
end
11021102

11031103
describe "delete_work_notification" do
1104-
subject(:email) { UserMailer.delete_work_notification(user, work) }
1104+
subject(:email) { UserMailer.delete_work_notification(user, work, user) }
11051105

11061106
let(:user) { create(:user) }
11071107
let(:work) { create(:work) }

test/mailers/previews/user_mailer_preview.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ def admin_deleted_work_notification
171171
UserMailer.admin_deleted_work_notification(user, work)
172172
end
173173

174+
def delete_work_notification_self
175+
user = create(:user, :for_mailer_preview)
176+
work = create(:work, authors: [user.default_pseud])
177+
UserMailer.delete_work_notification(user, work, user)
178+
end
179+
180+
def delete_work_notification_co_creator
181+
first_creator = create(:user, :for_mailer_preview)
182+
second_creator = create(:user, :for_mailer_preview)
183+
work = create(:work, authors: [first_creator.default_pseud, second_creator.default_pseud])
184+
UserMailer.delete_work_notification(first_creator, work, second_creator)
185+
end
186+
174187
private
175188

176189
def creatorship_notification_data(creation_type)

0 commit comments

Comments
 (0)