From 8876045205cf7d787a07a3e26e41fa845b7e580d Mon Sep 17 00:00:00 2001 From: Ximi1970 Date: Sun, 5 Nov 2023 22:57:50 +0100 Subject: [PATCH] Optimize count for TB115 --- webext/background.js | 12 ++++++ webext/js/defaults.js | 98 ++++++++++++++++++++++++++++++++----------- 2 files changed, 86 insertions(+), 24 deletions(-) diff --git a/webext/background.js b/webext/background.js index 2872fbc..3200b46 100644 --- a/webext/background.js +++ b/webext/background.js @@ -27,6 +27,7 @@ SysTrayX.Messaging = { accounts: [], folderTree: {}, countType: "0", + showNewIndicator: "false", closeType: "1", apiCountMethod: "false", filters: undefined, @@ -108,6 +109,10 @@ SysTrayX.Messaging = { const getCountTypePromise = () => new Promise((res) => res(getCountType())); SysTrayX.Messaging.countType = await getCountTypePromise(); + // Get the show new indicator + const getShowNewIndicatorPromise = () => new Promise((res) => res(getShowNewIndicator())); + SysTrayX.Messaging.showNewIndicator = await getShowNewIndicatorPromise(); + // Check the filters for the accounts SysTrayX.Messaging.accountFilterCheck(); @@ -400,6 +405,13 @@ SysTrayX.Messaging = { sendMailCount(); } + if ("showNewIndicator" in changes && changes["showNewIndicator"].newValue) { + SysTrayX.Messaging.showNewIndicator = changes["showNewIndicator"].newValue; + + sendMailCountPre115(); + sendMailCount(); + } + if ("addonprefchanged" in changes && changes["addonprefchanged"].newValue) { // // Send new preferences to the app diff --git a/webext/js/defaults.js b/webext/js/defaults.js index b7af2bf..0ee444d 100644 --- a/webext/js/defaults.js +++ b/webext/js/defaults.js @@ -275,12 +275,27 @@ async function getCountType() { } function reject() { - return undefined; + return "0"; } return await storage().get("countType").then(resolve, reject); } +// +// Get show new indicator +// +async function getShowNewIndicator() { + function resolve(result) { + return result.showNewIndicator || "false"; + } + + function reject() { + return "false"; + } + + return await storage().get("showNewIndicator").then(resolve, reject); +} + // // Get start app parameters // @@ -476,37 +491,72 @@ const sendMailCount = async () => { let unreadCount = 0; let newCount = 0; - for (const filter of SysTrayX.Messaging.filters) { - for (const path of filter.folders) { - const folder = { - accountId: filter.accountId, - path: path, - }; + if( SysTrayX.Messaging.countType === "1" || SysTrayX.Messaging.newIndicator === "true" ) { - async function* listMessages(folder) { - let page = await messenger.messages.list(folder); - for (let message of page.messages) { - yield message; - } - - while (page.id) { - page = await messenger.messages.continueList(page.id); + // Get both unread and new message count + + for (const filter of SysTrayX.Messaging.filters) { + for (const path of filter.folders) { + const folder = { + accountId: filter.accountId, + path: path, + }; + + async function* listMessages(folder) { + let page = await messenger.messages.list(folder); for (let message of page.messages) { yield message; } + + while (page.id) { + page = await messenger.messages.continueList(page.id); + for (let message of page.messages) { + yield message; + } + } + } + + let messages = listMessages(folder); + for await (let message of messages) { + if( message.new ) + { + newCount = newCount + 1; + } + + if( !message.read ) + { + unreadCount = unreadCount + 1; + } } } - - let messages = listMessages(folder); - for await (let message of messages) { - if( message.new ) - { - newCount = newCount + 1; + } + } else { + + // Only unread count + + for (const filter of SysTrayX.Messaging.filters) { + for (const path of filter.folders) { + const folder = { + accountId: filter.accountId, + path: path, + }; + + let mailFolderInfo = {}; + try { + mailFolderInfo = await browser.folders.getFolderInfo(folder); + } catch (err) { + //console.debug("Filter error: " + err); + //console.debug("Filter error: " + JSON.stringify(folder)); + + // Get all accounts + SysTrayX.Messaging.accounts = await browser.accounts.list(false); + + // Check the filters for the accounts + SysTrayX.Messaging.accountFilterCheck(); } - if( !message.read ) - { - unreadCount = unreadCount + 1; + if (mailFolderInfo.unreadMessageCount !== undefined) { + unreadCount = unreadCount + mailFolderInfo.unreadMessageCount; } } }