Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Unread count badge shown for topics that user is not tracking #17506

Merged
merged 1 commit into from Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions app/assets/javascripts/discourse/app/lib/topic-list-tracker.js
@@ -1,4 +1,6 @@
import { Promise } from "rsvp";
import { NotificationLevels } from "discourse/lib/notification-levels";

let model, currentTopicId;

let lastTopicId, lastHighestRead;
Expand All @@ -7,14 +9,21 @@ export function setTopicList(incomingModel) {
model = incomingModel;

model?.topics?.forEach((topic) => {
let highestRead = getHighestReadCache(topic.id);
if (highestRead && highestRead >= topic.last_read_post_number) {
let count = Math.max(topic.highest_post_number - highestRead, 0);
topic.setProperties({
unread_posts: count,
new_posts: count,
});
resetHighestReadCache();
// Only update unread counts for tracked topics

if (topic.notification_level >= NotificationLevels.TRACKING) {
const highestRead = getHighestReadCache(topic.id);

if (highestRead && highestRead >= topic.last_read_post_number) {
const count = Math.max(topic.highest_post_number - highestRead, 0);

topic.setProperties({
unread_posts: count,
new_posts: count,
});

resetHighestReadCache();
}
}
});
currentTopicId = null;
Expand Down
8 changes: 6 additions & 2 deletions app/assets/javascripts/discourse/app/services/screen-track.js
Expand Up @@ -38,6 +38,7 @@ export default class ScreenTrack extends Service {

start(topicId, topicController) {
const currentTopicId = this._topicId;

if (currentTopicId && currentTopicId !== topicId) {
this.tick();
this.flush();
Expand Down Expand Up @@ -277,9 +278,12 @@ export default class ScreenTrack extends Service {
this.topicTrackingState.updateSeen(topicId, highestSeen);

if (newTimingsKeys.length > 0) {
if (this.currentUser && !isTesting()) {
if (this.currentUser) {
this.consolidateTimings(newTimings, this._topicTime, topicId);
this.sendNextConsolidatedTiming();

if (!isTesting()) {
this.sendNextConsolidatedTiming();
}
} else if (this._anonCallback) {
// Anonymous viewer - save to localStorage
const storage = this.keyValueStore;
Expand Down
Expand Up @@ -3,11 +3,45 @@ import {
previousTopicUrl,
setTopicId,
} from "discourse/lib/topic-list-tracker";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { acceptance, exists } from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit";
import { visit } from "@ember/test-helpers";
import { click, visit } from "@ember/test-helpers";
import topicFixtures from "discourse/tests/fixtures/topic";
import discoveryFixtures from "discourse/tests/fixtures/discovery-fixtures";
import { cloneJSON } from "discourse-common/lib/object";
import { NotificationLevels } from "discourse/lib/notification-levels";

acceptance("Topic list tracking", function (needs) {
let notificationLevel;

needs.hooks.afterEach(() => {
notificationLevel = null;
});

needs.user();

needs.pretender((server, helper) => {
server.get("/latest.json", () => {
const fixture = cloneJSON(discoveryFixtures["/latest.json"]);

if (notificationLevel) {
fixture["topic_list"]["topics"].find((t) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to clone this before changing the object/value, otherwise it will modify it everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I missed this one here.

if (t.id === 11557) {
t.notification_level = notificationLevel;
}
});
}

return helper.response(cloneJSON(fixture));
});

server.get("/t/11557.json", () => {
const topicFixture = topicFixtures["/t/130.json"];
topicFixture.id = 11557;
return helper.response(cloneJSON(topicFixture));
});
});

acceptance("Topic list tracking", function () {
test("Navigation", async function (assert) {
await visit("/");
let url = await nextTopicUrl();
Expand All @@ -21,4 +55,34 @@ acceptance("Topic list tracking", function () {
url = await previousTopicUrl();
assert.strictEqual(url, "/t/error-after-upgrade-to-0-9-7-9/11557");
});

test("unread count is set on topic that user is tracking", async function (assert) {
notificationLevel = NotificationLevels.TRACKING;

await visit("/");

await click(".raw-topic-link[data-topic-id='11557']");

await visit("/");

assert.ok(
exists("tr[data-topic-id='11557'] .unread-posts"),
"unread count for topic is shown"
);
});

test("unread count is not set on topic that user is not tracking", async function (assert) {
notificationLevel = NotificationLevels.REGULAR;

await visit("/");

await click(".raw-topic-link[data-topic-id='11557']");

await visit("/");

assert.notOk(
exists("tr[data-topic-id='11557'] .unread-posts"),
"unread count for topic is not shown"
);
});
});