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:
evazion
2020-02-29 12:52:07 -06:00
parent b232470248
commit 980103e443
4 changed files with 4 additions and 15 deletions

View File

@@ -273,7 +273,7 @@ class Post < ApplicationRecord
module ApprovalMethods
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
def flag!(reason, is_deletion: false)
@@ -288,10 +288,6 @@ class Post < ApplicationRecord
approvals.create(user: approver)
end
def approved_by?(user)
approver == user || approvals.where(user: user).exists?
end
def disapproved_by?(user)
PostDisapproval.where(:user_id => user.id, :post_id => id).exists?
end
@@ -989,7 +985,7 @@ class Post < ApplicationRecord
end
def has_active_pools?
!pools.undeleted.empty?
pools.undeleted.present?
end
def belongs_to_pool?(pool)

View File

@@ -20,7 +20,7 @@ class PostApproval < ApplicationRecord
errors.add(:base, "You cannot approve a post you uploaded")
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")
end
end