fix order with two posts with the same timestamp for all streams

This commit is contained in:
Benjamin Neff 2016-02-28 01:36:28 +01:00
parent 7583568be8
commit e3d1f0fd16
4 changed files with 18 additions and 17 deletions

View file

@ -14,7 +14,7 @@ module User::Querying
opts = prep_opts(klass, opts)
shareable_ids = visible_shareable_ids(klass, opts)
klass.where(id: shareable_ids).select("DISTINCT #{klass.table_name}.*")
.limit(opts[:limit]).order(opts[:order_with_table]).order(klass.table_name + ".id DESC")
.limit(opts[:limit]).order(opts[:order_with_table])
end
def visible_shareable_ids(klass, opts={})

View file

@ -40,7 +40,7 @@ module Diaspora
end
def self.for_visible_shareable_sql(max_time, order, limit=15, types=Stream::Base::TYPES_OF_POST_IN_STREAM)
by_max_time(max_time, order).where(type: types).limit(limit)
by_max_time(max_time, order).order(table_name + ".id DESC").where(type: types).limit(limit)
end
def self.by_max_time(max_time, order="created_at")

View file

@ -148,6 +148,22 @@ describe Post, :type => :model do
Post.for_visible_shareable_sql(Time.now + 1, "created_at")
end
context "with two posts with the same timestamp" do
before do
aspect_id = alice.aspects.where(name: "generic").first.id
Timecop.freeze Time.now do
alice.post(:status_message, text: "first", to: aspect_id)
alice.post(:status_message, text: "second", to: aspect_id)
end
end
it "returns them in reverse creation order" do
posts = Post.for_visible_shareable_sql(Time.now + 1, "created_at")
expect(posts.first.text).to eq("second")
expect(posts.at(1).text).to eq("first")
expect(posts.last.text).to eq("alice - 5")
end
end
end
end
end

View file

@ -113,21 +113,6 @@ describe User::Querying, :type => :model do
expect(bob.visible_shareables(Post).count(:all)).to eq(0)
end
context 'with two posts with the same timestamp' do
before do
aspect_id = alice.aspects.where(:name => "generic").first.id
Timecop.freeze Time.now do
alice.post :status_message, :text => "first", :to => aspect_id
alice.post :status_message, :text => "second", :to => aspect_id
end
end
it "returns them in reverse creation order" do
expect(bob.visible_shareables(Post).first.text).to eq("second")
expect(bob.visible_shareables(Post).last.text).to eq("first")
end
end
context 'with many posts' do
before do
time_interval = 1000