[Posts] Allow locking comment sections

This commit is contained in:
Earlopain 2022-05-16 14:20:11 +02:00
parent dd194122a0
commit 6d3f272e90
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
12 changed files with 70 additions and 12 deletions

View File

@ -173,7 +173,7 @@ class PostsController < ApplicationController
]
permitted_params += %i[is_rating_locked] if CurrentUser.is_privileged?
permitted_params += %i[is_note_locked bg_color] if CurrentUser.is_janitor?
permitted_params += %i[is_status_locked locked_tags hide_from_anonymous hide_from_search_engines] if CurrentUser.is_admin?
permitted_params += %i[is_status_locked is_comment_locked locked_tags hide_from_anonymous hide_from_search_engines] if CurrentUser.is_admin?
params.require(:post).permit(permitted_params)
end

View File

@ -5,6 +5,7 @@ class Comment < ApplicationRecord
belongs_to_updater
validate :validate_post_exists, on: :create
validate :validate_creator_is_not_limited, on: :create
validate :post_not_comment_locked, on: :create
validates :body, presence: { message: "has no content" }
validates :body, length: { minimum: 1, maximum: Danbooru.config.comment_max_size }
@ -136,6 +137,10 @@ class Comment < ApplicationRecord
true
end
def post_not_comment_locked
errors.add(:base, "Post is comment locked") if Post.find_by(id: post_id)&.is_comment_locked && !CurrentUser.is_admin?
end
def update_last_commented_at_on_create
post = Post.find(post_id)
return unless post

View File

@ -1651,6 +1651,10 @@ class Post < ApplicationRecord
action = is_note_locked? ? :note_locked : :note_unlocked
PostEvent.add(id, CurrentUser.user, action)
end
if saved_change_to_is_comment_locked?
action = is_comment_locked? ? :comment_locked : :comment_unlocked
PostEvent.add(id, CurrentUser.user, action)
end
end
end

View File

@ -15,6 +15,8 @@ class PostEvent < ApplicationRecord
status_unlocked: 11,
note_locked: 12,
note_unlocked: 13,
comment_locked: 18,
comment_unlocked: 19,
replacement_accepted: 14,
replacement_rejected: 15,
replacement_deleted: 16,

View File

@ -99,6 +99,7 @@ class PostSerializer < ActiveModel::Serializer
note_locked: nullable_to_truthy(object.is_note_locked),
status_locked: nullable_to_truthy(object.is_status_locked),
rating_locked: nullable_to_truthy(object.is_rating_locked),
comment_locked: nullable_to_truthy(object.is_comment_locked),
deleted: object.is_deleted
}
end

View File

@ -21,11 +21,16 @@
<% if CurrentUser.is_member? %>
<div class="new-comment">
<% if !CurrentUser.is_anonymous? && !CurrentUser.user.is_janitor? %>
<h2>Before commenting, read the <%= link_to "how to comment guide", wiki_pages_path(:search => {:title => "howto:comment"}) %>.</h2>
<% if post.is_comment_locked %>
Comments are locked.
<% end %>
<% if !post.is_comment_locked || CurrentUser.is_admin? %>
<% if !CurrentUser.is_anonymous? && !CurrentUser.user.is_janitor? %>
<h2>Before commenting, read the <%= link_to "how to comment guide", wiki_pages_path(:search => {:title => "howto:comment"}) %>.</h2>
<% end %>
<p><%= link_to "Post comment", new_comment_path(comment: { post_id: post.id }), :class => "expand-comment-response" %></p>
<%= render "comments/form", comment: post.comments.new, hidden: true %>
<% end %>
<p><%= link_to "Post comment", new_comment_path(comment: { post_id: post.id }), :class => "expand-comment-response" %></p>
<%= render "comments/form", comment: post.comments.new, hidden: true %>
</div>
<% else %>
<h5 id="respond-link"><%= link_to "Login to respond &raquo;".html_safe, new_session_path %></h5>

View File

