forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic_view.rb
324 lines (251 loc) · 8.79 KB
/
topic_view.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
require_dependency 'guardian'
require_dependency 'topic_query'
require_dependency 'summarize'
class TopicView
attr_reader :topic, :posts, :guardian, :filtered_posts
attr_accessor :draft, :draft_key, :draft_sequence
def initialize(topic_id, user=nil, options={})
@user = user
@topic = find_topic(topic_id)
raise Discourse::NotFound if @topic.blank?
@guardian = Guardian.new(@user)
# 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)
@post_number, @page = options[:post_number], options[:page].to_i
@page = 1 if @page == 0
@limit = options[:limit] || SiteSetting.posts_per_page;
@filtered_posts = @topic.posts
@filtered_posts = @filtered_posts.with_deleted.without_nuked_users if user.try(:staff?)
@filtered_posts = @filtered_posts.best_of if options[:filter] == 'best_of'
@filtered_posts = @filtered_posts.where('posts.post_type <> ?', Post.types[:moderator_action]) if options[:best].present?
if options[:username_filters].present?
usernames = options[: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)
end
@initial_load = true
@index_reverse = false
filter_posts(options)
@draft_key = @topic.draft_key
@draft_sequence = DraftSequence.current(@user, @draft_key)
end
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
end
def last_post
return nil if @posts.blank?
@last_post ||= @posts.last
end
def next_page
@next_page ||= begin
if last_post && (@topic.highest_post_number > last_post.post_number)
@page + 1
end
end
end
def next_page_path
"#{@topic.relative_url}?page=#{next_page}"
end
def absolute_url
"#{Discourse.base_url}#{@topic.relative_url}"
end
def relative_url
@topic.relative_url
end
def title
@topic.title
end
def desired_post
return @desired_post if @desired_post.present?
return nil if posts.blank?
@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?
Summarize.new(desired_post.cooked).summary
end
def image_url
return nil if desired_post.blank?
desired_post.user.small_avatar_url
end
def filter_posts(opts = {})
return filter_posts_near(opts[:post_number].to_i) if opts[:post_number].present?
return filter_posts_by_ids(opts[:post_ids]) if opts[:post_ids].present?
return filter_best(opts[:best], opts) if opts[:best].present?
filter_posts_paged(opts[:page].to_i)
end
# 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
.select(:sort_order)
.first
.try(:sort_order)
end
# Filter to all posts near a particular post number
def filter_posts_near(post_number)
# 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)
end
def filtered_post_ids
@filtered_post_ids ||= @filtered_posts.order(:sort_order).pluck(:id)
end
def filter_posts_paged(page)
page = [page, 1].max
min = SiteSetting.posts_per_page * (page - 1)
max = (min + SiteSetting.posts_per_page) - 1
filter_posts_in_range(min, max)
end
def filter_best(max, opts={})
if opts[:min_replies] && @topic.posts_count < opts[:min_replies] + 1
@posts = []
return
end
if opts[:only_moderator_liked]
liked_by_moderators = PostAction.where(post_id: @filtered_posts.pluck(:id), post_action_type_id: PostActionType.types[:like])
liked_by_moderators = liked_by_moderators.joins(:user).where('users.moderator').pluck(:post_id)
@filtered_posts = @filtered_posts.where(id: liked_by_moderators)
end
@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)
min_trust_level = opts[:min_trust_level]
if min_trust_level && min_trust_level > 0
bypass_trust_level_score = opts[:bypass_trust_level_score]
if bypass_trust_level_score && bypass_trust_level_score > 0
@posts = @posts.where('COALESCE(users.trust_level,0) >= ? OR posts.score >= ?',
min_trust_level,
bypass_trust_level_score
)
else
@posts = @posts.where('COALESCE(users.trust_level,0) >= ?', min_trust_level)
end
end
min_score = opts[:min_score]
if min_score && min_score > 0
@posts = @posts.where('posts.score >= ?', min_score)
end
@posts = @posts.to_a
@posts.sort!{|a,b| a.post_number <=> b.post_number}
@posts
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
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
end
def participants
@participants ||= begin
participants = {}
User.where(id: post_counts_by_user.map {|k,v| k}).each {|u| participants[u.id] = u}
participants
end
end
def all_post_actions
@all_post_actions ||= PostAction.counts_for(posts, @user)
end
def links
@links ||= TopicLink.topic_summary(guardian, @topic.id)
end
def link_counts
@link_counts ||= TopicLink.counts_for(guardian,@topic, posts)
end
# 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
# This is pending a larger refactor, that allows custom orders
# 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
# streams
def highest_post_number
@highest_post_number ||= @filtered_posts.maximum(:post_number)
end
def recent_posts
@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 }
else
@posts.pluck(:post_number)
end
end
protected
def read_posts_set
@read_posts_set ||= begin
result = Set.new
return result unless @user.present?
return result unless topic_user.present?
post_numbers = PostTiming.select(:post_number)
.where(topic_id: @topic.id, user_id: @user.id)
.where(post_number: current_post_ids)
.pluck(:post_number)
post_numbers.each {|pn| result << pn}
result
end
end
private
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
def filter_posts_in_range(min, max)
post_count = (filtered_post_ids.length - 1)
max = [max, post_count].min
return @posts = [] if min > max
min = [[min, max].min, 0].max
@posts = filter_posts_by_ids(filtered_post_ids[min..max])
@posts
end
def find_topic(topic_id)
finder = Topic.where(id: topic_id).includes(:category)
finder = finder.with_deleted if @user.try(:staff?)
finder.first
end
end