fix appearance of deleted comments on /comments and user thread pages

Also fixes 1 + n queries on those pages.
This commit is contained in:
Peter Bhat Harkins 2018-11-24 08:51:47 -06:00
parent 54abea7ed6
commit b23f106477
4 changed files with 17 additions and 29 deletions

View File

@ -235,17 +235,12 @@ class CommentsController < ApplicationController
raise ActionController::RoutingError.new("page out of bounds")
end
@comments = Comment.where(
:is_deleted => false, :is_moderated => false
).order(
"id DESC"
).offset(
(@page - 1) * COMMENTS_PER_PAGE
).limit(
COMMENTS_PER_PAGE
).includes(
:user, :story
)
@comments = Comment.for_user(@user)
.order("id DESC")
.includes(:user, :hat, :story => :user)
.joins(:story).where.not(stories: { is_expired: true })
.limit(COMMENTS_PER_PAGE)
.offset((@page - 1) * COMMENTS_PER_PAGE)
if @user
@comments = @comments.where("NOT EXISTS (SELECT 1 FROM " <<
@ -290,16 +285,14 @@ class CommentsController < ApplicationController
thread_ids = @showing_user.recent_threads(
20,
include_submitted_stories: !!(@user && @user.id == @showing_user.id),
include_deleted: @user && @user.is_moderator?
for_user: @user
)
comments = Comment.where(
:thread_id => thread_ids
).includes(
:user, :story, :hat, :votes => :user
).arrange_for_user(
@user
)
comments = Comment.for_user(@user)
.where(:thread_id => thread_ids)
.includes(:user, :hat, :story => :user, :votes => :user)
.joins(:story).where.not(stories: { is_expired: true })
.arrange_for_user(@user)
comments_by_thread_id = comments.group_by(&:thread_id)
@threads = comments_by_thread_id.values_at(*thread_ids).compact

View File

@ -31,6 +31,7 @@ class Comment < ApplicationRecord
scope :not_deleted, -> { where(is_deleted: false) }
scope :not_moderated, -> { where(is_moderated: false) }
scope :active, -> { not_deleted.not_moderated }
scope :for_user, ->(user) { user && user.is_moderator? ? all : active }
DOWNVOTABLE_DAYS = 7
DELETEABLE_DAYS = DOWNVOTABLE_DAYS * 2
@ -70,7 +71,7 @@ class Comment < ApplicationRecord
end
def self.arrange_for_user(user)
parents = self.order(Arel.sql("(upvotes - downvotes) < 0 ASC, confidence DESC"))
parents = self.order(Arel.sql("(comments.upvotes - comments.downvotes) < 0 ASC, comments.confidence DESC"))
.group_by(&:parent_comment_id)
# top-down list of comments, regardless of indent level

View File

@ -30,8 +30,7 @@ class Hat < ApplicationRecord
h = "<span class=\"hat " <<
"hat_#{self.hat.gsub(/[^A-Za-z0-9]/, '_').downcase}\" " <<
"title=\"Granted by " << "#{self.granted_by_user.username} on " <<
self.created_at.strftime("%Y-%m-%d")
"title=\"Granted by #{self.created_at.strftime('%Y-%m-%d')}"
if !hl && self.link.present?
h << " - #{ERB::Util.html_escape(self.link)}"

View File

@ -461,13 +461,8 @@ class User < ApplicationRecord
end
end
def recent_threads(amount, include_submitted_stories: false, include_deleted: false)
comments =
if include_deleted
self.comments.not_moderated
else
self.comments.active
end
def recent_threads(amount, include_submitted_stories: false, for_user: user)
comments = self.comments.for_user(for_user)
thread_ids = comments.group(:thread_id).order('MAX(created_at) DESC').limit(amount)
.pluck(:thread_id)