mirror of
https://github.com/discourse/discourse.git
synced 2025-06-05 14:07:30 +08:00
FEATURE: Topic timeline widget
This commit is contained in:
26
lib/timeline_lookup.rb
Normal file
26
lib/timeline_lookup.rb
Normal 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
|
@ -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)
|
||||
|
Reference in New Issue
Block a user