Skip to content

Commit

Permalink
AO3-6503 Allow superadmins to disable guest comments across the site (#…
Browse files Browse the repository at this point in the history
…4492)

* AO3-6503 Add guest comment off option

* AO3-6503 Fix tests

* AO3-6503 Pleasing the hound

* AO3-6503 Fix tests

* AO3-6503 Fix tests 2

* AO3-6503 Fix tests 3

* AO3-6503 Fix typo

* AO3-6503 Hide reply button

* AO3-6503 Pleasing the hound

* AO3-6503 Expand tests

* AO3-6503 Pleasing the hound

* AO3-6503 Change cache key & refactor tests

* AO3-6503 Refactor tests

* AO3-6503 Expand tests

* AO3-6503 Fix test

* AO3-6503 Normalise en.yml

* AO3-6503 Fix tests
  • Loading branch information
EchoEkhi authored Jun 19, 2023
1 parent 3a6c84e commit 754e816
Show file tree
Hide file tree
Showing 15 changed files with 391 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def transform_sanitized_hash_to_ac_params(key, value)
helper_method :current_admin
helper_method :logged_in?
helper_method :logged_in_as_admin?
helper_method :guest?

# Title helpers
helper_method :process_title
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CommentsController < ApplicationController
before_action :check_ownership, only: [:edit, :update, :cancel_comment_edit]
before_action :check_permission_to_edit, only: [:edit, :update ]
before_action :check_permission_to_delete, only: [:delete_comment, :destroy]
before_action :check_guest_comment_admin_setting, only: [:new, :create, :add_comment_reply]
before_action :check_parent_comment_permissions, only: [:new, :create, :add_comment_reply]
before_action :check_unreviewed, only: [:add_comment_reply]
before_action :check_frozen, only: [:new, :create, :add_comment_reply]
Expand Down Expand Up @@ -130,6 +131,15 @@ def check_parent_comment_permissions
end
end

def check_guest_comment_admin_setting
admin_settings = AdminSetting.current

return unless admin_settings.guest_comments_off? && guest?

flash[:error] = t("comments.commentable.guest_comments_disabled")
redirect_back(fallback_location: root_path)
end

def check_unreviewed
return unless @commentable.respond_to?(:unreviewed?) && @commentable.unreviewed?

Expand Down
5 changes: 4 additions & 1 deletion app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ def show_hide_comments_link(commentable, options={})
#### HELPERS FOR CHECKING WHICH BUTTONS/FORMS TO DISPLAY #####

def can_reply_to_comment?(comment)
admin_settings = AdminSetting.current

!(comment.unreviewed? ||
comment.iced? ||
comment.hidden_by_admin? ||
parent_disallows_comments?(comment) ||
comment_parent_hidden?(comment) ||
blocked_by_comment?(comment) ||
blocked_by?(comment.ultimate_parent))
blocked_by?(comment.ultimate_parent) ||
guest? && admin_settings.guest_comments_off?)
end

def can_edit_comment?(comment)
Expand Down
4 changes: 2 additions & 2 deletions app/models/admin_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.default
end

def self.current
Rails.cache.fetch("admin_settings", race_condition_ttl: 10.seconds) { AdminSetting.first } || OpenStruct.new(DEFAULT_SETTINGS)
Rails.cache.fetch("admin_settings-v1", race_condition_ttl: 10.seconds) { AdminSetting.first } || OpenStruct.new(DEFAULT_SETTINGS)
end

class << self
Expand Down Expand Up @@ -79,7 +79,7 @@ def recache_settings
self.reload

# However, we only cache it if the transaction is successful.
after_commit { Rails.cache.write("admin_settings", self) }
after_commit { Rails.cache.write("admin_settings-v1", self) }
end

private
Expand Down
1 change: 1 addition & 0 deletions app/policies/admin_setting_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AdminSettingPolicy < ApplicationPolicy
downloads_enabled
enable_test_caching
hide_spam
guest_comments_off
invite_from_queue_enabled
invite_from_queue_frequency
invite_from_queue_number
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/settings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@

<dt><%= admin_setting_checkbox(f, :hide_spam) %></dt>
<dd><%= f.label :hide_spam, t(".fields.hide_spam") %></dd>