@ -31,7 +31,7 @@
<% end %>
<div class="content-menu">
<menu>
<% if post %>
<% if !post&.is_comment_locked %>
<li><%= tag.a "Reply", href: '#', class: "reply-link comment-reply-link" %></li>
<% end %>
<% if comment.editable_by?(CurrentUser.user) %>

View File

@ -87,6 +87,8 @@
<% if CurrentUser.is_admin? %>
<%= f.check_box :is_status_locked %>
<%= f.label :is_status_locked, "Status" %>
<%= f.check_box :is_comment_locked %>
<%= f.label :is_comment_locked, "New Comments"%>
<% end %>
</fieldset>
</div>
@ -104,7 +106,7 @@
<%= f.label :blank, "Limits" %>
<fieldset class="limits">
<%= f.check_box :hide_from_anonymous %>
<%= f.label :hide_from_anonymous, "Hide from Anon." %>
<%= f.label :hide_from_anonymous, "Hide from Anon" %>
<%= f.check_box :hide_from_search_engines %>
<%= f.label :hide_from_search_engines, "Hide from search engines" %>
</fieldset>

View File

@ -0,0 +1,7 @@
class PostCommentLocked < ActiveRecord::Migration[6.1]
def change
Post.without_timeout do
add_column :posts, :is_comment_locked, :boolean, null: false, default: false
end
end
end

View File

@ -1699,7 +1699,8 @@ CREATE TABLE public.posts (
tag_count_lore integer DEFAULT 0 NOT NULL,
bg_color character varying,
generated_samples character varying[],
duration numeric
duration numeric,
is_comment_locked boolean DEFAULT false NOT NULL
);
@ -4745,5 +4746,6 @@ INSERT INTO "schema_migrations" (version) VALUES
('20220106081415'),
('20220203154846'),
('20220219202441'),
('20220316162257');
('20220316162257'),
('20220516103329');

View File

@ -99,7 +99,7 @@ class CommentTest < ActiveSupport::TestCase
user2 = FactoryBot.create(:user)
post = FactoryBot.create(:post)
c1 = FactoryBot.create(:comment, post: post)
CurrentUser.scoped(user2, "127.0.0.1") do
VoteManager.comment_vote!(user: user2, comment: c1, score: -1)
c1.reload
@ -113,7 +113,7 @@ class CommentTest < ActiveSupport::TestCase
post = FactoryBot.create(:post)
c1 = FactoryBot.create(:comment, post: post)
c2 = FactoryBot.create(:comment, post: post)
CurrentUser.scoped(user2, "127.0.0.1") do
assert_nothing_raised { VoteManager.comment_vote!(user: user2, comment: c1, score: -1) }
assert_equal(:need_unvote, VoteManager.comment_vote!(user: user2, comment: c1, score: -1))
@ -133,7 +133,7 @@ class CommentTest < ActiveSupport::TestCase
exception = assert_raises(ActiveRecord::RecordInvalid) { VoteManager.comment_vote!(user: user, comment: c1, score: 1) }
assert_equal("Validation failed: You cannot vote on your own comments", exception.message)
end
should "not allow downvotes by the creator" do
user = FactoryBot.create(:user)
post = FactoryBot.create(:post)
@ -243,6 +243,26 @@ class CommentTest < ActiveSupport::TestCase
EOS
end
end
context "on a comment locked post" do
setup do
@post = create(:post, is_comment_locked: true)
end
should "prevent new comments" do
comment = FactoryBot.build(:comment, post: @post)
comment.save
assert_equal(["Post is comment locked"], comment.errors.full_messages)
end
should "still allow comments from admins" do
as create(:admin_user) do
@comment = FactoryBot.build(:comment, post: @post)
@comment.save
end
assert @comment.errors.empty?
end
end
end
context "during validation" do

View File

@ -70,6 +70,16 @@ class PostEventTest < ActiveSupport::TestCase
@post.save
end
assert_post_events_created(@admin, :comment_locked) do
@post.is_comment_locked = true
@post.save
end
assert_post_events_created(@admin, :comment_unlocked) do
@post.is_comment_locked = false
@post.save
end
assert_post_events_created(@admin, :note_locked) do
@post.is_note_locked = true
@post.save