2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) do
|
2013-05-21 14:39:51 +08:00
|
|
|
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
|
|
|
|
|
2018-03-05 16:18:23 +08:00
|
|
|
let(:topic) { post.topic }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:private_message_post) { Fabricate(:private_message_post) }
|
2018-03-06 14:38:43 +08:00
|
|
|
let(:private_message_topic) { private_message_post.topic }
|
2018-03-05 16:18:23 +08:00
|
|
|
|
|
|
|
describe '#publish_latest' do
|
|
|
|
it 'can correctly publish latest' do
|
|
|
|
message = MessageBus.track_publish("/latest") do
|
|
|
|
described_class.publish_latest(topic)
|
|
|
|
end.first
|
|
|
|
|
|
|
|
data = message.data
|
|
|
|
|
|
|
|
expect(data["topic_id"]).to eq(topic.id)
|
|
|
|
expect(data["message_type"]).to eq(described_class::LATEST_MESSAGE_TYPE)
|
|
|
|
expect(data["payload"]["archetype"]).to eq(Archetype.default)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'private message' do
|
|
|
|
it 'should not publish any message' do
|
|
|
|
messages = MessageBus.track_publish do
|
2018-03-06 14:38:43 +08:00
|
|
|
described_class.publish_latest(private_message_topic)
|
2018-03-05 16:18:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#publish_unread' do
|
|
|
|
it "can correctly publish unread" do
|
|
|
|
message = MessageBus.track_publish(described_class.unread_channel_key(post.user.id)) do
|
|
|
|
TopicTrackingState.publish_unread(post)
|
|
|
|
end.first
|
|
|
|
|
|
|
|
data = message.data
|
|
|
|
|
|
|
|
expect(data["topic_id"]).to eq(topic.id)
|
|
|
|
expect(data["message_type"]).to eq(described_class::UNREAD_MESSAGE_TYPE)
|
|
|
|
expect(data["payload"]["archetype"]).to eq(Archetype.default)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for a private message' do
|
|
|
|
before do
|
|
|
|
TopicUser.change(
|
2018-03-06 14:38:43 +08:00
|
|
|
private_message_topic.allowed_users.first.id,
|
|
|
|
private_message_topic.id,
|
2018-03-05 16:18:23 +08:00
|
|
|
notification_level: TopicUser.notification_levels[:tracking]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not publish any message' do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
TopicTrackingState.publish_unread(private_message_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2013-05-29 16:11:04 +08:00
|
|
|
end
|
|
|
|
|
2018-03-06 14:38:43 +08:00
|
|
|
describe '#publish_private_message' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-03-06 14:38:43 +08:00
|
|
|
|
|
|
|
describe 'normal topic' do
|
|
|
|
it 'should publish the right message' do
|
|
|
|
allowed_users = private_message_topic.allowed_users
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
TopicTrackingState.publish_private_message(private_message_topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.count).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.channel).to eq('/private-messages/inbox')
|
|
|
|
expect(message.data["topic_id"]).to eq(private_message_topic.id)
|
2019-10-04 07:56:28 +08:00
|
|
|
expect(message.user_ids).to contain_exactly(*allowed_users.map(&:id))
|
2018-03-06 14:38:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'topic with groups' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:group1) { Fabricate(:group, users: [Fabricate(:user)]) }
|
|
|
|
fab!(:group2) { Fabricate(:group, users: [Fabricate(:user), Fabricate(:user)]) }
|
2018-03-06 14:38:43 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
[group1, group2].each do |group|
|
|
|
|
private_message_topic.allowed_groups << group
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should publish the right message" do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
TopicTrackingState.publish_private_message(
|
2018-03-15 16:01:40 +08:00
|
|
|
private_message_topic
|
2018-03-06 14:38:43 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-24 15:36:41 +08:00
|
|
|
expect(messages.map(&:channel)).to contain_exactly(
|
|
|
|
'/private-messages/inbox',
|
|
|
|
"/private-messages/group/#{group1.name}",
|
|
|
|
"/private-messages/group/#{group2.name}"
|
|
|
|
)
|
|
|
|
|
2018-09-04 10:16:21 +08:00
|
|
|
message = messages.find do |m|
|
|
|
|
m.channel == '/private-messages/inbox'
|
2018-05-24 15:36:41 +08:00
|
|
|
end
|
2018-03-15 16:01:40 +08:00
|
|
|
|
|
|
|
expect(message.data["topic_id"]).to eq(private_message_topic.id)
|
|
|
|
expect(message.user_ids).to eq(private_message_topic.allowed_users.map(&:id))
|
2018-03-07 14:01:20 +08:00
|
|
|
|
2018-03-06 14:38:43 +08:00
|
|
|
[group1, group2].each do |group|
|
2018-09-04 10:16:21 +08:00
|
|
|
message = messages.find do |m|
|
|
|
|
m.channel == "/private-messages/group/#{group.name}"
|
2018-03-06 14:38:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(message.data["topic_id"]).to eq(private_message_topic.id)
|
2018-03-07 11:28:29 +08:00
|
|
|
expect(message.user_ids).to eq(group.users.map(&:id))
|
2018-03-06 14:38:43 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-07 14:01:20 +08:00
|
|
|
|
|
|
|
describe "archiving topic" do
|
|
|
|
it "should publish the right message" do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
TopicTrackingState.publish_private_message(
|
|
|
|
private_message_topic,
|
|
|
|
group_archive: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-24 15:36:41 +08:00
|
|
|
expect(messages.map(&:channel)).to contain_exactly(
|
|
|
|
'/private-messages/inbox',
|
|
|
|
"/private-messages/group/#{group1.name}",
|
|
|
|
"/private-messages/group/#{group1.name}/archive",
|
|
|
|
"/private-messages/group/#{group2.name}",
|
|
|
|
"/private-messages/group/#{group2.name}/archive",
|
|
|
|
)
|
|
|
|
|
2018-09-04 10:16:21 +08:00
|
|
|
message = messages.find { |m| m.channel == '/private-messages/inbox' }
|
2018-03-15 16:01:40 +08:00
|
|
|
|
|
|
|
expect(message.data["topic_id"]).to eq(private_message_topic.id)
|
|
|
|
expect(message.user_ids).to eq(private_message_topic.allowed_users.map(&:id))
|
2018-03-07 14:01:20 +08:00
|
|
|
|
|
|
|
[group1, group2].each do |group|
|
|
|
|
group_channel = "/private-messages/group/#{group.name}"
|
|
|
|
|
|
|
|
[
|
|
|
|
group_channel,
|
|
|
|
"#{group_channel}/archive"
|
|
|
|
].each do |channel|
|
2018-09-04 10:16:21 +08:00
|
|
|
message = messages.find { |m| m.channel == channel }
|
2018-03-07 14:01:20 +08:00
|
|
|
expect(message.data["topic_id"]).to eq(private_message_topic.id)
|
|
|
|
expect(message.user_ids).to eq(group.users.map(&:id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 14:38:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'topic with new post' do
|
|
|
|
let(:user) { private_message_topic.allowed_users.last }
|
|
|
|
|
|
|
|
let!(:post) do
|
|
|
|
Fabricate(:post,
|
|
|
|
topic: private_message_topic,
|
|
|
|
user: user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-03-15 16:01:40 +08:00
|
|
|
let!(:group) do
|
|
|
|
group = Fabricate(:group, users: [Fabricate(:user)])
|
|
|
|
private_message_topic.allowed_groups << group
|
|
|
|
group
|
|
|
|
end
|
|
|
|
|
2018-03-06 14:38:43 +08:00
|
|
|
it 'should publish the right message' do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
TopicTrackingState.publish_private_message(
|
|
|
|
private_message_topic,
|
|
|
|
post: post
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-24 15:36:41 +08:00
|
|
|
expected_channels = [
|
|
|
|
'/private-messages/inbox',
|
|
|
|
'/private-messages/sent',
|
|
|
|
"/private-messages/group/#{group.name}"
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(messages.map(&:channel)).to contain_exactly(*expected_channels)
|
2018-03-06 14:38:43 +08:00
|
|
|
|
2018-05-24 15:36:41 +08:00
|
|
|
expected_channels.zip([
|
|
|
|
private_message_topic.allowed_users.map(&:id),
|
|
|
|
[user.id],
|
|
|
|
[group.users.first.id]
|
|
|
|
]).each do |channel, user_ids|
|
2018-09-04 10:16:21 +08:00
|
|
|
message = messages.find { |m| m.channel == channel }
|
2018-03-06 14:38:43 +08:00
|
|
|
|
|
|
|
expect(message.data["topic_id"]).to eq(private_message_topic.id)
|
2018-03-07 11:28:29 +08:00
|
|
|
expect(message.user_ids).to eq(user_ids)
|
2018-03-06 14:38:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'archived topic' do
|
|
|
|
it 'should publish the right message' do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
TopicTrackingState.publish_private_message(
|
|
|
|
private_message_topic,
|
2018-03-13 08:35:15 +08:00
|
|
|
archive_user_id: private_message_post.user_id,
|
2018-03-06 14:38:43 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-24 15:36:41 +08:00
|
|
|
expected_channels = [
|
2018-03-07 11:39:23 +08:00
|
|
|
"/private-messages/archive",
|
|
|
|
"/private-messages/inbox",
|
|
|
|
"/private-messages/sent",
|
2018-05-24 15:36:41 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
expect(messages.map(&:channel)).to eq(expected_channels)
|
|
|
|
|
|
|
|
expected_channels.each do |channel|
|
2018-09-04 10:16:21 +08:00
|
|
|
message = messages.find { |m| m.channel = channel }
|
2018-03-07 11:28:29 +08:00
|
|
|
expect(message.data["topic_id"]).to eq(private_message_topic.id)
|
|
|
|
expect(message.user_ids).to eq([private_message_post.user_id])
|
|
|
|
end
|
2018-03-06 14:38:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for a regular topic' do
|
|
|
|
it 'should not publish any message' do
|
|
|
|
topic.allowed_users << Fabricate(:user)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
TopicTrackingState.publish_private_message(topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-27 20:09:00 +08:00
|
|
|
describe '#publish_read_private_message' do
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
2019-08-29 23:03:43 +08:00
|
|
|
let(:read_topic_key) { "/private-messages/unread-indicator/#{@group_message.id}" }
|
2019-08-27 20:09:00 +08:00
|
|
|
let(:read_post_key) { "/topic/#{@group_message.id}" }
|
|
|
|
let(:latest_post_number) { 3 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add(user)
|
|
|
|
@group_message = Fabricate(:private_message_topic,
|
|
|
|
allowed_groups: [group],
|
|
|
|
topic_allowed_users: [Fabricate.build(:topic_allowed_user, user: user)],
|
|
|
|
highest_post_number: latest_post_number
|
|
|
|
)
|
|
|
|
@post = Fabricate(:post, topic: @group_message, post_number: latest_post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not trigger a read count update if no allowed groups have the option enabled' do
|
|
|
|
messages = MessageBus.track_publish(read_post_key) do
|
|
|
|
TopicTrackingState.publish_read_indicator_on_read(@group_message.id, latest_post_number, user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the read indicator is enabled' do
|
|
|
|
before { group.update!(publish_read_state: true) }
|
|
|
|
|
2019-08-29 23:03:43 +08:00
|
|
|
it 'publishes a message to hide the unread indicator' do
|
2019-08-27 20:09:00 +08:00
|
|
|
message = MessageBus.track_publish(read_topic_key) do
|
|
|
|
TopicTrackingState.publish_read_indicator_on_read(@group_message.id, latest_post_number, user.id)
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(message.data['topic_id']).to eq @group_message.id
|
2019-08-29 23:03:43 +08:00
|
|
|
expect(message.data['show_indicator']).to eq false
|
2019-08-27 20:09:00 +08:00
|
|
|
end
|
|
|
|
|
2019-08-29 23:03:43 +08:00
|
|
|
it 'publishes a message to show the unread indicator when a non-member creates a new post' do
|
|
|
|
allowed_user = Fabricate(:topic_allowed_user, topic: @group_message)
|
|
|
|
message = MessageBus.track_publish(read_topic_key) do
|
|
|
|
TopicTrackingState.publish_read_indicator_on_write(@group_message.id, latest_post_number, allowed_user.id)
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(message.data['topic_id']).to eq @group_message.id
|
|
|
|
expect(message.data['show_indicator']).to eq true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not publish the unread indicator if the message is not the last one' do
|
2019-08-27 20:09:00 +08:00
|
|
|
not_last_post_number = latest_post_number - 1
|
|
|
|
Fabricate(:post, topic: @group_message, post_number: not_last_post_number)
|
|
|
|
messages = MessageBus.track_publish(read_topic_key) do
|
|
|
|
TopicTrackingState.publish_read_indicator_on_read(@group_message.id, not_last_post_number, user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not publish the read indicator if the user is not a group member' do
|
|
|
|
allowed_user = Fabricate(:topic_allowed_user, topic: @group_message)
|
|
|
|
messages = MessageBus.track_publish(read_topic_key) do
|
|
|
|
TopicTrackingState.publish_read_indicator_on_read(@group_message.id, latest_post_number, allowed_user.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publish a read count update to every client' do
|
|
|
|
message = MessageBus.track_publish(read_post_key) do
|
|
|
|
TopicTrackingState.publish_read_indicator_on_read(@group_message.id, latest_post_number, user.id)
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(message.data[:type]).to eq :read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
|
FIX: New/unread count after dismissing new topics in a regular category (#8659)
6e1fe22 introduced the possiblity for category_users to have a NULL notification_level, so that we can store `last_seen_at` dates without locking the notification level. At the time, this did not affect the topic-tracking-state query. However, the query changes in f434de2 introduced a slight change in behavior.
Previously, a subquery would look for a category_user with notification_level=mute. f434de2 refactored this to remove the subquery, and inverted some of the logic to suit.
The new query checked for `notification_level <> :muted`. If `notification_level` is NULL, this comparison will return NULL. In this scenario, notification_level=NULL means that we should fall back to the default tracking level (regular), and so we want the expression to resolve as true, not false. There was already a check for the existence of the category_users row, but it did not check for the existence of a NOT NULL notification_level.
This commit amends the expression so that the notification_level will only be compared if it is non-null.
2020-01-07 00:15:24 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly handles category_users with null notification level" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
post
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
|
|
|
|
CategoryUser.create!(user_id: user.id, category_id: post.topic.category_id)
|
|
|
|
|
2020-01-07 02:22:42 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works when categories are default muted" do
|
|
|
|
SiteSetting.mute_all_categories_by_default = true
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
post
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(0)
|
|
|
|
|
|
|
|
CategoryUser.create!(user_id: user.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:regular],
|
|
|
|
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-07-21 19:53:54 +08:00
|
|
|
expect(report.length).to eq(1)
|
2015-04-17 12:03:08 +08:00
|
|
|
end
|
|
|
|
|
2019-12-10 06:50:05 +08:00
|
|
|
context 'muted tags' do
|
|
|
|
it "remove_muted_tags_from_latest is set to always" do
|
|
|
|
SiteSetting.remove_muted_tags_from_latest = 'always'
|
|
|
|
user = Fabricate(:user)
|
|
|
|
tag1 = Fabricate(:tag)
|
|
|
|
tag2 = Fabricate(:tag)
|
|
|
|
Fabricate(:topic_tag, tag: tag1, topic: topic)
|
|
|
|
Fabricate(:topic_tag, tag: tag2, topic: topic)
|
|
|
|
post
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
|
|
|
|
TagUser.create!(user_id: user.id,
|
|
|
|
notification_level: TagUser.notification_levels[:muted],
|
|
|
|
tag_id: tag1.id
|
|
|
|
)
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remove_muted_tags_from_latest is set to only_muted" do
|
|
|
|
SiteSetting.remove_muted_tags_from_latest = 'only_muted'
|
|
|
|
user = Fabricate(:user)
|
|
|
|
tag1 = Fabricate(:tag)
|
|
|
|
tag2 = Fabricate(:tag)
|
|
|
|
Fabricate(:topic_tag, tag: tag1, topic: topic)
|
|
|
|
Fabricate(:topic_tag, tag: tag2, topic: topic)
|
|
|
|
post
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
|
|
|
|
TagUser.create!(user_id: user.id,
|
|
|
|
notification_level: TagUser.notification_levels[:muted],
|
|
|
|
tag_id: tag1.id
|
|
|
|
)
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
|
|
|
|
TagUser.create!(user_id: user.id,
|
|
|
|
notification_level: TagUser.notification_levels[:muted],
|
|
|
|
tag_id: tag2.id
|
|
|
|
)
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remove_muted_tags_from_latest is set to never" do
|
|
|
|
SiteSetting.remove_muted_tags_from_latest = 'never'
|
|
|
|
user = Fabricate(:user)
|
|
|
|
tag1 = Fabricate(:tag)
|
|
|
|
Fabricate(:topic_tag, tag: tag1, topic: topic)
|
|
|
|
post
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
|
|
|
|
TagUser.create!(user_id: user.id,
|
|
|
|
notification_level: TagUser.notification_levels[:muted],
|
|
|
|
tag_id: tag1.id
|
|
|
|
)
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-14 13:11:34 +08:00
|
|
|
it "correctly handles seen categories" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
post
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
|
|
|
|
CategoryUser.create!(user_id: user.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:regular],
|
|
|
|
category_id: post.topic.category_id,
|
|
|
|
last_seen_at: post.topic.created_at
|
|
|
|
)
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(0)
|
|
|
|
|
|
|
|
post.topic.touch(:created_at)
|
|
|
|
|
|
|
|
report = TopicTrackingState.report(user)
|
|
|
|
expect(report.length).to eq(1)
|
|
|
|
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
|