mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 13:06:56 +08:00
FIX: Post.reply_ids should also handle quotes
This commit is contained in:
@ -656,28 +656,26 @@ class Post < ActiveRecord::Base
|
|||||||
|
|
||||||
def reply_ids(guardian = nil)
|
def reply_ids(guardian = nil)
|
||||||
replies = Post.exec_sql("
|
replies = Post.exec_sql("
|
||||||
WITH RECURSIVE breadcrumb(id, post_number, level) AS (
|
WITH RECURSIVE breadcrumb(id, level) AS (
|
||||||
SELECT id, post_number, 0
|
SELECT :post_id, 0
|
||||||
FROM posts
|
|
||||||
WHERE id = :post_id
|
|
||||||
UNION
|
UNION
|
||||||
SELECT p.id, p.post_number, b.level + 1
|
SELECT reply_id, level + 1
|
||||||
FROM posts p, breadcrumb b
|
FROM post_replies, breadcrumb
|
||||||
WHERE b.post_number = p.reply_to_post_number
|
WHERE post_id = id
|
||||||
AND p.topic_id = :topic_id
|
), breadcrumb_with_count AS (
|
||||||
), breadcrumb_with_replies AS (
|
SELECT id, level, COUNT(*)
|
||||||
SELECT b.id, b.level, COUNT(*)
|
FROM post_replies, breadcrumb
|
||||||
FROM breadcrumb b, post_replies pr
|
WHERE reply_id = id
|
||||||
WHERE pr.reply_id = b.id
|
GROUP BY id, level
|
||||||
GROUP BY b.id, b.level
|
)
|
||||||
) SELECT id, level FROM breadcrumb_with_replies WHERE count = 1 ORDER BY id
|
SELECT id, level FROM breadcrumb_with_count WHERE level > 0 AND count = 1 ORDER BY id
|
||||||
", post_id: id, topic_id: topic_id).to_a
|
", post_id: id).to_a
|
||||||
|
|
||||||
replies.map! { |r| { id: r["id"].to_i, level: r["level"].to_i } }
|
replies.map! { |r| { id: r["id"].to_i, level: r["level"].to_i } }
|
||||||
|
|
||||||
secured_ids = Post.secured(guardian).where(id: replies.map { |r| r[:id] }).pluck(:id).to_set
|
secured_ids = Post.secured(guardian).where(id: replies.map { |r| r[:id] }).pluck(:id).to_set
|
||||||
|
|
||||||
replies.reject { |r| r[:id] == id || !secured_ids.include?(r[:id]) }
|
replies.reject { |r| !secured_ids.include?(r[:id]) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def revert_to(number)
|
def revert_to(number)
|
||||||
|
@ -785,20 +785,23 @@ describe Post do
|
|||||||
let!(:p3) { Fabricate(:post, topic: topic, post_number: 3) }
|
let!(:p3) { Fabricate(:post, topic: topic, post_number: 3) }
|
||||||
let!(:p4) { Fabricate(:post, topic: topic, post_number: 4, reply_to_post_number: 2) }
|
let!(:p4) { Fabricate(:post, topic: topic, post_number: 4, reply_to_post_number: 2) }
|
||||||
let!(:p5) { Fabricate(:post, topic: topic, post_number: 5, reply_to_post_number: 4) }
|
let!(:p5) { Fabricate(:post, topic: topic, post_number: 5, reply_to_post_number: 4) }
|
||||||
|
let!(:p6) { Fabricate(:post, topic: topic, post_number: 6) }
|
||||||
|
|
||||||
before {
|
before {
|
||||||
PostReply.create!(post: p1, reply: p2)
|
PostReply.create!(post: p1, reply: p2)
|
||||||
PostReply.create!(post: p2, reply: p4)
|
PostReply.create!(post: p2, reply: p4)
|
||||||
PostReply.create!(post: p3, reply: p5)
|
PostReply.create!(post: p2, reply: p6) # simulates p6 quoting p2
|
||||||
|
PostReply.create!(post: p3, reply: p5) # simulates p5 quoting p3
|
||||||
PostReply.create!(post: p4, reply: p5)
|
PostReply.create!(post: p4, reply: p5)
|
||||||
}
|
}
|
||||||
|
|
||||||
it "returns the reply ids and their level" do
|
it "returns the reply ids and their level" do
|
||||||
expect(p1.reply_ids).to eq([{ id: p2.id, level: 1 }, { id: p4.id, level: 2 }])
|
expect(p1.reply_ids).to eq([{ id: p2.id, level: 1 }, { id: p4.id, level: 2 }, { id: p6.id, level: 2 }])
|
||||||
expect(p2.reply_ids).to eq([{ id: p4.id, level: 1 }])
|
expect(p2.reply_ids).to eq([{ id: p4.id, level: 1 }, { id: p6.id, level: 1 }])
|
||||||
expect(p3.reply_ids).to be_empty # no replies
|
expect(p3.reply_ids).to be_empty # has no replies
|
||||||
expect(p4.reply_ids).to be_empty # not a direct reply
|
expect(p4.reply_ids).to be_empty # p5 replies to 2 posts (p4 and p3)
|
||||||
expect(p5.reply_ids).to be_empty # last post
|
expect(p5.reply_ids).to be_empty # has no replies
|
||||||
|
expect(p6.reply_ids).to be_empty # last post
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user