2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'guardian'
|
|
|
|
require_dependency 'topic_query'
|
2013-07-12 16:33:45 +08:00
|
|
|
require_dependency 'filter_best_posts'
|
2013-12-05 04:56:09 +08:00
|
|
|
require_dependency 'gaps'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class TopicView
|
|
|
|
|
2018-06-26 12:54:14 +08:00
|
|
|
attr_reader :topic, :posts, :guardian, :filtered_posts, :chunk_size, :print, :message_bus_last_id
|
2018-03-24 09:44:39 +08:00
|
|
|
attr_accessor :draft, :draft_key, :draft_sequence, :user_custom_fields, :post_custom_fields, :post_number
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-12-15 07:57:34 +08:00
|
|
|
def self.slow_chunk_size
|
|
|
|
10
|
|
|
|
end
|
|
|
|
|
2016-08-01 12:45:05 +08:00
|
|
|
def self.print_chunk_size
|
|
|
|
1000
|
|
|
|
end
|
|
|
|
|
2014-12-15 07:57:34 +08:00
|
|
|
def self.chunk_size
|
|
|
|
20
|
|
|
|
end
|
|
|
|
|
2016-01-11 19:42:06 +08:00
|
|
|
def self.default_post_custom_fields
|
|
|
|
@default_post_custom_fields ||= ["action_code_who"]
|
|
|
|
end
|
|
|
|
|
2017-09-29 23:04:05 +08:00
|
|
|
def self.post_custom_fields_whitelisters
|
|
|
|
@post_custom_fields_whitelisters ||= Set.new
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
def self.add_post_custom_fields_whitelister(&block)
|
|
|
|
post_custom_fields_whitelisters << block
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.whitelisted_post_custom_fields(user)
|
2016-01-11 19:42:06 +08:00
|
|
|
wpcf = default_post_custom_fields + post_custom_fields_whitelisters.map { |w| w.call(user) }
|
|
|
|
wpcf.flatten.uniq
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
2018-05-28 17:06:47 +08:00
|
|
|
def initialize(topic_or_topic_id, user = nil, options = {})
|
|
|
|
@topic = find_topic(topic_or_topic_id)
|
2013-07-12 04:38:46 +08:00
|
|
|
@user = user
|
|
|
|
@guardian = Guardian.new(@user)
|
2018-03-24 09:44:39 +08:00
|
|
|
|
2013-07-12 16:33:45 +08:00
|
|
|
check_and_raise_exceptions
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-05-28 17:06:47 +08:00
|
|
|
@message_bus_last_id = MessageBus.last_id("/topic/#{@topic.id}")
|
|
|
|
@print = options[:print].present?
|
|
|
|
|
2013-08-13 22:29:25 +08:00
|
|
|
options.each do |key, value|
|
2013-10-28 14:12:07 +08:00
|
|
|
self.instance_variable_set("@#{key}".to_sym, value)
|
2013-08-13 22:29:25 +08:00
|
|
|
end
|
|
|
|
|
2018-06-25 22:10:57 +08:00
|
|
|
@_post_number = @post_number.dup
|
2018-03-24 09:44:39 +08:00
|
|
|
@post_number = [@post_number.to_i, 1].max
|
|
|
|
@page = [@page.to_i, 1].max
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
@chunk_size =
|
|
|
|
case
|
|
|
|
when options[:slow_platform] then TopicView.slow_chunk_size
|
|
|
|
when @print then TopicView.print_chunk_size
|
|
|
|
else TopicView.chunk_size
|
|
|
|
end
|
|
|
|
|
2014-12-13 00:47:20 +08:00
|
|
|
@limit ||= @chunk_size
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-12 16:33:45 +08:00
|
|
|
setup_filtered_posts
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
@initial_load = true
|
2013-03-27 02:18:35 +08:00
|
|
|
@index_reverse = false
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
filter_posts(options)
|
|
|
|
|
2017-09-07 21:35:16 +08:00
|
|
|
if @posts
|
2017-08-12 10:18:04 +08:00
|
|
|
if (added_fields = User.whitelisted_user_custom_fields(@guardian)).present?
|
2017-09-07 17:22:39 +08:00
|
|
|
@user_custom_fields = User.custom_fields_for_ids(@posts.pluck(:user_id), added_fields)
|
2016-03-12 04:52:18 +08:00
|
|
|
end
|
2015-03-03 14:51:01 +08:00
|
|
|
|
2017-08-12 10:18:04 +08:00
|
|
|
if (whitelisted_fields = TopicView.whitelisted_post_custom_fields(@user)).present?
|
2017-09-07 17:22:39 +08:00
|
|
|
@post_custom_fields = Post.custom_fields_for_ids(@posts.pluck(:id), whitelisted_fields)
|
2017-08-12 10:18:04 +08:00
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
@draft_key = @topic.draft_key
|
2013-07-12 04:38:46 +08:00
|
|
|
@draft_sequence = DraftSequence.current(@user, @draft_key)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
def canonical_path
|
2016-08-04 12:15:22 +08:00
|
|
|
path = relative_url
|
2017-07-28 09:20:09 +08:00
|
|
|
path <<
|
2018-03-24 09:44:39 +08:00
|
|
|
if @page > 1
|
|
|
|
"?page=#{@page}"
|
2017-07-28 09:20:09 +08:00
|
|
|
else
|
2018-03-24 09:44:39 +08:00
|
|
|
page = ((@post_number - 1) / @limit) + 1
|
|
|
|
page > 1 ? "?page=#{page}" : ""
|
2017-07-28 09:20:09 +08:00
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
path
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-06-26 12:54:14 +08:00
|
|
|
def contains_gaps?
|
|
|
|
@contains_gaps
|
|
|
|
end
|
|
|
|
|
2013-12-05 04:56:09 +08:00
|
|
|
def gaps
|
|
|
|
return unless @contains_gaps
|
2018-06-26 12:54:14 +08:00
|
|
|
@gaps ||= Gaps.new(filtered_post_ids, unfiltered_posts.order(:sort_order).pluck(:id))
|
2013-12-05 04:56:09 +08:00
|
|
|
end
|
|
|
|
|
2013-07-06 02:45:54 +08:00
|
|
|
def last_post
|
|
|
|
return nil if @posts.blank?
|
|
|
|
@last_post ||= @posts.last
|
|
|
|
end
|
|
|
|
|
2014-03-04 01:56:37 +08:00
|
|
|
def prev_page
|
2018-03-24 09:44:39 +08:00
|
|
|
@page > 1 && posts.size > 0 ? @page - 1 : nil
|
2014-03-04 01:56:37 +08:00
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
def next_page
|
2013-07-06 02:45:54 +08:00
|
|
|
@next_page ||= begin
|
|
|
|
if last_post && (@topic.highest_post_number > last_post.post_number)
|
|
|
|
@page + 1
|
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-04 01:56:37 +08:00
|
|
|
def prev_page_path
|
|
|
|
if prev_page > 1
|
2016-08-04 12:15:22 +08:00
|
|
|
"#{relative_url}?page=#{prev_page}"
|
2014-03-04 01:56:37 +08:00
|
|
|
else
|
2016-08-04 12:15:22 +08:00
|
|
|
relative_url
|
2014-03-04 01:56:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
def next_page_path
|
2016-08-04 12:15:22 +08:00
|
|
|
"#{relative_url}?page=#{next_page}"
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
|
|
|
|
2013-03-08 06:31:06 +08:00
|
|
|
def absolute_url
|
2016-08-04 12:15:22 +08:00
|
|
|
"#{Discourse.base_url}#{relative_url}"
|
2013-03-08 06:31:06 +08:00
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
def relative_url
|
2016-08-09 11:53:08 +08:00
|
|
|
"#{@topic.relative_url}#{@print ? '/print' : ''}"
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
|
|
|
|
2015-07-22 08:26:58 +08:00
|
|
|
def page_title
|
|
|
|
title = @topic.title
|
2017-02-08 05:55:42 +08:00
|
|
|
if SiteSetting.topic_page_title_includes_category
|
|
|
|
if @topic.category_id != SiteSetting.uncategorized_category_id && @topic.category_id && @topic.category
|
|
|
|
title += " - #{@topic.category.name}"
|
|
|
|
elsif SiteSetting.tagging_enabled && @topic.tags.exists?
|
|
|
|
title += " - #{@topic.tags.order('tags.topic_count DESC').first.name}"
|
|
|
|
end
|
2015-07-22 08:26:58 +08:00
|
|
|
end
|
|
|
|
title
|
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
def title
|
|
|
|
@topic.title
|
|
|
|
end
|
|
|
|
|
2013-07-09 00:21:39 +08:00
|
|
|
def desired_post
|
|
|
|
return @desired_post if @desired_post.present?
|
2013-03-08 06:31:06 +08:00
|
|
|
return nil if posts.blank?
|
2013-07-09 00:21:39 +08:00
|
|
|
|
2018-03-24 09:44:39 +08:00
|
|
|
@desired_post = posts.detect { |p| p.post_number == @post_number }
|
2013-07-09 00:21:39 +08:00
|
|
|
@desired_post ||= posts.first
|
|
|
|
@desired_post
|
|
|
|
end
|
|
|
|
|
2017-11-28 19:27:43 +08:00
|
|
|
def summary(opts = {})
|
2013-07-09 00:21:39 +08:00
|
|
|
return nil if desired_post.blank?
|
2013-09-05 07:33:30 +08:00
|
|
|
# TODO, this is actually quite slow, should be cached in the post table
|
2017-11-28 19:27:43 +08:00
|
|
|
excerpt = desired_post.excerpt(500, opts.merge(strip_links: true, text_entities: true))
|
2014-12-08 07:23:53 +08:00
|
|
|
(excerpt || "").gsub(/\n/, ' ').strip
|
2013-03-08 06:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-12-28 20:52:31 +08:00
|
|
|
def read_time
|
2018-03-24 09:44:39 +08:00
|
|
|
return nil if @post_number > 1 # only show for topic URLs
|
2017-07-28 09:20:09 +08:00
|
|
|
(@topic.word_count / SiteSetting.read_time_word_count).floor if @topic.word_count
|
2015-12-28 20:52:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def like_count
|
2018-03-24 09:44:39 +08:00
|
|
|
return nil if @post_number > 1 # only show for topic URLs
|
2015-12-28 20:52:31 +08:00
|
|
|
@topic.like_count
|
|
|
|
end
|
|
|
|
|
2013-03-09 04:58:37 +08:00
|
|
|
def image_url
|
2018-03-24 09:44:39 +08:00
|
|
|
if @post_number > 1 && @desired_post.present?
|
2016-10-31 17:41:33 +08:00
|
|
|
if @desired_post.image_url.present?
|
|
|
|
@desired_post.image_url
|
|
|
|
elsif @desired_post.user
|
|
|
|
# show poster avatar
|
2016-11-27 01:25:39 +08:00
|
|
|
@desired_post.user.avatar_template_url.gsub("{size}", "200")
|
2016-10-31 17:41:33 +08:00
|
|
|
end
|
2016-04-15 02:31:20 +08:00
|
|
|
else
|
2016-08-19 19:34:43 +08:00
|
|
|
@topic.image_url
|
2016-04-15 02:31:20 +08:00
|
|
|
end
|
2013-03-09 04:58:37 +08:00
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
def filter_posts(opts = {})
|
2013-03-26 23:58:49 +08:00
|
|
|
return filter_posts_near(opts[:post_number].to_i) if opts[:post_number].present?
|
2013-06-21 05:20:08 +08: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 19:29:45 +08:00
|
|
|
|
2016-02-25 11:32:16 +08:00
|
|
|
filter_posts_paged(@page)
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
|
|
|
|
2014-02-11 05:59:36 +08:00
|
|
|
def primary_group_names
|
|
|
|
return @group_names if @group_names
|
|
|
|
|
|
|
|
primary_group_ids = Set.new
|
|
|
|
@posts.each do |p|
|
|
|
|
primary_group_ids << p.user.primary_group_id if p.user.try(:primary_group_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
unless primary_group_ids.empty?
|
|
|
|
Group.where(id: primary_group_ids.to_a).pluck(:id, :name).each do |g|
|
|
|
|
result[g[0]] = g[1]
|
|
|
|
end
|
|
|
|
end
|
2015-09-28 14:38:34 +08:00
|
|
|
|
|
|
|
@group_names = result
|
2014-02-11 05:59:36 +08:00
|
|
|
end
|
2013-03-28 13:53:11 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
# Find the sort order for a post in the topic
|
|
|
|
def sort_order_for_post_number(post_number)
|
2015-09-11 04:01:23 +08:00
|
|
|
posts = Post.where(topic_id: @topic.id, post_number: post_number).with_deleted
|
|
|
|
posts = filter_post_types(posts)
|
|
|
|
posts.select(:sort_order).first.try(:sort_order)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
# Filter to all posts near a particular post number
|
|
|
|
def filter_posts_near(post_number)
|
2013-08-13 22:29:25 +08:00
|
|
|
min_idx, max_idx = get_minmax_ids(post_number)
|
2013-03-26 23:58:49 +08:00
|
|
|
filter_posts_in_range(min_idx, max_idx)
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def filter_posts_paged(page)
|
2013-04-04 06:12:27 +08:00
|
|
|
page = [page, 1].max
|
2014-01-04 01:52:24 +08:00
|
|
|
min = @limit * (page - 1)
|
|
|
|
|
|
|
|
# Sometimes we don't care about the OP, for example when embedding comments
|
|
|
|
min = 1 if min == 0 && @exclude_first
|
|
|
|
|
|
|
|
max = (min + @limit) - 1
|
2013-07-06 02:45:54 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
filter_posts_in_range(min, max)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def filter_best(max, opts = {})
|
2013-07-12 16:33:45 +08:00
|
|
|
filter = FilterBestPosts.new(@topic, @filtered_posts, max, opts)
|
|
|
|
@posts = filter.posts
|
|
|
|
@filtered_posts = filter.filtered_posts
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def read?(post_number)
|
2014-06-03 09:48:52 +08:00
|
|
|
return true unless @user
|
2013-02-06 03:16:51 +08:00
|
|
|
read_posts_set.include?(post_number)
|
|
|
|
end
|
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
def has_deleted?
|
2014-08-04 23:29:01 +08:00
|
|
|
@predelete_filtered_posts.with_deleted
|
2017-07-28 09:20:09 +08:00
|
|
|
.where("posts.deleted_at IS NOT NULL")
|
|
|
|
.where("posts.post_number > 1")
|
|
|
|
.exists?
|
2014-07-16 05:02:43 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def topic_user
|
|
|
|
@topic_user ||= begin
|
|
|
|
return nil if @user.blank?
|
2014-05-06 21:41:59 +08:00
|
|
|
@topic.topic_users.find_by(user_id: @user.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-13 14:19:42 +08:00
|
|
|
MAX_PARTICIPANTS = 24
|
|
|
|
|
2013-03-27 02:06:24 +08:00
|
|
|
def post_counts_by_user
|
2017-09-13 23:14:03 +08:00
|
|
|
@post_counts_by_user ||= begin
|
2018-06-21 09:09:45 +08:00
|
|
|
if is_mega_topic?
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
post_ids = unfiltered_post_ids
|
2017-10-02 23:47:04 +08:00
|
|
|
|
2018-06-21 09:09:45 +08:00
|
|
|
return {} if post_ids.blank?
|
2017-09-12 14:34:43 +08:00
|
|
|
|
2018-06-21 09:09:45 +08:00
|
|
|
sql = <<~SQL
|
|
|
|
SELECT user_id, count(*) AS count_all
|
|
|
|
FROM posts
|
|
|
|
WHERE id in (:post_ids)
|
|
|
|
AND user_id IS NOT NULL
|
|
|
|
GROUP BY user_id
|
|
|
|
ORDER BY count_all DESC
|
|
|
|
LIMIT #{MAX_PARTICIPANTS}
|
|
|
|
SQL
|
2017-09-12 14:34:43 +08:00
|
|
|
|
2018-06-21 09:09:45 +08:00
|
|
|
Hash[*DB.query_single(sql, post_ids: post_ids)]
|
|
|
|
end
|
2017-09-13 23:14:03 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-06-20 16:11:39 +08:00
|
|
|
# if a topic has more that N posts no longer attempt to
|
|
|
|
# get accurate participant count, instead grab cached count
|
|
|
|
# from topic
|
|
|
|
MAX_POSTS_COUNT_PARTICIPANTS = 500
|
|
|
|
|
2017-12-13 14:19:42 +08:00
|
|
|
def participant_count
|
|
|
|
@participant_count ||=
|
|
|
|
begin
|
|
|
|
if participants.size == MAX_PARTICIPANTS
|
2018-06-20 16:11:39 +08:00
|
|
|
if unfiltered_post_ids.length > MAX_POSTS_COUNT_PARTICIPANTS
|
|
|
|
@topic.participant_count
|
|
|
|
else
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT COUNT(DISTINCT user_id)
|
|
|
|
FROM posts
|
|
|
|
WHERE id IN (:post_ids)
|
|
|
|
AND user_id IS NOT NULL
|
|
|
|
SQL
|
|
|
|
DB.query_single(sql, post_ids: unfiltered_post_ids).first.to_i
|
|
|
|
end
|
2017-12-13 14:19:42 +08:00
|
|
|
else
|
|
|
|
participants.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def participants
|
|
|
|
@participants ||= begin
|
|
|
|
participants = {}
|
2017-09-13 23:14:03 +08:00
|
|
|
User.where(id: post_counts_by_user.keys).includes(:primary_group).each { |u| participants[u.id] = u }
|
2013-02-06 03:16:51 +08:00
|
|
|
participants
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_post_actions
|
2014-06-04 23:41:11 +08:00
|
|
|
@all_post_actions ||= PostAction.counts_for(@posts, @user)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-08-04 23:29:01 +08:00
|
|
|
def all_active_flags
|
|
|
|
@all_active_flags ||= PostAction.active_flags_counts_for(@posts)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def links
|
2017-09-15 02:08:16 +08:00
|
|
|
@links ||= TopicLink.topic_map(@guardian, @topic.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_counts
|
2017-09-15 02:08:16 +08:00
|
|
|
@link_counts ||= TopicLink.counts_for(@guardian, @topic, posts)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08: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
|
|
|
|
@suggested_topics ||= TopicQuery.new(@user).list_suggested_for(topic)
|
|
|
|
end
|
|
|
|
|
2013-02-15 09:58:14 +08:00
|
|
|
# This is pending a larger refactor, that allows custom orders
|
2013-02-26 00:42:20 +08: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-15 09:58:14 +08:00
|
|
|
# streams
|
|
|
|
def highest_post_number
|
2013-03-26 23:58:49 +08:00
|
|
|
@highest_post_number ||= @filtered_posts.maximum(:post_number)
|
2013-02-15 09:58:14 +08:00
|
|
|
end
|
|
|
|
|
2013-02-22 02:20:00 +08:00
|
|
|
def recent_posts
|
2013-03-26 23:58:49 +08:00
|
|
|
@filtered_posts.by_newest.with_user.first(25)
|
2013-02-22 02:20:00 +08:00
|
|
|
end
|
|
|
|
|
2018-06-27 11:11:22 +08:00
|
|
|
# Returns an array of [id, days_ago] tuples.
|
2017-08-04 23:28:25 +08:00
|
|
|
# `days_ago` is there for the timeline calculations.
|
2016-05-18 01:03:08 +08:00
|
|
|
def filtered_post_stream
|
2018-06-20 16:24:09 +08:00
|
|
|
@filtered_post_stream ||= begin
|
|
|
|
posts = @filtered_posts
|
|
|
|
.order(:sort_order)
|
|
|
|
|
2018-06-27 11:11:22 +08:00
|
|
|
columns = [:id]
|
2018-06-20 16:24:09 +08:00
|
|
|
|
2018-06-20 16:58:52 +08:00
|
|
|
if !is_mega_topic?
|
2018-06-20 16:24:09 +08:00
|
|
|
columns << 'EXTRACT(DAYS FROM CURRENT_TIMESTAMP - created_at)::INT AS days_ago'
|
|
|
|
end
|
|
|
|
|
|
|
|
posts.pluck(*columns)
|
|
|
|
end
|
2016-05-18 01:03:08 +08:00
|
|
|
end
|
|
|
|
|
2013-08-13 22:29:25 +08:00
|
|
|
def filtered_post_ids
|
2018-06-27 11:11:22 +08:00
|
|
|
@filtered_post_ids ||= filtered_post_stream.map do |tuple|
|
|
|
|
if is_mega_topic?
|
|
|
|
tuple
|
|
|
|
else
|
|
|
|
tuple[0]
|
|
|
|
end
|
|
|
|
end
|
2013-08-13 22:29:25 +08:00
|
|
|
end
|
|
|
|
|
2017-12-13 14:19:42 +08:00
|
|
|
def unfiltered_post_ids
|
|
|
|
@unfiltered_post_ids ||=
|
|
|
|
begin
|
|
|
|
if @contains_gaps
|
2017-12-13 14:36:36 +08:00
|
|
|
unfiltered_posts.pluck(:id)
|
2017-12-13 14:19:42 +08:00
|
|
|
else
|
|
|
|
filtered_post_ids
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-27 11:11:22 +08:00
|
|
|
def last_read_post_id(post_number)
|
|
|
|
@filtered_posts.where(post_number: post_number).pluck(:id).first
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
protected
|
|
|
|
|
2013-02-11 02:50:26 +08: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-06 03:16:51 +08:00
|
|
|
|
2013-10-04 16:06:32 +08:00
|
|
|
post_numbers = PostTiming
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(topic_id: @topic.id, user_id: @user.id)
|
2017-09-07 18:41:44 +08:00
|
|
|
.where(post_number: @posts.pluck(:post_number))
|
2017-07-28 09:20:09 +08:00
|
|
|
.pluck(:post_number)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
post_numbers.each { |pn| result << pn }
|
2013-02-11 02:50:26 +08:00
|
|
|
result
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-09-11 04:01:23 +08:00
|
|
|
def filter_post_types(posts)
|
2015-09-22 06:50:52 +08:00
|
|
|
visible_types = Topic.visible_post_types(@user)
|
2015-09-11 04:01:23 +08:00
|
|
|
|
|
|
|
if @user.present?
|
2016-04-21 03:29:27 +08:00
|
|
|
posts.where("posts.user_id = ? OR post_type IN (?)", @user.id, visible_types)
|
2015-09-11 04:01:23 +08:00
|
|
|
else
|
|
|
|
posts.where(post_type: visible_types)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
def filter_posts_by_ids(post_ids)
|
|
|
|
# TODO: Sort might be off
|
2014-05-29 19:55:55 +08:00
|
|
|
@posts = Post.where(id: post_ids, topic_id: @topic.id)
|
2017-08-04 23:23:53 +08:00
|
|
|
.includes({ user: :primary_group }, :reply_to_user, :deleted_by, :incoming_email, :topic)
|
2017-07-28 09:20:09 +08:00
|
|
|
.order('sort_order')
|
2015-09-11 04:01:23 +08:00
|
|
|
@posts = filter_post_types(@posts)
|
2014-07-16 05:02:43 +08:00
|
|
|
@posts = @posts.with_deleted if @guardian.can_see_deleted_posts?
|
2013-06-21 05:20:08 +08:00
|
|
|
@posts
|
|
|
|
end
|
|
|
|
|
2013-02-11 02:50:26 +08:00
|
|
|
def filter_posts_in_range(min, max)
|
2013-05-19 04:11:01 +08:00
|
|
|
post_count = (filtered_post_ids.length - 1)
|
2013-03-26 23:58:49 +08:00
|
|
|
|
2013-05-19 04:11:01 +08:00
|
|
|
max = [max, post_count].min
|
2013-05-20 08:29:49 +08:00
|
|
|
|
2017-09-07 21:35:16 +08:00
|
|
|
return @posts = Post.none if min > max
|
2013-05-20 08:29:49 +08:00
|
|
|
|
2013-05-19 04:11:01 +08:00
|
|
|
min = [[min, max].min, 0].max
|
2013-03-26 23:58:49 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
@posts = filter_posts_by_ids(filtered_post_ids[min..max])
|
2013-03-26 23:58:49 +08:00
|
|
|
@posts
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-05-28 17:06:47 +08:00
|
|
|
def find_topic(topic_or_topic_id)
|
|
|
|
if topic_or_topic_id.is_a?(Topic)
|
|
|
|
topic_or_topic_id
|
|
|
|
else
|
|
|
|
# with_deleted covered in #check_and_raise_exceptions
|
|
|
|
finder = Topic.with_deleted.where(id: topic_or_topic_id).includes(:category)
|
|
|
|
finder.first
|
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
2013-07-12 16:33:45 +08:00
|
|
|
|
2013-12-05 04:56:09 +08:00
|
|
|
def unfiltered_posts
|
2015-09-11 04:01:23 +08:00
|
|
|
result = filter_post_types(@topic.posts)
|
2014-07-16 05:02:43 +08:00
|
|
|
result = result.with_deleted if @guardian.can_see_deleted_posts?
|
2016-05-04 03:19:59 +08:00
|
|
|
result = result.where("user_id IS NOT NULL") if @exclude_deleted_users
|
|
|
|
result = result.where(hidden: false) if @exclude_hidden
|
2013-12-05 04:56:09 +08:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-07-12 16:33:45 +08:00
|
|
|
def setup_filtered_posts
|
2013-12-05 04:56:09 +08: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
|
|
|
|
|
|
|
|
# Filters
|
2018-06-26 11:05:25 +08:00
|
|
|
if @filter == 'summary'
|
2015-01-30 14:19:42 +08:00
|
|
|
@filtered_posts = @filtered_posts.summary(@topic.id)
|
2018-06-26 11:05:25 +08:00
|
|
|
@contains_gaps = true
|
2013-12-05 04:56:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if @best.present?
|
2015-07-25 04:39:03 +08:00
|
|
|
@filtered_posts = @filtered_posts.where('posts.post_type = ?', Post.types[:regular])
|
2013-12-05 04:56:09 +08:00
|
|
|
@contains_gaps = true
|
|
|
|
end
|
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
# Username filters
|
2013-12-05 04:56:09 +08:00
|
|
|
if @username_filters.present?
|
2017-07-28 09:20:09 +08:00
|
|
|
usernames = @username_filters.map { |u| u.downcase }
|
2014-08-04 23:29:01 +08:00
|
|
|
@filtered_posts = @filtered_posts.where('post_number = 1 OR posts.user_id IN (SELECT u.id FROM users u WHERE username_lower IN (?))', usernames)
|
2013-12-05 04:56:09 +08:00
|
|
|
@contains_gaps = true
|
|
|
|
end
|
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
# Deleted
|
|
|
|
# This should be last - don't want to tell the admin about deleted posts that clicking the button won't show
|
|
|
|
# copy the filter for has_deleted? method
|
|
|
|
@predelete_filtered_posts = @filtered_posts.spawn
|
|
|
|
if @guardian.can_see_deleted_posts? && !@show_deleted && has_deleted?
|
2014-08-08 01:12:35 +08:00
|
|
|
@filtered_posts = @filtered_posts.where("deleted_at IS NULL OR post_number = 1")
|
2014-07-16 05:02:43 +08:00
|
|
|
@contains_gaps = true
|
|
|
|
end
|
|
|
|
|
2013-07-12 16:33:45 +08: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!
|
2018-02-26 00:31:51 +08:00
|
|
|
if @topic.present? && @topic.private_message? && @user.blank?
|
|
|
|
raise Discourse::NotLoggedIn.new
|
2013-07-12 16:33:45 +08:00
|
|
|
end
|
2018-02-26 00:31:51 +08:00
|
|
|
# can user see this topic?
|
2017-09-15 02:08:16 +08:00
|
|
|
raise Discourse::InvalidAccess.new("can't see #{@topic}", @topic) unless @guardian.can_see?(@topic)
|
2018-02-26 00:31:51 +08:00
|
|
|
# log personal message views
|
|
|
|
if SiteSetting.log_personal_messages_views && @topic.present? && @topic.private_message? && @topic.all_allowed_users.where(id: @user.id).blank?
|
2018-03-12 19:10:17 +08:00
|
|
|
unless UserHistory.where(acting_user_id: @user.id, action: UserHistory.actions[:check_personal_message], topic_id: @topic.id).where("created_at > ?", 1.hour.ago).exists?
|
2018-03-11 11:51:46 +08:00
|
|
|
StaffActionLogger.new(@user).log_check_personal_message(@topic)
|
|
|
|
end
|
2018-02-26 00:31:51 +08:00
|
|
|
end
|
2013-07-12 16:33:45 +08:00
|
|
|
end
|
|
|
|
|
2013-08-13 22:29:25 +08:00
|
|
|
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
|
2014-01-04 01:52:24 +08:00
|
|
|
posts_before = (@limit.to_f / 4).floor
|
2013-08-13 22:29:25 +08:00
|
|
|
posts_before = 1 if posts_before.zero?
|
|
|
|
|
|
|
|
min_idx = closest_index - posts_before
|
|
|
|
min_idx = 0 if min_idx < 0
|
2014-01-04 01:52:24 +08:00
|
|
|
max_idx = min_idx + (@limit - 1)
|
2013-08-13 22:29:25 +08:00
|
|
|
|
|
|
|
# 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
|
2014-01-04 01:52:24 +08:00
|
|
|
return (upper_limit - @limit) + 1, upper_limit
|
2013-08-13 22:29:25 +08:00
|
|
|
else
|
|
|
|
return min, max
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def closest_post_to(post_number)
|
2015-05-04 14:11:04 +08:00
|
|
|
# happy path
|
|
|
|
closest_post = @filtered_posts.where("post_number = ?", post_number).limit(1).pluck(:id)
|
|
|
|
|
|
|
|
if closest_post.empty?
|
|
|
|
# less happy path, missing post
|
|
|
|
closest_post = @filtered_posts.order("@(post_number - #{post_number})").limit(1).pluck(:id)
|
|
|
|
end
|
|
|
|
|
2015-04-22 22:10:09 +08:00
|
|
|
return nil if closest_post.empty?
|
2013-08-13 22:29:25 +08:00
|
|
|
|
2015-04-22 22:10:09 +08:00
|
|
|
filtered_post_ids.index(closest_post.first) || filtered_post_ids[0]
|
2013-08-13 22:29:25 +08:00
|
|
|
end
|
|
|
|
|
2018-06-20 16:58:52 +08:00
|
|
|
MEGA_TOPIC_POSTS_COUNT = 10000
|
|
|
|
|
2018-06-20 16:24:09 +08:00
|
|
|
def is_mega_topic?
|
2018-06-27 11:11:22 +08:00
|
|
|
@is_mega_topic ||= (@topic.posts_count >= MEGA_TOPIC_POSTS_COUNT)
|
2018-06-20 16:24:09 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|