<dt><%= admin_setting_checkbox(f, :guest_comments_off) %></dt>
<dd><%= f.label :guest_comments_off, t(".fields.guest_comments_off") %></dd>
</dl>
</fieldset>

Expand Down
6 changes: 5 additions & 1 deletion app/views/comments/_commentable.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@
<%= flash_div :comment_error, :comment_notice %>

<% commentable_parent = find_parent(commentable) %>
<% if commentable_parent.is_a?(AdminPost) && commentable_parent.disable_all_comments? %>
<% if @admin_settings.guest_comments_off? && guest? %>
<p class="notice">
<%= t(".guest_comments_disabled") %>
</p>
<% elsif commentable_parent.is_a?(AdminPost) && commentable_parent.disable_all_comments? %>
<p class="notice">
<%= t(".permissions.admin_post.disable_all") %>
</p>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/views/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ en:
disabled_support_form_text: Disabled support form text
downloads_enabled: Allow downloads
enable_test_caching: Turn on caching (currently experimental)
guest_comments_off: Turn off guest comments across the site
hide_spam: Automatically hide spam works
invite_from_queue_enabled: Invite from queue enabled (People can add themselves to the queue and invitations are sent out automatically)
invite_from_queue_frequency: How often (in days) should we invite people from the queue
Expand Down Expand Up @@ -341,6 +342,7 @@ en:
actions:
comment: Comment
blocked: Sorry, you have been blocked by one or more of this work's creators.
guest_comments_disabled: Sorry, the Archive doesn't allow guests to comment right now.
invite_to_collections_link: Invite To Collections
permissions:
admin_post:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddGuestCommentOffToAdminSettings < ActiveRecord::Migration[6.1]
def change
add_column :admin_settings, :guest_comments_off, :boolean, default: false, null: false
end
end
112 changes: 112 additions & 0 deletions features/admins/admin_settings.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,115 @@ Feature: Admin Settings Page
When I am logged in as a random user
And I go to the support page
Then I should see "We can answer Support inquiries in"

Scenario Outline: Guests can comment when guest coments are enabled
Given guest comments are on
And I am logged out
And <commentable>
And I view <commentable> with comments
When I post a guest comment
Then I should see a link "Reply"

Examples:
| commentable |
| the work "Generic Work" |
| the admin post "Generic Post" |

Scenario Outline: Guests cannot comment when guest comments are disabled, even if works or admin posts allow commets
Given guest comments are off
And I am logged out
And <commentable>
And a guest comment on <commentable>
When I view <commentable> with comments
Then I should see "Sorry, the Archive doesn't allow guests to comment right now."
And I should not see a link "Reply"
When I am logged in
And I view <commentable> with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."
When I am logged in as a super admin
And I view <commentable> with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."

Examples:
| commentable |
| the work "Generic Work" |
| the admin post "Generic Post" |

Scenario: Turn off guest comments (when the work itself does not allow guest comments)
Given guest comments are off
And I am logged in as "author"
And I set up the draft "Generic Work"
And I choose "Only registered users can comment"
And I post the work without preview
And a comment "Nice job" by "user" on the work "Generic Work"
When I am logged out
And I view the work "Generic Work" with comments
Then I should see "Sorry, the Archive doesn't allow guests to comment right now."
And I should not see a link "Reply"
When I am logged in
And I view the work "Generic Work" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."
When I am logged in as a super admin
And I view the work "Generic Work" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."

Scenario: Turn off guest comments (when the admin post itself does not allow guest comments)
Given guest comments are off
And I have posted an admin post with guest comments disabled
And a comment "Nice job" by "user" on the admin post "Default Admin Post"
When I view the admin post "Default Admin Post" with comments
Then I should see "Sorry, the Archive doesn't allow guests to comment right now."
And I should not see a link "Reply"
When I am logged in
And I view the admin post "Default Admin Post" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."
When I am logged in as a super admin
And I view the admin post "Default Admin Post" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."

