2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'guardian'
|
|
|
|
require_dependency 'topic_query'
|
2013-07-12 04:33:45 -04:00
|
|
|
require_dependency 'filter_best_posts'
|
2013-03-08 15:04:37 -05:00
|
|
|
require_dependency 'summarize'
|
2013-12-04 15:56:09 -05:00
|
|
|
require_dependency 'gaps'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class TopicView
|
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
attr_reader :topic, :posts, :guardian, :filtered_posts
|
2013-03-26 14:18:35 -04:00
|
|
|
attr_accessor :draft, :draft_key, :draft_sequence
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def initialize(topic_id, user=nil, options={})
|
2013-07-11 16:38:46 -04:00
|
|
|
@user = user
|
2013-02-10 13:50:26 -05:00
|
|
|
@topic = find_topic(topic_id)
|
2013-07-11 16:38:46 -04:00
|
|
|
@guardian = Guardian.new(@user)
|
2013-07-12 04:33:45 -04:00
|
|
|
check_and_raise_exceptions
|
2013-06-20 17:20:08 -04:00
|
|
|
|
2013-08-13 10:29:25 -04:00
|
|
|
options.each do |key, value|
|
2013-10-28 02:12:07 -04:00
|
|
|
self.instance_variable_set("@#{key}".to_sym, value)
|
2013-08-13 10:29:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@page = @page.to_i
|
|
|
|
@page = 1 if @page.zero?
|
|
|
|
@limit ||= SiteSetting.posts_per_page
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-12 04:33:45 -04:00
|
|
|
setup_filtered_posts
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
@initial_load = true
|
2013-03-26 14:18:35 -04:00
|
|
|
@index_reverse = false
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
filter_posts(options)
|
|
|
|
|
|
|
|
@draft_key = @topic.draft_key
|
2013-07-11 16:38:46 -04:00
|
|
|
@draft_sequence = DraftSequence.current(@user, @draft_key)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
def canonical_path
|
|
|
|
path = @topic.relative_url
|
|
|
|
path << if @post_number
|
|
|
|
page = ((@post_number.to_i - 1) / SiteSetting.posts_per_page) + 1
|
|
|
|
(page > 1) ? "?page=#{page}" : ""
|
|
|
|
else
|
|
|
|
(@page && @page.to_i > 1) ? "?page=#{@page}" : ""
|
|
|
|
end
|
|
|
|
path
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-12-04 15:56:09 -05:00
|
|
|
def contains_gaps?
|
|
|
|
@contains_gaps
|
|
|
|
end
|
|
|
|
|
|
|
|
def gaps
|
|
|
|
return unless @contains_gaps
|
|
|
|
Gaps.new(filtered_post_ids, unfiltered_posts.order(:sort_order).pluck(:id))
|
|
|
|
end
|
|
|
|
|
2013-07-05 14:45:54 -04:00
|
|
|
def last_post
|
|
|
|
return nil if @posts.blank?
|
|
|
|
@last_post ||= @posts.last
|
|
|
|
end
|
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
def next_page
|
2013-07-05 14:45:54 -04:00
|
|
|
@next_page ||= begin
|
|
|
|
if last_post && (@topic.highest_post_number > last_post.post_number)
|
|
|
|
@page + 1
|
|
|
|
end
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_page_path
|
|
|
|
"#{@topic.relative_url}?page=#{next_page}"
|
|
|
|
end
|
|
|
|
|
2013-03-07 17:31:06 -05:00
|
|
|
def absolute_url
|
|
|
|
"#{Discourse.base_url}#{@topic.relative_url}"
|
|
|
|
end
|
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
def relative_url
|
|
|
|
@topic.relative_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
|
|
|
@topic.title
|
|
|
|
end
|
|
|
|
|
2013-07-08 12:21:39 -04:00
|
|
|
def desired_post
|
|
|
|
return @desired_post if @desired_post.present?
|
2013-03-07 17:31:06 -05:00
|
|
|
return nil if posts.blank?
|
2013-07-08 12:21:39 -04:00
|
|
|
|
|
|
|
@desired_post = posts.detect {|p| p.post_number == @post_number.to_i}
|
|
|
|
@desired_post ||= posts.first
|
|
|
|
@desired_post
|
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
|
|
|
return nil if desired_post.blank?
|
2013-09-04 19:33:30 -04:00
|
|
|
# TODO, this is actually quite slow, should be cached in the post table
|
2013-07-08 12:21:39 -04:00
|
|
|
Summarize.new(desired_post.cooked).summary
|
2013-03-07 17:31:06 -05:00
|
|
|
end
|
|
|
|
|
2013-03-08 15:58:37 -05:00
|
|
|
def image_url
|
2013-07-08 12:21:39 -04:00
|
|
|
return nil if desired_post.blank?
|
2013-09-03 17:19:29 -04:00
|
|
|
desired_post.user.try(:small_avatar_url)
|
2013-03-08 15:58:37 -05:00
|
|
|
end
|
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
def filter_posts(opts = {})
|
2013-03-26 11:58:49 -04:00
|
|
|
return filter_posts_near(opts[:post_number].to_i) if opts[:post_number].present?
|
2013-06-20 17:20:08 -04:00
|
|
|
return filter_posts_by_ids(opts[:post_ids]) if opts[:post_ids].present?
|
|
|
|
return filter_best(opts[:best], opts) if opts[:best].present?
|
2013-07-01 07:29:45 -04:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
filter_posts_paged(opts[:page].to_i)
|
|
|
|
end
|
|
|
|
|
2013-03-28 01:53:11 -04:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
# Find the sort order for a post in the topic
|
|
|
|
def sort_order_for_post_number(post_number)
|
|
|
|
Post.where(topic_id: @topic.id, post_number: post_number)
|
|
|
|
.with_deleted
|
2013-03-28 01:53:11 -04:00
|
|
|
.select(:sort_order)
|
2013-03-26 11:58:49 -04:00
|
|
|
.first
|
|
|
|
.try(:sort_order)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
# Filter to all posts near a particular post number
|
|
|
|
def filter_posts_near(post_number)
|
2013-08-13 10:29:25 -04:00
|
|
|
min_idx, max_idx = get_minmax_ids(post_number)
|
2013-03-26 11:58:49 -04:00
|
|
|
filter_posts_in_range(min_idx, max_idx)
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
def filter_posts_paged(page)
|
2013-04-03 18:12:27 -04:00
|
|
|
page = [page, 1].max
|
|
|
|
min = SiteSetting.posts_per_page * (page - 1)
|
2013-07-05 14:45:54 -04:00
|
|
|
max = (min + SiteSetting.posts_per_page) - 1
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
filter_posts_in_range(min, max)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-01 07:29:45 -04:00
|
|
|
def filter_best(max, opts={})
|
2013-07-12 04:33:45 -04:00
|
|
|
filter = FilterBestPosts.new(@topic, @filtered_posts, max, opts)
|
|
|
|
@posts = filter.posts
|
|
|
|
@filtered_posts = filter.filtered_posts
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def read?(post_number)
|
|
|
|
read_posts_set.include?(post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_user
|
|
|
|
@topic_user ||= begin
|
|
|
|
return nil if @user.blank?
|
|
|
|
@topic.topic_users.where(user_id: @user.id).first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-26 14:06:24 -04:00
|
|
|
def post_counts_by_user
|
|
|
|
@post_counts_by_user ||= Post.where(topic_id: @topic.id).group(:user_id).order('count_all desc').limit(24).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def participants
|
|
|
|
@participants ||= begin
|
|
|
|
participants = {}
|
2013-03-26 14:06:24 -04:00
|
|
|
User.where(id: post_counts_by_user.map {|k,v| k}).each {|u| participants[u.id] = u}
|
2013-02-05 14:16:51 -05:00
|
|
|
participants
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_post_actions
|
2013-02-25 11:42:20 -05:00
|
|
|
@all_post_actions ||= PostAction.counts_for(posts, @user)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def links
|
2013-11-15 12:15:46 -05:00
|
|
|
@links ||= TopicLink.topic_map(guardian, @topic.id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_counts
|
2013-06-05 02:10:26 -04:00
|
|
|
@link_counts ||= TopicLink.counts_for(guardian,@topic, posts)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Are we the initial page load? If so, we can return extra information like
|
|
|
|
# user post counts, etc.
|
|
|
|
def initial_load?
|
|
|
|
@initial_load
|
|
|
|
end
|
|
|
|
|
|
|
|
def suggested_topics
|
|
|
|
return nil if topic.private_message?
|
|
|
|
@suggested_topics ||= TopicQuery.new(@user).list_suggested_for(topic)
|
|
|
|
end
|
|
|
|
|
2013-02-14 20:58:14 -05:00
|
|
|
# This is pending a larger refactor, that allows custom orders
|
2013-02-25 11:42:20 -05:00
|
|
|
# for now we need to look for the highest_post_number in the stream
|
|
|
|
# the cache on topics is not correct if there are deleted posts at
|
|
|
|
# the end of the stream (for mods), nor is it correct for filtered
|
2013-02-14 20:58:14 -05:00
|
|
|
# streams
|
|
|
|
def highest_post_number
|
2013-03-26 11:58:49 -04:00
|
|
|
@highest_post_number ||= @filtered_posts.maximum(:post_number)
|
2013-02-14 20:58:14 -05:00
|
|
|
end
|
|
|
|
|
2013-02-21 13:20:00 -05:00
|
|
|
def recent_posts
|
2013-03-26 11:58:49 -04:00
|
|
|
@filtered_posts.by_newest.with_user.first(25)
|
2013-02-21 13:20:00 -05:00
|
|
|
end
|
|
|
|
|
2013-06-28 12:20:06 -04:00
|
|
|
|
|
|
|
def current_post_ids
|
|
|
|
@current_post_ids ||= if @posts.is_a?(Array)
|
|
|
|
@posts.map {|p| p.id }
|
|
|
|
else
|
2013-10-04 04:06:32 -04:00
|
|
|
@posts.pluck(:post_number)
|
2013-06-28 12:20:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-13 10:29:25 -04:00
|
|
|
def filtered_post_ids
|
|
|
|
@filtered_post_ids ||= filter_post_ids_by(:sort_order)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
protected
|
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
def read_posts_set
|
|
|
|
@read_posts_set ||= begin
|
|
|
|
result = Set.new
|
|
|
|
return result unless @user.present?
|
|
|
|
return result unless topic_user.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-10-04 04:06:32 -04:00
|
|
|
post_numbers = PostTiming
|
2013-03-26 11:58:49 -04:00
|
|
|
.where(topic_id: @topic.id, user_id: @user.id)
|
2013-06-28 12:20:06 -04:00
|
|
|
.where(post_number: current_post_ids)
|
2013-03-26 11:58:49 -04:00
|
|
|
.pluck(:post_number)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
post_numbers.each {|pn| result << pn}
|
2013-02-10 13:50:26 -05:00
|
|
|
result
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
def filter_posts_by_ids(post_ids)
|
|
|
|
# TODO: Sort might be off
|
|
|
|
@posts = Post.where(id: post_ids)
|
|
|
|
.includes(:user)
|
|
|
|
.includes(:reply_to_user)
|
|
|
|
.order('sort_order')
|
|
|
|
@posts = @posts.with_deleted if @user.try(:staff?)
|
|
|
|
@posts
|
|
|
|
end
|
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
def filter_posts_in_range(min, max)
|
2013-05-18 16:11:01 -04:00
|
|
|
post_count = (filtered_post_ids.length - 1)
|
2013-03-26 11:58:49 -04:00
|
|
|
|
2013-05-18 16:11:01 -04:00
|
|
|
max = [max, post_count].min
|
2013-05-19 20:29:49 -04:00
|
|
|
|
|
|
|
return @posts = [] if min > max
|
|
|
|
|
2013-05-18 16:11:01 -04:00
|
|
|
min = [[min, max].min, 0].max
|
2013-03-26 11:58:49 -04:00
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
@posts = filter_posts_by_ids(filtered_post_ids[min..max])
|
2013-03-26 11:58:49 -04:00
|
|
|
@posts
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-10 13:50:26 -05:00
|
|
|
def find_topic(topic_id)
|
2013-07-11 16:38:46 -04:00
|
|
|
finder = Topic.where(id: topic_id).includes(:category)
|
|
|
|
finder = finder.with_deleted if @user.try(:staff?)
|
|
|
|
finder.first
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
2013-07-12 04:33:45 -04:00
|
|
|
|
2013-12-04 15:56:09 -05:00
|
|
|
def unfiltered_posts
|
|
|
|
result = @topic.posts.where(hidden: false)
|
|
|
|
result = result.with_deleted if @user.try(:staff?)
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-07-12 04:33:45 -04:00
|
|
|
def setup_filtered_posts
|
2013-12-04 15:56:09 -05:00
|
|
|
|
|
|
|
# Certain filters might leave gaps between posts. If that's true, we can return a gap structure
|
|
|
|
@contains_gaps = false
|
|
|
|
@filtered_posts = unfiltered_posts
|
2013-09-03 17:19:29 -04:00
|
|
|
@filtered_posts = @filtered_posts.with_deleted if @user.try(:staff?)
|
2013-12-04 15:56:09 -05:00
|
|
|
|
|
|
|
# Filters
|
|
|
|
if @filter == 'summary'
|
|
|
|
@filtered_posts = @filtered_posts.summary
|
|
|
|
@contains_gaps = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if @best.present?
|
|
|
|
@filtered_posts = @filtered_posts.where('posts.post_type <> ?', Post.types[:moderator_action])
|
|
|
|
@contains_gaps = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if @username_filters.present?
|
|
|
|
usernames = @username_filters.map{|u| u.downcase}
|
|
|
|
@filtered_posts = @filtered_posts.where('post_number = 1 or user_id in (select u.id from users u where username_lower in (?))', usernames)
|
|
|
|
@contains_gaps = true
|
|
|
|
end
|
|
|
|
|
2013-07-12 04:33:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_and_raise_exceptions
|
|
|
|
raise Discourse::NotFound if @topic.blank?
|
|
|
|
# Special case: If the topic is private and the user isn't logged in, ask them
|
|
|
|
# to log in!
|
|
|
|
if @topic.present? && @topic.private_message? && @user.blank?
|
|
|
|
raise Discourse::NotLoggedIn.new
|
|
|
|
end
|
|
|
|
guardian.ensure_can_see!(@topic)
|
|
|
|
end
|
|
|
|
|
2013-08-13 10:29:25 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
return nil if closest_index.nil?
|
|
|
|
|
|
|
|
# Make sure to get at least one post before, even with rounding
|
|
|
|
posts_before = (SiteSetting.posts_per_page.to_f / 4).floor
|
|
|
|
posts_before = 1 if posts_before.zero?
|
|
|
|
|
|
|
|
min_idx = closest_index - posts_before
|
|
|
|
min_idx = 0 if min_idx < 0
|
|
|
|
max_idx = min_idx + (SiteSetting.posts_per_page - 1)
|
|
|
|
|
|
|
|
# Get a full page even if at the end
|
|
|
|
ensure_full_page(min_idx, max_idx)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_full_page(min, max)
|
|
|
|
upper_limit = (filtered_post_ids.length - 1)
|
|
|
|
if max >= upper_limit
|
|
|
|
return (upper_limit - SiteSetting.posts_per_page) + 1, upper_limit
|
|
|
|
else
|
|
|
|
return min, max
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def closest_post_to(post_number)
|
|
|
|
closest_posts = filter_post_ids_by("@(post_number - #{post_number})")
|
|
|
|
return nil if closest_posts.empty?
|
|
|
|
|
|
|
|
filtered_post_ids.index(closest_posts.first)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|