diff --git a/TODO.md b/TODO.md index 659eb49..a30e24e 100644 --- a/TODO.md +++ b/TODO.md @@ -4,6 +4,7 @@ - [x] Rename the program to **ep-mail** across the app UI, package metadata, desktop builds, and documentation. - [x] Replace the temporary sun/asterisk app mark with the Git and webmail logo, using the EP Daylight and EP Wasteland color variants. +- [x] Show the app version after **ep-mail** in the window title. ## Mail security @@ -18,6 +19,8 @@ ## Account settings +- [ ] Fix the mailbox space indicator; it is not working. +- [x] Fix automatic mail sync so the message list refreshes when new mail arrives. - [x] Sync all connected accounts' mailboxes in the Electron main process at launch/resume and periodically in the background. - [x] Track `UIDVALIDITY`, the last processed UID, and `HIGHESTMODSEQ` per account and folder; fall back to checking message flags when mod-sequences are unavailable. - [ ] Add persistent IMAP IDLE watchers; bounded periodic polling currently provides updates and reconnect/backoff. diff --git a/desktop/main.cjs b/desktop/main.cjs index 4b5d632..87267e2 100644 --- a/desktop/main.cjs +++ b/desktop/main.cjs @@ -444,6 +444,8 @@ async function runBackgroundSync() { } } +ipcMain.handle('app:get-version', () => app.getVersion()); + ipcMain.handle('mail:get-account', async () => { const account = await readAccount(); return account ? publicAccount(account) : null; @@ -813,7 +815,7 @@ function createWindow() { height: 920, minWidth: 920, minHeight: 680, - title: 'ep-mail', + title: `ep-mail v${app.getVersion()}`, backgroundColor: '#f4f5f1', show: false, autoHideMenuBar: true, diff --git a/desktop/preload.cjs b/desktop/preload.cjs index 5603bb7..9e996a8 100644 --- a/desktop/preload.cjs +++ b/desktop/preload.cjs @@ -1,6 +1,7 @@ const { contextBridge, ipcRenderer } = require('electron'); contextBridge.exposeInMainWorld('daybreakMail', { + getAppVersion: () => ipcRenderer.invoke('app:get-version'), getAccount: () => ipcRenderer.invoke('mail:get-account'), listAccounts: () => ipcRenderer.invoke('mail:list-accounts'), selectAccount: id => ipcRenderer.invoke('mail:select-account', id), diff --git a/mail-ui.js b/mail-ui.js index f5bc420..d943b8d 100644 --- a/mail-ui.js +++ b/mail-ui.js @@ -1,6 +1,9 @@ let connectedMailAccount = null; let mailPageOffset = 0; let mailTotal = 0; +let mailboxSyncPromise = null; +let mailboxSyncQueued = false; +let mailboxRefreshPending = false; function updateAccountCard(account) { const card = $('#account-settings'); @@ -73,61 +76,102 @@ function openMailAccount(existingAccount = null) { $('#mail-account-email').focus(); } async function syncMailbox(showToast, options = {}) { + if (mailboxSyncPromise) { + mailboxSyncQueued = true; + return mailboxSyncPromise; + } if (!connectedMailAccount || !window.daybreakMail) { openMailAccount(); return; } const refresh = $('#refresh'); if (refresh) refresh.disabled = true; - try { - const offset = options.offset ?? (options.append ? mailPageOffset + messages.length : mailPageOffset); - const result = await window.daybreakMail.syncFolder(activeFolder, offset); - if (options.append) messages.push(...result.messages); - else messages.splice(0, messages.length, ...result.messages); - messages.forEach(message => { - try { - const localTags = JSON.parse(localStorage.getItem(`daybreak-message-labels-${message.id}`) || '[]'); - message.tags = [...new Set([...(message.tags || []), ...localTags])]; - } catch {} - }); - if (!options.append) mailPageOffset = offset; - mailTotal = result.total; - activeMessage = messages[0]?.id; - selectedIds.clear(); - const unread = messages.filter(message => message.unread).length; - const inboxCount = $('.folder[data-folder="Inbox"] .count'); - if (inboxCount) inboxCount.textContent = String(unread); - const pagination = $('#pagination'); - if (pagination) pagination.textContent = result.total ? `${options.append ? 1 : result.start}–${options.append ? Math.min(offset + result.messages.length, result.total) : result.end} of ${result.total}` : '0 messages'; - const loadMore = $('#load-more'); - if (loadMore) loadMore.classList.toggle('hidden', mailPageOffset + messages.length >= result.total); - renderList(); - if (activeMessage) { - openMessage(activeMessage); - const current = messages.find(message => message.id === activeMessage); - if (current?.uid) window.daybreakMail.updateMessage({ uid: current.uid, folder: activeFolder, action: 'unread', enabled: false }).catch(() => {}); + const sync = (async () => { + try { + const previouslyActiveMessage = activeMessage; + const offset = options.offset ?? (options.append ? mailPageOffset + messages.length : mailPageOffset); + const result = await window.daybreakMail.syncFolder(activeFolder, offset); + if (options.append) messages.push(...result.messages); + else messages.splice(0, messages.length, ...result.messages); + messages.forEach(message => { + try { + const localTags = JSON.parse(localStorage.getItem(`daybreak-message-labels-${message.id}`) || '[]'); + message.tags = [...new Set([...(message.tags || []), ...localTags])]; + } catch {} + }); + if (!options.append) mailPageOffset = offset; + mailTotal = result.total; + activeMessage = messages.some(message => message.id === previouslyActiveMessage) + ? previouslyActiveMessage + : messages[0]?.id; + selectedIds.clear(); + const unread = messages.filter(message => message.unread).length; + const inboxCount = $('.folder[data-folder="Inbox"] .count'); + if (inboxCount) inboxCount.textContent = String(unread); + const pagination = $('#pagination'); + if (pagination) pagination.textContent = result.total ? `${options.append ? 1 : result.start}–${options.append ? Math.min(offset + result.messages.length, result.total) : result.end} of ${result.total}` : '0 messages'; + const loadMore = $('#load-more'); + if (loadMore) loadMore.classList.toggle('hidden', mailPageOffset + messages.length >= result.total); + renderList(); + if (activeMessage) { + openMessage(activeMessage); + const current = messages.find(message => message.id === activeMessage); + if (current?.uid) window.daybreakMail.updateMessage({ uid: current.uid, folder: activeFolder, action: 'unread', enabled: false }).catch(() => {}); + } + else $('#reader-pane').innerHTML = emptyReader(); + updateAccountCard(connectedMailAccount); + if (showToast) toast(`Synced ${messages.length} messages`); + } catch (error) { + toast(error.message || 'Could not sync the inbox'); + } finally { + if (refresh) refresh.disabled = false; } - else $('#reader-pane').innerHTML = emptyReader(); - updateAccountCard(connectedMailAccount); - if (showToast) toast(`Synced ${messages.length} messages`); - } catch (error) { - toast(error.message || 'Could not sync the inbox'); + })(); + mailboxSyncPromise = sync; + try { + await sync; } finally { - if (refresh) refresh.disabled = false; + mailboxSyncPromise = null; + if (mailboxSyncQueued) { + mailboxSyncQueued = false; + void syncMailbox(false); + } } } if (window.daybreakMail) { + window.daybreakMail.getAppVersion().then(version => { + document.title = `ep-mail v${version}`; + }).catch(error => { + console.error('Could not read app version:', error); + }); window.daybreakMail.onSyncUpdate(update => { if (update.accountId !== connectedMailAccount?.id) return; if (update.error) { toast(update.error); return; } - if ((update.foldersChanged || []).includes(activeFolder)) { + if (update.newCount || (update.foldersChanged || []).length) { + if (document.visibilityState === 'visible') { + syncMailbox(false, update.newCount ? { offset: 0 } : {}); + } + else mailboxRefreshPending = true; + } + }); + document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'visible' && mailboxRefreshPending) { + mailboxRefreshPending = false; syncMailbox(false, { offset: 0 }); } }); + window.addEventListener('focus', () => { + if (connectedMailAccount) syncMailbox(false); + }); + setInterval(() => { + if (connectedMailAccount && document.visibilityState === 'visible' && activeView === 'mail') { + syncMailbox(false); + } + }, 60 * 1000); $('#account-settings').addEventListener('click', openMailAccount); $('#account-settings').addEventListener('keydown', event => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); openMailAccount(); } diff --git a/package-lock.json b/package-lock.json index b940524..a2ffa4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ep-mail", - "version": "0.1.13", + "version": "0.1.14", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ep-mail", - "version": "0.1.13", + "version": "0.1.14", "dependencies": { "ical.js": "^2.2.1", "imapflow": "2.2.1", diff --git a/package.json b/package.json index 1b6dd0e..29d94c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ep-mail", - "version": "0.1.13", + "version": "0.1.14", "private": true, "description": "Eternity Project mail client", "author": "Eternity Project",