FIX: triggers update last read when message is not staged (#19565)

This commit is contained in:
Joffrey JAFFEUX 2022-12-21 22:55:34 +01:00 committed by GitHub
parent 4eee1320b0
commit f6174587ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 8 deletions

View File

@ -5,17 +5,26 @@ import { bind } from "discourse-common/utils/decorators";
export default class ChatMessageVisibilityObserver extends Service { export default class ChatMessageVisibilityObserver extends Service {
@service chat; @service chat;
observer = new IntersectionObserver(this._observerCallback, { intersectionObserver = new IntersectionObserver(
this._intersectionObserverCallback,
{
root: document,
rootMargin: "-10px",
}
);
mutationObserver = new MutationObserver(this._mutationObserverCallback, {
root: document, root: document,
rootMargin: "-10px", rootMargin: "-10px",
}); });
willDestroy() { willDestroy() {
this.observer.disconnect(); this.intersectionObserver.disconnect();
this.mutationObserver.disconnect();
} }
@bind @bind
_observerCallback(entries) { _intersectionObserverCallback(entries) {
entries.forEach((entry) => { entries.forEach((entry) => {
entry.target.dataset.visible = entry.isIntersecting; entry.target.dataset.visible = entry.isIntersecting;
@ -29,11 +38,26 @@ export default class ChatMessageVisibilityObserver extends Service {
}); });
} }
@bind
_mutationObserverCallback(mutationList) {
mutationList.forEach((mutation) => {
const data = mutation.target.dataset;
if (data.id && data.visible && !data.stagedId) {
this.chat.updateLastReadMessage();
}
});
}
observe(element) { observe(element) {
this.observer.observe(element); this.intersectionObserver.observe(element);
this.mutationObserver.observe(element, {
attributes: true,
attributeOldValue: true,
attributeFilter: ["data-staged-id"],
});
} }
unobserve(element) { unobserve(element) {
this.observer.unobserve(element); this.intersectionObserver.unobserve(element);
} }
} }

View File

@ -144,7 +144,7 @@ export default class ChatSubscriptionsManager extends Service {
_onUserTrackingStateUpdate(busData) { _onUserTrackingStateUpdate(busData) {
this.chatChannelsManager.find(busData.chat_channel_id).then((channel) => { this.chatChannelsManager.find(busData.chat_channel_id).then((channel) => {
if ( if (
channel?.currentUserMembership?.last_read_message_id < channel?.currentUserMembership?.last_read_message_id <=
busData.chat_message_id busData.chat_message_id
) { ) {
channel.currentUserMembership.last_read_message_id = channel.currentUserMembership.last_read_message_id =

View File

@ -432,9 +432,14 @@ export default class Chat extends Service {
10 10
); );
const membership = channel.currentUserMembership;
const hasUnreadMessages = const hasUnreadMessages =
latestUnreadMsgId > channel.currentUserMembership.last_read_message_id; latestUnreadMsgId > membership.last_read_message_id;
if (hasUnreadMessages) { if (
hasUnreadMessages ||
membership.unread_count > 0 ||
membership.unread_mentions > 0
) {
channel.updateLastReadMessage(latestUnreadMsgId); channel.updateLastReadMessage(latestUnreadMsgId);
} }
} }