Initial comment hide vs delete distinction

This commit is contained in:
Kira 2019-09-05 05:35:27 -07:00
parent 5c2fcbfae4
commit 6127ea6d25
13 changed files with 77 additions and 53 deletions

View File

@ -1,7 +1,8 @@
class CommentsController < ApplicationController
respond_to :html, :xml, :json
respond_to :js, only: [:new, :destroy, :undelete]
respond_to :js, only: [:new, :destroy, :unhide, :hide]
before_action :member_only, :except => [:index, :search, :show]
before_action :moderator_only, only: [:unhide, :destroy]
skip_before_action :api_check
def index
@ -54,15 +55,21 @@ class CommentsController < ApplicationController
def destroy
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.delete!
@comment.destroy
respond_with(@comment)
end
def undelete
def hide
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.undelete!
@comment.hide!
respond_with(@comment)
end
def unhide
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.unhide!
respond_with(@comment)
end
@ -108,8 +115,7 @@ private
def comment_params(context)
permitted_params = %i[body post_id]
permitted_params += %i[do_not_bump_post] if context == :create
permitted_params += %i[is_deleted] if context == :update
permitted_params += %i[is_sticky] if CurrentUser.is_moderator?
permitted_params += %i[is_sticky is_hidden] if CurrentUser.is_moderator?
params.fetch(:comment, {}).permit(permitted_params)
end

View File

