Skip to content

Commit

Permalink
Fix old unread / new mail detect
Browse files Browse the repository at this point in the history
  • Loading branch information
Ximi1970 committed Jul 28, 2024
1 parent 8fb1a55 commit edbf5c8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 192 deletions.
170 changes: 13 additions & 157 deletions webext/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ SysTrayX.Messaging = {
SysTrayX.Messaging.newMailCache.push({ folder, messages });
}

//console.debug("listenerNewMailCache: New mail in: " + JSON.stringify(folder));
//console.debug("listenerNewMailCache: New messages: " + JSON.stringify(messages));
//console.debug("listenerNewMailCache: Cache: " + SysTrayX.Messaging.newMailCache.length );
//console.debug("listenerNewMail: New mail in: " + JSON.stringify(folder));
//console.debug("listenerNewMail: New messages: " + JSON.stringify(messages));
//console.debug("listenerNewMail: Cache: " + SysTrayX.Messaging.newMailCache.length );

if (SysTrayX.Messaging.startupDelayFinished)
{
Expand All @@ -234,172 +234,28 @@ SysTrayX.Messaging = {
}
});

//console.debug("listenerNewMail: new: " + JSON.stringify(SysTrayX.Messaging.new));

// Cache handled
SysTrayX.Messaging.newMailCache = [];
}
},

listenerFolderInfoChanged: async function (folder, folderInfo) {

console.debug("folderInfoChanged: folder: " + JSON.stringify(folder));
console.debug("folderInfoChanged: folderinfo: " + JSON.stringify(folderInfo));
//console.debug("folderInfoChanged: Cache: " + SysTrayX.Messaging.folderInfoChangeCache.length );
//console.debug("listenerFolderInfoChanged: folder: " + JSON.stringify(folder));
//console.debug("listenerFolderInfoChanged: folderinfo: " + JSON.stringify(folderInfo));
//console.debug("listenerFolderInfoChanged: Cache: " + SysTrayX.Messaging.folderInfoChangeCache.length );

if (SysTrayX.Info.browserInfo.majorVersion < 115 || SysTrayX.Messaging.apiCountMethod === "false") {
if (!SysTrayX.Messaging.startupDelayFinished) {
// Cache the folder change
if (folder)
{
SysTrayX.Messaging.folderInfoChangeCache.push({ folder, folderInfo });
}
if (SysTrayX.Messaging.startupDelayFinished) {
if (SysTrayX.Info.browserInfo.majorVersion < 115 || SysTrayX.Messaging.apiCountMethod === "false") {
sendMailCountPre115();
} else {
if (SysTrayX.Messaging.startupDelayFinished)
{
console.debug("folderInfoChanged: delay finished");

if( SysTrayX.Messaging.folderInfoChangeCache.length > 0 ) {

console.debug("folderInfoChanged: handle cache");
console.debug("FolderInfoChanged: cache: " + JSON.stringify(SysTrayX.Messaging.folderInfoChangeCache));

// 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] = [];
}

console.debug("FolderInfoChanged: Unread: " + JSON.stringify(cache.folderInfo.unreadMessageCount));

SysTrayX.Messaging.unread[cache.folder.accountId][cache.folder.path] =
cache.folderInfo.unreadMessageCount;

// Check if the new mails have been read, removed 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 {

console.debug("folderInfoChanged: handle default");

// Count the initial unread messages
for (const filter of SysTrayX.Messaging.filters) {
const accountId = filter.accountId;
for (const storedFolder of filter.folders) {
let path;
let folderParam;
if (typeof(storedFolder) === "string") {
// Filters pre TB 121
path = storedFolder;
folderParam = {
accountId: accountId,
path: storedFolder,
};
} else {
// Filters TB 121
if (storedFolder.mailFolderId === undefined) {
// TB 121 filter setup but older TB
path = storedFolder.path;
folderParam = {
accountId: accountId,
path: path,
};
} else {
path = storedFolder.path;
folderParam = storedFolder.mailFolderId;
}
}

// Check unread mails
let mailFolderInfo = {};
try {
mailFolderInfo = await browser.folders.getFolderInfo(folderParam);
} catch (err) {
console.debug("Filter error: " + err);
console.debug("Filter error: " + JSON.stringify(folder));
}

if (mailFolderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[accountId] === undefined) {
SysTrayX.Messaging.unread[accountId] = {};
}

SysTrayX.Messaging.unread[accountId][path] =
mailFolderInfo.unreadMessageCount;
}

// Check if the new mails have been read, removed from new storage
if (SysTrayX.Messaging.new[accountId] !== undefined &&
SysTrayX.Messaging.new[accountId][path] !== undefined ) {
const messages = SysTrayX.Messaging.new[accountId][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);
}
}

SysTrayX.Messaging.new[accountId][path] = [
...newMessages,
];
}
}
}
}
}
sendMailCountPre115();
}
}
} else {
if (SysTrayX.Messaging.startupDelayFinished)
{
sendMailCount();
}
}
}
},

