From 0f8559f22af4e140ed7cd099ce36d37d2aeccc07 Mon Sep 17 00:00:00 2001 From: Midgard Date: Thu, 9 Jun 2022 15:14:40 +0200 Subject: [PATCH] Mark incoming messages in current channel as read but only if tab is focused. When tab gets focus, mark channel as read. --- js/controller.js | 17 +++++++++++++++++ js/main.js | 9 +++++++++ js/pubsub.js | 1 + 3 files changed, 27 insertions(+) diff --git a/js/controller.js b/js/controller.js index 5047a1d..da82b88 100644 --- a/js/controller.js +++ b/js/controller.js @@ -207,3 +207,20 @@ function checkKeyPress(event) { } return false; } + +pubsub.subscribe("MESSAGES_NEW", post => { + if (!window.hasFocus) { + return; + } + + const curChan = currentChannel(); + if (post.endpoint === curChan.endpoint && post.channel_id === curChan.channel_id) { + mm_client.getOrCreate(post.endpoint).markChannelRead(post.channel_id); + } +}); + +pubsub.subscribe("WINDOW_FOCUSED", () => { + const curChan = currentChannel(); + if (!curChan.channel_id) return; + mm_client.getOrCreate(curChan.endpoint).markChannelRead(curChan.channel_id); +}); diff --git a/js/main.js b/js/main.js index fa4adab..aa94dc6 100644 --- a/js/main.js +++ b/js/main.js @@ -21,3 +21,12 @@ localstorage_credentials.getServers() .forEach(mm_client.getOrCreate); populateChannelList(); + +window.hasFocus = true; +window.addEventListener("focus", _ => { + window.hasFocus = true; + pubsub.publish("WINDOW_FOCUSED"); +}); +window.addEventListener("blur", _ => { + window.hasFocus = false; +}); diff --git a/js/pubsub.js b/js/pubsub.js index 0e5102d..79ae073 100644 --- a/js/pubsub.js +++ b/js/pubsub.js @@ -10,6 +10,7 @@ const topics = [ "CHANNEL_MEMBERS_NEW", "CHANNEL_MEMBERS_REMOVED", "CHANNEL_READ", // {endpoint, channel_id} + "WINDOW_FOCUSED", ]; let subscribers = Object.create(null);