diff --git a/js/focus_listener.js b/js/focus_listener.js index c9a728233..29b027598 100644 --- a/js/focus_listener.js +++ b/js/focus_listener.js @@ -3,12 +3,15 @@ 'use strict'; let windowFocused = false; + let windowFocusedListener = function() {} window.addEventListener('blur', () => { windowFocused = false; }); window.addEventListener('focus', () => { windowFocused = true; + windowFocusedListener(); }); window.isFocused = () => windowFocused; + window.setFocusListener = (listener) => windowFocusedListener = listener; })(); diff --git a/js/read_syncs.js b/js/read_syncs.js index 70aebf1c4..c7337ec07 100644 --- a/js/read_syncs.js +++ b/js/read_syncs.js @@ -59,7 +59,7 @@ // If message is unread, we mark it read. Otherwise, we update the expiration // timer to the time specified by the read sync if it's earlier than // the previous read time. - if (message.isUnread()) { + if (message.isUnread() && window.isFocused()) { await message.markRead(readAt); // onReadMessage may result in messages older than this one being diff --git a/ts/components/conversation/Message.tsx b/ts/components/conversation/Message.tsx index 2fd928ccf..6a4ba4c98 100644 --- a/ts/components/conversation/Message.tsx +++ b/ts/components/conversation/Message.tsx @@ -713,7 +713,7 @@ class MessageInner extends React.PureComponent { const isShowingImage = this.isShowingImage(); const isIncoming = direction === 'incoming'; - const shouldMarkReadWhenVisible = isIncoming && isUnread; + const shouldMarkReadWhenVisible = isIncoming && isUnread && window.isFocused(); const divClasses = ['session-message-wrapper']; if (selected) { diff --git a/ts/components/session/conversation/SessionMessagesList.tsx b/ts/components/session/conversation/SessionMessagesList.tsx index e3af18792..20c7dc350 100644 --- a/ts/components/session/conversation/SessionMessagesList.tsx +++ b/ts/components/session/conversation/SessionMessagesList.tsx @@ -75,6 +75,7 @@ export class SessionMessagesList extends React.Component { this.messageContainerRef = this.props.messageContainerRef; this.ignoreScrollEvents = true; + window.setFocusListener(() => this.updateReadMessages()); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -378,7 +379,7 @@ export class SessionMessagesList extends React.Component { return; } - if (this.getScrollOffsetBottomPx() === 0) { + if (this.getScrollOffsetBottomPx() === 0 && window.isFocused()) { void conversation.markRead(messages[0].attributes.received_at); } } diff --git a/ts/window.d.ts b/ts/window.d.ts index 5f43a4e83..70c74c782 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -40,6 +40,8 @@ declare global { getFriendsFromContacts: any; getSettingValue: any; i18n: LocalizerType; + isFocused: any; + setFocusListener: (listener: any) => any; libloki: Libloki; libsignal: LibsignalProtocol; log: any;