diff --git a/app/controllers/admin/user_creations_controller.rb b/app/controllers/admin/user_creations_controller.rb index 6e5d0300a33..9bffc42a017 100644 --- a/app/controllers/admin/user_creations_controller.rb +++ b/app/controllers/admin/user_creations_controller.rb @@ -37,8 +37,11 @@ def set_spam action = "mark as " + (params[:spam] == "true" ? "spam" : "not spam") AdminActivity.log_action(current_admin, @creation, action: action, summary: @creation.inspect) if params[:spam] == "true" + unless @creation.hidden_by_admin + @creation.notify_of_hiding_for_spam if @creation_class == Work + @creation.hidden_by_admin = true + end @creation.mark_as_spam! - @creation.update_attribute(:hidden_by_admin, true) flash[:notice] = ts("Work was marked as spam and hidden.") else @creation.mark_as_ham! diff --git a/app/models/moderated_work.rb b/app/models/moderated_work.rb index e6124be2471..75760fd6365 100644 --- a/app/models/moderated_work.rb +++ b/app/models/moderated_work.rb @@ -41,6 +41,7 @@ def self.bulk_review(ids) where(id: ids).update_all(reviewed: true, approved: false) # Ensure works are hidden and spam if they weren't already Work.joins(:moderated_work).where("moderated_works.id IN (?)", ids).each do |work| + work.notify_of_hiding_for_spam unless work.hidden_by_admin? work.update(hidden_by_admin: true, spam: true) end end diff --git a/app/models/work.rb b/app/models/work.rb index 0881823f956..aaf904c38af 100755 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -75,6 +75,9 @@ def create_stat_counter attr_accessor :new_gifts attr_accessor :preview_mode + # Virtual attribute for whether the hidden-for-spam email has been sent, so the normal work-hidden email should not be sent + attr_accessor :notified_of_hiding_for_spam + # return title.html_safe to overcome escaping done by sanitiser def title read_attribute(:title).try(:html_safe) @@ -1121,7 +1124,10 @@ def hide_spam return unless spam? admin_settings = AdminSetting.current if admin_settings.hide_spam? + return if self.hidden_by_admin + self.hidden_by_admin = true + notify_of_hiding_for_spam end end @@ -1145,13 +1151,18 @@ def mark_as_ham! def notify_of_hiding return unless hidden_by_admin? && saved_change_to_hidden_by_admin? + return if notified_of_hiding_for_spam + users.each do |user| - if spam? - UserMailer.admin_spam_work_notification(id, user.id).deliver_after_commit - else - UserMailer.admin_hidden_work_notification(id, user.id).deliver_after_commit - end + UserMailer.admin_hidden_work_notification(id, user.id).deliver_after_commit + end + end + + def notify_of_hiding_for_spam + users.each do |user| + UserMailer.admin_spam_work_notification(id, user.id).deliver_after_commit end + self.notified_of_hiding_for_spam = true end ############################################################################# diff --git a/features/admins/admin_spam.feature b/features/admins/admin_spam.feature index ded7525a80c..fb7ef69484a 100644 --- a/features/admins/admin_spam.feature +++ b/features/admins/admin_spam.feature @@ -4,10 +4,15 @@ Feature: Admin spam management As an an admin I want to be able to view and update works marked as spam -Scenario: Review spam - Given the spam work "Spammity Spam" +Scenario: Review spam when spam works are already hidden + Given the following admin settings are configured: + | hide_spam | 1 | + And the spam work "Spammity Spam" And the spam work "Totally Legit" + And the work "Spammity Spam" should be hidden + And the work "Totally Legit" should be hidden And I am logged in as a "superadmin" admin + And all emails have been delivered Then I should see "Spam" When I follow "Spam" Then I should see "Works Marked as Spam" @@ -20,3 +25,29 @@ Scenario: Review spam And I should not see "Totally Legit" And the work "Spammity Spam" should be hidden And the work "Totally Legit" should not be hidden + And 0 emails should be delivered + + +Scenario: Review spam when spam works are not already hidden + Given the following admin settings are configured: + | hide_spam | 0 | + And the spam work "Spammity Spam" + And the spam work "Totally Legit" + And the work "Spammity Spam" should not be hidden + And the work "Totally Legit" should not be hidden + And I am logged in as a "superadmin" admin + And all emails have been delivered + Then I should see "Spam" + When I follow "Spam" + Then I should see "Works Marked as Spam" + And I should see "Spammity" + And I should see "Totally Legit" + When I check "spam_3" + And I check "ham_4" + And I press "Update Works" + Then I should not see "Spammity" + And I should not see "Totally Legit" + And the work "Spammity Spam" should be hidden + And the work "Totally Legit" should not be hidden + And 1 email should be delivered + And the email should contain "has been flagged by our automated system as spam" diff --git a/features/admins/admin_works.feature b/features/admins/admin_works.feature index 39807cbd650..4478dc0d9fc 100644 --- a/features/admins/admin_works.feature +++ b/features/admins/admin_works.feature @@ -32,6 +32,27 @@ Feature: Admin Actions for Works, Comments, Series, Bookmarks | superadmin | | legal | | policy_and_abuse | + + Scenario Outline: Can hide works already marked as spam + Given I am logged in as "regular_user" + And I post the work "ToS Violation + Spam" + And the work "ToS Violation + Spam" is marked as spam + When I am logged in as a "" admin + And all emails have been delivered + And I view the work "ToS Violation + Spam" + And I follow "Hide Work" + Then I should see "Item has been hidden." + And logged out users should not see the hidden work "ToS Violation + Spam" by "regular_user" + And logged in users should not see the hidden work "ToS Violation + Spam" by "regular_user" + And "regular_user" should see their work "ToS Violation + Spam" is hidden + And 1 email should be delivered + And the email should contain "you will be required to take action to correct the violation" + + Examples: + | role | + | superadmin | + | legal | + | policy_and_abuse | Scenario Outline: Can unhide works Given I am logged in as "regular_user" @@ -361,12 +382,16 @@ Feature: Admin Actions for Works, Comments, Series, Bookmarks Given the work "Spammity Spam" And I am logged in as a "policy_and_abuse" admin And I view the work "Spammity Spam" + And all emails have been delivered Then I should see "Mark As Spam" When I follow "Mark As Spam" Then I should see "marked as spam and hidden" And I should see "Mark Not Spam" And the work "Spammity Spam" should be marked as spam And the work "Spammity Spam" should be hidden + And 1 email should be delivered + And the email should contain "has been flagged by our automated system as spam" + Scenario: can mark a spam work as not-spam Given the spam work "Spammity Spam" diff --git a/features/other_a/hit_counts.feature b/features/other_a/hit_counts.feature index 187f94380d4..be99a5e2f7b 100644 --- a/features/other_a/hit_counts.feature +++ b/features/other_a/hit_counts.feature @@ -41,7 +41,7 @@ Feature: Hit Counts Then I should see "Hits: 0" Scenario: Viewing a work hidden by an admin doesn't increment the hit count - Given the spam work "Hit Count Test" + Given the hidden work "Hit Count Test" And I am logged in as an admin And all hit count information is reset When I go to the work "Hit Count Test" diff --git a/features/step_definitions/work_steps.rb b/features/step_definitions/work_steps.rb index 0086cbfe51f..ca77e376fdf 100644 --- a/features/step_definitions/work_steps.rb +++ b/features/step_definitions/work_steps.rb @@ -256,14 +256,25 @@ step %{I am logged in as "#{work.users.first.login}"} end -Given /^the spam work "([^\"]*)"$/ do |work| +Given "the spam work {string}" do |work| step %{I have a work "#{work}"} step %{I log out} - w = Work.find_by_title(work) + w = Work.find_by(title: work) w.update_attribute(:spam, true) +end + +Given "the hidden work {string}" do |work| + step %{I have a work "#{work}"} + step %{I log out} + w = Work.find_by(title: work) w.update_attribute(:hidden_by_admin, true) end +Given "the work {string} is marked as spam" do |work| + w = Work.find_by(title: work) + w.update_attribute(:spam, true) +end + Given "the user-defined tag limit is {int}" do |count| allow(ArchiveConfig).to receive(:USER_DEFINED_TAGS_MAX).and_return(count) end