modqueue: optimize sql queries.
* Include appeals and flags. * Avoid an existence query for pools. * Avoid a query checking if the user has previously approved the post. This is a rare condition and it will be prevented anyway if the user tries to reapprove the post.
This commit is contained in:
@@ -10,10 +10,7 @@ module Moderator
|
|||||||
cookies.permanent["mq_per_page"] = search_params[:per_page]
|
cookies.permanent["mq_per_page"] = search_params[:per_page]
|
||||||
end
|
end
|
||||||
|
|
||||||
::Post.without_timeout do
|
@posts = ::Post.includes(:appeals, :disapprovals, :uploader, flags: [:creator]).reorder(id: :asc).pending_or_flagged.available_for_moderation(search_params[:hidden]).tag_match(search_params[:tags]).paginate(params[:page], :limit => per_page)
|
||||||
@posts = ::Post.includes(:disapprovals, :uploader).order("posts.id asc").pending_or_flagged.available_for_moderation(search_params[:hidden]).tag_match(search_params[:tags]).paginate(params[:page], :limit => per_page)
|
|
||||||
@posts.each # hack to force rails to eager load
|
|
||||||
end
|
|
||||||
respond_with(@posts)
|
respond_with(@posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -273,7 +273,7 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
module ApprovalMethods
|
module ApprovalMethods
|
||||||
def is_approvable?(user = CurrentUser.user)
|
def is_approvable?(user = CurrentUser.user)
|
||||||
!is_status_locked? && (is_pending? || is_flagged? || is_deleted?) && uploader != user && !approved_by?(user)
|
!is_status_locked? && (is_pending? || is_flagged? || is_deleted?) && uploader != user
|
||||||
end
|
end
|
||||||
|
|
||||||
def flag!(reason, is_deletion: false)
|
def flag!(reason, is_deletion: false)
|
||||||
@@ -288,10 +288,6 @@ class Post < ApplicationRecord
|
|||||||
approvals.create(user: approver)
|
approvals.create(user: approver)
|
||||||
end
|
end
|
||||||
|
|
||||||
def approved_by?(user)
|
|
||||||
approver == user || approvals.where(user: user).exists?
|
|
||||||
end
|
|
||||||
|
|
||||||
def disapproved_by?(user)
|
def disapproved_by?(user)
|
||||||
PostDisapproval.where(:user_id => user.id, :post_id => id).exists?
|
PostDisapproval.where(:user_id => user.id, :post_id => id).exists?
|
||||||
end
|
end
|
||||||
@@ -989,7 +985,7 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def has_active_pools?
|
def has_active_pools?
|
||||||
!pools.undeleted.empty?
|
pools.undeleted.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def belongs_to_pool?(pool)
|
def belongs_to_pool?(pool)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class PostApproval < ApplicationRecord
|
|||||||
errors.add(:base, "You cannot approve a post you uploaded")
|
errors.add(:base, "You cannot approve a post you uploaded")
|
||||||
end
|
end
|
||||||
|
|
||||||
if post.approved_by?(user)
|
if post.approver == user || post.approvals.where(user: user).exists?
|
||||||
errors.add(:base, "You have previously approved this post and cannot approve it again")
|
errors.add(:base, "You have previously approved this post and cannot approve it again")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,10 +22,6 @@ class PostApprovalTest < ActiveSupport::TestCase
|
|||||||
CurrentUser.ip_addr = nil
|
CurrentUser.ip_addr = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
should "allow approval" do
|
|
||||||
assert_equal(false, @post.approved_by?(@approver))
|
|
||||||
end
|
|
||||||
|
|
||||||
context "That is approved" do
|
context "That is approved" do
|
||||||
should "create a postapproval record" do
|
should "create a postapproval record" do
|
||||||
assert_difference("PostApproval.count") do
|
assert_difference("PostApproval.count") do
|
||||||
|
|||||||
Reference in New Issue
Block a user