2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'guardian'
|
|
|
|
require_dependency 'topic_query'
|
2013-03-08 15:04:37 -05:00
|
|
|
require_dependency 'summarize'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class TopicView
|
|
|
|
|
2013-06-05 02:10:26 -04:00
|
|
|
attr_reader :topic, :posts, :index_offset, :index_reverse, :guardian
|
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-02-10 13:50:26 -05:00
|
|
|
@topic = find_topic(topic_id)
|
2013-02-05 14:16:51 -05:00
|
|
|
raise Discourse::NotFound if @topic.blank?
|
|
|
|
|
2013-06-05 02:10:26 -04:00
|
|
|
@guardian = Guardian.new(user)
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Special case: If the topic is private and the user isn't logged in, ask them
|
|
|
|
# to log in!
|
2013-03-04 19:42:44 -05:00
|
|
|
if @topic.present? && @topic.private_message? && user.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
raise Discourse::NotLoggedIn.new
|
|
|
|
end
|
|
|
|
|
2013-06-05 02:10:26 -04:00
|
|
|
guardian.ensure_can_see!(@topic)
|
2013-02-10 13:50:26 -05:00
|
|
|
@post_number, @page = options[:post_number], options[:page]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-28 01:53:11 -04:00
|
|
|
@limit = options[:limit] || SiteSetting.posts_per_page;
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
@filtered_posts = @topic.posts
|
2013-05-02 03:22:27 -04:00
|
|
|
@filtered_posts = @filtered_posts.with_deleted if user.try(:staff?)
|
2013-03-26 11:58:49 -04:00
|
|
|
@filtered_posts = @filtered_posts.best_of if options[:best_of].present?
|
2013-05-22 01:14:47 -04:00
|
|
|
@filtered_posts = @filtered_posts.where('posts.post_type <> ?', Post.types[:moderator_action]) if options[:best].present?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
if options[:username_filters].present?
|
|
|
|
usernames = options[:username_filters].map{|u| u.downcase}
|
2013-03-26 11:58:49 -04:00
|
|
|
@filtered_posts = @filtered_posts.where('post_number = 1 or user_id in (select u.id from users u where username_lower in (?))', usernames)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@user = user
|
|
|
|
@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
|
|
|
|
@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-02-10 13:50:26 -05:00
|
|
|
def next_page
|
2013-03-26 11:58:49 -04:00
|
|
|
last_post = @filtered_posts.last
|
2013-03-04 19:42:44 -05:00
|
|
|
if last_post.present? && (@topic.highest_post_number > last_post.post_number)
|
2013-03-26 11:58:49 -04:00
|
|
|
(@filtered_posts[0].post_number / SiteSetting.posts_per_page) + 1
|
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-03-26 14:18:35 -04:00
|
|
|
def filtered_posts_count
|
|
|
|
@filtered_posts_count ||= @filtered_posts.count
|
|
|
|
end
|
|
|
|
|
2013-03-07 17:31:06 -05:00
|
|
|
def summary
|
|
|
|
return nil if posts.blank?
|
2013-03-08 15:04:37 -05:00
|
|
|
Summarize.new(posts.first.cooked).summary
|
2013-03-07 17:31:06 -05:00
|
|
|
end
|
|
|
|
|
2013-03-08 15:58:37 -05:00
|
|
|
def image_url
|
|
|
|
return nil if posts.blank?
|
|
|
|
posts.first.user.small_avatar_url
|
|
|
|
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?
|
|
|
|
return filter_posts_before(opts[:posts_before].to_i) if opts[:posts_before].present?
|
|
|
|
return filter_posts_after(opts[:posts_after].to_i) if opts[:posts_after].present?
|
2013-03-28 01:53:11 -04:00
|
|
|
return filter_best(opts[:best]) if opts[:best].present?
|
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-03-26 11:58:49 -04:00
|
|
|
|
|
|
|
# Find the closest number we have
|
|
|
|
closest_post_id = @filtered_posts.order("@(post_number - #{post_number})").first.try(:id)
|
|
|
|
return nil if closest_post_id.blank?
|
|
|
|
|
|
|
|
closest_index = filtered_post_ids.index(closest_post_id)
|
|
|
|
return nil if closest_index.blank?
|
|
|
|
|
|
|
|
# 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 == 0
|
|
|
|
|
|
|
|
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
|
|
|
|
upper_limit = (filtered_post_ids.length - 1)
|
|
|
|
if max_idx >= upper_limit
|
|
|
|
max_idx = upper_limit
|
|
|
|
min_idx = (upper_limit - SiteSetting.posts_per_page) + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
def filtered_post_ids
|
|
|
|
@filtered_post_ids ||= @filtered_posts.order(:sort_order).pluck(:id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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-02-05 14:16:51 -05:00
|
|
|
max = min + SiteSetting.posts_per_page
|
2013-03-26 11:58:49 -04:00
|
|
|
filter_posts_in_range(min, max)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Filter to all posts before a particular post number
|
|
|
|
def filter_posts_before(post_number)
|
|
|
|
@initial_load = false
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
sort_order = sort_order_for_post_number(post_number)
|
|
|
|
return nil unless sort_order
|
2013-03-19 19:51:39 -04:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
# Find posts before the `sort_order`
|
|
|
|
@posts = @filtered_posts.order('sort_order desc').where("sort_order < ?", sort_order)
|
2013-03-26 14:18:35 -04:00
|
|
|
@index_offset = @posts.count
|
|
|
|
@index_reverse = true
|
|
|
|
|
2013-03-28 01:53:11 -04:00
|
|
|
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(@limit)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Filter to all posts after a particular post number
|
|
|
|
def filter_posts_after(post_number)
|
|
|
|
@initial_load = false
|
2013-03-26 11:58:49 -04:00
|
|
|
|
|
|
|
sort_order = sort_order_for_post_number(post_number)
|
|
|
|
return nil unless sort_order
|
|
|
|
|
2013-03-26 14:18:35 -04:00
|
|
|
@index_offset = @filtered_posts.where("sort_order <= ?", sort_order).count
|
2013-03-26 11:58:49 -04:00
|
|
|
@posts = @filtered_posts.order('sort_order').where("sort_order > ?", sort_order)
|
2013-03-28 01:53:11 -04:00
|
|
|
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(@limit)
|
|
|
|
end
|
2013-04-03 18:12:27 -04:00
|
|
|
|
2013-03-28 01:53:11 -04:00
|
|
|
def filter_best(max)
|
|
|
|
@index_offset = 0
|
|
|
|
@posts = @filtered_posts.order('percent_rank asc, sort_order asc').where("post_number > 1")
|
|
|
|
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(max)
|
2013-06-29 07:09:13 -04:00
|
|
|
@posts = @posts.where('COALESCE(users.trust_level,0) > 0')
|
2013-03-28 01:53:11 -04:00
|
|
|
@posts = @posts.to_a
|
|
|
|
@posts.sort!{|a,b| a.post_number <=> b.post_number}
|
|
|
|
@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 voted_in_topic?
|
2013-02-10 13:50:26 -05:00
|
|
|
return false
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# all post_actions is not the way to do this, cut down on the query, roll it up into topic if we need it
|
|
|
|
|
|
|
|
@voted_in_topic ||= begin
|
|
|
|
return false unless all_post_actions.present?
|
2013-03-01 07:07:44 -05:00
|
|
|
all_post_actions.values.flatten.map {|ac| ac.keys}.flatten.include?(PostActionType.types[:vote])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_action_visibility
|
|
|
|
@post_action_visibility ||= begin
|
|
|
|
result = []
|
2013-03-01 07:07:44 -05:00
|
|
|
PostActionType.types.each do |k, v|
|
2013-06-05 02:10:26 -04:00
|
|
|
result << v if guardian.can_see_post_actors?(@topic, v)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def links
|
2013-06-05 02:10:26 -04:00
|
|
|
@links ||= TopicLink.topic_summary(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
|
|
|
|
@posts.pluck(:post_number)
|
|
|
|
end
|
|
|
|
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-03-26 11:58:49 -04:00
|
|
|
post_numbers = PostTiming.select(:post_number)
|
|
|
|
.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
|
|
|
|
|
|
|
|
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-03-26 14:18:35 -04:00
|
|
|
@index_offset = min
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
# TODO: Sort might be off
|
|
|
|
@posts = Post.where(id: filtered_post_ids[min..max])
|
|
|
|
.includes(:user)
|
|
|
|
.includes(:reply_to_user)
|
|
|
|
.order('sort_order')
|
2013-05-02 03:22:27 -04:00
|
|
|
@posts = @posts.with_deleted if @user.try(:staff?)
|
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)
|
|
|
|
Topic.where(id: topic_id).includes(:category).first
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|