A platform for community discussion. Free, open, simple.
PERF: Don't display days ago on timeline for megatopics.
Guo Xiang Tan9c925a66ff2b
diff --git a/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6 b/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6index e2ae57af20a..882675fc023 100644--- a/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6+++ b/app/assets/javascripts/discourse/widgets/topic-timeline.js.es6@@ -151,8 +151,14 @@ createWidget("timeline-scrollarea", {const current = clamp(Math.floor(total * percentage) + 1, 1, total);const daysAgo = postStream.closestDaysAgoFor(current);- const date = new Date();- date.setDate(date.getDate() - daysAgo || 0);+ let date;++ if (daysAgo !== null) {+ date = new Date();+ date.setDate(date.getDate() - daysAgo || 0);+ } else {+ date = null;+ }const result = {current,diff --git a/lib/topic_view.rb b/lib/topic_view.rbindex e064a3ba93f..14fb158c2e2 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -372,9 +372,18 @@ class TopicView# Returns an array of [id, post_number, days_ago] tuples.# `days_ago` is there for the timeline calculations.def filtered_post_stream- @filtered_post_stream ||= @filtered_posts- .order(:sort_order)- .pluck(:id, :post_number, 'EXTRACT(DAYS FROM CURRENT_TIMESTAMP - created_at)::INT AS days_ago')+ @filtered_post_stream ||= begin+ posts = @filtered_posts+ .order(:sort_order)++ columns = [:id, :post_number]++ if is_mega_topic?+ columns << 'EXTRACT(DAYS FROM CURRENT_TIMESTAMP - created_at)::INT AS days_ago'+ end++ posts.pluck(*columns)+ endenddef filtered_post_ids@@ -554,4 +563,7 @@ class TopicViewfiltered_post_ids.index(closest_post.first) || filtered_post_ids[0]end+ def is_mega_topic?+ [email protected]_message? && @topic.posts_count >= SiteSetting.auto_close_topics_post_count+ endenddiff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rbindex 06fb8099a08..a905b665bf0 100644--- a/spec/components/topic_view_spec.rb+++ b/spec/components/topic_view_spec.rb@@ -549,4 +549,30 @@ describe TopicView doendendend++ describe '#filtered_post_stream' do+ let!(:post) { Fabricate(:post, topic: topic, user: first_poster) }+ let!(:post2) { Fabricate(:post, topic: topic, user: evil_trout) }+ let!(:post3) { Fabricate(:post, topic: topic, user: first_poster) }++ it 'should return the right columns' do+ expect(topic_view.filtered_post_stream).to eq([+ [post.id, post.post_number, 0],+ [post2.id, post2.post_number, 0],+ [post3.id, post3.post_number, 0]+ ])+ end++ describe 'for mega topics' do+ it 'should return the right columns' do+ SiteSetting.auto_close_topics_post_count = 2++ expect(topic_view.filtered_post_stream).to eq([+ [post.id, post.post_number],+ [post2.id, post2.post_number],+ [post3.id, post3.post_number]+ ])+ end+ end+ endend