This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/models/private-message-topic-tracking-state-test.js
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

175 lines
5.1 KiB
JavaScript

import { test } from "qunit";
import pretender from "discourse/tests/helpers/create-pretender";
import {
discourseModule,
publishToMessageBus,
} from "discourse/tests/helpers/qunit-helpers";
import MessageBus from "message-bus-client";
import PrivateMessageTopicTrackingState from "discourse/models/private-message-topic-tracking-state";
import User from "discourse/models/user";
function setupPretender() {
pretender.get(`/u/test/private-message-topic-tracking-state`, () => {
return [
200,
{ "Content-Type": "application/json" },
[
{
topic_id: 123,
highest_post_number: 12,
last_read_post_number: 12,
notification_level: 3,
group_ids: [],
},
],
];
});
}
discourseModule(
"Unit | Model | private-message-topic-tracking-state",
function (hooks) {
let pmTopicTrackingState;
hooks.beforeEach(function () {
pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: User.create({ id: 77889, username: "test" }),
});
});
test("modifying state calls onStateChange callbacks", function (assert) {
let callbackCalled = false;
pmTopicTrackingState.onStateChange("testing", () => {
callbackCalled = true;
});
pmTopicTrackingState.set("isTracking", true);
pmTopicTrackingState.removeTopics([]);
assert.ok(callbackCalled);
});
}
);
discourseModule(
"Unit | Model | private-message-topic-tracking-state | processing new_topic message",
function (hooks) {
let pmTopicTrackingState;
hooks.beforeEach(function () {
setupPretender();
pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: User.create({ id: 77889, username: "test" }),
});
pmTopicTrackingState.startTracking();
});
test("modifies the topic state only if the topic was not created by the current user", function (assert) {
let payload = {
last_read_post_number: null,
highest_post_number: 1,
group_ids: [],
created_by_user_id: 5,
};
publishToMessageBus("/private-message-topic-tracking-state/user/77889", {
message_type: "new_topic",
topic_id: 4398,
payload,
});
assert.deepEqual(
pmTopicTrackingState.findState(4398),
payload,
"the new topic created by a different user is loaded into state"
);
payload = {
last_read_post_number: null,
highest_post_number: 1,
group_ids: [],
created_by_user_id: 77889,
};
publishToMessageBus("/private-message-topic-tracking-state/user/77889", {
message_type: "new_topic",
topic_id: 4400,
payload,
});
assert.deepEqual(
pmTopicTrackingState.findState(4400),
undefined,
"the new topic created by the current user is not loaded into state"
);
});
}
);
discourseModule(
"Unit | Model | private-message-topic-tracking-state | processing unread message",
function (hooks) {
let pmTopicTrackingState;
hooks.beforeEach(function () {
setupPretender();
pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: User.create({ id: 77889, username: "test" }),
});
pmTopicTrackingState.startTracking();
});
test("modifies the last_read_post_number and highest_post_number", function (assert) {
let payload = {
last_read_post_number: 12,
highest_post_number: 13,
notification_level: 3,
group_ids: [],
created_by_user_id: 5,
};
publishToMessageBus("/private-message-topic-tracking-state/user/77889", {
message_type: "unread",
topic_id: 123,
payload,
});
let state = pmTopicTrackingState.findState(123);
assert.deepEqual(
state.highest_post_number,
13,
"the unread payload triggered by a different user creating a new post updates the state with the correct highest_post_number"
);
assert.deepEqual(
state.last_read_post_number,
12,
"the unread payload triggered by a different user creating a new post updates the state with the correct last_read_post_number"
);
payload = {
last_read_post_number: 14,
highest_post_number: 14,
notification_level: 3,
group_ids: [],
created_by_user_id: 77889,
};
publishToMessageBus("/private-message-topic-tracking-state/user/77889", {
message_type: "unread",
topic_id: 123,
payload,
});
state = pmTopicTrackingState.findState(123);
assert.deepEqual(
state.highest_post_number,
14,
"the unread payload triggered by the current user creating a new post updates the state with the correct highest_post_number"
);
assert.deepEqual(
state.last_read_post_number,
14,
"the unread payload triggered by the current user creating a new post updates the state with the correct last_read_post_number"
);
});
}
);