diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7cbdd04c..fe908b7f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 1d4dfde4..3260e2bd 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/hat.rb b/app/models/hat.rb index 55bf12b0..4ab3ac1e 100644 --- a/app/models/hat.rb +++ b/app/models/hat.rb @@ -30,8 +30,7 @@ class Hat < ApplicationRecord h = "