listenerFolderCreated: function (createdFolder) {
const found = isParentFolderInFilters(createdFolder);
if (found) {
Expand Down
98 changes: 63 additions & 35 deletions webext/js/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,50 +577,50 @@ async function addFolderToFilters(newFolder) {

// Collect unread mail
const collectUnreadMail = async () => {
// Count the initial unread messages
for (const filter of SysTrayX.Messaging.filters) {

const accountId = filter.accountId;
for (const storedFolder of filter.folders) {
let mailFolderInfo = {};

let path;
let folderParam;
if (typeof(storedFolder) === "string") {
// Filters pre TB 121
path = storedFolder;

try {
mailFolderInfo = await browser.folders.getFolderInfo({
accountId: accountId,
path: storedFolder,
});
} catch (err) {
//console.debug("Filter error: " + err);
//console.debug("Filter error: " + JSON.stringify(folder));

// Get all accounts
SysTrayX.Messaging.accounts = await browser.accounts.list();

// Check the filters for the accounts
SysTrayX.Messaging.accountFilterCheck();
}
folderParam = {
accountId: accountId,
path: storedFolder,
};
} else {
// Filters TB 121
path = storedFolder.path;

try {
mailFolderInfo = await browser.folders.getFolderInfo(storedFolder.mailFolderId);
} catch (err) {
//console.debug("Filter error: " + err);
//console.debug("Filter error: " + JSON.stringify(folder));

// Get all accounts
SysTrayX.Messaging.accounts = await browser.accounts.list();

// Check the filters for the accounts
SysTrayX.Messaging.accountFilterCheck();
if (storedFolder.mailFolderId === undefined ||
SysTrayX.Info.browserInfo.majorVersion < 121) {
// TB 121 filter setup but older TB
path = storedFolder.path;
folderParam = {
accountId: accountId,
path: path,
};
} else {
path = storedFolder.path;
folderParam = storedFolder.mailFolderId;
}
}

// Check unread mails
let mailFolderInfo = {};
try {
mailFolderInfo = await browser.folders.getFolderInfo(folderParam);
} catch (err) {
//console.debug("Filter error: " + err);
//console.debug("Filter error: " + JSON.stringify(folder));

// Get all accounts
SysTrayX.Messaging.accounts = await browser.accounts.list();

// Check the filters for the accounts
SysTrayX.Messaging.accountFilterCheck();
}

if (mailFolderInfo.unreadMessageCount !== undefined) {
if (SysTrayX.Messaging.unread[accountId] === undefined) {
SysTrayX.Messaging.unread[accountId] = {};
Expand All @@ -639,16 +639,41 @@ const collectUnreadMail = async () => {
SysTrayX.Messaging.unread[accountId][path] =
mailFolderInfo.unreadMessageCount;
}

// Check if the new mails have been read, removed from new storage
if (SysTrayX.Messaging.new[accountId] !== undefined &&
SysTrayX.Messaging.new[accountId][path] !== undefined ) {
const messages = SysTrayX.Messaging.new[accountId][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);
}
}

SysTrayX.Messaging.new[accountId][path] = [
...newMessages,
];
}
}
}
}
};
}

// Count and send the unread and new mails
const sendMailCountPre115 = () => {
const sendMailCountPre115 = async () => {
if (SysTrayX.Info.browserInfo.majorVersion < 115 || SysTrayX.Messaging.apiCountMethod === "false") {

// Collect the unread mail
collectUnreadMail();
await collectUnreadMail();

// Count the collected mail
let unreadCount = 0;
Expand Down Expand Up @@ -682,6 +707,9 @@ const sendMailCountPre115 = () => {
//console.debug("Filters: " + JSON.stringify(SysTrayX.Messaging.filters));
//console.debug("New: " + JSON.stringify(SysTrayX.Messaging.new));

//console.debug("sendMailCountPre115 Unread storage: " + JSON.stringify(SysTrayX.Messaging.unread));
//console.debug("sendMailCountPre115 New storage: " + JSON.stringify(SysTrayX.Messaging.new));

//console.debug("sendMailCountPre115 Unread: " + unreadCount);
//console.debug("sendMailCountPre115 New: " + newCount);

Expand Down

0 comments on commit edbf5c8

Please sign in to comment.