mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 11:44:49 +08:00
FIX: /t/:topic_id/last
route did not return any posts.
This commit is contained in:
parent
c2cfb6ebae
commit
c722b07057
|
@ -248,20 +248,21 @@ class TopicView
|
|||
def filter_posts_near(post_number)
|
||||
posts_before = (@limit.to_f / 4).floor
|
||||
posts_before = 1 if posts_before.zero?
|
||||
sort_order = get_sort_order(post_number)
|
||||
|
||||
before_post_ids = @filtered_posts.order(sort_order: :desc)
|
||||
.where("posts.sort_order < #{sort_order_sql(post_number)}",)
|
||||
.where("posts.sort_order < ?", sort_order)
|
||||
.limit(posts_before)
|
||||
.pluck(:id)
|
||||
|
||||
post_ids = before_post_ids + @filtered_posts.order(sort_order: :asc)
|
||||
.where("posts.sort_order >= #{sort_order_sql(post_number)}")
|
||||
.where("posts.sort_order >= ?", sort_order)
|
||||
.limit(@limit - before_post_ids.length)
|
||||
.pluck(:id)
|
||||
|
||||
if post_ids.length < @limit
|
||||
post_ids = post_ids + @filtered_posts.order(sort_order: :desc)
|
||||
.where("posts.sort_order < #{sort_order_sql(post_number)}")
|
||||
.where("posts.sort_order < ?", sort_order)
|
||||
.offset(before_post_ids.length)
|
||||
.limit(@limit - post_ids.length)
|
||||
.pluck(:id)
|
||||
|
@ -483,16 +484,30 @@ class TopicView
|
|||
|
||||
private
|
||||
|
||||
def sort_order_sql(post_number)
|
||||
<<~SQL
|
||||
(
|
||||
def get_sort_order(post_number)
|
||||
sql = <<~SQL
|
||||
SELECT posts.sort_order
|
||||
FROM posts
|
||||
WHERE posts.post_number = #{post_number.to_i}
|
||||
AND posts.topic_id = #{@topic.id.to_i}
|
||||
LIMIT 1
|
||||
SQL
|
||||
|
||||
sort_order = DB.query_single(sql).first
|
||||
|
||||
if !sort_order
|
||||
sql = <<~SQL
|
||||
SELECT posts.sort_order
|
||||
FROM posts
|
||||
WHERE posts.post_number = #{post_number.to_i}
|
||||
AND posts.topic_id = #{@topic.id.to_i}
|
||||
WHERE posts.topic_id = #{@topic.id.to_i}
|
||||
ORDER BY @(post_number - #{post_number.to_i})
|
||||
LIMIT 1
|
||||
)
|
||||
SQL
|
||||
SQL
|
||||
|
||||
sort_order = DB.query_single(sql).first
|
||||
end
|
||||
|
||||
sort_order
|
||||
end
|
||||
|
||||
def filter_post_types(posts)
|
||||
|
@ -506,14 +521,16 @@ class TopicView
|
|||
end
|
||||
|
||||
def filter_posts_by_post_number(post_number, asc)
|
||||
sort_order = get_sort_order(post_number)
|
||||
|
||||
posts =
|
||||
if asc
|
||||
@filtered_posts
|
||||
.where("sort_order > #{sort_order_sql(post_number)}")
|
||||
.where("sort_order > ?", sort_order)
|
||||
.order(sort_order: :asc)
|
||||
else
|
||||
@filtered_posts
|
||||
.where("sort_order < #{sort_order_sql(post_number)}")
|
||||
.where("sort_order < ?", sort_order)
|
||||
.order(sort_order: :desc)
|
||||
end
|
||||
|
||||
|
|
|
@ -458,6 +458,18 @@ describe TopicView do
|
|||
expect(near_view.contains_gaps?).to eq(false)
|
||||
end
|
||||
|
||||
describe 'when post_number is too large' do
|
||||
it "snaps to the lower boundary" do
|
||||
near_view = TopicView.new(topic.id, evil_trout,
|
||||
post_number: 99999999,
|
||||
)
|
||||
|
||||
expect(near_view.desired_post).to eq(p2)
|
||||
expect(near_view.posts).to eq([p2, p3, p5])
|
||||
expect(near_view.contains_gaps?).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
it "gaps deleted posts to an admin" do
|
||||
evil_trout.admin = true
|
||||
near_view = topic_view_near(p3)
|
||||
|
|
Loading…
Reference in New Issue
Block a user