Update handlers

This commit is contained in:
Ximi1970
2023-09-11 21:31:07 +02:00
parent a8fdcde16d
commit 19748422ea
2 changed files with 196 additions and 179 deletions

View File

@@ -72,10 +72,12 @@ SysTrayX.Messaging = {
// Send start app trigger
SysTrayX.Messaging.sendStartApp();
// Catch the new incomming mail
browser.messages.onNewMailReceived.addListener(
SysTrayX.Messaging.listenerNewMail
);
if (SysTrayX.Info.browserInfo.majorVersion < 115) {
// Catch the new incomming mail
browser.messages.onNewMailReceived.addListener(
SysTrayX.Messaging.listenerNewMail
);
}
// Set catch folder changes
browser.folders.onFolderInfoChanged.addListener(
@@ -113,7 +115,9 @@ SysTrayX.Messaging = {
// Handle cached mail changes on startup
SysTrayX.Messaging.startupDelayFinished = true;
SysTrayX.Messaging.listenerNewMail();
if (SysTrayX.Info.browserInfo.majorVersion < 115) {
SysTrayX.Messaging.listenerNewMail();
}
SysTrayX.Messaging.listenerFolderInfoChanged();
// Catch folder changes
@@ -127,11 +131,6 @@ SysTrayX.Messaging = {
SysTrayX.Messaging.listenerFolderDeleted
);
// Catch a folder change to reset the new counter
browser.mailTabs.onDisplayedFolderChanged.addListener(
SysTrayX.Window.folderChanged
);
// Try to catch the window state
browser.windows.onFocusChanged.addListener(SysTrayX.Window.focusChanged);
},
@@ -175,101 +174,103 @@ SysTrayX.Messaging = {
},
listenerFolderInfoChanged: async function (folder, folderInfo) {
if (SysTrayX.Info.browserInfo.majorVersion < 115) {
// Cache the folder change
if (folder)
{
SysTrayX.Messaging.folderInfoChangeCache.push({ folder, folderInfo });
}
console.debug("folderInfoChanged: " + JSON.stringify(folder));
console.debug("folderInfoChanged: " + JSON.stringify(folderInfo));
console.debug("folderInfoChanged: Cache: " + SysTrayX.Messaging.folderInfoChangeCache.length );
if (SysTrayX.Messaging.startupDelayFinished)
{
if( SysTrayX.Messaging.folderInfoChangeCache.length > 0 ) {
// Process the received messages
for (const cache of SysTrayX.Messaging.folderInfoChangeCache) {
if (cache.folderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[cache.folder.accountId] === undefined) {
SysTrayX.Messaging.unread[cache.folder.accountId] = {};
}
if (SysTrayX.Messaging.new[cache.folder.accountId] === undefined) {
SysTrayX.Messaging.new[cache.folder.accountId] = {};
}
if (SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path] === undefined) {
SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path] = [];
}
SysTrayX.Messaging.unread[cache.folder.accountId][cache.folder.path] =
cache.folderInfo.unreadMessageCount;
// Check if the new mails have been read, remove from new storage
const messages = SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path];
if (messages.length > 0) {
const newMessages = [];
for (let i = 0; i < messages.length; ++i) {
const message = messages[i];
const getHeaderPromise = (messageId) =>
new Promise((res) => res(messenger.messages.get(messageId)));
const header = await getHeaderPromise(message.id);
if (!header.read) {
newMessages.push(message);
}
}
console.debug("FolderInfoChanged: Clear");
console.debug("FolderInfoChanged: Old: " + JSON.stringify(SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path]));
console.debug("FolderInfoChanged: New: " + JSON.stringify(newMessages));
SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path] = [
...newMessages,
];
}
}
// Cache handled
SysTrayX.Messaging.folderInfoChangeCache = [];
}
} else {
// Count the initial unread messages
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));
}
if (mailFolderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[folder.accountId] === undefined) {
SysTrayX.Messaging.unread[folder.accountId] = {};
}
SysTrayX.Messaging.unread[folder.accountId][folder.path] =
mailFolderInfo.unreadMessageCount;
}
}
}
// Cache the folder change
if (folder)
{
SysTrayX.Messaging.folderInfoChangeCache.push({ folder, folderInfo });
}
console.debug("folderInfoChanged: " + JSON.stringify(folder));
console.debug("folderInfoChanged: " + JSON.stringify(folderInfo));
console.debug("folderInfoChanged: Cache: " + SysTrayX.Messaging.folderInfoChangeCache.length );
if (SysTrayX.Messaging.startupDelayFinished)
{
if( SysTrayX.Messaging.folderInfoChangeCache.length > 0 ) {
// Process the received messages
for (const cache of SysTrayX.Messaging.folderInfoChangeCache) {
if (cache.folderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[cache.folder.accountId] === undefined) {
SysTrayX.Messaging.unread[cache.folder.accountId] = {};
}
if (SysTrayX.Messaging.new[cache.folder.accountId] === undefined) {
SysTrayX.Messaging.new[cache.folder.accountId] = {};
}
if (SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path] === undefined) {
SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path] = [];
}
SysTrayX.Messaging.unread[cache.folder.accountId][cache.folder.path] =
cache.folderInfo.unreadMessageCount;
// Check if the new mails have been read, remove from new storage
const messages = SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path];
if (messages.length > 0) {
const newMessages = [];
for (let i = 0; i < messages.length; ++i) {
const message = messages[i];
const getHeaderPromise = (messageId) =>
new Promise((res) => res(messenger.messages.get(messageId)));
const header = await getHeaderPromise(message.id);
if (!header.read) {
newMessages.push(message);
}
}
console.debug("FolderInfoChanged: Clear");
console.debug("FolderInfoChanged: Old: " + JSON.stringify(SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path]));
console.debug("FolderInfoChanged: New: " + JSON.stringify(newMessages));
SysTrayX.Messaging.new[cache.folder.accountId][cache.folder.path] = [
...newMessages,
];
}
}
// Cache handled
SysTrayX.Messaging.folderInfoChangeCache = [];
}
} else {
// Count the initial unread messages
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));
}
if (mailFolderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[folder.accountId] === undefined) {
SysTrayX.Messaging.unread[folder.accountId] = {};
}
SysTrayX.Messaging.unread[folder.accountId][folder.path] =
mailFolderInfo.unreadMessageCount;
}
}
}
}
sendMailCountPre115();
}
} else {
sendMailCount();
sendMailCount2();
}
},
@@ -313,8 +314,8 @@ SysTrayX.Messaging = {
if ("filters" in changes && changes["filters"].newValue) {
SysTrayX.Messaging.filters = changes["filters"].newValue;
getMailCount();
getMailCount2();
sendMailCountPre115();
sendMailCount();
}
if ("closeType" in changes && changes["closeType"].newValue) {
@@ -336,8 +337,8 @@ SysTrayX.Messaging = {
if ("countType" in changes && changes["countType"].newValue) {
SysTrayX.Messaging.countType = changes["countType"].newValue;
getMailCount();
getMailCount2();
sendMailCountPre115();
sendMailCount();
}
if ("addonprefchanged" in changes && changes["addonprefchanged"].newValue) {
@@ -887,31 +888,37 @@ SysTrayX.Window = {
},
folderChanged: function (tab, displayedFolder) {
/*
console.debug("Folder changed tab: " + JSON.stringify(tab));
console.debug(
"Folder changed displayedFolder: " + JSON.stringify(displayedFolder)
);
*/
const oldDisplayedFolder = SysTrayX.Messaging.displayedFolder;
if (oldDisplayedFolder !== undefined) {
if (
SysTrayX.Messaging.new[oldDisplayedFolder.accountId] !== undefined &&
SysTrayX.Messaging.new[oldDisplayedFolder.accountId][
oldDisplayedFolder.path
] !== undefined
) {
SysTrayX.Messaging.new[oldDisplayedFolder.accountId][
oldDisplayedFolder.path
] = [];
if (SysTrayX.Info.browserInfo.majorVersion < 115) {
const oldDisplayedFolder = SysTrayX.Messaging.displayedFolder;
if (oldDisplayedFolder !== undefined) {
if (
SysTrayX.Messaging.new[oldDisplayedFolder.accountId] !== undefined &&
SysTrayX.Messaging.new[oldDisplayedFolder.accountId][
oldDisplayedFolder.path
] !== undefined
) {
SysTrayX.Messaging.new[oldDisplayedFolder.accountId][
oldDisplayedFolder.path
] = [];
}
if (SysTrayX.Messaging.startupDelayFinished) {
sendMailCountPre115();
}
}
getMailCount();
getMailCount2();
SysTrayX.Messaging.displayedFolder = displayedFolder;
} else {
if (SysTrayX.Messaging.startupDelayFinished) {
sendMailCount();
}
}
SysTrayX.Messaging.displayedFolder = displayedFolder;
},
};
@@ -935,6 +942,11 @@ async function start() {
SysTrayX.Info.displayInfo();
// Catch a folder change to reset the new counter
browser.mailTabs.onDisplayedFolderChanged.addListener(
SysTrayX.Window.folderChanged
);
// Get the prefered start state
const state = await getStartupState();
SysTrayX.startupState = state;

View File

@@ -409,85 +409,90 @@ const collectUnreadMail = async () => {
};
// Count and send the unread and new mails
const sendMailCount = () => {
const sendMailCountPre115 = () => {
if (SysTrayX.Info.browserInfo.majorVersion < 115) {
// Collect the unread mail
collectUnreadMail();
// Collect the unread mail
collectUnreadMail();
// Count the collected mail
let unreadCount = 0;
let newCount = 0;
SysTrayX.Messaging.filters.forEach((filter) => {
const accountId = filter.accountId;
filter.folders.forEach((path) => {
if (SysTrayX.Messaging.unread[accountId] !== undefined) {
if (SysTrayX.Messaging.unread[accountId][path] !== undefined) {
unreadCount = unreadCount + SysTrayX.Messaging.unread[accountId][path];
// Count the collected mail
let unreadCount = 0;
let newCount = 0;
SysTrayX.Messaging.filters.forEach((filter) => {
const accountId = filter.accountId;
filter.folders.forEach((path) => {
if (SysTrayX.Messaging.unread[accountId] !== undefined) {
if (SysTrayX.Messaging.unread[accountId][path] !== undefined) {
unreadCount = unreadCount + SysTrayX.Messaging.unread[accountId][path];
}
}
}
if (SysTrayX.Messaging.new[accountId] !== undefined) {
if (SysTrayX.Messaging.new[accountId][path] !== undefined) {
newCount = newCount + SysTrayX.Messaging.new[accountId][path].length;
if (SysTrayX.Messaging.new[accountId] !== undefined) {
if (SysTrayX.Messaging.new[accountId][path] !== undefined) {
newCount = newCount + SysTrayX.Messaging.new[accountId][path].length;
}
}
}
});
});
});
//console.debug("Filters: " + JSON.stringify(SysTrayX.Messaging.filters));
//console.debug("New: " + JSON.stringify(SysTrayX.Messaging.new));
//console.debug("Filters: " + JSON.stringify(SysTrayX.Messaging.filters));
//console.debug("New: " + JSON.stringify(SysTrayX.Messaging.new));
console.debug("sendMailCount Unread: " + unreadCount);
console.debug("sendMailCount New: " + newCount);
console.debug("sendMailCountPre115 Unread: " + unreadCount);
console.debug("sendMailCountPre115 New: " + newCount);
SysTrayX.Link.postSysTrayXMessage( { mailCount: { unread: unreadCount, new: newCount } } );
SysTrayX.Link.postSysTrayXMessage( { mailCount: { unread: unreadCount, new: newCount } } );
}
};
// Count and send the unread and new mails (>TB115)
const sendMailCount2 = async () => {
// New only works for >=TB106
const sendMailCount = async () => {
if (SysTrayX.Info.browserInfo.majorVersion >= 115) {
let unreadCount = 0;
let newCount = 0;
// New only works for >=TB106
for (const filter of SysTrayX.Messaging.filters) {
for (const path of filter.folders) {
const folder = {
accountId: filter.accountId,
path: path,
};
let unreadCount = 0;
let newCount = 0;
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 (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;
}
let messages = listMessages(folder);
for await (let message of messages) {
if( message.new )
{
newCount = newCount + 1;
}
if( !message.read )
{
unreadCount = unreadCount + 1;
if( !message.read )
{
unreadCount = unreadCount + 1;
}
}
}
}
}
console.debug("getMailCount2 Unread: " + unreadCount);
console.debug("getMailCount2 New: " + newCount);
//SysTrayX.Link.postSysTrayXMessage( { mailCount: { unread: unreadCount, new: newCount } } );
console.debug("sendMailCount Unread: " + unreadCount);
console.debug("sendMailCount New: " + newCount);
SysTrayX.Link.postSysTrayXMessage( { mailCount: { unread: unreadCount, new: newCount } } );
}
};