diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 51ac3dea5..a7d7d0fbb 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -174,7 +174,8 @@ 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 is_comment_locked locked_tags hide_from_anonymous hide_from_search_engines] if CurrentUser.is_admin? + permitted_params += %i[is_comment_locked] if CurrentUser.is_moderator? + permitted_params += %i[is_status_locked is_comment_disabled locked_tags hide_from_anonymous hide_from_search_engines] if CurrentUser.is_admin? params.require(:post).permit(permitted_params) end diff --git a/app/logical/vote_manager.rb b/app/logical/vote_manager.rb index 6e29cdb93..331f32009 100644 --- a/app/logical/vote_manager.rb +++ b/app/logical/vote_manager.rb @@ -93,6 +93,7 @@ class VoteManager raise UserVote::Error, "Invalid vote" unless [1, -1].include?(score) raise UserVote::Error, "You do not have permission to vote" unless user.is_member? raise UserVote::Error, "Comment section is locked" if comment.post.is_comment_locked? + raise UserVote::Error, "Comment section is disabled" if comment.post.is_comment_disabled? CommentVote.transaction(**ISOLATION) do CommentVote.uncached do score_modifier = score diff --git a/app/models/comment.rb b/app/models/comment.rb index 111bcd5e4..859aa119b 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -31,6 +31,10 @@ class Comment < ApplicationRecord belongs_to :warning_user, class_name: "User", optional: true has_many :votes, :class_name => "CommentVote", :dependent => :destroy + scope :deleted, -> { where(is_hidden: true) } + scope :undeleted, -> { where(is_hidden: false) } + scope :stickied, -> { where(is_sticky: true) } + module SearchMethods def recent reorder("comments.id desc").limit(RECENT_COUNT) @@ -47,21 +51,16 @@ class Comment < ApplicationRecord end def visible(user) - if user.is_moderator? - where("comments.score >= ? or comments.is_sticky = true", user.comment_threshold) - elsif user.is_janitor? - where("(comments.score >= ? or comments.is_sticky = true) and (comments.is_sticky = true or comments.is_hidden = false or comments.creator_id = ?)", user.comment_threshold, user.id) - else - where("(comments.score >= ? or comments.is_sticky = true) and (comments.is_hidden = false or comments.creator_id = ?)", user.comment_threshold, user.id) + q = where("comments.score >= ? or comments.is_sticky = true", user.comment_threshold) + unless user.is_moderator? + q = q.joins(:post).where("comments.is_sticky = true or posts.is_comment_disabled = false or comments.creator_id = ?", user.id) + if user.is_janitor? + q = q.where("comments.is_sticky = true or comments.is_hidden = false or comments.creator_id = ?", user.id) + else + q = q.where("comments.is_hidden = false or comments.creator_id = ?", user.id) + end end - end - - def deleted - where("comments.is_hidden = true") - end - - def undeleted - where("comments.is_hidden = false") + q end def post_tags_match(query) @@ -139,7 +138,11 @@ class Comment < ApplicationRecord end def post_not_comment_locked - errors.add(:base, "Post has comments locked") if !CurrentUser.is_moderator? && Post.find_by(id: post_id)&.is_comment_locked? + return if CurrentUser.is_moderator? + post = Post.find_by(id: post_id) + return if post.blank? + errors.add(:base, "Post has comments locked") if post.is_comment_locked? + errors.add(:base, "Post has comments disabled") if post.is_comment_disabled? end def update_last_commented_at_on_create @@ -179,25 +182,26 @@ class Comment < ApplicationRecord def can_reply?(user) return false if is_sticky? - return false if post.is_comment_locked? && !user.is_moderator? + return false if (post.is_comment_locked? || post.is_comment_disabled?) && !user.is_moderator? true end def editable_by?(user) return true if user.is_admin? - return false if post.is_comment_locked? && !user.is_moderator? + return false if (post.is_comment_locked? || post.is_comment_disabled?) && !user.is_moderator? return false if was_warned? creator_id == user.id end def can_hide?(user) return true if user.is_moderator? - return false if was_warned? + return false if !visible_to?(user) || was_warned? || post&.is_comment_disabled? user.id == creator_id end def visible_to?(user) return true if user.is_moderator? + return false if !is_sticky? && (post&.is_comment_disabled? && creator_id != user.id) return true if user.is_janitor? && is_sticky? return true if is_hidden? == false creator_id == user.id # Can always see your own comments, even if hidden. diff --git a/app/models/post.rb b/app/models/post.rb index aa2685c01..680a11af5 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -58,9 +58,6 @@ class Post < ApplicationRecord attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating, :do_not_version_changes, :tag_string_diff, :source_diff, :edit_reason - # FIXME: Remove this - alias_attribute :is_comment_locked, :is_comment_disabled - has_many :versions, -> {order("post_versions.id ASC")}, :class_name => "PostVersion", :dependent => :destroy IMAGE_TYPES = %i[original large preview crop] @@ -1573,6 +1570,10 @@ class Post < ApplicationRecord action = is_comment_locked? ? :comment_locked : :comment_unlocked PostEvent.add(id, CurrentUser.user, action) end + if saved_change_to_is_comment_disabled? + action = is_comment_disabled? ? :comment_disabled : :comment_enabled + PostEvent.add(id, CurrentUser.user, action) + end if saved_change_to_bg_color? PostEvent.add(id, CurrentUser.user, :changed_bg_color, { bg_color: bg_color }) end @@ -1770,8 +1771,12 @@ class Post < ApplicationRecord !has_tag?("grandfathered_content") && created_at.after?("2015-01-01") end - def visible_comment_count(_user) - comment_count + def visible_comment_count(user) + if user.is_moderator? || !is_comment_disabled? + comment_count + else + comments.visible(user).count + end end def avoid_posting_artists diff --git a/app/models/post_event.rb b/app/models/post_event.rb index e27ab8706..9e5f3adde 100644 --- a/app/models/post_event.rb +++ b/app/models/post_event.rb @@ -19,6 +19,8 @@ class PostEvent < ApplicationRecord note_unlocked: 13, comment_locked: 18, comment_unlocked: 19, + comment_disabled: 22, + comment_enabled: 23, replacement_accepted: 14, replacement_rejected: 15, replacement_promoted: 20, @@ -26,9 +28,11 @@ class PostEvent < ApplicationRecord expunged: 17, changed_bg_color: 21, } - MOD_ONLY_ACTIONS = [ + MOD_ONLY_SEARCH_ACTIONS = [ actions[:comment_locked], actions[:comment_unlocked], + actions[:comment_disabled], + actions[:comment_enabled], ].freeze def self.add(post_id, creator, action, data = {}) @@ -47,11 +51,8 @@ class PostEvent < ApplicationRecord def self.search(params) q = super - unless CurrentUser.is_moderator? - q = q.where.not(action: MOD_ONLY_ACTIONS) - end if params[:post_id].present? - q = q.where("post_id = ?", params[:post_id].to_i) + q = q.where(post_id: params[:post_id]) end q = q.where_user(:creator_id, :creator, params) do |condition, user_ids| @@ -62,9 +63,18 @@ class PostEvent < ApplicationRecord end if params[:action].present? - q = q.where('action = ?', actions[params[:action]]) + if !CurrentUser.user.is_moderator? && MOD_ONLY_SEARCH_ACTIONS.include?(actions[params[:action]]) + raise(User::PrivilegeError) + end + q = q.where(action: actions[params[:action]]) end q.apply_basic_order(params) end + + def self.search_options_for(user) + options = actions.keys + return options if user.is_moderator? + options.reject { |action| MOD_ONLY_SEARCH_ACTIONS.any?(actions[action]) } + end end diff --git a/app/views/comments/_index_by_post.html.erb b/app/views/comments/_index_by_post.html.erb index a4d1669d1..0e3fc14b2 100644 --- a/app/views/comments/_index_by_post.html.erb +++ b/app/views/comments/_index_by_post.html.erb @@ -19,7 +19,7 @@
There are no visible comments.
- <% else %> -There are no comments.
<% end %> - <% else %> - <%= render partial: "comments/partials/show/comment", collection: comments, locals: { post: post } %> - <% end %> -There are no visible comments.
+ <% else %> +There are no comments.
+ <% end %> + <% else %> + <%= render partial: "comments/partials/show/comment", collection: comments, locals: { post: post } %> + <% end %> +