discourse/spec/models/private_message_topic_tracking_state_spec.rb
Martin Brennan c6c633e041
FIX: Issues with incorrect unread and private message topic tracking state (#16474)
This commit fixes two issues at play. The first was introduced
in f6c852b (or maybe not introduced
but rather revealed). When a user posted a new message in a topic,
they received the unread topic tracking state MessageBus message,
and the Unread (X) indicator was incremented by one, because with the
aforementioned perf commit we "guess" the correct last read post
for the user, because we no longer calculate individual users' read
status there. This meant that every time a user posted in a topic
they tracked, the unread indicator was incremented. To get around
this, we can just exclude the user who created the post from the
target users of the unread state message.

The second issue was related to the private message topic tracking
state, and was somewhat similar. Whenever a user created a new private
message, the New (X) indicator was incremented, and could not be
cleared until the page was refreshed. To solve this, we just don't
update the topic state for the user when the new_topic tracking state
message comes through if the user who created the topic is the
same as the current user.

cf. https://meta.discourse.org/t/bottom-of-topic-shows-there-is-1-unread-remaining-when-there-are-actually-0-unread-topics-remaining/220817
2022-04-19 11:37:01 +10:00

228 lines
7.1 KiB
Ruby

# frozen_string_literal: true
describe PrivateMessageTopicTrackingState do
fab!(:user) { Fabricate(:user) }
fab!(:user_2) { Fabricate(:user) }
fab!(:group) do
Fabricate(:group, messageable_level: Group::ALIAS_LEVELS[:everyone]).tap do |g|
g.add(user_2)
end
end
fab!(:group_message) do
create_post(
user: user,
target_group_names: [group.name],
archetype: Archetype.private_message
).topic
end
fab!(:private_message) do
create_post(
user: user,
target_usernames: [user_2.username],
archetype: Archetype.private_message
).topic
end
fab!(:private_message_2) do
create_post(
user: user,
target_usernames: [Fabricate(:user).username],
archetype: Archetype.private_message
).topic
end
describe '.report' do
it 'returns the right tracking state' do
TopicUser.find_by(user: user_2, topic: group_message).update!(
last_read_post_number: 1
)
expect(described_class.report(user_2).map(&:topic_id))
.to contain_exactly(private_message.id)
create_post(user: user, topic: group_message)
report = described_class.report(user_2)
expect(report.map(&:topic_id)).to contain_exactly(
group_message.id,
private_message.id
)
state = report.first
expect(state.topic_id).to eq(private_message.id)
expect(state.user_id).to eq(user_2.id)
expect(state.last_read_post_number).to eq(nil)
expect(state.notification_level).to eq(NotificationLevels.all[:watching])
expect(state.highest_post_number).to eq(1)
expect(state.group_ids).to eq([])
expect(report.last.group_ids).to contain_exactly(group.id)
end
it 'returns the right tracking state when topics contain whispers' do
TopicUser.find_by(user: user_2, topic: private_message).update!(
last_read_post_number: 1
)
create_post(
raw: "this is a test post",
topic: private_message,
post_type: Post.types[:whisper],
user: Fabricate(:admin)
)
expect(described_class.report(user_2).map(&:topic_id))
.to contain_exactly(group_message.id)
user_2.grant_admin!
tracking_state = described_class.report(user_2)
expect(tracking_state.map { |topic| [topic.topic_id, topic.highest_post_number] })
.to contain_exactly(
[group_message.id, 1],
[private_message.id, 2]
)
end
it 'returns the right tracking state when topics have been dismissed' do
DismissedTopicUser.create!(
user_id: user_2.id,
topic_id: group_message.id
)
expect(described_class.report(user_2).map(&:topic_id))
.to contain_exactly(private_message.id)
end
end
describe '.publish_new' do
it 'should publish the right message_bus message' do
messages = MessageBus.track_publish do
described_class.publish_new(private_message)
end
expect(messages.map(&:channel)).to contain_exactly(
described_class.user_channel(user.id),
described_class.user_channel(user_2.id)
)
data = messages.find do |message|
message.channel == described_class.user_channel(user.id)
end.data
expect(data['message_type']).to eq(described_class::NEW_MESSAGE_TYPE)
end
it 'should publish the right message_bus message for a group message' do
messages = MessageBus.track_publish do
described_class.publish_new(group_message)
end
expect(messages.map(&:channel)).to contain_exactly(
described_class.group_channel(group.id),
described_class.user_channel(user.id)
)
data = messages.find do |message|
message.channel == described_class.group_channel(group.id)
end.data
expect(data['message_type']).to eq(described_class::NEW_MESSAGE_TYPE)
expect(data['topic_id']).to eq(group_message.id)
expect(data['payload']['last_read_post_number']).to eq(nil)
expect(data['payload']['highest_post_number']).to eq(1)
expect(data['payload']['group_ids']).to eq([group.id])
expect(data['payload']['created_by_user_id']).to eq(group_message.user_id)
end
end
describe '.publish_unread' do
it 'should publish the right message_bus message' do
messages = MessageBus.track_publish do
described_class.publish_unread(private_message.first_post)
end
expect(messages.map(&:channel)).to contain_exactly(
described_class.user_channel(user.id),
described_class.user_channel(user_2.id)
)
data = messages.find do |message|
message.channel == described_class.user_channel(user.id)
end.data
expect(data['message_type']).to eq(described_class::UNREAD_MESSAGE_TYPE)
expect(data['topic_id']).to eq(private_message.id)
expect(data['payload']['last_read_post_number']).to eq(1)
expect(data['payload']['highest_post_number']).to eq(1)
expect(data['payload']['created_by_user_id']).to eq(private_message.first_post.user_id)
expect(data['payload']['notification_level'])
.to eq(NotificationLevels.all[:watching])
expect(data['payload']['group_ids']).to eq([])
end
it 'does not publish message_bus message if post in topic is not new for user' do
group_message.update!(created_at: 3.days.ago)
user_2.user_option.update!(new_topic_duration_minutes: 2.days.minutes)
messages = MessageBus.track_publish do
described_class.publish_unread(group_message.first_post)
end
expect(messages).to eq([])
end
end
describe '.publish_group_archived' do
it 'should publish the right message_bus message' do
user_3 = Fabricate(:user)
group.add(user_3)
messages = MessageBus.track_publish do
described_class.publish_group_archived(
topic: group_message,
group_id: group.id,
acting_user_id: user_3.id
)
end
expect(messages.map(&:channel)).to contain_exactly(
described_class.group_channel(group.id)
)
data = messages.find do |message|
message.channel == described_class.group_channel(group.id)
end.data
expect(data['message_type']).to eq(described_class::GROUP_ARCHIVE_MESSAGE_TYPE)
expect(data['topic_id']).to eq(group_message.id)
expect(data['payload']['group_ids']).to contain_exactly(group.id)
expect(data['payload']['acting_user_id']).to eq(user_3.id)
end
end
describe '.publish_read' do
it 'should publish the right message_bus message' do
message = MessageBus.track_publish(described_class.user_channel(user.id)) do
PrivateMessageTopicTrackingState.publish_read(private_message.id, 1, user)
end.first
data = message.data
expect(message.user_ids).to contain_exactly(user.id)
expect(message.group_ids).to eq(nil)
expect(data["topic_id"]).to eq(private_message.id)
expect(data["message_type"]).to eq(described_class::READ_MESSAGE_TYPE)
expect(data["payload"]["last_read_post_number"]).to eq(1)
expect(data["payload"]["highest_post_number"]).to eq(1)
expect(data["payload"]["notification_level"]).to eq(nil)
end
end
end