@ -11,12 +11,13 @@ class Comment < ApplicationRecord
validates :body, length: { minimum: 1, maximum: 10_000 }
after_create :update_last_commented_at_on_create
after_update(:if => ->(rec) {(!rec.is_deleted? || !rec.saved_change_to_is_deleted?) && CurrentUser.id != rec.creator_id}) do |rec|
after_update(:if => ->(rec) {(!rec.is_hidden? || !rec.saved_change_to_is_hidden?) && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(:comment_update, {comment_id: rec.id, user_id: rec.creator_id})
end
after_save :update_last_commented_at_on_destroy, :if => ->(rec) {rec.is_deleted? && rec.saved_change_to_is_deleted?}
after_save(:if => ->(rec) {rec.is_deleted? && rec.saved_change_to_is_deleted? && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(:comment_delete, {comment_id: rec.id, user_id: rec.creator_id})
after_save :update_last_commented_at_on_destroy, :if => ->(rec) {rec.is_hidden? && rec.saved_change_to_is_hidden?}
after_destroy :update_last_commented_at_on_destroy
after_save(:if => ->(rec) {rec.is_hidden? && rec.saved_change_to_is_hidden? && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(:comment_hide, {comment_id: rec.id, user_id: rec.creator_id})
end
@ -36,26 +37,26 @@ class Comment < ApplicationRecord
def hidden(user)
if user.is_moderator?
where("(score < ? and is_sticky = false) or is_deleted = true", user.comment_threshold)
else
where("score < ? and is_sticky = false", user.comment_threshold)
else
where("(score < ? and is_sticky = false) or (is_hidden = true and creator_id != ?)", user.comment_threshold, user.id)
end
end
def visible(user)
if user.is_moderator?
where("(score >= ? or is_sticky = true) and is_deleted = false", user.comment_threshold)
else
where("score >= ? or is_sticky = true", user.comment_threshold)
else
where("(score >= ? or is_sticky = true) and (is_hidden = false or creator_id = ?)", user.comment_threshold, user.id)
end
end
def deleted
where("comments.is_deleted = true")
where("comments.is_hidden = true")
end
def undeleted
where("comments.is_deleted = false")
where("comments.is_hidden = false")
end
def post_tags_match(query)
@ -99,7 +100,7 @@ class Comment < ApplicationRecord
q = q.poster_id(params[:poster_id].to_i)
end
q = q.attribute_matches(:is_deleted, params[:is_deleted])
q = q.attribute_matches(:is_hidden, params[:is_hidden])
q = q.attribute_matches(:is_sticky, params[:is_sticky])
q = q.attribute_matches(:do_not_bump_post, params[:do_not_bump_post])
@ -176,8 +177,12 @@ class Comment < ApplicationRecord
creator_id == user.id || user.is_moderator?
end
def can_hide?(user)
user.is_moderator? || user.id == creator_id
end
def visible_to?(user)
is_deleted? == false || (creator_id == user.id || user.is_moderator?)
is_hidden? == false || ((creator_id == user.id && user.show_hidden_comments?) || user.is_moderator?)
end
def hidden_attributes
@ -188,12 +193,12 @@ class Comment < ApplicationRecord
super + [:creator_name, :updater_name]
end
def delete!
update(is_deleted: true)
def hide!
update(is_hidden: true)
end
def undelete!
update(is_deleted: false)
def unhide!
update(is_hidden: false)
end
def quoted_response

View File

@ -11,7 +11,7 @@ class ForumTopic < ApplicationRecord
has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy
has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
has_many :subscriptions, :class_name => "ForumSubscription"
before_validation :initialize_is_deleted, :on => :create
before_validation :initialize_is_hidden, :on => :create
validates :title, :creator_id, presence: true
validates_associated :original_post
validates_associated :category
@ -48,7 +48,7 @@ class ForumTopic < ApplicationRecord
module SearchMethods
def active
where("is_deleted = false")
where("is_hidden = false")
end
def permitted
@ -83,7 +83,7 @@ class ForumTopic < ApplicationRecord
q = q.attribute_matches(:is_sticky, params[:is_sticky])
q = q.attribute_matches(:is_locked, params[:is_locked])
q = q.attribute_matches(:is_deleted, params[:is_deleted])
q = q.attribute_matches(:is_hidden, params[:is_hidden])
case params[:order]
when "sticky"
@ -159,8 +159,8 @@ class ForumTopic < ApplicationRecord
ModAction.log(:forum_topic_unhide, {forum_topic_id: id, forum_topic_title: title, user_id: creator_id})
end
def initialize_is_deleted
self.is_deleted = false if is_deleted.nil?
def initialize_is_hidden
self.is_hidden = false if is_hidden.nil?
end
def page_for(post_id)
@ -194,15 +194,15 @@ class ForumTopic < ApplicationRecord
def merge(topic)
ForumPost.where(:id => self.posts.map(&:id)).update_all(:topic_id => topic.id)
topic.update_attributes(:response_count => topic.response_count + self.posts.length, :updater_id => CurrentUser.id)
self.update_columns(:response_count => 0, :is_deleted => true, :updater_id => CurrentUser.id)
self.update_columns(:response_count => 0, :is_hidden => true, :updater_id => CurrentUser.id)
end
def delete!
update(is_deleted: true)
def hide!
update(is_hidden: true)
end
def undelete!
update(is_deleted: false)
def unhide!
update(is_hidden: false)
end
def update_orignal_post

View File

@ -29,10 +29,8 @@
<% end %>
<li><%= tag.a "@", href: '#', onclick: "Danbooru.Blip.atme(#{blip.id})" %></li>
<% if !blip.is_hidden %>
<% if blip.can_hide?(CurrentUser.user) %>
<% if !blip.is_hidden && blip.can_hide?(CurrentUser.user) %>
<li><%= link_to "Hide", hide_blip_path(blip), data: {confirm: "Are you sure you want to hide this blip?"}, method: :post %></li>
<% end %>
<% elsif CurrentUser.is_moderator? %>
<li><%= link_to "Unhide", unhide_blip_path(blip), data: {confirm: "Are you sure you want to unhide this blip?"}, method: :post %></li>
<% end %>

View File

@ -1,7 +1,7 @@
<div id="p-index-by-comment" class="comments-for-post">
<div class="list-of-comments">
<% @comments.each do |comment| %>
<% if CurrentUser.is_moderator? || (params[:search] && params[:search][:is_deleted] =~ /t/) || !comment.is_deleted? %>
<% if CurrentUser.is_moderator? || (params[:search] && params[:search][:is_hidden] =~ /t/) || !comment.is_hidden? %>
<%= content_tag(:div, { id: "post_#{comment.post.id}", class: ["post", *PostPresenter.preview_class(comment.post)].join(" ") }.merge(PostPresenter.data_attributes(comment.post))) do %>
<div class="preview">
<% if comment.post.visible? %>

View File

@ -1 +1 @@
$(".comment[data-comment-id=<%= @comment.id %>] div.author h1").append(" (deleted)");
location.reload();

View File

@ -0,0 +1,2 @@
$(".comment[data-comment-id=<%= @comment.id %>] div.author h1").append(" (hidden)");
$(".comment[data-comment-id=<%= @comment.id %>]").addClass("hidden");

View File

@ -1,12 +1,12 @@
<% if CurrentUser.is_moderator? || (params[:search] && params[:search][:is_deleted] =~ /t/) || !comment.is_deleted? %>
<% if comment.visible_to?(CurrentUser.user) %>
<a name="comment-<%= comment.id %>"></a>
<article class="comment <%= "below-threshold" if comment.below_threshold? %>" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator_name %>" data-is-sticky="<%= comment.is_sticky %>">
<article class="comment <%= "below-threshold" if comment.below_threshold? %> <%= "hidden" if comment.is_hidden? %>" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator_name %>" data-is-sticky="<%= comment.is_sticky %>">
<div class="author">
<%= user_avatar(comment.creator) %>
<h1>
<%= link_to_user comment.creator %>
<% if comment.is_deleted? %>
(deleted)
<% if comment.is_hidden? %>
(hidden)
<% end %>
</h1>
<p>
@ -25,13 +25,17 @@
<% if @post || @posts %>
<li><%= link_to "Reply", new_comment_path(id: comment, comment: {post_id: comment.post_id}), class: "reply-link", remote: true %></li>
<% if comment.editable_by?(CurrentUser.user) %>
<% if comment.is_deleted? %>
<li><%= link_to "Undelete", undelete_comment_path(comment.id), :method => :post, :remote => true %></li>
<% else %>
<li><%= link_to "Delete", comment_path(comment.id), :data => {:confirm => "Are you sure you want to delete this comment?"}, :method => :delete, :remote => true %></li>
<% end %>
<li><%= link_to "Edit", edit_comment_path(comment.id), :id => "edit_comment_link_#{comment.id}", :class => "edit_comment_link" %></li>
<% end %>
<% if !comment.is_hidden? && comment.can_hide?(CurrentUser.user) %>
<li><%= link_to "Hide", hide_comment_path(comment), data: {confirm: "Are you sure you want to hide this comment?"}, method: :post, remote: true %></li>
<% elsif CurrentUser.is_moderator? %>
<li><%= link_to "Unhide", unhide_comment_path(comment), data: {confirm: "Are you sure you want to unhide this comment?"}, method: :post, remote: true %></li>
<% end %>
<% if CurrentUser.is_moderator? %>
<li><%= link_to "Delete", comment_path(comment), data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete, remote: true %></li>
<% end %>
<li>|</li>
<%= comment_vote_block(comment, @comment_votes[comment.id]) %>
<% if CurrentUser.is_admin? %>

View File

@ -7,7 +7,7 @@
<%= f.input :creator_name, label: "Commenter", input_html: { data: { autocomplete: "user" } } %>
<%= f.input :body_matches, label: "Body" %>
<%= f.input :post_tags_match, label: "Tags", input_html: { data: { autocomplete: "tag-query" } } %>
<%= f.input :is_deleted, label: "Deleted?", collection: [["Yes", true], ["No", false]] %>
<%= f.input :is_hidden, label: "Hidden?", collection: [["Yes", true], ["No", false]] %>
<%= f.input :is_sticky, label: "Sticky?", collection: [["Yes", true], ["No", false]] %>
<%= f.input :do_not_bump_post, label: "Bumping?", collection: [["Yes", false], ["No", true]] %>
<%= f.input :order, include_blank: false, collection: [%w(Created id_desc), %w(Updated updated_at_desc), %w(Score score_desc), %w(Post post_id_desc)] %>

View File

@ -129,7 +129,8 @@ Rails.application.routes.draw do
get :search
end
member do
post :undelete
post :hide
post :unhide
end
end
resources :comment_votes, only: [:index, :delete, :lock] do

View File

@ -0,0 +1,7 @@
class ChangeIsDeletedToIsHidden < ActiveRecord::Migration[6.0]
def change
rename_column :comments, :is_deleted, :is_hidden
rename_column :forum_posts, :is_deleted, :is_hidden
rename_column :forum_topics, :is_deleted, :is_hidden
end
end

View File

@ -657,7 +657,7 @@ CREATE TABLE public.comments (
updater_id integer,
updater_ip_addr inet,
do_not_bump_post boolean DEFAULT false NOT NULL,
is_deleted boolean DEFAULT false NOT NULL,
is_hidden boolean DEFAULT false NOT NULL,
is_sticky boolean DEFAULT false NOT NULL
);
@ -1048,7 +1048,7 @@ CREATE TABLE public.forum_posts (
updater_id integer NOT NULL,
body text NOT NULL,
text_index tsvector NOT NULL,
is_deleted boolean DEFAULT false NOT NULL,
is_hidden boolean DEFAULT false NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone,
creator_ip_addr inet
@ -1154,7 +1154,7 @@ CREATE TABLE public.forum_topics (
response_count integer DEFAULT 0 NOT NULL,
is_sticky boolean DEFAULT false NOT NULL,
is_locked boolean DEFAULT false NOT NULL,
is_deleted boolean DEFAULT false NOT NULL,
is_hidden boolean DEFAULT false NOT NULL,
text_index tsvector NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone,
@ -5256,6 +5256,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190815131908'),
('20190827223818'),
('20190827233008'),
('20190829044313');
('20190829044313'),
('20190905111159');