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/app/lib/topic-list-tracker.js
Alan Guo Xiang Tan 0525455ef6
FIX: Unread count badge shown for topics that user is not tracking (#17506)
When navigating to a topic and directly back to a topic list,
an unread count may be shown for the topic even if the user is
not tracking it.
2022-07-15 12:48:51 +08:00

96 lines
2.0 KiB
JavaScript

import { Promise } from "rsvp";
import { NotificationLevels } from "discourse/lib/notification-levels";
let model, currentTopicId;
let lastTopicId, lastHighestRead;
export function setTopicList(incomingModel) {
model = incomingModel;
model?.topics?.forEach((topic) => {
// 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;
}
export function nextTopicUrl() {
return urlAt(1);
}
export function previousTopicUrl() {
return urlAt(-1);
}
export function setHighestReadCache(topicId, postNumber) {
lastTopicId = topicId;
lastHighestRead = postNumber;
}
export function getHighestReadCache(topicId) {
if (topicId === lastTopicId) {
return lastHighestRead;
}
}
export function resetHighestReadCache() {
lastTopicId = undefined;
lastHighestRead = undefined;
}
function urlAt(delta) {
if (!model || !model.topics) {
return Promise.resolve(null);
}
let index = currentIndex();
if (index === -1) {
index = 0;
} else {
index += delta;
}
const topic = model.topics[index];
if (!topic && index > 0 && model.more_topics_url && model.loadMore) {
return model.loadMore().then(() => urlAt(delta));
}
if (topic) {
currentTopicId = topic.id;
return Promise.resolve(topic.lastUnreadUrl);
}
return Promise.resolve(null);
}
export function setTopicId(topicId) {
currentTopicId = topicId;
}
function currentIndex() {
if (currentTopicId && model && model.topics) {
const idx = model.topics.findIndex((t) => t.id === currentTopicId);
if (idx > -1) {
return idx;
}
}
return -1;
}