2021-08-24 22:17:56 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe PrivateMessageTopicTrackingState do
|
2024-01-25 00:28:26 -06:00
|
|
|
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
|
2021-08-24 22:17:56 -05:00
|
|
|
fab!(:user_2) { Fabricate(:user) }
|
|
|
|
|
|
|
|
fab!(:group) do
|
|
|
|
Fabricate(:group, messageable_level: Group::ALIAS_LEVELS[:everyone]).tap { |g| g.add(user_2) }
|
|
|
|
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
|
2022-12-16 10:42:51 -06:00
|
|
|
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
|
2021-08-24 22:17:56 -05:00
|
|
|
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 { described_class.publish_new(private_message) }
|
|
|
|
|
2024-03-18 12:05:46 -05:00
|
|
|
expect(messages.map(&:channel)).to contain_exactly(described_class.user_channel(user_2.id))
|
2021-08-24 22:17:56 -05:00
|
|
|
|
2024-03-18 12:05:46 -05:00
|
|
|
data = messages.first.data
|
2021-08-24 22:17:56 -05:00
|
|
|
|
|
|
|
expect(data["message_type"]).to eq(described_class::NEW_MESSAGE_TYPE)
|
2024-03-18 12:05:46 -05:00
|
|
|
expect(data["topic_id"]).to eq(private_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([])
|
|
|
|
expect(data["payload"]["created_by_user_id"]).to eq(private_message.user_id)
|
2021-08-24 22:17:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should publish the right message_bus message for a group message" do
|
|
|
|
messages = MessageBus.track_publish { described_class.publish_new(group_message) }
|
|
|
|
|
2024-03-18 12:05:46 -05:00
|
|
|
expect(messages.map(&:channel)).to contain_exactly(described_class.group_channel(group.id))
|
2021-08-24 22:17:56 -05:00
|
|
|
|
2024-03-18 12:05:46 -05:00
|
|
|
data = messages.first.data
|
2021-08-24 22:17:56 -05:00
|
|
|
|
|
|
|
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])
|
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-18 20:37:01 -05:00
|
|
|
expect(data["payload"]["created_by_user_id"]).to eq(group_message.user_id)
|
2021-08-24 22:17:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".publish_unread" do
|
|
|
|
it "should publish the right message_bus message" do
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish { described_class.publish_unread(private_message.first_post) }
|
|
|
|
|
2024-03-18 12:05:46 -05:00
|
|
|
expect(messages.map(&:channel)).to contain_exactly(described_class.user_channel(user_2.id))
|
2021-08-24 22:17:56 -05:00
|
|
|
|
2024-03-18 12:05:46 -05:00
|
|
|
data = messages.first.data
|
2023-01-09 05:18:21 -06:00
|
|
|
|
2021-08-24 22:17:56 -05:00
|
|
|
expect(data["message_type"]).to eq(described_class::UNREAD_MESSAGE_TYPE)
|
|
|
|
expect(data["topic_id"]).to eq(private_message.id)
|
2024-03-18 12:05:46 -05:00
|
|
|
expect(data["payload"]["last_read_post_number"]).to eq(nil)
|
2021-08-24 22:17:56 -05:00
|
|
|
expect(data["payload"]["highest_post_number"]).to eq(1)
|
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-18 20:37:01 -05:00
|
|
|
expect(data["payload"]["created_by_user_id"]).to eq(private_message.first_post.user_id)
|
2021-08-24 22:17:56 -05:00
|
|
|
expect(data["payload"]["notification_level"]).to eq(NotificationLevels.all[:watching])
|
|
|
|
expect(data["payload"]["group_ids"]).to eq([])
|
|
|
|
end
|
2021-09-29 00:54:24 -05:00
|
|
|
|
|
|
|
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 { described_class.publish_unread(group_message.first_post) }
|
|
|
|
|
|
|
|
expect(messages).to eq([])
|
|
|
|
end
|
2021-08-24 22:17:56 -05:00
|
|
|
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
|
2021-09-09 20:20:50 -05:00
|
|
|
described_class.publish_group_archived(
|
|
|
|
topic: group_message,
|
|
|
|
group_id: group.id,
|
|
|
|
acting_user_id: user_3.id,
|
|
|
|
)
|
2021-08-24 22:17:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.map(&:channel)).to contain_exactly(described_class.group_channel(group.id))
|
|
|
|
|
|
|
|
data =
|
|
|
|
messages.find { |message| message.channel == described_class.group_channel(group.id) }.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)
|
2021-09-09 20:20:50 -05:00
|
|
|
expect(data["payload"]["acting_user_id"]).to eq(user_3.id)
|
2021-08-24 22:17:56 -05:00
|
|
|
end
|
|
|
|
end
|
2021-09-08 20:16:53 -05:00
|
|
|
|
|
|
|
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
|
2021-08-24 22:17:56 -05:00
|
|
|
end
|