mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 23:11:44 +08:00
FIX: Only include pending/agreed scores in the total score
This should prevent posts from being hidden if a previous flag was rejected and a new one was added.
This commit is contained in:
parent
c63268467e
commit
eedec7d79b
|
@ -465,29 +465,45 @@ class Reviewable < ActiveRecord::Base
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def recalculate_score
|
def recalculate_score
|
||||||
# Recalculate the pending score and return it
|
# pending/agreed scores count
|
||||||
result = DB.query(<<~SQL, id: self.id, pending: ReviewableScore.statuses[:pending])
|
sql = <<~SQL
|
||||||
UPDATE reviewables
|
UPDATE reviewables
|
||||||
SET score = COALESCE((
|
SET score = COALESCE((
|
||||||
SELECT sum(score)
|
SELECT sum(score)
|
||||||
FROM reviewable_scores AS rs
|
FROM reviewable_scores AS rs
|
||||||
WHERE rs.reviewable_id = :id
|
WHERE rs.reviewable_id = :id
|
||||||
|
AND rs.status IN (:pending, :agreed)
|
||||||
), 0.0)
|
), 0.0)
|
||||||
WHERE id = :id
|
WHERE id = :id
|
||||||
RETURNING score
|
RETURNING score
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
|
result = DB.query(
|
||||||
|
sql,
|
||||||
|
id: self.id,
|
||||||
|
pending: ReviewableScore.statuses[:pending],
|
||||||
|
agreed: ReviewableScore.statuses[:agreed]
|
||||||
|
)
|
||||||
|
|
||||||
# Update topic score
|
# Update topic score
|
||||||
DB.query(<<~SQL, topic_id: topic_id, pending: Reviewable.statuses[:pending])
|
sql = <<~SQL
|
||||||
UPDATE topics
|
UPDATE topics
|
||||||
SET reviewable_score = COALESCE((
|
SET reviewable_score = COALESCE((
|
||||||
SELECT SUM(score)
|
SELECT SUM(score)
|
||||||
FROM reviewables AS r
|
FROM reviewables AS r
|
||||||
WHERE r.topic_id = :topic_id
|
WHERE r.topic_id = :topic_id
|
||||||
|
AND r.status IN (:pending, :approved)
|
||||||
), 0.0)
|
), 0.0)
|
||||||
WHERE id = :topic_id
|
WHERE id = :topic_id
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
|
DB.query(
|
||||||
|
sql,
|
||||||
|
topic_id: topic_id,
|
||||||
|
pending: Reviewable.statuses[:pending],
|
||||||
|
approved: Reviewable.statuses[:approved]
|
||||||
|
)
|
||||||
|
|
||||||
self.score = result[0].score
|
self.score = result[0].score
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ RSpec.describe ReviewableScore, type: :model do
|
||||||
expect(rs.reload).to be_disagreed
|
expect(rs.reload).to be_disagreed
|
||||||
expect(rs.reviewed_by).to eq(moderator)
|
expect(rs.reviewed_by).to eq(moderator)
|
||||||
expect(rs.reviewed_at).to be_present
|
expect(rs.reviewed_at).to be_present
|
||||||
expect(reviewable.score).to eq(4.0)
|
expect(reviewable.score).to eq(0.0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "increases the score by the post action type's score bonus" do
|
it "increases the score by the post action type's score bonus" do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user