2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
require 'topic_view'
|
|
|
|
|
|
|
|
describe TopicView do
|
2019-05-06 16:58:39 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2019-05-01 01:09:40 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:evil_trout) { Fabricate(:evil_trout) }
|
2019-05-01 01:09:40 +08:00
|
|
|
fab!(:first_poster) { topic.user }
|
2019-05-01 01:14:49 +08:00
|
|
|
fab!(:anonymous) { Fabricate(:anonymous) }
|
2013-03-26 23:58:49 +08:00
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
let(:topic_view) { TopicView.new(topic.id, evil_trout) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2021-05-25 00:46:57 +08:00
|
|
|
context "preload" do
|
|
|
|
it "allows preloading of data" do
|
|
|
|
preloaded_topic_view = nil
|
|
|
|
preloader = lambda do |view|
|
|
|
|
preloaded_topic_view = view
|
|
|
|
end
|
|
|
|
|
|
|
|
TopicView.on_preload(&preloader)
|
|
|
|
|
|
|
|
expect(preloaded_topic_view).to eq(nil)
|
|
|
|
topic_view
|
|
|
|
expect(preloaded_topic_view).to eq(topic_view)
|
|
|
|
|
|
|
|
TopicView.cancel_preload(&preloader)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "raises a not found error if the topic doesn't exist" do
|
2018-01-27 20:51:22 +08:00
|
|
|
expect { TopicView.new(1231232, evil_trout) }.to raise_error(Discourse::NotFound)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-05-28 17:06:47 +08:00
|
|
|
it "accepts a topic or a topic id" do
|
|
|
|
expect(TopicView.new(topic, evil_trout).topic).to eq(topic)
|
|
|
|
expect(TopicView.new(topic.id, evil_trout).topic).to eq(topic)
|
|
|
|
end
|
|
|
|
|
2015-09-12 02:37:36 +08:00
|
|
|
# see also spec/controllers/topics_controller_spec.rb TopicsController::show::permission errors
|
2013-02-06 03:16:51 +08:00
|
|
|
it "raises an error if the user can't see the topic" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(topic).returns(false)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect { topic_view }.to raise_error(Discourse::InvalidAccess)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-12 04:38:46 +08:00
|
|
|
it "handles deleted topics" do
|
2014-05-13 03:26:36 +08:00
|
|
|
topic.trash!(admin)
|
2019-05-06 16:58:39 +08:00
|
|
|
expect { TopicView.new(topic.id, user) }.to raise_error(Discourse::InvalidAccess)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect { TopicView.new(topic.id, admin) }.not_to raise_error
|
2013-07-12 04:38:46 +08:00
|
|
|
end
|
|
|
|
|
2021-01-22 01:47:03 +08:00
|
|
|
context "filter options" do
|
|
|
|
fab!(:p0) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:p1) { Fabricate(:post, topic: topic, post_type: Post.types[:moderator_action]) }
|
|
|
|
fab!(:p2) { Fabricate(:post, topic: topic, post_type: Post.types[:small_action]) }
|
|
|
|
|
|
|
|
it "omits moderator actions and small posts when only_regular is set" do
|
|
|
|
tv = TopicView.new(topic.id, nil)
|
|
|
|
expect(tv.filtered_post_ids).to eq([p0.id, p1.id, p2.id])
|
|
|
|
|
|
|
|
tv = TopicView.new(topic.id, nil, only_regular: true)
|
|
|
|
expect(tv.filtered_post_ids).to eq([p0.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "omits the first post when exclude_first is set" do
|
|
|
|
tv = TopicView.new(topic.id, nil, exclude_first: true)
|
|
|
|
expect(tv.filtered_post_ids).to eq([p0.id, p1.id, p2.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-10 06:57:58 +08:00
|
|
|
context 'custom filters' do
|
|
|
|
fab!(:p0) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:p1) { Fabricate(:post, topic: topic, wiki: true) }
|
|
|
|
|
|
|
|
it 'allows to register custom filters' do
|
|
|
|
tv = TopicView.new(topic.id, evil_trout, { filter: 'wiki' })
|
|
|
|
expect(tv.filter_posts({ filter: "wiki" })).to eq([p0, p1])
|
|
|
|
|
|
|
|
TopicView.add_custom_filter("wiki") do |posts, topic_view|
|
|
|
|
posts.where(wiki: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
tv = TopicView.new(topic.id, evil_trout, { filter: 'wiki' })
|
|
|
|
expect(tv.filter_posts).to eq([p1])
|
|
|
|
|
|
|
|
tv = TopicView.new(topic.id, evil_trout, { filter: 'whatever' })
|
|
|
|
expect(tv.filter_posts).to eq([p0, p1])
|
|
|
|
|
|
|
|
ensure
|
2021-05-11 09:24:14 +08:00
|
|
|
TopicView.instance_variable_set(:@custom_filters, {})
|
2021-05-10 06:57:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-04 22:29:05 +08:00
|
|
|
context "setup_filtered_posts" do
|
|
|
|
describe "filters posts with ignored users" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:ignored_user) { Fabricate(:ignored_user, user: evil_trout, ignored_user: user) }
|
2019-03-04 22:29:05 +08:00
|
|
|
let!(:post) { Fabricate(:post, topic: topic, user: first_poster) }
|
|
|
|
let!(:post2) { Fabricate(:post, topic: topic, user: evil_trout) }
|
|
|
|
let!(:post3) { Fabricate(:post, topic: topic, user: user) }
|
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
it "filters out ignored user posts" do
|
|
|
|
tv = TopicView.new(topic.id, evil_trout)
|
|
|
|
expect(tv.filtered_post_ids).to eq([post.id, post2.id])
|
2019-03-04 22:29:05 +08:00
|
|
|
end
|
|
|
|
|
2019-12-04 20:52:24 +08:00
|
|
|
it "returns nil for next_page" do
|
|
|
|
tv = TopicView.new(topic.id, evil_trout)
|
|
|
|
expect(tv.next_page).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
describe "when an ignored user made the original post" do
|
|
|
|
let!(:post) { Fabricate(:post, topic: topic, user: user) }
|
2019-03-04 22:29:05 +08:00
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
it "filters out ignored user posts only" do
|
2019-03-04 22:29:05 +08:00
|
|
|
tv = TopicView.new(topic.id, evil_trout)
|
2019-03-20 18:18:46 +08:00
|
|
|
expect(tv.filtered_post_ids).to eq([post.id, post2.id])
|
2019-03-04 22:29:05 +08:00
|
|
|
end
|
2019-04-10 18:54:59 +08:00
|
|
|
end
|
2019-03-04 22:29:05 +08:00
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
describe "when an anonymous user made a post" do
|
|
|
|
let!(:post4) { Fabricate(:post, topic: topic, user: anonymous) }
|
2019-03-04 22:29:05 +08:00
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
it "filters out ignored user posts only" do
|
|
|
|
tv = TopicView.new(topic.id, evil_trout)
|
|
|
|
expect(tv.filtered_post_ids).to eq([post.id, post2.id, post4.id])
|
2019-03-04 22:29:05 +08:00
|
|
|
end
|
2019-04-10 18:54:59 +08:00
|
|
|
end
|
2019-03-04 22:29:05 +08:00
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
describe "when an anonymous (non signed-in) user is viewing a Topic" do
|
|
|
|
let!(:post4) { Fabricate(:post, topic: topic, user: anonymous) }
|
2019-03-04 22:29:05 +08:00
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
it "filters out ignored user posts only" do
|
|
|
|
tv = TopicView.new(topic.id, nil)
|
|
|
|
expect(tv.filtered_post_ids).to eq([post.id, post2.id, post3.id, post4.id])
|
2019-03-20 18:18:46 +08:00
|
|
|
end
|
2019-04-10 18:54:59 +08:00
|
|
|
end
|
2019-03-20 18:18:46 +08:00
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
describe "when a staff user is ignored" do
|
|
|
|
let!(:admin) { Fabricate(:user, admin: true) }
|
|
|
|
let!(:admin_ignored_user) { Fabricate(:ignored_user, user: evil_trout, ignored_user: admin) }
|
|
|
|
let!(:post4) { Fabricate(:post, topic: topic, user: admin) }
|
2019-03-20 18:18:46 +08:00
|
|
|
|
2019-04-10 18:54:59 +08:00
|
|
|
it "filters out ignored user excluding the staff user" do
|
|
|
|
tv = TopicView.new(topic.id, evil_trout)
|
|
|
|
expect(tv.filtered_post_ids).to eq([post.id, post2.id, post4.id])
|
2019-03-04 22:29:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-13 00:47:20 +08:00
|
|
|
context "chunk_size" do
|
2014-12-15 07:57:34 +08:00
|
|
|
it "returns `chunk_size` by default" do
|
2018-01-27 20:51:22 +08:00
|
|
|
expect(TopicView.new(topic.id, evil_trout).chunk_size).to eq(TopicView.chunk_size)
|
2014-12-13 00:47:20 +08:00
|
|
|
end
|
|
|
|
|
2016-08-08 07:01:23 +08:00
|
|
|
it "returns `print_chunk_size` when print param is true" do
|
2018-01-27 20:51:22 +08:00
|
|
|
tv = TopicView.new(topic.id, evil_trout, print: true)
|
2016-08-08 07:01:23 +08:00
|
|
|
expect(tv.chunk_size).to eq(TopicView.print_chunk_size)
|
|
|
|
end
|
2014-12-13 00:47:20 +08:00
|
|
|
end
|
2013-07-12 04:38:46 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
context "with a few sample posts" do
|
2019-05-01 01:50:57 +08:00
|
|
|
fab!(:p1) { Fabricate(:post, topic: topic, user: first_poster, percent_rank: 1) }
|
|
|
|
fab!(:p2) { Fabricate(:post, topic: topic, user: evil_trout, percent_rank: 0.5) }
|
|
|
|
fab!(:p3) { Fabricate(:post, topic: topic, user: first_poster, percent_rank: 0) }
|
2013-03-28 13:53:11 +08:00
|
|
|
|
2013-05-22 13:04:30 +08:00
|
|
|
it "it can find the best responses" do
|
2013-07-03 08:21:10 +08:00
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
best2 = TopicView.new(topic.id, evil_trout, best: 2)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best2.posts.count).to eq(2)
|
|
|
|
expect(best2.posts[0].id).to eq(p2.id)
|
|
|
|
expect(best2.posts[1].id).to eq(p3.id)
|
2013-05-22 13:04:30 +08:00
|
|
|
|
2019-05-06 16:58:39 +08:00
|
|
|
topic.update_status('closed', true, admin)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic.posts.count).to eq(4)
|
2013-05-22 13:04:30 +08:00
|
|
|
|
|
|
|
# should not get the status post
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(2)
|
|
|
|
expect(best.filtered_post_ids.size).to eq(3)
|
2017-09-07 18:59:02 +08:00
|
|
|
expect(best.posts.pluck(:id)).to match_array([p2.id, p3.id])
|
2013-07-01 19:29:45 +08:00
|
|
|
|
|
|
|
# should get no results for trust level too low
|
2018-01-27 20:51:22 +08:00
|
|
|
best = TopicView.new(topic.id, nil, best: 99, min_trust_level: evil_trout.trust_level + 1)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-01 19:29:45 +08:00
|
|
|
|
|
|
|
# should filter out the posts with a score that is too low
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, min_score: 99)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-01 19:29:45 +08:00
|
|
|
|
|
|
|
# should filter out everything if min replies not met
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, min_replies: 99)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-03 08:21:10 +08:00
|
|
|
|
|
|
|
# should punch through posts if the score is high enough
|
|
|
|
p2.update_column(:score, 100)
|
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
best = TopicView.new(topic.id, nil, best: 99, bypass_trust_level_score: 100, min_trust_level: evil_trout.trust_level + 1)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(1)
|
2013-07-03 08:21:10 +08:00
|
|
|
|
|
|
|
# 0 means ignore
|
2018-01-27 20:51:22 +08:00
|
|
|
best = TopicView.new(topic.id, nil, best: 99, bypass_trust_level_score: 0, min_trust_level: evil_trout.trust_level + 1)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-06 04:07:24 +08:00
|
|
|
|
|
|
|
# If we restrict to posts a moderator liked, return none
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, only_moderator_liked: true)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-06 04:07:24 +08:00
|
|
|
|
|
|
|
# It doesn't count likes from admins
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(admin, p3)
|
2013-07-06 04:07:24 +08:00
|
|
|
best = TopicView.new(topic.id, nil, best: 99, only_moderator_liked: true)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-06 04:07:24 +08:00
|
|
|
|
|
|
|
# It should find the post liked by the moderator
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(moderator, p2)
|
2013-07-06 04:07:24 +08:00
|
|
|
best = TopicView.new(topic.id, nil, best: 99, only_moderator_liked: true)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(best.posts.count).to eq(1)
|
2013-03-28 13:53:11 +08:00
|
|
|
end
|
2013-03-26 23:58:49 +08:00
|
|
|
|
2013-03-28 13:53:11 +08:00
|
|
|
it "raises NotLoggedIn if the user isn't logged in and is trying to view a private message" do
|
|
|
|
Topic.any_instance.expects(:private_message?).returns(true)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect { TopicView.new(topic.id, nil) }.to raise_error(Discourse::NotLoggedIn)
|
2013-03-28 13:53:11 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-01-29 15:39:07 +08:00
|
|
|
context 'log_check_personal_message is enabled' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
fab!(:private_message) { Fabricate(:private_message_topic, allowed_groups: [group]) }
|
2018-01-29 15:39:07 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.log_personal_messages_views = true
|
|
|
|
evil_trout.admin = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs view if Admin views personal message for other user/group" do
|
|
|
|
allowed_user = private_message.topic_allowed_users.first.user
|
|
|
|
TopicView.new(private_message.id, allowed_user)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(0)
|
|
|
|
|
|
|
|
TopicView.new(private_message.id, evil_trout)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not log personal message view for group he belongs to" do
|
|
|
|
group.users << evil_trout
|
|
|
|
TopicView.new(private_message.id, evil_trout)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not log personal message view for his own personal message" do
|
|
|
|
private_message.allowed_users << evil_trout
|
|
|
|
TopicView.new(private_message.id, evil_trout)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(0)
|
|
|
|
end
|
2018-02-26 00:31:51 +08:00
|
|
|
|
|
|
|
it "does not log personal message view if user can't see the message" do
|
2019-05-06 16:58:39 +08:00
|
|
|
expect { TopicView.new(private_message.id, user) }.to raise_error(Discourse::InvalidAccess)
|
2018-02-26 00:31:51 +08:00
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(0)
|
|
|
|
end
|
2018-03-11 11:51:46 +08:00
|
|
|
|
|
|
|
it "does not log personal message view if there exists a similar log in previous hour" do
|
|
|
|
2.times { TopicView.new(private_message.id, evil_trout) }
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(1)
|
|
|
|
|
|
|
|
freeze_time (2.hours.from_now)
|
|
|
|
|
|
|
|
TopicView.new(private_message.id, evil_trout)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(2)
|
|
|
|
end
|
2018-01-27 20:51:22 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it "provides an absolute url" do
|
2018-08-28 06:05:08 +08:00
|
|
|
expect(topic_view.absolute_url).to eq("http://test.localhost/t/#{topic.slug}/#{topic.id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'subfolder' do
|
|
|
|
it "provides the correct absolute url" do
|
2019-11-15 13:48:24 +08:00
|
|
|
set_subfolder "/forum"
|
2018-08-28 06:05:08 +08:00
|
|
|
expect(topic_view.absolute_url).to eq("http://test.localhost/forum/t/#{topic.slug}/#{topic.id}")
|
|
|
|
end
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-03-08 06:31:06 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it "provides a summary of the first post" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_view.summary).to be_present
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-03-08 06:31:06 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
describe "#get_canonical_path" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2013-03-26 23:58:49 +08:00
|
|
|
let(:path) { "/1234" }
|
2013-02-11 02:50:26 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
before do
|
2013-07-22 13:06:53 +08:00
|
|
|
topic.stubs(:relative_url).returns(path)
|
|
|
|
TopicView.any_instance.stubs(:find_topic).with(1234).returns(topic)
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
|
2013-07-22 13:06:53 +08:00
|
|
|
it "generates canonical path correctly" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(TopicView.new(1234, user).canonical_path).to eql(path)
|
|
|
|
expect(TopicView.new(1234, user, page: 5).canonical_path).to eql("/1234?page=5")
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
|
2013-07-22 13:06:53 +08:00
|
|
|
it "generates a canonical correctly for paged results" do
|
2020-07-05 16:34:31 +08:00
|
|
|
5.times { |i| Fabricate(:post, post_number: i + 1, topic: topic) }
|
|
|
|
|
|
|
|
expect(TopicView.new(1234, user, post_number: 5, limit: 2)
|
|
|
|
.canonical_path).to eql("/1234?page=3")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "generates canonical path correctly by skipping whisper posts" do
|
|
|
|
2.times { |i| Fabricate(:post, post_number: i + 1, topic: topic) }
|
|
|
|
2.times { |i| Fabricate(:whisper, post_number: i + 3, topic: topic) }
|
|
|
|
Fabricate(:post, post_number: 5, topic: topic)
|
|
|
|
|
|
|
|
expect(TopicView.new(1234, user, post_number: 5, limit: 2)
|
|
|
|
.canonical_path).to eql("/1234?page=2")
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
2020-07-07 14:50:31 +08:00
|
|
|
|
|
|
|
it "generates canonical path correctly for mega topics" do
|
|
|
|
2.times { |i| Fabricate(:post, post_number: i + 1, topic: topic) }
|
|
|
|
2.times { |i| Fabricate(:whisper, post_number: i + 3, topic: topic) }
|
|
|
|
Fabricate(:post, post_number: 5, topic: topic)
|
|
|
|
|
|
|
|
expect(TopicView.new(1234, user, post_number: 5, limit: 2, is_mega_topic: true)
|
|
|
|
.canonical_path).to eql("/1234?page=3")
|
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
describe "#next_page" do
|
2019-12-04 20:52:24 +08:00
|
|
|
let!(:post) { Fabricate(:post, topic: topic, user: user) }
|
|
|
|
let!(:post2) { Fabricate(:post, topic: topic, user: user) }
|
|
|
|
let!(:post3) { Fabricate(:post, topic: topic, user: user) }
|
2020-10-19 14:11:49 +08:00
|
|
|
let!(:post4) { Fabricate(:post, topic: topic, user: user) }
|
|
|
|
let!(:post5) { Fabricate(:post, topic: topic, user: user) }
|
2013-02-11 02:50:26 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
before do
|
2014-12-15 07:57:34 +08:00
|
|
|
TopicView.stubs(:chunk_size).returns(2)
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it "should return the next page" do
|
2020-10-19 14:11:49 +08:00
|
|
|
expect(TopicView.new(topic.id, user, { post_number: post.post_number }).next_page).to eql(3)
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
|
|
|
|
2013-03-27 02:06:24 +08:00
|
|
|
context '.post_counts_by_user' do
|
2017-09-13 23:14:03 +08:00
|
|
|
it 'returns the two posters with their appropriate counts' do
|
2018-01-27 20:51:22 +08:00
|
|
|
Fabricate(:post, topic: topic, user: evil_trout, post_type: Post.types[:whisper])
|
2020-08-05 09:51:28 +08:00
|
|
|
# Should not be counted
|
|
|
|
Fabricate(:post, topic: topic, user: evil_trout, post_type: Post.types[:whisper], action_code: 'assign')
|
2017-09-13 23:14:03 +08:00
|
|
|
|
2020-07-14 09:42:09 +08:00
|
|
|
expect(TopicView.new(topic.id, admin).post_counts_by_user.to_a).to match_array([[first_poster.id, 2], [evil_trout.id, 2]])
|
2017-09-13 23:14:03 +08:00
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
expect(TopicView.new(topic.id, first_poster).post_counts_by_user.to_a).to match_array([[first_poster.id, 2], [evil_trout.id, 1]])
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2015-01-16 04:39:26 +08:00
|
|
|
|
|
|
|
it "doesn't return counts for posts with authors who have been deleted" do
|
|
|
|
p2.user_id = nil
|
|
|
|
p2.save!
|
2017-09-13 23:14:03 +08:00
|
|
|
|
2015-01-16 04:39:26 +08:00
|
|
|
expect(topic_view.post_counts_by_user.to_a).to match_array([[first_poster.id, 2]])
|
|
|
|
end
|
2013-02-11 02:50:26 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
context '.participants' do
|
|
|
|
it 'returns the two participants hashed by id' do
|
2018-01-27 20:51:22 +08:00
|
|
|
expect(topic_view.participants.to_a).to match_array([[first_poster.id, first_poster], [evil_trout.id, evil_trout]])
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
context '.all_post_actions' do
|
|
|
|
it 'is blank at first' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_view.all_post_actions).to be_blank
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it 'returns the like' do
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(evil_trout, p1)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_view.all_post_actions[p1.id][PostActionType.types[:like]]).to be_present
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
context '.read?' do
|
2013-07-22 13:06:53 +08:00
|
|
|
it 'tracks correctly' do
|
2014-06-03 09:48:52 +08:00
|
|
|
# anon is assumed to have read everything
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(TopicView.new(topic.id).read?(1)).to eq(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-22 13:06:53 +08:00
|
|
|
# random user has nothing
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_view.read?(1)).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
evil_trout.created_at = 2.days.ago
|
2015-04-15 00:05:09 +08:00
|
|
|
|
2013-07-22 13:06:53 +08:00
|
|
|
# a real user that just read it should have it marked
|
2018-01-27 20:51:22 +08:00
|
|
|
PostTiming.process_timings(evil_trout, topic.id, 1, [[1, 1000]])
|
|
|
|
expect(TopicView.new(topic.id, evil_trout).read?(1)).to eq(true)
|
|
|
|
expect(TopicView.new(topic.id, evil_trout).topic_user).to be_present
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2020-07-09 13:46:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "#user_post_bookmarks" do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
let!(:bookmark1) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic), user: user) }
|
|
|
|
let!(:bookmark2) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic), user: user) }
|
|
|
|
let!(:bookmark3) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic)) }
|
|
|
|
|
|
|
|
it "returns all the bookmarks in the topic for a user" do
|
|
|
|
expect(TopicView.new(topic.id, user).user_post_bookmarks.pluck(:id)).to match_array(
|
|
|
|
[bookmark1.id, bookmark2.id]
|
|
|
|
)
|
|
|
|
end
|
2020-09-07 12:52:14 +08:00
|
|
|
end
|
|
|
|
|
2021-09-21 06:45:47 +08:00
|
|
|
context "#bookmarks" do
|
2020-09-07 12:52:14 +08:00
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
let!(:bookmark1) { Fabricate(:bookmark_next_business_day_reminder, post: topic.first_post, user: user) }
|
2021-06-28 16:24:23 +08:00
|
|
|
let!(:bookmark2) { Fabricate(:bookmark_next_business_day_reminder, post: topic.posts[1], user: user) }
|
2020-09-07 12:52:14 +08:00
|
|
|
|
|
|
|
it "gets the first post bookmark reminder at for the user" do
|
2021-06-28 16:24:23 +08:00
|
|
|
topic_view = TopicView.new(topic.id, user)
|
|
|
|
|
2021-09-21 06:45:47 +08:00
|
|
|
first, second = topic_view.bookmarks
|
2021-06-28 16:24:23 +08:00
|
|
|
expect(first[:post_id]).to eq(bookmark1.post_id)
|
|
|
|
expect(first[:reminder_at]).to eq_time(bookmark1.reminder_at)
|
|
|
|
expect(second[:post_id]).to eq(bookmark2.post_id)
|
2021-06-30 22:32:28 +08:00
|
|
|
expect(second[:reminder_at]).to eq_time(bookmark2.reminder_at)
|
2020-09-07 12:52:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the topic is deleted" do
|
2021-09-21 06:45:47 +08:00
|
|
|
it "returns []" do
|
2020-09-07 12:52:14 +08:00
|
|
|
topic_view = TopicView.new(topic, user)
|
|
|
|
PostDestroyer.new(Fabricate(:admin), topic.first_post).destroy
|
|
|
|
topic.reload
|
2021-06-28 16:24:23 +08:00
|
|
|
|
2021-09-21 06:45:47 +08:00
|
|
|
expect(topic_view.bookmarks).to eq([])
|
2021-09-15 08:16:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when one of the posts is deleted" do
|
|
|
|
it "does not return that post's bookmark" do
|
|
|
|
topic_view = TopicView.new(topic, user)
|
|
|
|
PostDestroyer.new(Fabricate(:admin), topic.posts.second).destroy
|
|
|
|
topic.reload
|
|
|
|
|
2021-09-21 06:45:47 +08:00
|
|
|
expect(topic_view.bookmarks.length).to eq(1)
|
|
|
|
first = topic_view.bookmarks.first
|
2021-06-28 16:24:23 +08:00
|
|
|
expect(first[:post_id]).to eq(bookmark1.post_id)
|
|
|
|
expect(first[:reminder_at]).to eq_time(bookmark1.reminder_at)
|
2020-09-07 12:52:14 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
context '.topic_user' do
|
|
|
|
it 'returns nil when there is no user' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(TopicView.new(topic.id, nil).topic_user).to be_blank
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
context '#recent_posts' do
|
|
|
|
before do
|
2013-08-21 03:45:58 +08:00
|
|
|
24.times do |t| # our let()s have already created 3
|
|
|
|
Fabricate(:post, topic: topic, user: first_poster, created_at: t.seconds.from_now)
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
|
|
|
end
|
2013-08-21 03:45:58 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it 'returns at most 25 recent posts ordered newest first' do
|
|
|
|
recent_posts = topic_view.recent_posts
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
# count
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(recent_posts.count).to eq(25)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
# ordering
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(recent_posts.include?(p1)).to eq(false)
|
|
|
|
expect(recent_posts.include?(p3)).to eq(true)
|
|
|
|
expect(recent_posts.first.created_at).to be > recent_posts.last.created_at
|
2013-03-26 23:58:49 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 04:01:23 +08:00
|
|
|
context 'whispers' do
|
|
|
|
it "handles their visibility properly" do
|
2018-01-27 20:51:22 +08:00
|
|
|
p1 = Fabricate(:post, topic: topic, user: evil_trout)
|
|
|
|
p2 = Fabricate(:post, topic: topic, user: evil_trout, post_type: Post.types[:whisper])
|
|
|
|
p3 = Fabricate(:post, topic: topic, user: evil_trout)
|
2015-09-11 04:01:23 +08:00
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
ch_posts = TopicView.new(topic.id, evil_trout).posts
|
2015-09-11 04:01:23 +08:00
|
|
|
expect(ch_posts.map(&:id)).to eq([p1.id, p2.id, p3.id])
|
|
|
|
|
|
|
|
anon_posts = TopicView.new(topic.id).posts
|
|
|
|
expect(anon_posts.map(&:id)).to eq([p1.id, p3.id])
|
|
|
|
|
2019-05-06 16:58:39 +08:00
|
|
|
admin_posts = TopicView.new(topic.id, moderator).posts
|
2015-09-11 04:01:23 +08:00
|
|
|
expect(admin_posts.map(&:id)).to eq([p1.id, p2.id, p3.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 15:41:26 +08:00
|
|
|
context '#posts' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
# Create the posts in a different order than the sort_order
|
2018-01-27 20:51:22 +08:00
|
|
|
let!(:p5) { Fabricate(:post, topic: topic, user: evil_trout) }
|
|
|
|
let!(:p2) { Fabricate(:post, topic: topic, user: evil_trout) }
|
2019-05-06 16:58:39 +08:00
|
|
|
let!(:p6) { Fabricate(:post, topic: topic, user: user, deleted_at: Time.now) }
|
2018-01-27 20:51:22 +08:00
|
|
|
let!(:p4) { Fabricate(:post, topic: topic, user: evil_trout, deleted_at: Time.now) }
|
2013-03-26 23:58:49 +08:00
|
|
|
let!(:p1) { Fabricate(:post, topic: topic, user: first_poster) }
|
2018-01-27 20:51:22 +08:00
|
|
|
let!(:p7) { Fabricate(:post, topic: topic, user: evil_trout, deleted_at: Time.now) }
|
2013-03-26 23:58:49 +08:00
|
|
|
let!(:p3) { Fabricate(:post, topic: topic, user: first_poster) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
before do
|
2014-12-15 07:57:34 +08:00
|
|
|
TopicView.stubs(:chunk_size).returns(3)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
# Update them to the sort order we're checking for
|
2014-07-16 05:02:43 +08:00
|
|
|
[p1, p2, p3, p4, p5, p6, p7].each_with_index do |p, idx|
|
2018-06-26 12:54:14 +08:00
|
|
|
p.sort_order = idx + 1
|
|
|
|
p.save
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2018-06-26 12:54:14 +08:00
|
|
|
p6.user_id = nil # user got nuked
|
|
|
|
p6.save!
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-06-26 12:54:14 +08:00
|
|
|
describe "contains_gaps?" do
|
|
|
|
it "works" do
|
|
|
|
# does not contain contains_gaps with default filtering
|
|
|
|
expect(topic_view.contains_gaps?).to eq(false)
|
|
|
|
# contains contains_gaps when filtered by username" do
|
|
|
|
expect(TopicView.new(topic.id, evil_trout, username_filters: ['eviltrout']).contains_gaps?).to eq(true)
|
|
|
|
# contains contains_gaps when filtered by summary
|
|
|
|
expect(TopicView.new(topic.id, evil_trout, filter: 'summary').contains_gaps?).to eq(true)
|
|
|
|
# contains contains_gaps when filtered by best
|
|
|
|
expect(TopicView.new(topic.id, evil_trout, best: 5).contains_gaps?).to eq(true)
|
2013-12-05 04:56:09 +08:00
|
|
|
end
|
2014-05-29 19:55:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "#restricts to correct topic" do
|
|
|
|
t2 = Fabricate(:topic)
|
|
|
|
|
|
|
|
category = Fabricate(:category, name: "my test")
|
|
|
|
category.set_permissions(Group[:admins] => :full)
|
|
|
|
category.save
|
|
|
|
|
|
|
|
topic.category_id = category.id
|
|
|
|
topic.save!
|
|
|
|
|
|
|
|
expect {
|
2018-01-27 20:51:22 +08:00
|
|
|
TopicView.new(topic.id, evil_trout).posts.count
|
2014-05-29 19:55:55 +08:00
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
expect(TopicView.new(t2.id, evil_trout, post_ids: [p1.id, p2.id]).posts.count).to eq(0)
|
2013-12-05 04:56:09 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-19 04:11:01 +08:00
|
|
|
describe '#filter_posts_paged' do
|
2014-12-15 07:57:34 +08:00
|
|
|
before { TopicView.stubs(:chunk_size).returns(2) }
|
2013-05-19 04:11:01 +08:00
|
|
|
|
2013-05-20 08:29:49 +08:00
|
|
|
it 'returns correct posts for all pages' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_view.filter_posts_paged(1)).to eq([p1, p2])
|
|
|
|
expect(topic_view.filter_posts_paged(2)).to eq([p3, p5])
|
|
|
|
expect(topic_view.filter_posts_paged(3)).to eq([])
|
|
|
|
expect(topic_view.filter_posts_paged(100)).to eq([])
|
2013-05-19 04:11:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-11 15:41:26 +08:00
|
|
|
describe '#filter_posts_by_post_number' do
|
|
|
|
def create_topic_view(post_number)
|
|
|
|
TopicView.new(
|
|
|
|
topic.id,
|
|
|
|
evil_trout,
|
|
|
|
filter_post_number: post_number,
|
|
|
|
asc: asc
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ascending' do
|
|
|
|
let(:asc) { true }
|
|
|
|
|
|
|
|
it 'should return the right posts' do
|
|
|
|
topic_view = create_topic_view(p3.post_number)
|
|
|
|
|
|
|
|
expect(topic_view.posts).to eq([p5])
|
|
|
|
|
|
|
|
topic_view = create_topic_view(p6.post_number)
|
|
|
|
expect(topic_view.posts).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'descending' do
|
|
|
|
let(:asc) { false }
|
|
|
|
|
|
|
|
it 'should return the right posts' do
|
|
|
|
topic_view = create_topic_view(p7.post_number)
|
|
|
|
|
|
|
|
expect(topic_view.posts).to eq([p5, p3, p2])
|
|
|
|
|
|
|
|
topic_view = create_topic_view(p2.post_number)
|
|
|
|
|
|
|
|
expect(topic_view.posts).to eq([p1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
describe "filter_posts_near" do
|
2014-07-16 05:02:43 +08:00
|
|
|
def topic_view_near(post, show_deleted = false)
|
2018-01-27 20:51:22 +08:00
|
|
|
TopicView.new(topic.id, evil_trout, post_number: post.post_number, show_deleted: show_deleted)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it "snaps to the lower boundary" do
|
2013-03-27 02:18:35 +08:00
|
|
|
near_view = topic_view_near(p1)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.desired_post).to eq(p1)
|
|
|
|
expect(near_view.posts).to eq([p1, p2, p3])
|
2018-06-26 12:54:14 +08:00
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it "snaps to the upper boundary" do
|
2013-03-27 02:18:35 +08:00
|
|
|
near_view = topic_view_near(p5)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.desired_post).to eq(p5)
|
|
|
|
expect(near_view.posts).to eq([p2, p3, p5])
|
2018-06-26 12:54:14 +08:00
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-26 23:58:49 +08:00
|
|
|
it "returns the posts in the middle" do
|
2013-03-27 02:18:35 +08:00
|
|
|
near_view = topic_view_near(p2)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.desired_post).to eq(p2)
|
|
|
|
expect(near_view.posts).to eq([p1, p2, p3])
|
2018-06-26 12:54:14 +08:00
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-07-13 14:25:12 +08:00
|
|
|
describe 'when post_number is too large' do
|
|
|
|
it "snaps to the lower boundary" do
|
|
|
|
near_view = TopicView.new(topic.id, evil_trout,
|
|
|
|
post_number: 99999999,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(near_view.desired_post).to eq(p2)
|
|
|
|
expect(near_view.posts).to eq([p2, p3, p5])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
it "gaps deleted posts to an admin" do
|
2018-01-27 20:51:22 +08:00
|
|
|
evil_trout.admin = true
|
2013-03-27 02:18:35 +08:00
|
|
|
near_view = topic_view_near(p3)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.desired_post).to eq(p3)
|
|
|
|
expect(near_view.posts).to eq([p2, p3, p5])
|
|
|
|
expect(near_view.gaps.before).to eq(p5.id => [p4.id])
|
|
|
|
expect(near_view.gaps.after).to eq(p5.id => [p6.id, p7.id])
|
2014-07-16 05:02:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns deleted posts to an admin with show_deleted" do
|
2018-01-27 20:51:22 +08:00
|
|
|
evil_trout.admin = true
|
2014-07-16 05:02:43 +08:00
|
|
|
near_view = topic_view_near(p3, true)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.desired_post).to eq(p3)
|
|
|
|
expect(near_view.posts).to eq([p2, p3, p4])
|
2018-06-26 12:54:14 +08:00
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-22 02:20:00 +08:00
|
|
|
end
|
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
it "gaps deleted posts by nuked users to an admin" do
|
2018-01-27 20:51:22 +08:00
|
|
|
evil_trout.admin = true
|
2013-09-04 05:19:29 +08:00
|
|
|
near_view = topic_view_near(p5)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.desired_post).to eq(p5)
|
2014-07-16 05:02:43 +08:00
|
|
|
# note: both p4 and p6 get skipped
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.posts).to eq([p2, p3, p5])
|
|
|
|
expect(near_view.gaps.before).to eq(p5.id => [p4.id])
|
|
|
|
expect(near_view.gaps.after).to eq(p5.id => [p6.id, p7.id])
|
2014-07-16 05:02:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns deleted posts by nuked users to an admin with show_deleted" do
|
2018-01-27 20:51:22 +08:00
|
|
|
evil_trout.admin = true
|
2014-07-16 05:02:43 +08:00
|
|
|
near_view = topic_view_near(p5, true)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.desired_post).to eq(p5)
|
|
|
|
expect(near_view.posts).to eq([p4, p5, p6])
|
2018-06-26 12:54:14 +08:00
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-09-04 05:19:29 +08:00
|
|
|
end
|
|
|
|
|
2013-05-19 04:11:01 +08:00
|
|
|
context "when 'posts per page' exceeds the number of posts" do
|
2014-12-15 07:57:34 +08:00
|
|
|
before { TopicView.stubs(:chunk_size).returns(100) }
|
2013-02-22 02:20:00 +08:00
|
|
|
|
2013-05-19 04:11:01 +08:00
|
|
|
it 'returns all the posts' do
|
|
|
|
near_view = topic_view_near(p5)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.posts).to eq([p1, p2, p3, p5])
|
2018-06-26 12:54:14 +08:00
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-05-19 04:11:01 +08:00
|
|
|
end
|
2013-09-04 05:19:29 +08:00
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
it 'gaps deleted posts to admins' do
|
2018-01-27 20:51:22 +08:00
|
|
|
evil_trout.admin = true
|
2013-09-04 05:19:29 +08:00
|
|
|
near_view = topic_view_near(p5)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.posts).to eq([p1, p2, p3, p5])
|
|
|
|
expect(near_view.gaps.before).to eq(p5.id => [p4.id])
|
|
|
|
expect(near_view.gaps.after).to eq(p5.id => [p6.id, p7.id])
|
2014-07-16 05:02:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns deleted posts to admins' do
|
2018-01-27 20:51:22 +08:00
|
|
|
evil_trout.admin = true
|
2014-07-16 05:02:43 +08:00
|
|
|
near_view = topic_view_near(p5, true)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(near_view.posts).to eq([p1, p2, p3, p4, p5, p6, p7])
|
2018-06-26 12:54:14 +08:00
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-09-04 05:19:29 +08:00
|
|
|
end
|
2013-05-19 04:11:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-08 05:55:42 +08:00
|
|
|
|
|
|
|
context "page_title" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:tag1) { Fabricate(:tag) }
|
|
|
|
fab!(:tag2) { Fabricate(:tag, topic_count: 2) }
|
2020-12-17 08:19:13 +08:00
|
|
|
fab!(:op_post) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:post1) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:whisper) { Fabricate(:post, topic: topic, post_type: Post.types[:whisper]) }
|
2017-02-08 05:55:42 +08:00
|
|
|
|
2018-01-27 20:51:22 +08:00
|
|
|
subject { TopicView.new(topic.id, evil_trout).page_title }
|
2017-02-08 05:55:42 +08:00
|
|
|
|
2020-12-17 08:19:13 +08:00
|
|
|
context "when a post number is specified" do
|
|
|
|
context "admins" do
|
|
|
|
it "see post number and username for all posts" do
|
|
|
|
title = TopicView.new(topic.id, admin, post_number: 0).page_title
|
|
|
|
expect(title).to eq(topic.title)
|
|
|
|
title = TopicView.new(topic.id, admin, post_number: 1).page_title
|
|
|
|
expect(title).to eq(topic.title)
|
|
|
|
|
|
|
|
title = TopicView.new(topic.id, admin, post_number: 2).page_title
|
|
|
|
expect(title).to eq("#{topic.title} - #2 by #{post1.user.username}")
|
|
|
|
title = TopicView.new(topic.id, admin, post_number: 3).page_title
|
|
|
|
expect(title).to eq("#{topic.title} - #3 by #{whisper.user.username}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "regular users" do
|
|
|
|
it "see post number and username for regular posts" do
|
|
|
|
title = TopicView.new(topic.id, evil_trout, post_number: 0).page_title
|
|
|
|
expect(title).to eq(topic.title)
|
|
|
|
title = TopicView.new(topic.id, evil_trout, post_number: 1).page_title
|
|
|
|
expect(title).to eq(topic.title)
|
|
|
|
|
|
|
|
title = TopicView.new(topic.id, evil_trout, post_number: 2).page_title
|
|
|
|
expect(title).to eq("#{topic.title} - #2 by #{post1.user.username}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "see only post number for whisper posts" do
|
|
|
|
title = TopicView.new(topic.id, evil_trout, post_number: 3).page_title
|
|
|
|
expect(title).to eq("#{topic.title} - #3")
|
|
|
|
post2 = Fabricate(:post, topic: topic)
|
|
|
|
topic.reload
|
|
|
|
title = TopicView.new(topic.id, evil_trout, post_number: 3).page_title
|
|
|
|
expect(title).to eq("#{topic.title} - #3")
|
|
|
|
title = TopicView.new(topic.id, evil_trout, post_number: 4).page_title
|
|
|
|
expect(title).to eq("#{topic.title} - #4 by #{post2.user.username}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-08 05:55:42 +08:00
|
|
|
context "uncategorized topic" do
|
|
|
|
context "topic_page_title_includes_category is false" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = false }
|
|
|
|
it { should eq(topic.title) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "topic_page_title_includes_category is true" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = true }
|
|
|
|
it { should eq(topic.title) }
|
|
|
|
|
|
|
|
context "tagged topic" do
|
|
|
|
before { topic.tags << [tag1, tag2] }
|
|
|
|
|
|
|
|
context "tagging enabled" do
|
|
|
|
before { SiteSetting.tagging_enabled = true }
|
|
|
|
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should_not include(tag1.name) }
|
|
|
|
it { should end_with(tag2.name) } # tag2 has higher topic count
|
|
|
|
end
|
|
|
|
|
|
|
|
context "tagging disabled" do
|
|
|
|
before { SiteSetting.tagging_enabled = false }
|
|
|
|
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should_not include(tag1.name) }
|
|
|
|
it { should_not include(tag2.name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "categorized topic" do
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
|
2019-04-29 15:32:25 +08:00
|
|
|
before { topic.update(category_id: category.id) }
|
2017-02-08 05:55:42 +08:00
|
|
|
|
|
|
|
context "topic_page_title_includes_category is false" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = false }
|
|
|
|
it { should eq(topic.title) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "topic_page_title_includes_category is true" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = true }
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should end_with(category.name) }
|
|
|
|
|
|
|
|
context "tagged topic" do
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
topic.tags << [tag1, tag2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should end_with(category.name) }
|
|
|
|
it { should_not include(tag1.name) }
|
|
|
|
it { should_not include(tag2.name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-20 16:24:09 +08:00
|
|
|
|
|
|
|
describe '#filtered_post_stream' do
|
2021-07-13 00:35:24 +08:00
|
|
|
let!(:post) { Fabricate(:post, topic: topic, user: first_poster, created_at: 18.hours.ago) }
|
|
|
|
let!(:post2) { Fabricate(:post, topic: topic, user: evil_trout, created_at: 6.hours.ago) }
|
2018-06-20 16:24:09 +08:00
|
|
|
let!(:post3) { Fabricate(:post, topic: topic, user: first_poster) }
|
|
|
|
|
|
|
|
it 'should return the right columns' do
|
|
|
|
expect(topic_view.filtered_post_stream).to eq([
|
2021-07-13 00:35:24 +08:00
|
|
|
[post.id, 1],
|
2018-06-27 11:11:22 +08:00
|
|
|
[post2.id, 0],
|
|
|
|
[post3.id, 0]
|
2018-06-20 16:24:09 +08:00
|
|
|
])
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for mega topics' do
|
|
|
|
it 'should return the right columns' do
|
2021-06-21 09:50:52 +08:00
|
|
|
stub_const(TopicView, "MEGA_TOPIC_POSTS_COUNT", 2) do
|
2018-06-20 16:58:52 +08:00
|
|
|
expect(topic_view.filtered_post_stream).to eq([
|
2018-06-27 11:11:22 +08:00
|
|
|
post.id,
|
|
|
|
post2.id,
|
|
|
|
post3.id
|
2018-06-20 16:58:52 +08:00
|
|
|
])
|
|
|
|
end
|
2018-06-20 16:24:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-27 11:11:22 +08:00
|
|
|
|
2018-06-27 12:33:57 +08:00
|
|
|
describe '#filtered_post_id' do
|
2018-06-27 11:11:22 +08:00
|
|
|
it 'should return the right id' do
|
|
|
|
post = Fabricate(:post, topic: topic)
|
|
|
|
|
2018-06-27 12:33:57 +08:00
|
|
|
expect(topic_view.filtered_post_id(nil)).to eq(nil)
|
|
|
|
expect(topic_view.filtered_post_id(post.post_number)).to eq(post.id)
|
2018-06-27 11:11:22 +08:00
|
|
|
end
|
|
|
|
end
|
2018-07-11 15:41:26 +08:00
|
|
|
|
2021-10-18 14:47:47 +08:00
|
|
|
describe '#last_post_id' do
|
2018-07-11 15:41:26 +08:00
|
|
|
let!(:p3) { Fabricate(:post, topic: topic) }
|
|
|
|
let!(:p2) { Fabricate(:post, topic: topic) }
|
|
|
|
let!(:p1) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
[p1, p2, p3].each_with_index do |post, index|
|
|
|
|
post.update!(sort_order: index + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right id' do
|
|
|
|
expect(topic_view.last_post_id).to eq(p3.id)
|
|
|
|
end
|
|
|
|
end
|
2019-07-19 23:15:38 +08:00
|
|
|
|
|
|
|
describe '#read_time' do
|
|
|
|
let!(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
PostCreator.create!(Discourse.system_user, topic_id: topic.id, raw: "![image|100x100](upload://upload.png)")
|
|
|
|
topic_view.topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right read time' do
|
|
|
|
SiteSetting.read_time_word_count = 500
|
|
|
|
expect(topic_view.read_time).to eq(1)
|
|
|
|
|
|
|
|
SiteSetting.read_time_word_count = 0
|
|
|
|
expect(topic_view.read_time).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2019-12-20 21:17:14 +08:00
|
|
|
|
|
|
|
describe '#image_url' do
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 16:07:50 +08:00
|
|
|
fab!(:op_upload) { Fabricate(:image_upload) }
|
|
|
|
fab!(:post3_upload) { Fabricate(:image_upload) }
|
|
|
|
|
|
|
|
fab!(:post1) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:post2) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:post3) { Fabricate(:post, topic: topic).tap { |p| p.update_column(:image_upload_id, post3_upload.id) }.reload }
|
2019-12-20 21:17:14 +08:00
|
|
|
|
|
|
|
def topic_view_for_post(post_number)
|
|
|
|
TopicView.new(topic.id, evil_trout, post_number: post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when op has an image" do
|
|
|
|
before do
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 16:07:50 +08:00
|
|
|
topic.update_column(:image_upload_id, op_upload.id)
|
|
|
|
post1.update_column(:image_upload_id, op_upload.id)
|
2019-12-20 21:17:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the topic image as a fallback when posts have no image" do
|
2020-05-15 20:35:20 +08:00
|
|
|
expect(topic_view_for_post(1).image_url).to end_with(op_upload.url)
|
|
|
|
expect(topic_view_for_post(2).image_url).to end_with(op_upload.url)
|
|
|
|
expect(topic_view_for_post(3).image_url).to end_with(post3_upload.url)
|
2019-12-20 21:17:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when op has no image" do
|
|
|
|
it "returns nil when posts have no image" do
|
|
|
|
expect(topic_view_for_post(1).image_url).to eq(nil)
|
|
|
|
expect(topic_view_for_post(2).image_url).to eq(nil)
|
2020-05-15 20:35:20 +08:00
|
|
|
expect(topic_view_for_post(3).image_url).to end_with(post3_upload.url)
|
2019-12-20 21:17:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-14 01:13:37 +08:00
|
|
|
|
|
|
|
describe '#show_read_indicator?' do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:pm_topic) { Fabricate(:private_message_topic) }
|
|
|
|
|
|
|
|
it "shows read indicator for private messages" do
|
|
|
|
group = Fabricate(:group, users: [admin], publish_read_state: true)
|
|
|
|
pm_topic.topic_allowed_groups = [Fabricate.build(:topic_allowed_group, group: group)]
|
|
|
|
|
|
|
|
topic_view = TopicView.new(pm_topic.id, admin)
|
|
|
|
expect(topic_view.show_read_indicator?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not show read indicator if groups do not have read indicator enabled" do
|
|
|
|
topic_view = TopicView.new(pm_topic.id, admin)
|
|
|
|
expect(topic_view.show_read_indicator?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not show read indicator for topics with allowed groups" do
|
|
|
|
group = Fabricate(:group, users: [admin], publish_read_state: true)
|
|
|
|
topic.topic_allowed_groups = [Fabricate.build(:topic_allowed_group, group: group)]
|
|
|
|
|
|
|
|
topic_view = TopicView.new(topic.id, admin)
|
|
|
|
expect(topic_view.show_read_indicator?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2021-06-22 23:12:39 +08:00
|
|
|
|
|
|
|
describe '#reviewable_counts' do
|
|
|
|
it 'exclude posts queued because the category needs approval' do
|
|
|
|
category = Fabricate.build(:category, user: admin)
|
|
|
|
category.custom_fields[Category::REQUIRE_TOPIC_APPROVAL] = true
|
|
|
|
category.save!
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'to the handler I say enqueue me!',
|
|
|
|
title: 'this is the title of the queued post',
|
|
|
|
category: category.id
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
reviewable = result.reviewable
|
|
|
|
reviewable.perform(admin, :approve_post)
|
|
|
|
|
|
|
|
topic_view = TopicView.new(reviewable.topic, admin)
|
|
|
|
|
|
|
|
expect(topic_view.reviewable_counts).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'include posts queued for other reasons' do
|
|
|
|
Fabricate(:watched_word, word: "darn", action: WatchedWord.actions[:require_approval])
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is darn new post content',
|
|
|
|
title: 'this is the title of the queued post'
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
reviewable = result.reviewable
|
|
|
|
reviewable.perform(admin, :approve_post)
|
|
|
|
|
|
|
|
topic_view = TopicView.new(reviewable.topic, admin)
|
|
|
|
|
|
|
|
expect(topic_view.reviewable_counts.keys).to contain_exactly(reviewable.target_id)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|