Scenario: Turn off guest comments (when work itself does not allow any comments)
Given guest comments are off
And I am logged in as "author"
And I post the work "Generic Work"
And a guest comment on the work "Generic Work"
And I edit the work "Generic Work"
And I choose "No one can comment"
And I press "Post"
When I am logged out
And I view the work "Generic Work" with comments
Then I should see "Sorry, the Archive doesn't allow guests to comment right now."
And I should not see a link "Reply"
When I am logged in
And I view the work "Generic Work" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."
When I am logged in as a super admin
And I view the work "Generic Work" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."

Scenario: Turn off guest comments (when the admin post itself does not allow any comments)
Given guest comments are off
And I have posted an admin post with comments disabled
And a comment "Nice job" by "user" on the admin post "Default Admin Post"
When I view the admin post "Default Admin Post" with comments
Then I should see "Sorry, the Archive doesn't allow guests to comment right now."
And I should not see a link "Reply"
When I am logged in
And I view the admin post "Default Admin Post" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."
When I am logged in as a super admin
And I view the admin post "Default Admin Post" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."

Scenario: Tag comments are not affected when guest comments are turned off
Given guest comments are off
And a fandom exists with name: "Stargate SG-1", canonical: true
When I am logged in as a super admin
And I view the tag "Stargate SG-1" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."
When I post the comment "Important policy decision" on the tag "Stargate SG-1"
Then I should see "Comment created!"
When I am logged in as a tag wrangler
And I view the tag "Stargate SG-1" with comments
Then I should not see "Sorry, the Archive doesn't allow guests to comment right now."
When I post the comment "Sent you a syn" on the tag "Stargate SG-1"
Then I should see "Comment created!"
22 changes: 22 additions & 0 deletions features/step_definitions/admin_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@
click_button("Update")
end

Given "guest comments are on" do
step("I am logged in as a super admin")
visit(admin_settings_path)
uncheck("Turn off guest comments across the site")
click_button("Update")
end

Given "guest comments are off" do
step("I am logged in as a super admin")
visit(admin_settings_path)
check("Turn off guest comments across the site")
click_button("Update")
end

Given /^I have posted known issues$/ do
step %{I am logged in as an admin}
step %{I follow "Admin Posts"}
Expand Down Expand Up @@ -159,6 +173,14 @@
end
end

Given /^I have posted an admin post with guest comments disabled$/ do
step %{I am logged in as a "communications" admin}
step %{I start to make an admin post}
choose("Only registered users can comment")
click_button("Post")
step %{I log out}
end

Given /^I have posted an admin post with comments disabled$/ do
step %{I am logged in as a "communications" admin}
step %{I start to make an admin post}
Expand Down
10 changes: 5 additions & 5 deletions features/step_definitions/comment_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
user.preference.save
end

Given "a guest comment on the work {string}" do |title|
work = Work.find_by(title: title)
FactoryBot.create(:comment, :by_guest, commentable: work.first_chapter)
end

ParameterType(
name: "commentable",
regexp: /the (work|admin post|tag) "([^"]*)"/,
Expand All @@ -35,6 +30,11 @@
}
)

Given "a guest comment on {commentable}" do |commentable|
commentable = Comment.commentable_object(commentable)
FactoryBot.create(:comment, :by_guest, commentable: commentable)
end

Given "a comment {string} by {string} on {commentable}" do |text, user, commentable|
user = ensure_user(user)
commentable = Comment.commentable_object(commentable)
Expand Down
7 changes: 5 additions & 2 deletions spec/controllers/admin/settings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
downloads_enabled: "1",
enable_test_caching: "0",
cache_expiration: "10",
hide_spam: "1"
hide_spam: "1",
guest_comments_off: "1"
}
}

Expand Down Expand Up @@ -107,6 +108,7 @@
{
downloads_enabled: false,
hide_spam: true,
guest_comments_off: true,
tag_wrangling_off: true
}.each_pair do |field, value|
it "prevents admins with support role from updating #{field}" do
Expand Down Expand Up @@ -137,7 +139,8 @@
{
disable_support_form: true,
downloads_enabled: false,
hide_spam: true
hide_spam: true,
guest_comments_off: true
}.each_pair do |field, value|
it "prevents admins with tag_wrangling role from updating #{field}" do
expect do
Expand Down
Loading

0 comments on commit 754e816

Please sign in to comment.