FEATURE: Topic timeline widget

This commit is contained in:
Robin Ward
2016-05-17 13:03:08 -04:00
parent 751e354ca6
commit 559fa36c18
27 changed files with 621 additions and 102 deletions

26
lib/timeline_lookup.rb Normal file
View File

@ -0,0 +1,26 @@
module TimelineLookup
# Given an array of tuples (id, post_number, days_ago), return at most `max_values` worth of a
# lookup table to help the front end timeline display dates associated with posts
def self.build(tuples, max_values=300)
result = []
every = (tuples.size.to_f / max_values).ceil
last_days_ago = -1
tuples.each_with_index do |t, idx|
next unless (idx % every) === 0
_, post_number, days_ago = t
if (days_ago != last_days_ago)
result << [post_number, days_ago]
last_days_ago = days_ago
end
end
result
end
end

View File

@ -311,7 +311,6 @@ class TopicView
@filtered_posts.by_newest.with_user.first(25)
end
def current_post_ids
@current_post_ids ||= if @posts.is_a?(Array)
@posts.map {|p| p.id }
@ -320,8 +319,17 @@ class TopicView
end
end
# 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')
end
def filtered_post_ids
@filtered_post_ids ||= filter_post_ids_by(:sort_order)
@filtered_post_ids ||= filtered_post_stream.map {|tuple| tuple[0]}
end
protected
@ -435,11 +443,6 @@ class TopicView
raise Discourse::InvalidAccess.new("can't see #{@topic}", @topic) unless guardian.can_see?(@topic)
end
def filter_post_ids_by(sort_order)
@filtered_posts.order(sort_order).pluck(:id)
end
def get_minmax_ids(post_number)
# Find the closest number we have
closest_index = closest_post_to(post_number)