From 6127ea6d25a9a70b7cd7499019b7666d013c9142 Mon Sep 17 00:00:00 2001 From: Kira Date: Thu, 5 Sep 2019 05:35:27 -0700 Subject: [PATCH] Initial comment hide vs delete distinction --- app/controllers/comments_controller.rb | 20 ++++++---- app/models/comment.rb | 37 +++++++++++-------- app/models/forum_topic.rb | 20 +++++----- app/views/blips/partials/show/_blip.html.erb | 4 +- app/views/comments/_index_by_comment.html.erb | 2 +- app/views/comments/destroy.js.erb | 2 +- app/views/comments/hide.js.erb | 2 + .../comments/partials/show/_comment.html.erb | 22 ++++++----- app/views/comments/search.html.erb | 2 +- .../{undelete.js.erb => unhide.js.erb} | 0 config/routes.rb | 3 +- ...05111159_change_is_deleted_to_is_hidden.rb | 7 ++++ db/structure.sql | 9 +++-- 13 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 app/views/comments/hide.js.erb rename app/views/comments/{undelete.js.erb => unhide.js.erb} (100%) create mode 100644 db/migrate/20190905111159_change_is_deleted_to_is_hidden.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 60005e638..fddee0a76 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index a4c2e0a63..361c92cba 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 49bce6841..41cdc67f5 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -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 diff --git a/app/views/blips/partials/show/_blip.html.erb b/app/views/blips/partials/show/_blip.html.erb index 7c30a6f9a..db563e87b 100644 --- a/app/views/blips/partials/show/_blip.html.erb +++ b/app/views/blips/partials/show/_blip.html.erb @@ -29,10 +29,8 @@ <% end %>
  • <%= tag.a "@", href: '#', onclick: "Danbooru.Blip.atme(#{blip.id})" %>
  • - <% if !blip.is_hidden %> - <% if blip.can_hide?(CurrentUser.user) %> + <% if !blip.is_hidden && blip.can_hide?(CurrentUser.user) %>
  • <%= link_to "Hide", hide_blip_path(blip), data: {confirm: "Are you sure you want to hide this blip?"}, method: :post %>
  • - <% end %> <% elsif CurrentUser.is_moderator? %>
  • <%= link_to "Unhide", unhide_blip_path(blip), data: {confirm: "Are you sure you want to unhide this blip?"}, method: :post %>
  • <% end %> diff --git a/app/views/comments/_index_by_comment.html.erb b/app/views/comments/_index_by_comment.html.erb index d79f9ead2..02048b87f 100644 --- a/app/views/comments/_index_by_comment.html.erb +++ b/app/views/comments/_index_by_comment.html.erb @@ -1,7 +1,7 @@
    <% @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 %>
    <% if comment.post.visible? %> diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb index 679b8ccc2..2636aa38c 100644 --- a/app/views/comments/destroy.js.erb +++ b/app/views/comments/destroy.js.erb @@ -1 +1 @@ -$(".comment[data-comment-id=<%= @comment.id %>] div.author h1").append(" (deleted)"); +location.reload(); diff --git a/app/views/comments/hide.js.erb b/app/views/comments/hide.js.erb new file mode 100644 index 000000000..7506840f6 --- /dev/null +++ b/app/views/comments/hide.js.erb @@ -0,0 +1,2 @@ +$(".comment[data-comment-id=<%= @comment.id %>] div.author h1").append(" (hidden)"); +$(".comment[data-comment-id=<%= @comment.id %>]").addClass("hidden"); diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index 13f71be17..941df4a05 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -1,12 +1,12 @@ -<% if CurrentUser.is_moderator? || (params[:search] && params[:search][:is_deleted] =~ /t/) || !comment.is_deleted? %> +<% if comment.visible_to?(CurrentUser.user) %> -
    " 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 %>"> +
    <%= "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 %>">
    <%= user_avatar(comment.creator) %>

    <%= link_to_user comment.creator %> - <% if comment.is_deleted? %> - (deleted) + <% if comment.is_hidden? %> + (hidden) <% end %>

    @@ -25,13 +25,17 @@ <% if @post || @posts %>

  • <%= link_to "Reply", new_comment_path(id: comment, comment: {post_id: comment.post_id}), class: "reply-link", remote: true %>
  • <% if comment.editable_by?(CurrentUser.user) %> - <% if comment.is_deleted? %> -
  • <%= link_to "Undelete", undelete_comment_path(comment.id), :method => :post, :remote => true %>
  • - <% else %> -
  • <%= link_to "Delete", comment_path(comment.id), :data => {:confirm => "Are you sure you want to delete this comment?"}, :method => :delete, :remote => true %>
  • - <% end %>
  • <%= link_to "Edit", edit_comment_path(comment.id), :id => "edit_comment_link_#{comment.id}", :class => "edit_comment_link" %>
  • <% end %> + <% if !comment.is_hidden? && comment.can_hide?(CurrentUser.user) %> +
  • <%= link_to "Hide", hide_comment_path(comment), data: {confirm: "Are you sure you want to hide this comment?"}, method: :post, remote: true %>
  • + <% elsif CurrentUser.is_moderator? %> +
  • <%= link_to "Unhide", unhide_comment_path(comment), data: {confirm: "Are you sure you want to unhide this comment?"}, method: :post, remote: true %>
  • + <% end %> + + <% if CurrentUser.is_moderator? %> +
  • <%= link_to "Delete", comment_path(comment), data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete, remote: true %>
  • + <% end %>
  • |
  • <%= comment_vote_block(comment, @comment_votes[comment.id]) %> <% if CurrentUser.is_admin? %> diff --git a/app/views/comments/search.html.erb b/app/views/comments/search.html.erb index 359cccfcf..b0a77b40e 100644 --- a/app/views/comments/search.html.erb +++ b/app/views/comments/search.html.erb @@ -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)] %> diff --git a/app/views/comments/undelete.js.erb b/app/views/comments/unhide.js.erb similarity index 100% rename from app/views/comments/undelete.js.erb rename to app/views/comments/unhide.js.erb diff --git a/config/routes.rb b/config/routes.rb index 5d2833e8e..ac1b577ca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20190905111159_change_is_deleted_to_is_hidden.rb b/db/migrate/20190905111159_change_is_deleted_to_is_hidden.rb new file mode 100644 index 000000000..62ffa5463 --- /dev/null +++ b/db/migrate/20190905111159_change_is_deleted_to_is_hidden.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index 4cf5d7a70..d7d6be315 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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');