diff --git a/app/assets/javascripts/discourse/models/post-stream.js.es6 b/app/assets/javascripts/discourse/models/post-stream.js.es6 index 64313ab20ef..8d639917a37 100644 --- a/app/assets/javascripts/discourse/models/post-stream.js.es6 +++ b/app/assets/javascripts/discourse/models/post-stream.js.es6 @@ -716,8 +716,9 @@ export default RestModel.extend({ closestDaysAgoFor(postNumber) { const timelineLookup = this.get("timelineLookup") || []; - let low = 0, - high = timelineLookup.length - 1; + let low = 0; + let high = timelineLookup.length - 1; + while (low <= high) { const mid = Math.floor(low + (high - low) / 2); const midValue = timelineLookup[mid][0]; @@ -732,9 +733,7 @@ export default RestModel.extend({ } const val = timelineLookup[high] || timelineLookup[low]; - if (val) { - return val[1]; - } + if (val) return val[1]; }, // Find a postId for a postNumber, respecting gaps diff --git a/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6 b/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6 index 882675fc023..1e79edbfd7e 100644 --- a/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6 +++ b/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6 @@ -153,7 +153,13 @@ createWidget("timeline-scrollarea", { const daysAgo = postStream.closestDaysAgoFor(current); let date; - if (daysAgo !== null) { + if (daysAgo === undefined) { + const post = postStream + .get("posts") + .findBy("id", postStream.get("stream")[current]); + + if (post) date = new Date(post.get("created_at")); + } else if (daysAgo !== null) { date = new Date(); date.setDate(date.getDate() - daysAgo || 0); } else { diff --git a/test/javascripts/models/post-stream-test.js.es6 b/test/javascripts/models/post-stream-test.js.es6 index acad4c6c780..e5983a24359 100644 --- a/test/javascripts/models/post-stream-test.js.es6 +++ b/test/javascripts/models/post-stream-test.js.es6 @@ -151,6 +151,9 @@ QUnit.test("closestDaysAgoFor", assert => { assert.equal(postStream.closestDaysAgoFor(-1), 10); assert.equal(postStream.closestDaysAgoFor(0), 10); assert.equal(postStream.closestDaysAgoFor(10), 1); + + postStream.set("timelineLookup", []); + assert.equal(postStream.closestDaysAgoFor(1), undefined); }); QUnit.test("closestDaysAgoFor - empty", assert => {