2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
require 'topic_view'
|
|
|
|
|
|
|
|
describe TopicQuery do
|
|
|
|
|
2014-03-04 12:46:37 -05:00
|
|
|
let!(:user) { Fabricate(:coding_horror) }
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:creator) { Fabricate(:user) }
|
|
|
|
let(:topic_query) { TopicQuery.new(user) }
|
|
|
|
|
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
2014-02-07 10:08:56 -05:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
context 'secure category' do
|
|
|
|
it "filters categories out correctly" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
group = Fabricate(:group)
|
2013-07-13 21:24:16 -04:00
|
|
|
category.set_permissions(group => :full)
|
2013-04-29 02:33:24 -04:00
|
|
|
category.save
|
|
|
|
|
2018-06-05 03:29:17 -04:00
|
|
|
Fabricate(:topic, category: category)
|
|
|
|
Fabricate(:topic, visible: false)
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(nil).list_latest.topics.count).to eq(0)
|
|
|
|
expect(TopicQuery.new(user).list_latest.topics.count).to eq(0)
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Topic.top_viewed(10).count).to eq(0)
|
|
|
|
expect(Topic.recent(10).count).to eq(0)
|
2013-06-12 20:27:17 -04:00
|
|
|
|
2014-02-07 10:08:56 -05:00
|
|
|
# mods can see hidden topics
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(moderator).list_latest.topics.count).to eq(1)
|
2014-02-07 10:08:56 -05:00
|
|
|
# admins can see all the topics
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(admin).list_latest.topics.count).to eq(3)
|
2013-04-29 02:33:24 -04:00
|
|
|
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(user).list_latest.topics.count).to eq(2)
|
2013-06-12 20:27:17 -04:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-02-15 15:25:43 -05:00
|
|
|
context "custom filters" do
|
|
|
|
it "allows custom filters to be applied" do
|
|
|
|
topic1 = Fabricate(:topic)
|
|
|
|
_topic2 = Fabricate(:topic)
|
|
|
|
|
|
|
|
TopicQuery.add_custom_filter(:only_topic_id) do |results, topic_query|
|
|
|
|
results = results.where('topics.id = ?', topic_query.options[:only_topic_id])
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(TopicQuery.new(nil, only_topic_id: topic1.id).list_latest.topics.map(&:id)).to eq([topic1.id])
|
2017-02-15 15:25:43 -05:00
|
|
|
|
|
|
|
TopicQuery.remove_custom_filter(:only_topic_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-23 18:12:37 -04:00
|
|
|
context "list_topics_by" do
|
|
|
|
|
|
|
|
it "allows users to view their own invisible topics" do
|
2015-06-22 04:09:08 -04:00
|
|
|
_topic = Fabricate(:topic, user: user)
|
|
|
|
_invisible_topic = Fabricate(:topic, user: user, visible: false)
|
2015-03-23 18:12:37 -04:00
|
|
|
|
|
|
|
expect(TopicQuery.new(nil).list_topics_by(user).topics.count).to eq(1)
|
|
|
|
expect(TopicQuery.new(user).list_topics_by(user).topics.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-10 18:25:29 -04:00
|
|
|
context "prioritize_pinned_topics" do
|
|
|
|
|
|
|
|
it "does the pagination correctly" do
|
|
|
|
|
|
|
|
num_topics = 15
|
|
|
|
per_page = 3
|
|
|
|
|
|
|
|
topics = []
|
|
|
|
(num_topics - 1).downto(0).each do |i|
|
2017-07-27 21:20:09 -04:00
|
|
|
topics[i] = Fabricate(:topic)
|
2017-07-10 18:25:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
topic_query = TopicQuery.new(user)
|
|
|
|
results = topic_query.send(:default_results)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(topic_query.prioritize_pinned_topics(results,
|
|
|
|
per_page: per_page,
|
|
|
|
page: 0)
|
|
|
|
).to eq(topics[0...per_page])
|
|
|
|
|
|
|
|
expect(topic_query.prioritize_pinned_topics(results,
|
|
|
|
per_page: per_page,
|
|
|
|
page: 1)
|
|
|
|
).to eq(topics[per_page...num_topics])
|
2017-07-10 18:25:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-01-07 02:20:10 -05:00
|
|
|
context 'bookmarks' do
|
|
|
|
it "filters and returns bookmarks correctly" do
|
|
|
|
post = Fabricate(:post)
|
2018-06-05 03:29:17 -04:00
|
|
|
reply = Fabricate(:post, topic: post.topic)
|
2015-01-07 02:20:10 -05:00
|
|
|
|
|
|
|
post2 = Fabricate(:post)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.create(user, post, :bookmark)
|
|
|
|
PostActionCreator.create(user, reply, :bookmark)
|
2015-01-07 02:20:10 -05:00
|
|
|
TopicUser.change(user, post.topic, notification_level: 1)
|
|
|
|
TopicUser.change(user, post2.topic, notification_level: 1)
|
|
|
|
|
|
|
|
query = TopicQuery.new(user, filter: 'bookmarked').list_latest
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(query.topics.length).to eq(1)
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(query.topics.first.user_data.post_action_data).to eq(PostActionType.types[:bookmark] => [1, 2])
|
2015-01-07 02:20:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-19 17:46:55 -05:00
|
|
|
context 'deleted filter' do
|
|
|
|
it "filters deleted topics correctly" do
|
2015-01-07 02:20:10 -05:00
|
|
|
_topic = Fabricate(:topic, deleted_at: 1.year.ago)
|
2014-11-19 17:46:55 -05:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(admin, status: 'deleted').list_latest.topics.size).to eq(1)
|
|
|
|
expect(TopicQuery.new(moderator, status: 'deleted').list_latest.topics.size).to eq(1)
|
|
|
|
expect(TopicQuery.new(user, status: 'deleted').list_latest.topics.size).to eq(0)
|
|
|
|
expect(TopicQuery.new(nil, status: 'deleted').list_latest.topics.size).to eq(0)
|
2014-11-19 17:46:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-31 16:10:54 -04:00
|
|
|
context 'category filter' do
|
|
|
|
let(:category) { Fabricate(:category) }
|
2013-12-13 17:18:28 -05:00
|
|
|
|
2014-10-08 12:44:47 -04:00
|
|
|
let(:diff_category) { Fabricate(:diff_category) }
|
2013-10-31 16:10:54 -04:00
|
|
|
|
|
|
|
it "returns topics in the category when we filter to it" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(moderator).list_latest.topics.size).to eq(0)
|
2013-10-31 16:10:54 -04:00
|
|
|
|
|
|
|
# Filter by slug
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(moderator, category: category.slug).list_latest.topics.size).to eq(1)
|
|
|
|
expect(TopicQuery.new(moderator, category: "#{category.id}-category").list_latest.topics.size).to eq(1)
|
2014-10-08 12:44:47 -04:00
|
|
|
|
|
|
|
list = TopicQuery.new(moderator, category: diff_category.slug).list_latest
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(list.topics.size).to eq(1)
|
|
|
|
expect(list.preload_key).to eq("topic_list_c/different-category/l/latest")
|
2013-12-13 17:18:28 -05:00
|
|
|
|
|
|
|
# Defaults to no category filter when slug does not exist
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(moderator, category: 'made up slug').list_latest.topics.size).to eq(2)
|
2013-10-31 16:10:54 -04:00
|
|
|
end
|
|
|
|
|
2013-12-13 17:18:28 -05:00
|
|
|
context 'subcategories' do
|
2017-07-27 21:20:09 -04:00
|
|
|
let!(:subcategory) { Fabricate(:category, parent_category_id: category.id) }
|
2013-12-13 17:18:28 -05:00
|
|
|
|
|
|
|
it "works with subcategories" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(moderator, category: category.id).list_latest.topics.size).to eq(1)
|
|
|
|
expect(TopicQuery.new(moderator, category: subcategory.id).list_latest.topics.size).to eq(1)
|
|
|
|
expect(TopicQuery.new(moderator, category: category.id, no_subcategories: true).list_latest.topics.size).to eq(1)
|
2013-12-13 17:18:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'tag filter' do
|
|
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
let(:other_tag) { Fabricate(:tag) }
|
2018-10-05 05:23:52 -04:00
|
|
|
let(:uppercase_tag) { Fabricate(:tag, name: "HeLlO") }
|
2016-05-04 14:02:47 -04:00
|
|
|
|
2016-05-26 18:03:36 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
end
|
|
|
|
|
2016-07-20 16:21:43 -04:00
|
|
|
context "no category filter" do
|
2017-07-27 21:20:09 -04:00
|
|
|
let!(:tagged_topic1) { Fabricate(:topic, tags: [tag]) }
|
|
|
|
let!(:tagged_topic2) { Fabricate(:topic, tags: [other_tag]) }
|
|
|
|
let!(:tagged_topic3) { Fabricate(:topic, tags: [tag, other_tag]) }
|
2018-10-05 05:23:52 -04:00
|
|
|
let!(:tagged_topic4) { Fabricate(:topic, tags: [uppercase_tag]) }
|
2016-07-20 16:21:43 -04:00
|
|
|
let!(:no_tags_topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
it "returns topics with the tag when filtered to it" do
|
2018-10-07 23:25:41 -04:00
|
|
|
expect(TopicQuery.new(moderator, tags: tag.name).list_latest.topics)
|
2018-03-19 01:39:29 -04:00
|
|
|
.to contain_exactly(tagged_topic1, tagged_topic3)
|
2016-07-20 16:21:43 -04:00
|
|
|
|
2018-03-19 01:39:29 -04:00
|
|
|
expect(TopicQuery.new(moderator, tags: [tag.id]).list_latest.topics)
|
|
|
|
.to contain_exactly(tagged_topic1, tagged_topic3)
|
2016-07-20 16:21:43 -04:00
|
|
|
|
2018-03-19 01:39:29 -04:00
|
|
|
expect(TopicQuery.new(
|
|
|
|
moderator, tags: [tag.name, other_tag.name]
|
|
|
|
).list_latest.topics).to contain_exactly(
|
|
|
|
tagged_topic1, tagged_topic2, tagged_topic3
|
|
|
|
)
|
2016-07-20 16:21:43 -04:00
|
|
|
|
2018-03-19 01:39:29 -04:00
|
|
|
expect(TopicQuery.new(moderator, tags: [tag.id, other_tag.id]).list_latest.topics)
|
|
|
|
.to contain_exactly(tagged_topic1, tagged_topic2, tagged_topic3)
|
2018-10-05 05:23:52 -04:00
|
|
|
|
|
|
|
expect(TopicQuery.new(moderator, tags: ["hElLo"]).list_latest.topics)
|
|
|
|
.to contain_exactly(tagged_topic4)
|
2016-07-20 16:21:43 -04:00
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
|
2016-08-11 01:38:16 -04:00
|
|
|
it "can return topics with all specified tags" do
|
|
|
|
expect(TopicQuery.new(moderator, tags: [tag.name, other_tag.name], match_all_tags: true).list_latest.topics.map(&:id)).to eq([tagged_topic3.id])
|
|
|
|
end
|
|
|
|
|
2016-08-15 15:42:06 -04:00
|
|
|
it "returns an empty relation when an invalid tag is passed" do
|
|
|
|
expect(TopicQuery.new(moderator, tags: [tag.name, 'notatag'], match_all_tags: true).list_latest.topics).to be_empty
|
|
|
|
end
|
|
|
|
|
2016-07-20 16:21:43 -04:00
|
|
|
it "can return topics with no tags" do
|
|
|
|
expect(TopicQuery.new(moderator, no_tags: true).list_latest.topics.map(&:id)).to eq([no_tags_topic.id])
|
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "and categories too" do
|
|
|
|
let(:category1) { Fabricate(:category) }
|
|
|
|
let(:category2) { Fabricate(:category) }
|
|
|
|
|
|
|
|
it "returns topics in the given category with the given tag" do
|
2017-07-27 21:20:09 -04:00
|
|
|
tagged_topic1 = Fabricate(:topic, category: category1, tags: [tag])
|
2018-10-28 19:47:59 -04:00
|
|
|
_tagged_topic2 = Fabricate(:topic, category: category2, tags: [tag])
|
2017-07-27 21:20:09 -04:00
|
|
|
tagged_topic3 = Fabricate(:topic, category: category1, tags: [tag, other_tag])
|
2018-10-28 19:47:59 -04:00
|
|
|
_no_tags_topic = Fabricate(:topic, category: category1)
|
2016-05-04 14:02:47 -04:00
|
|
|
|
|
|
|
expect(TopicQuery.new(moderator, category: category1.id, tags: [tag.name]).list_latest.topics.map(&:id).sort).to eq([tagged_topic1.id, tagged_topic3.id].sort)
|
|
|
|
expect(TopicQuery.new(moderator, category: category2.id, tags: [other_tag.name]).list_latest.topics.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2013-10-31 16:10:54 -04:00
|
|
|
end
|
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
context 'muted categories' do
|
|
|
|
it 'is removed from new and latest lists' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
CategoryUser.create!(user_id: user.id,
|
|
|
|
category_id: category.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:muted])
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_query.list_new.topics.map(&:id)).not_to include(topic.id)
|
|
|
|
expect(topic_query.list_latest.topics.map(&:id)).not_to include(topic.id)
|
2014-02-03 00:05:49 -05:00
|
|
|
end
|
|
|
|
end
|
2013-10-31 16:10:54 -04:00
|
|
|
|
2016-08-04 11:54:39 -04:00
|
|
|
context 'muted tags' do
|
|
|
|
it 'is removed from new and latest lists' do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.remove_muted_tags_from_latest = true
|
|
|
|
|
|
|
|
muted_tag, other_tag = Fabricate(:tag), Fabricate(:tag)
|
|
|
|
|
|
|
|
muted_topic = Fabricate(:topic, tags: [muted_tag])
|
|
|
|
tagged_topic = Fabricate(:topic, tags: [other_tag])
|
|
|
|
untagged_topic = Fabricate(:topic)
|
|
|
|
|
|
|
|
TagUser.create!(user_id: user.id,
|
|
|
|
tag_id: muted_tag.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:muted])
|
|
|
|
|
|
|
|
topic_ids = topic_query.list_latest.topics.map(&:id)
|
|
|
|
expect(topic_ids).not_to include(muted_topic.id)
|
|
|
|
expect(topic_ids).to include(tagged_topic.id)
|
|
|
|
expect(topic_ids).to include(untagged_topic.id)
|
|
|
|
|
|
|
|
topic_ids = topic_query.list_new.topics.map(&:id)
|
|
|
|
expect(topic_ids).not_to include(muted_topic.id)
|
|
|
|
expect(topic_ids).to include(tagged_topic.id)
|
|
|
|
expect(topic_ids).to include(untagged_topic.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'a bunch of topics' do
|
2013-11-13 14:17:06 -05:00
|
|
|
let!(:regular_topic) do
|
|
|
|
Fabricate(:topic, title: 'this is a regular topic',
|
|
|
|
user: creator,
|
|
|
|
views: 100,
|
|
|
|
like_count: 66,
|
|
|
|
posts_count: 3,
|
2013-11-14 15:50:36 -05:00
|
|
|
participant_count: 11,
|
2013-11-13 14:17:06 -05:00
|
|
|
bumped_at: 15.minutes.ago)
|
|
|
|
end
|
|
|
|
let!(:pinned_topic) do
|
|
|
|
Fabricate(:topic, title: 'this is a pinned topic',
|
|
|
|
user: creator,
|
|
|
|
views: 10,
|
|
|
|
like_count: 100,
|
|
|
|
posts_count: 5,
|
2013-11-14 15:50:36 -05:00
|
|
|
participant_count: 12,
|
2013-11-13 14:17:06 -05:00
|
|
|
pinned_at: 10.minutes.ago,
|
2014-04-07 02:38:51 -04:00
|
|
|
pinned_globally: true,
|
2013-11-13 14:17:06 -05:00
|
|
|
bumped_at: 10.minutes.ago)
|
|
|
|
end
|
|
|
|
let!(:archived_topic) do
|
|
|
|
Fabricate(:topic, title: 'this is an archived topic',
|
|
|
|
user: creator,
|
|
|
|
views: 50,
|
|
|
|
like_count: 30,
|
|
|
|
posts_count: 4,
|
|
|
|
archived: true,
|
2013-11-14 15:50:36 -05:00
|
|
|
participant_count: 1,
|
2013-11-13 14:17:06 -05:00
|
|
|
bumped_at: 6.minutes.ago)
|
|
|
|
end
|
|
|
|
let!(:invisible_topic) do
|
|
|
|
Fabricate(:topic, title: 'this is an invisible topic',
|
|
|
|
user: creator,
|
|
|
|
views: 1,
|
|
|
|
like_count: 5,
|
|
|
|
posts_count: 2,
|
|
|
|
visible: false,
|
2013-11-14 15:50:36 -05:00
|
|
|
participant_count: 3,
|
2013-11-13 14:17:06 -05:00
|
|
|
bumped_at: 5.minutes.ago)
|
|
|
|
end
|
|
|
|
let!(:closed_topic) do
|
|
|
|
Fabricate(:topic, title: 'this is a closed topic',
|
|
|
|
user: creator,
|
|
|
|
views: 2,
|
|
|
|
like_count: 1,
|
|
|
|
posts_count: 1,
|
|
|
|
closed: true,
|
2013-11-14 15:50:36 -05:00
|
|
|
participant_count: 2,
|
2013-11-13 14:17:06 -05:00
|
|
|
bumped_at: 1.minute.ago)
|
|
|
|
end
|
2014-08-12 03:51:54 -04:00
|
|
|
let!(:future_topic) do
|
|
|
|
Fabricate(:topic, title: 'this is a topic in far future',
|
|
|
|
user: creator,
|
|
|
|
views: 30,
|
|
|
|
like_count: 11,
|
|
|
|
posts_count: 6,
|
|
|
|
participant_count: 5,
|
|
|
|
bumped_at: 1000.years.from_now)
|
|
|
|
end
|
2013-11-13 14:17:06 -05:00
|
|
|
|
2013-03-27 16:17:49 -04:00
|
|
|
let(:topics) { topic_query.list_latest.topics }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-27 16:17:49 -04:00
|
|
|
context 'list_latest' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it "returns the topics in the correct order" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics.map(&:id)).to eq([pinned_topic, future_topic, closed_topic, archived_topic, regular_topic].map(&:id))
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# includes the invisible topic if you're a moderator
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(moderator).list_latest.topics.include?(invisible_topic)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# includes the invisible topic if you're an admin" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new(admin).list_latest.topics.include?(invisible_topic)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-11-13 14:17:06 -05:00
|
|
|
|
|
|
|
context 'sort_order' do
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def ids_in_order(order, descending = true)
|
2014-04-16 12:05:54 -04:00
|
|
|
TopicQuery.new(admin, order: order, ascending: descending ? 'false' : 'true').list_latest.topics.map(&:id)
|
2013-11-13 14:17:06 -05:00
|
|
|
end
|
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
it "returns the topics in correct order" do
|
|
|
|
# returns the topics in likes order if requested
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('posts')).to eq([future_topic, pinned_topic, archived_topic, regular_topic, invisible_topic, closed_topic].map(&:id))
|
2013-11-13 14:17:06 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# returns the topics in reverse likes order if requested
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('posts', false)).to eq([closed_topic, invisible_topic, regular_topic, archived_topic, pinned_topic, future_topic].map(&:id))
|
2013-11-13 14:17:06 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# returns the topics in likes order if requested
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('likes')).to eq([pinned_topic, regular_topic, archived_topic, future_topic, invisible_topic, closed_topic].map(&:id))
|
2013-11-13 14:17:06 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# returns the topics in reverse likes order if requested
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('likes', false)).to eq([closed_topic, invisible_topic, future_topic, archived_topic, regular_topic, pinned_topic].map(&:id))
|
2013-11-13 14:17:06 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# returns the topics in views order if requested
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('views')).to eq([regular_topic, archived_topic, future_topic, pinned_topic, closed_topic, invisible_topic].map(&:id))
|
2013-11-13 14:17:06 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# returns the topics in reverse views order if requested" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('views', false)).to eq([invisible_topic, closed_topic, pinned_topic, future_topic, archived_topic, regular_topic].map(&:id))
|
2013-11-13 14:17:06 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# returns the topics in posters order if requested" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('posters')).to eq([pinned_topic, regular_topic, future_topic, invisible_topic, closed_topic, archived_topic].map(&:id))
|
2013-11-14 15:50:36 -05:00
|
|
|
|
2014-02-03 00:05:49 -05:00
|
|
|
# returns the topics in reverse posters order if requested" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ids_in_order('posters', false)).to eq([archived_topic, closed_topic, invisible_topic, future_topic, regular_topic, pinned_topic].map(&:id))
|
2016-05-16 05:31:39 -04:00
|
|
|
|
2016-02-25 11:22:23 -05:00
|
|
|
# sets a custom field for each topic to emulate a plugin
|
|
|
|
regular_topic.custom_fields["sheep"] = 26
|
|
|
|
pinned_topic.custom_fields["sheep"] = 47
|
|
|
|
archived_topic.custom_fields["sheep"] = 69
|
|
|
|
invisible_topic.custom_fields["sheep"] = 12
|
|
|
|
closed_topic.custom_fields["sheep"] = 31
|
|
|
|
future_topic.custom_fields["sheep"] = 53
|
2016-05-16 05:31:39 -04:00
|
|
|
|
2016-02-25 11:22:23 -05:00
|
|
|
regular_topic.save
|
|
|
|
pinned_topic.save
|
|
|
|
archived_topic.save
|
|
|
|
invisible_topic.save
|
|
|
|
closed_topic.save
|
|
|
|
future_topic.save
|
|
|
|
|
|
|
|
# adds the custom field as a viable sort option
|
|
|
|
class ::TopicQuery
|
|
|
|
SORTABLE_MAPPING["sheep"] = "custom_fields.sheep"
|
|
|
|
end
|
|
|
|
# returns the topics in the sheep order if requested" do
|
|
|
|
expect(ids_in_order('sheep')).to eq([archived_topic, future_topic, pinned_topic, closed_topic, regular_topic, invisible_topic].map(&:id))
|
|
|
|
|
|
|
|
# returns the topics in reverse sheep order if requested" do
|
|
|
|
expect(ids_in_order('sheep', false)).to eq([invisible_topic, regular_topic, closed_topic, pinned_topic, future_topic, archived_topic].map(&:id))
|
2016-05-16 05:31:39 -04:00
|
|
|
|
2013-11-14 15:50:36 -05:00
|
|
|
end
|
|
|
|
|
2013-11-13 14:17:06 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-06 15:17:07 -05:00
|
|
|
context 'after clearring a pinned topic' do
|
|
|
|
before do
|
|
|
|
pinned_topic.clear_pin_for(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "no longer shows the pinned topic at the top" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to eq([future_topic, closed_topic, archived_topic, pinned_topic, regular_topic])
|
2013-03-06 15:17:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'categorized' do
|
2013-02-25 11:42:20 -05:00
|
|
|
let(:category) { Fabricate(:category) }
|
2013-03-07 12:45:49 -05:00
|
|
|
let(:topic_category) { category.topic }
|
2013-02-25 11:42:20 -05:00
|
|
|
let!(:topic_no_cat) { Fabricate(:topic) }
|
2015-02-24 22:39:50 -05:00
|
|
|
let!(:topic_in_cat1) { Fabricate(:topic, category: category,
|
|
|
|
bumped_at: 10.minutes.ago,
|
|
|
|
created_at: 10.minutes.ago) }
|
|
|
|
let!(:topic_in_cat2) { Fabricate(:topic, category: category) }
|
2013-02-27 22:36:12 -05:00
|
|
|
|
|
|
|
describe '#list_new_in_category' do
|
2013-03-07 12:45:49 -05:00
|
|
|
it 'returns the topic category and the categorized topic' do
|
2015-02-24 22:39:50 -05:00
|
|
|
expect(
|
|
|
|
topic_query.list_new_in_category(category).topics.map(&:id)
|
|
|
|
).to eq([topic_in_cat2.id, topic_category.id, topic_in_cat1.id])
|
2013-02-27 22:36:12 -05:00
|
|
|
end
|
|
|
|
end
|
2016-11-01 12:18:31 -04:00
|
|
|
|
|
|
|
describe "category default sort order" do
|
|
|
|
it "can use category's default sort order" do
|
|
|
|
category.update_attributes!(sort_order: 'created', sort_ascending: true)
|
|
|
|
topic_ids = TopicQuery.new(user, category: category.id).list_latest.topics.map(&:id)
|
|
|
|
expect(topic_ids - [topic_category.id]).to eq([topic_in_cat1.id, topic_in_cat2.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores invalid order value" do
|
|
|
|
category.update_attributes!(sort_order: 'funny')
|
|
|
|
topic_ids = TopicQuery.new(user, category: category.id).list_latest.topics.map(&:id)
|
|
|
|
expect(topic_ids - [topic_category.id]).to eq([topic_in_cat2.id, topic_in_cat1.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be overridden" do
|
|
|
|
category.update_attributes!(sort_order: 'created', sort_ascending: true)
|
|
|
|
topic_ids = TopicQuery.new(user, category: category.id, order: 'activity').list_latest.topics.map(&:id)
|
|
|
|
expect(topic_ids - [topic_category.id]).to eq([topic_in_cat2.id, topic_in_cat1.id])
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'unread / read topics' do
|
|
|
|
|
|
|
|
context 'with no data' do
|
|
|
|
it "has no unread topics" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_query.list_unread.topics).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-02 01:03:31 -05:00
|
|
|
context 'with whispers' do
|
|
|
|
|
|
|
|
it 'correctly shows up in unread for staff' do
|
|
|
|
|
|
|
|
first = create_post(raw: 'this is the first post', title: 'super amazing title')
|
|
|
|
|
|
|
|
_whisper = create_post(topic_id: first.topic.id,
|
2017-07-27 21:20:09 -04:00
|
|
|
post_type: Post.types[:whisper],
|
|
|
|
raw: 'this is a whispered reply')
|
2016-12-02 01:03:31 -05:00
|
|
|
|
|
|
|
topic_id = first.topic.id
|
|
|
|
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, topic_id, first.post_number, 1, 1)
|
|
|
|
TopicUser.update_last_read(admin, topic_id, first.post_number, 1, 1)
|
2016-12-02 01:03:31 -05:00
|
|
|
|
|
|
|
TopicUser.change(user.id, topic_id, notification_level: TopicUser.notification_levels[:tracking])
|
|
|
|
TopicUser.change(admin.id, topic_id, notification_level: TopicUser.notification_levels[:tracking])
|
|
|
|
|
|
|
|
expect(TopicQuery.new(user).list_unread.topics).to eq([])
|
|
|
|
expect(TopicQuery.new(admin).list_unread.topics).to eq([first.topic])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'with read data' do
|
|
|
|
let!(:partially_read) { Fabricate(:post, user: creator).topic }
|
|
|
|
let!(:fully_read) { Fabricate(:post, user: creator).topic }
|
|
|
|
|
|
|
|
before do
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, partially_read.id, 0, 0, 0)
|
|
|
|
TopicUser.update_last_read(user, fully_read.id, 1, 1, 0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'list_unread' do
|
2016-12-02 01:03:31 -05:00
|
|
|
it 'lists topics correctly' do
|
2018-10-28 19:47:59 -04:00
|
|
|
_new_topic = Fabricate(:post, user: creator).topic
|
2017-04-26 12:26:37 -04:00
|
|
|
|
2017-11-19 22:49:09 -05:00
|
|
|
expect(topic_query.list_unread.topics).to eq([])
|
|
|
|
expect(topic_query.list_read.topics).to match_array([fully_read, partially_read])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
context 'user with auto_track_topics list_unread' do
|
|
|
|
before do
|
2016-02-18 00:57:22 -05:00
|
|
|
user.user_option.auto_track_topics_after_msecs = 0
|
|
|
|
user.user_option.save
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'only contains the partially read topic' do
|
2017-11-19 22:49:09 -05:00
|
|
|
expect(topic_query.list_unread.topics).to eq([partially_read])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'list_new' do
|
|
|
|
|
|
|
|
context 'without a new topic' do
|
|
|
|
it "has no new topics" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_query.list_new.topics).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-05 02:01:52 -04:00
|
|
|
context 'preload api' do
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:topics) {}
|
2015-08-05 02:01:52 -04:00
|
|
|
|
|
|
|
it "preloads data correctly" do
|
|
|
|
TopicList.preloaded_custom_fields << "tag"
|
|
|
|
TopicList.preloaded_custom_fields << "age"
|
|
|
|
TopicList.preloaded_custom_fields << "foo"
|
|
|
|
|
|
|
|
topic = Fabricate.build(:topic, user: creator, bumped_at: 10.minutes.ago)
|
2017-07-27 21:20:09 -04:00
|
|
|
topic.custom_fields["tag"] = ["a", "b", "c"]
|
2015-08-05 02:01:52 -04:00
|
|
|
topic.custom_fields["age"] = 22
|
|
|
|
topic.save
|
|
|
|
|
|
|
|
new_topic = topic_query.list_new.topics.first
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(new_topic.custom_fields["tag"].sort).to eq(["a", "b", "c"])
|
2015-08-05 02:01:52 -04:00
|
|
|
expect(new_topic.custom_fields["age"]).to eq("22")
|
|
|
|
|
|
|
|
expect(new_topic.custom_field_preloaded?("tag")).to eq(true)
|
|
|
|
expect(new_topic.custom_field_preloaded?("age")).to eq(true)
|
|
|
|
expect(new_topic.custom_field_preloaded?("foo")).to eq(true)
|
|
|
|
expect(new_topic.custom_field_preloaded?("bar")).to eq(false)
|
|
|
|
|
|
|
|
TopicList.preloaded_custom_fields.clear
|
|
|
|
|
|
|
|
# if we attempt to access non preloaded fields explode
|
2017-07-27 21:20:09 -04:00
|
|
|
expect { new_topic.custom_fields["boom"] }.to raise_error(StandardError)
|
2015-08-05 02:01:52 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'with a new topic' do
|
2013-02-25 11:42:20 -05:00
|
|
|
let!(:new_topic) { Fabricate(:topic, user: creator, bumped_at: 10.minutes.ago) }
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:topics) { topic_query.list_new.topics }
|
|
|
|
|
2013-02-14 01:32:58 -05:00
|
|
|
it "contains no new topics for a user that has missed the window" do
|
2015-08-05 02:01:52 -04:00
|
|
|
|
|
|
|
expect(topic_query.list_new.topics).to eq([new_topic])
|
|
|
|
|
2016-02-18 00:57:22 -05:00
|
|
|
user.user_option.new_topic_duration_minutes = 5
|
|
|
|
user.user_option.save
|
2013-02-14 01:32:58 -05:00
|
|
|
new_topic.created_at = 10.minutes.ago
|
|
|
|
new_topic.save
|
2015-08-05 02:01:52 -04:00
|
|
|
expect(topic_query.list_new.topics).to eq([])
|
2013-02-14 01:32:58 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context "muted topics" do
|
|
|
|
before do
|
|
|
|
new_topic.notify_muted!(user)
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "returns an empty set" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to be_blank
|
2015-11-01 23:05:08 -05:00
|
|
|
expect(topic_query.list_latest.topics).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'un-muted' do
|
|
|
|
before do
|
|
|
|
new_topic.notify_tracking!(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the topic again" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to eq([new_topic])
|
2015-11-01 23:05:08 -05:00
|
|
|
expect(topic_query.list_latest.topics).not_to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'list_posted' do
|
|
|
|
let(:topics) { topic_query.list_posted.topics }
|
|
|
|
|
|
|
|
it "returns blank when there are no posted topics" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'created topics' do
|
2013-07-22 01:06:53 -04:00
|
|
|
let!(:created_topic) { create_post(user: user).topic }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "includes the created topic" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics.include?(created_topic)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "topic you've posted in" do
|
2013-07-22 01:06:53 -04:00
|
|
|
let(:other_users_topic) { create_post(user: creator).topic }
|
2017-07-27 21:20:09 -04:00
|
|
|
let!(:your_post) { create_post(user: user, topic: other_users_topic) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "includes the posted topic" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics.include?(other_users_topic)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-03-30 17:13:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "topic you haven't posted in" do
|
|
|
|
let(:other_users_topic) { create_post(user: creator).topic }
|
|
|
|
|
|
|
|
it "does not include the topic" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to be_blank
|
2014-03-30 17:13:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "but interacted with" do
|
|
|
|
|
|
|
|
it "is not included if read" do
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, other_users_topic.id, 0, 0, 0)
|
2014-03-30 17:13:06 -04:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to be_blank
|
2014-03-30 17:13:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is not included if muted" do
|
|
|
|
other_users_topic.notify_muted!(user)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to be_blank
|
2014-03-30 17:13:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is not included if tracking" do
|
|
|
|
other_users_topic.notify_tracking!(user)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topics).to be_blank
|
2014-03-30 17:13:06 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-11 21:04:30 -05:00
|
|
|
context 'list_related_for do' do
|
2016-02-03 02:50:05 -05:00
|
|
|
|
|
|
|
let(:user) do
|
|
|
|
Fabricate(:admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:sender) do
|
|
|
|
Fabricate(:admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:group_with_user) do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
group
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def create_pm(user, opts = nil)
|
2016-02-03 02:50:05 -05:00
|
|
|
unless opts
|
|
|
|
opts = user
|
|
|
|
user = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
create_post(opts.merge(user: user, archetype: Archetype.private_message)).topic
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def read(user, topic, post_number)
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, topic, post_number, post_number, 10000)
|
2016-02-03 02:50:05 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-02-03 02:50:05 -05:00
|
|
|
it 'returns the correct suggestions' do
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
pm_to_group = create_pm(sender, target_group_names: [group_with_user.name])
|
2016-02-03 02:50:05 -05:00
|
|
|
pm_to_user = create_pm(sender, target_usernames: [user.username])
|
|
|
|
|
|
|
|
old_unrelated_pm = create_pm(target_usernames: [user.username])
|
|
|
|
read(user, old_unrelated_pm, 1)
|
|
|
|
|
|
|
|
related_by_user_pm = create_pm(sender, target_usernames: [user.username])
|
|
|
|
read(user, related_by_user_pm, 1)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
related_by_group_pm = create_pm(sender, target_group_names: [group_with_user.name])
|
2016-02-03 02:50:05 -05:00
|
|
|
read(user, related_by_group_pm, 1)
|
|
|
|
|
2018-11-11 21:04:30 -05:00
|
|
|
expect(TopicQuery.new(user).list_related_for(pm_to_group).topics.map(&:id)).to(
|
2018-10-28 19:47:59 -04:00
|
|
|
eq([related_by_group_pm.id])
|
2016-02-03 02:50:05 -05:00
|
|
|
)
|
|
|
|
|
2018-11-11 21:04:30 -05:00
|
|
|
expect(TopicQuery.new(user).list_related_for(pm_to_user).topics.map(&:id)).to(
|
|
|
|
eq([related_by_user_pm.id])
|
2016-02-03 02:50:05 -05:00
|
|
|
)
|
2018-01-23 12:05:44 -05:00
|
|
|
|
2018-01-31 01:03:12 -05:00
|
|
|
SiteSetting.enable_personal_messages = false
|
2018-11-11 21:04:30 -05:00
|
|
|
expect(TopicQuery.new(user).list_related_for(pm_to_group)).to be_blank
|
|
|
|
expect(TopicQuery.new(user).list_related_for(pm_to_user)).to be_blank
|
2016-02-03 02:50:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'suggested_for' do
|
2016-11-15 04:01:29 -05:00
|
|
|
def clear_cache!
|
2017-07-27 21:20:09 -04:00
|
|
|
$redis.keys('random_topic_cache*').each { |k| $redis.del k }
|
2016-11-15 04:01:29 -05:00
|
|
|
end
|
2015-02-25 02:09:45 -05:00
|
|
|
|
|
|
|
before do
|
2016-11-15 04:01:29 -05:00
|
|
|
clear_cache!
|
2015-02-25 02:09:45 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'when anonymous' do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let!(:new_topic) { Fabricate(:post, user: creator).topic }
|
|
|
|
|
|
|
|
it "should return the new topic" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new.list_suggested_for(topic).topics).to eq([new_topic])
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-19 14:38:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-04 12:26:38 -05:00
|
|
|
context "anonymously browsing with invisible, closed and archived" do
|
2013-02-19 14:38:59 -05:00
|
|
|
let!(:topic) { Fabricate(:topic) }
|
|
|
|
let!(:regular_topic) { Fabricate(:post, user: creator).topic }
|
|
|
|
let!(:closed_topic) { Fabricate(:topic, user: creator, closed: true) }
|
|
|
|
let!(:archived_topic) { Fabricate(:topic, user: creator, archived: true) }
|
|
|
|
let!(:invisible_topic) { Fabricate(:topic, user: creator, visible: false) }
|
|
|
|
|
|
|
|
it "should omit the closed/archived/invisbiel topics from suggested" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicQuery.new.list_suggested_for(topic).topics).to eq([regular_topic])
|
2013-02-19 14:38:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in' do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2015-03-05 01:47:34 -05:00
|
|
|
let(:suggested_topics) {
|
|
|
|
tt = topic
|
|
|
|
# lets clear cache once category is created - working around caching is hard
|
2016-11-15 04:01:29 -05:00
|
|
|
clear_cache!
|
2017-07-27 21:20:09 -04:00
|
|
|
topic_query.list_suggested_for(tt).topics.map { |t| t.id }
|
2015-03-05 01:47:34 -05:00
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "should return empty results when there is nothing to find" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(suggested_topics).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-07-03 20:34:54 -04:00
|
|
|
context 'random suggested' do
|
|
|
|
|
|
|
|
let!(:new_topic) { Fabricate(:topic, created_at: 2.days.ago) }
|
|
|
|
let!(:old_topic) { Fabricate(:topic, created_at: 3.years.ago) }
|
|
|
|
|
|
|
|
it 'respects suggested_topics_max_days_old' do
|
|
|
|
SiteSetting.suggested_topics_max_days_old = 1365
|
|
|
|
tt = topic
|
|
|
|
|
2016-11-15 04:01:29 -05:00
|
|
|
clear_cache!
|
2016-07-03 20:34:54 -04:00
|
|
|
expect(topic_query.list_suggested_for(tt).topics.length).to eq(2)
|
|
|
|
|
|
|
|
SiteSetting.suggested_topics_max_days_old = 365
|
2016-11-15 04:01:29 -05:00
|
|
|
clear_cache!
|
2016-07-03 20:34:54 -04:00
|
|
|
|
|
|
|
expect(topic_query.list_suggested_for(tt).topics.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-09-15 10:45:01 -04:00
|
|
|
context 'with private messages' do
|
|
|
|
let(:group_user) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
let(:another_group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
let!(:topic) do
|
|
|
|
Fabricate(:private_message_topic,
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user)
|
|
|
|
],
|
|
|
|
topic_allowed_groups: [
|
|
|
|
Fabricate.build(:topic_allowed_group, group: group)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:private_message) do
|
|
|
|
Fabricate(:private_message_topic,
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user)
|
|
|
|
],
|
|
|
|
topic_allowed_groups: [
|
|
|
|
Fabricate.build(:topic_allowed_group, group: group),
|
|
|
|
Fabricate.build(:topic_allowed_group, group: another_group),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:private_group_topic) do
|
|
|
|
Fabricate(:private_message_topic,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic_allowed_groups: [
|
|
|
|
Fabricate.build(:topic_allowed_group, group: group)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add(group_user)
|
|
|
|
another_group.add(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'as user not part of group' do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it 'should not return topics by the group user' do
|
|
|
|
expect(suggested_topics).to eq([private_message.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'as user part of group' do
|
|
|
|
let!(:user) { group_user }
|
|
|
|
|
|
|
|
it 'should return the group topics' do
|
|
|
|
expect(suggested_topics).to eq([private_group_topic.id, private_message.id])
|
|
|
|
end
|
|
|
|
end
|
2018-02-22 09:57:02 -05:00
|
|
|
|
|
|
|
context "by tag filter" do
|
|
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
let!(:user) { group_user }
|
|
|
|
|
|
|
|
it 'should return only tagged topics' do
|
|
|
|
Fabricate(:topic_tag, topic: private_message, tag: tag)
|
|
|
|
Fabricate(:topic_tag, topic: private_group_topic)
|
|
|
|
|
|
|
|
expect(TopicQuery.new(user, tags: [tag.name]).list_private_messages_tag(user).topics).to eq([private_message])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2017-09-15 10:45:01 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'with some existing topics' do
|
|
|
|
let!(:partially_read) { Fabricate(:post, user: creator).topic }
|
|
|
|
let!(:new_topic) { Fabricate(:post, user: creator).topic }
|
2013-02-25 11:42:20 -05:00
|
|
|
let!(:fully_read) { Fabricate(:post, user: creator).topic }
|
2013-02-19 14:38:59 -05:00
|
|
|
let!(:closed_topic) { Fabricate(:topic, user: creator, closed: true) }
|
|
|
|
let!(:archived_topic) { Fabricate(:topic, user: creator, archived: true) }
|
|
|
|
let!(:invisible_topic) { Fabricate(:topic, user: creator, visible: false) }
|
2014-02-04 12:26:38 -05:00
|
|
|
let!(:fully_read_closed) { Fabricate(:post, user: creator).topic }
|
|
|
|
let!(:fully_read_archived) { Fabricate(:post, user: creator).topic }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
before do
|
2016-02-18 00:57:22 -05:00
|
|
|
user.user_option.auto_track_topics_after_msecs = 0
|
|
|
|
user.user_option.save
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, partially_read.id, 0, 0, 0)
|
|
|
|
TopicUser.update_last_read(user, fully_read.id, 1, 1, 0)
|
|
|
|
TopicUser.update_last_read(user, fully_read_closed.id, 1, 1, 0)
|
|
|
|
TopicUser.update_last_read(user, fully_read_archived.id, 1, 1, 0)
|
2014-02-04 12:26:38 -05:00
|
|
|
fully_read_closed.closed = true
|
|
|
|
fully_read_closed.save
|
|
|
|
fully_read_archived.archived = true
|
|
|
|
fully_read_archived.save
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-03-02 18:20:42 -05:00
|
|
|
it "returns unread, then new, then random" do
|
|
|
|
SiteSetting.suggested_topics = 7
|
|
|
|
expect(suggested_topics[0]).to eq(partially_read.id)
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(suggested_topics[1, 3]).to include(new_topic.id)
|
|
|
|
expect(suggested_topics[1, 3]).to include(closed_topic.id)
|
|
|
|
expect(suggested_topics[1, 3]).to include(archived_topic.id)
|
2015-03-11 22:44:13 -04:00
|
|
|
|
|
|
|
# The line below appears to randomly fail, no idea why need to restructure test
|
|
|
|
#expect(suggested_topics[4]).to eq(fully_read.id)
|
2015-03-02 18:20:42 -05:00
|
|
|
# random doesn't include closed and archived
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it "won't return new or fully read if there are enough partially read topics" do
|
2015-02-26 17:40:10 -05:00
|
|
|
SiteSetting.suggested_topics = 1
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(suggested_topics).to eq([partially_read.id])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-02-04 12:26:38 -05:00
|
|
|
it "won't return fully read if there are enough partially read topics and new topics" do
|
2015-02-26 17:40:10 -05:00
|
|
|
SiteSetting.suggested_topics = 4
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(suggested_topics[0]).to eq(partially_read.id)
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(suggested_topics[1, 3]).to include(new_topic.id)
|
|
|
|
expect(suggested_topics[1, 3]).to include(closed_topic.id)
|
|
|
|
expect(suggested_topics[1, 3]).to include(archived_topic.id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-14 07:40:28 -04:00
|
|
|
describe '#list_group_topics' do
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
let(:user) do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add(user)
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user2) do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add(user)
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2018-03-16 04:18:26 -04:00
|
|
|
let(:user3) { Fabricate(:user) }
|
|
|
|
|
2018-03-14 07:40:28 -04:00
|
|
|
let(:private_category) do
|
|
|
|
Fabricate(:private_category, group: group)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:private_message_topic) { Fabricate(:private_message_post, user: user).topic }
|
|
|
|
let!(:topic1) { Fabricate(:topic, user: user) }
|
|
|
|
let!(:topic2) { Fabricate(:topic, user: user, category: Fabricate(:category)) }
|
|
|
|
let!(:topic3) { Fabricate(:topic, user: user, category: private_category) }
|
|
|
|
let!(:topic4) { Fabricate(:topic) }
|
|
|
|
let!(:topic5) { Fabricate(:topic, user: user, visible: false) }
|
|
|
|
let!(:topic6) { Fabricate(:topic, user: user2) }
|
|
|
|
|
2018-03-16 04:18:26 -04:00
|
|
|
it 'should return the right lists for anon user' do
|
2018-03-14 07:40:28 -04:00
|
|
|
topics = TopicQuery.new.list_group_topics(group).topics
|
|
|
|
|
|
|
|
expect(topics).to contain_exactly(topic1, topic2, topic6)
|
2018-03-16 04:18:26 -04:00
|
|
|
end
|
2018-03-14 07:40:28 -04:00
|
|
|
|
2018-03-16 04:18:26 -04:00
|
|
|
it 'should retun the right list for users in the same group' do
|
2018-03-14 07:40:28 -04:00
|
|
|
topics = TopicQuery.new(user).list_group_topics(group).topics
|
|
|
|
|
|
|
|
expect(topics).to contain_exactly(topic1, topic2, topic3, topic6)
|
|
|
|
|
|
|
|
topics = TopicQuery.new(user2).list_group_topics(group).topics
|
|
|
|
|
|
|
|
expect(topics).to contain_exactly(topic1, topic2, topic3, topic6)
|
|
|
|
end
|
2018-03-16 04:18:26 -04:00
|
|
|
|
|
|
|
it 'should return the right list for user no in the group' do
|
|
|
|
topics = TopicQuery.new(user3).list_group_topics(group).topics
|
|
|
|
|
|
|
|
expect(topics).to contain_exactly(topic1, topic2, topic6)
|
|
|
|
end
|
2018-03-14 07:40:28 -04:00
|
|
|
end
|
2018-03-19 02:12:01 -04:00
|
|
|
|
|
|
|
describe '#list_private_messages_group' do
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
let!(:group_message) do
|
|
|
|
Fabricate(:private_message_topic,
|
|
|
|
allowed_groups: [group],
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: Fabricate(:user)),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add(creator)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right list for a group user' do
|
|
|
|
topics = TopicQuery.new(nil, group_name: group.name)
|
|
|
|
.list_private_messages_group(creator)
|
|
|
|
.topics
|
|
|
|
|
|
|
|
expect(topics).to contain_exactly(group_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right list for an admin not part of the group' do
|
|
|
|
topics = TopicQuery.new(nil, group_name: group.name)
|
|
|
|
.list_private_messages_group(Fabricate(:admin))
|
|
|
|
.topics
|
|
|
|
|
|
|
|
expect(topics).to contain_exactly(group_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right list for a user not part of the group' do
|
|
|
|
topics = TopicQuery.new(nil, group_name: group.name)
|
|
|
|
.list_private_messages_group(Fabricate(:user))
|
|
|
|
.topics
|
|
|
|
|
|
|
|
expect(topics).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2018-03-26 10:43:30 -04:00
|
|
|
|
|
|
|
context "shared drafts" do
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
let(:shared_drafts_category) { Fabricate(:category) }
|
|
|
|
let!(:topic) { Fabricate(:topic, category: shared_drafts_category) }
|
|
|
|
let!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
shared_drafts_category.set_permissions(group => :full)
|
|
|
|
shared_drafts_category.save
|
|
|
|
SiteSetting.shared_drafts_category = shared_drafts_category.id
|
|
|
|
end
|
|
|
|
|
|
|
|
context "destination_category_id" do
|
|
|
|
it "doesn't allow regular users to query destination_category_id" do
|
|
|
|
list = TopicQuery.new(user, destination_category_id: category.id).list_latest
|
|
|
|
expect(list.topics).not_to include(topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows staff users to query destination_category_id" do
|
|
|
|
list = TopicQuery.new(admin, destination_category_id: category.id).list_latest
|
|
|
|
expect(list.topics).to include(topic)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "latest" do
|
|
|
|
it "doesn't include shared topics unless filtering by category" do
|
|
|
|
list = TopicQuery.new(moderator).list_latest
|
|
|
|
expect(list.topics).not_to include(topic)
|
|
|
|
end
|
2018-12-06 13:59:29 -05:00
|
|
|
|
|
|
|
it "doesn't include shared draft topics for regular users" do
|
|
|
|
group.add(user)
|
|
|
|
SiteSetting.shared_drafts_category = nil
|
|
|
|
list = TopicQuery.new(user).list_latest
|
|
|
|
expect(list.topics).to include(topic)
|
|
|
|
|
|
|
|
SiteSetting.shared_drafts_category = shared_drafts_category.id
|
|
|
|
list = TopicQuery.new(user).list_latest
|
|
|
|
expect(list.topics).not_to include(topic)
|
|
|
|
end
|
2018-03-26 10:43:30 -04:00
|
|
|
end
|
2018-12-07 07:44:23 -05:00
|
|
|
|
|
|
|
context "unread" do
|
|
|
|
let!(:partially_read) do
|
|
|
|
topic = Fabricate(:topic, category: shared_drafts_category)
|
|
|
|
Fabricate(:post, user: creator, topic: topic).topic
|
|
|
|
TopicUser.update_last_read(admin, topic.id, 0, 0, 0)
|
|
|
|
TopicUser.change(admin.id, topic.id, notification_level: TopicUser.notification_levels[:tracking])
|
|
|
|
topic
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove topics from unread' do
|
|
|
|
expect(TopicQuery.new(admin).list_latest.topics).not_to include(partially_read) # Check we set up the topic/category correctly
|
|
|
|
expect(TopicQuery.new(admin).list_unread.topics).to include(partially_read)
|
|
|
|
end
|
|
|
|
end
|
2018-03-26 10:43:30 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|