2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-05-21 14:39:51 +08:00
|
|
|
|
2013-05-29 16:11:04 +08:00
|
|
|
describe TopicTrackingState do
|
2013-05-21 14:39:51 +08:00
|
|
|
|
|
|
|
let(:user) do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:post) do
|
2013-07-22 13:06:53 +08:00
|
|
|
create_post
|
2013-05-21 14:39:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-29 16:11:04 +08:00
|
|
|
it "can correctly publish unread" do
|
|
|
|
# TODO setup stuff and look at messages
|
|
|
|
TopicTrackingState.publish_unread(post)
|
|
|
|
end
|
|
|
|
|
2015-04-17 12:03:08 +08:00
|
|
|
it "correctly handles muted categories" do
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
post
|
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
2015-04-17 12:03:08 +08:00
|
|
|
expect(report.length).to eq(1)
|
|
|
|
|
|
|
|
CategoryUser.create!(user_id: user.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:muted],
|
|
|
|
category_id: post.topic.category_id
|
|
|
|
)
|
|
|
|
|
|
|
|
create_post(topic_id: post.topic_id)
|
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
2015-04-17 12:03:08 +08:00
|
|
|
expect(report.length).to eq(0)
|
|
|
|
|
|
|
|
TopicUser.create!(user_id: user.id, topic_id: post.topic_id, last_read_post_number: 1, notification_level: 3)
|
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
2015-07-21 19:53:54 +08:00
|
|
|
expect(report.length).to eq(1)
|
2015-04-17 12:03:08 +08:00
|
|
|
end
|
|
|
|
|
2015-09-07 09:57:50 +08:00
|
|
|
|
|
|
|
it "correctly handles capping" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
post1 = create_post
|
|
|
|
Fabricate(:post, topic: post1.topic)
|
|
|
|
|
|
|
|
post2 = create_post
|
|
|
|
Fabricate(:post, topic: post2.topic)
|
|
|
|
|
|
|
|
post3 = create_post
|
|
|
|
Fabricate(:post, topic: post3.topic)
|
|
|
|
|
|
|
|
tracking = {
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
last_read_post_number: 1,
|
|
|
|
highest_seen_post_number: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
TopicUser.change(user.id, post1.topic_id, tracking)
|
|
|
|
TopicUser.change(user.id, post2.topic_id, tracking)
|
|
|
|
TopicUser.change(user.id, post3.topic_id, tracking)
|
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
2015-09-07 09:57:50 +08:00
|
|
|
expect(report.length).to eq(3)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-23 13:21:07 +08:00
|
|
|
it "correctly gets the tracking state" do
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(report.length).to eq(0)
|
2013-05-21 14:39:51 +08:00
|
|
|
|
2013-07-22 09:40:39 +08:00
|
|
|
post.topic.notifier.watch_topic!(post.topic.user_id)
|
2013-05-21 14:39:51 +08:00
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
2013-05-23 13:21:07 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(report.length).to eq(1)
|
2013-05-23 13:21:07 +08:00
|
|
|
row = report[0]
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(row.topic_id).to eq(post.topic_id)
|
|
|
|
expect(row.highest_post_number).to eq(1)
|
|
|
|
expect(row.last_read_post_number).to eq(nil)
|
|
|
|
expect(row.user_id).to eq(user.id)
|
2013-05-23 13:21:07 +08:00
|
|
|
|
|
|
|
# lets not leak out random users
|
2016-12-02 14:03:31 +08:00
|
|
|
expect(TopicTrackingState.report(post.user)).to be_empty
|
2013-05-23 13:21:07 +08:00
|
|
|
|
|
|
|
# lets not return anything if we scope on non-existing topic
|
2016-12-02 14:03:31 +08:00
|
|
|
expect(TopicTrackingState.report(user, post.topic_id + 1)).to be_empty
|
2013-05-23 13:21:07 +08:00
|
|
|
|
|
|
|
# when we reply the poster should have an unread row
|
2013-07-22 13:06:53 +08:00
|
|
|
create_post(user: user, topic: post.topic)
|
2013-05-21 14:39:51 +08:00
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
2015-07-21 19:48:07 +08:00
|
|
|
expect(report.length).to eq(0)
|
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(post.user)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(report.length).to eq(1)
|
2013-05-21 14:39:51 +08:00
|
|
|
|
2013-05-23 13:21:07 +08:00
|
|
|
row = report[0]
|
2013-05-21 14:39:51 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(row.topic_id).to eq(post.topic_id)
|
|
|
|
expect(row.highest_post_number).to eq(2)
|
|
|
|
expect(row.last_read_post_number).to eq(1)
|
|
|
|
expect(row.user_id).to eq(post.user_id)
|
2013-05-21 14:39:51 +08:00
|
|
|
|
2013-05-24 11:32:41 +08:00
|
|
|
# when we have no permission to see a category, don't show its stats
|
2013-07-14 09:24:16 +08:00
|
|
|
category = Fabricate(:category, read_restricted: true)
|
2013-05-24 11:32:41 +08:00
|
|
|
|
|
|
|
post.topic.category_id = category.id
|
|
|
|
post.topic.save
|
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
expect(TopicTrackingState.report(post.user)).to be_empty
|
|
|
|
expect(TopicTrackingState.report(user)).to be_empty
|
2013-05-21 14:39:51 +08:00
|
|
|
end
|
|
|
|
end
|