Refractoring Stage 6.10 Completed the next renderer modularization slice. mail-ui.js now uses an explicit epMailShell API for core UI helpers and renderer hooks, and the OpenPGP key UI uses feature APIs instead of legacy globals.

This commit is contained in:
2026-10-16 22:05:31 +03:00
parent 718ceb6efe
commit 3013e489ed
10 changed files with 490 additions and 437 deletions
+49 -47
View File
@@ -1,8 +1,13 @@
// Encryption key management (desktop). Hidden where the platform has no key store yet.
const pgpKeyMailUi = window.epMailFeatures.mail;
const selectPgpKeyUi = pgpKeyMailUi.select;
const selectAllPgpKeyUi = pgpKeyMailUi.selectAll;
const escapePgpKeyUi = pgpKeyMailUi.escapeHtml;
const toastPgpKeyUi = pgpKeyMailUi.toast;
function pgpAvailable() { return Boolean(window.daybreakMail?.pgp); }
function openEncryptionKeys() {
const root = $('#modal-root');
const root = selectPgpKeyUi('#modal-root');
const account = window.epMailFeatures.mail.connectedAccount || {};
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal attachments-modal"><div class="modal-head"><span>Encryption keys</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields">
<p class="account-note">OpenPGP keys are protected by a passphrase and stored encrypted by your operating system. Back up your private key and revocation certificate: without them, encrypted mail cannot be recovered. Contact keys below are used to encrypt mail to your correspondents and to check signatures.</p>
@@ -18,7 +23,7 @@ function openEncryptionKeys() {
<h3 class="pgp-heading">Public key servers</h3>
<p class="account-note">Servers are disabled by default. Enable the servers you want to use; searching checks every enabled server, which can learn which email address you searched for. Discovered keys remain unverified until you check their fingerprints independently.</p>
<div id="pgp-server-list" class="attachment-list">Loading key servers…</div>
<label class="compose-line"><span>Email address</span><input id="pgp-search-email" type="email" value="${esc(account.email || '')}"></label>
<label class="compose-line"><span>Email address</span><input id="pgp-search-email" type="email" value="${escapePgpKeyUi(account.email || '')}"></label>
<div class="pgp-actions"><button class="discard" id="pgp-find-key">Search all enabled servers</button></div>
<div id="pgp-found-keys" class="attachment-list"></div>
<label class="compose-line"><span>Publish to one server</span><select id="pgp-publish-server"></select></label>
@@ -33,8 +38,8 @@ function openEncryptionKeys() {
<div class="pgp-actions"><button class="discard" id="pgp-save-cache">Save</button><button class="discard" id="pgp-forget">Forget passphrases now</button></div>
<p class="account-note">Unlocked passphrases are kept in memory only, for this many minutes (0 asks every time). Opening encrypted attachments needs the passphrase to be remembered.</p>
<h3 class="pgp-heading">Create a key</h3>
<label class="compose-line"><span>Name</span><input id="pgp-name" maxlength="100" value="${esc(account.displayName || '')}"></label>
<label class="compose-line"><span>Email</span><input id="pgp-email" type="email" value="${esc(account.email || '')}"></label>
<label class="compose-line"><span>Name</span><input id="pgp-name" maxlength="100" value="${escapePgpKeyUi(account.displayName || '')}"></label>
<label class="compose-line"><span>Email</span><input id="pgp-email" type="email" value="${escapePgpKeyUi(account.email || '')}"></label>
<label class="compose-line"><span>Passphrase</span><input id="pgp-passphrase" type="password" autocomplete="new-password" placeholder="At least 8 characters"></label>
<div class="pgp-actions"><button class="send-button" id="pgp-generate">Create key</button><button class="discard" id="pgp-import">Import private key…</button></div>
<p class="account-note" id="pgp-status" role="status"></p>
@@ -42,13 +47,13 @@ function openEncryptionKeys() {
const close = () => { root.innerHTML = ''; };
root.querySelector('.modal-close').onclick = close;
root.querySelector('.modal-overlay').onclick = event => { if (event.target.classList.contains('modal-overlay')) close(); };
const status = message => { $('#pgp-status').textContent = message; };
const passphraseField = () => $('#pgp-passphrase');
const status = message => { selectPgpKeyUi('#pgp-status').textContent = message; };
const passphraseField = () => selectPgpKeyUi('#pgp-passphrase');
let keyServers = [];
const selectedPublishServer = () => keyServers.find(server => server.id === $('#pgp-publish-server').value && server.enabled);
const selectedPublishServer = () => keyServers.find(server => server.id === selectPgpKeyUi('#pgp-publish-server').value && server.enabled);
const updatePublishButtons = () => {
const server = selectedPublishServer();
$$('.pgp-publish').forEach(button => {
selectAllPgpKeyUi('.pgp-publish').forEach(button => {
button.disabled = !server;
button.textContent = server ? `Publish to ${server.name}…` : 'Enable a server to publish…';
});
@@ -60,7 +65,7 @@ function openEncryptionKeys() {
};
async function render() {
const list = $('#pgp-key-list');
const list = selectPgpKeyUi('#pgp-key-list');
let keys = [];
try { keys = await window.daybreakMail.pgp.list(); } catch (error) { list.textContent = error.message || 'Could not load keys.'; return; }
list.replaceChildren();
@@ -86,21 +91,21 @@ function openEncryptionKeys() {
changePassphrase.className = 'discard';
changePassphrase.textContent = 'Change passphrase…';
changePassphrase.onclick = async () => {
const currentPassphrase = $('#pgp-change-current-passphrase').value;
const currentPassphrase = selectPgpKeyUi('#pgp-change-current-passphrase').value;
if (!currentPassphrase) {
status('Enter the current passphrase for this key.');
$('#pgp-change-current-passphrase').focus();
selectPgpKeyUi('#pgp-change-current-passphrase').focus();
return;
}
const newPassphrase = $('#pgp-new-passphrase').value;
const newPassphrase = selectPgpKeyUi('#pgp-new-passphrase').value;
if (newPassphrase.length < 8) {
status('Use a new passphrase of at least 8 characters.');
$('#pgp-new-passphrase').focus();
selectPgpKeyUi('#pgp-new-passphrase').focus();
return;
}
if (newPassphrase !== $('#pgp-confirm-passphrase').value) {
if (newPassphrase !== selectPgpKeyUi('#pgp-confirm-passphrase').value) {
status('The new passphrases do not match.');
$('#pgp-confirm-passphrase').focus();
selectPgpKeyUi('#pgp-confirm-passphrase').focus();
return;
}
changePassphrase.disabled = true;
@@ -110,12 +115,12 @@ function openEncryptionKeys() {
currentPassphrase,
newPassphrase
});
$('#pgp-change-current-passphrase').value = '';
selectPgpKeyUi('#pgp-change-current-passphrase').value = '';
status(`Passphrase changed for ${key.email || key.fingerprint}.`);
} catch (error) { status(error.message || 'Could not change the key passphrase.'); }
finally {
$('#pgp-new-passphrase').value = '';
$('#pgp-confirm-passphrase').value = '';
selectPgpKeyUi('#pgp-new-passphrase').value = '';
selectPgpKeyUi('#pgp-confirm-passphrase').value = '';
changePassphrase.disabled = false;
}
};
@@ -164,20 +169,20 @@ function openEncryptionKeys() {
updatePublishButtons();
}
$('#pgp-generate').onclick = async () => {
const button = $('#pgp-generate');
selectPgpKeyUi('#pgp-generate').onclick = async () => {
const button = selectPgpKeyUi('#pgp-generate');
button.disabled = true;
status('Creating key…');
try {
await window.daybreakMail.pgp.generate({
name: $('#pgp-name').value, email: $('#pgp-email').value, passphrase: passphraseField().value, accountId: account.id || null
name: selectPgpKeyUi('#pgp-name').value, email: selectPgpKeyUi('#pgp-email').value, passphrase: passphraseField().value, accountId: account.id || null
});
status('Key created. Export your private key and revocation certificate now and keep them safe.');
await render();
} catch (error) { status(error.message || 'Could not create the key.'); }
button.disabled = false;
};
$('#pgp-import').onclick = async () => {
selectPgpKeyUi('#pgp-import').onclick = async () => {
const passphrase = needPassphrase();
if (!passphrase) return;
try {
@@ -187,7 +192,7 @@ function openEncryptionKeys() {
} catch (error) { status(error.message || 'Could not import the key.'); }
};
async function renderContacts() {
const list = $('#pgp-contact-list');
const list = selectPgpKeyUi('#pgp-contact-list');
let contacts = [];
try { contacts = await window.daybreakMail.pgp.contacts(); } catch (error) { list.textContent = error.message || 'Could not load contacts.'; return; }
list.replaceChildren();
@@ -244,7 +249,7 @@ function openEncryptionKeys() {
});
}
$('#pgp-add-contact').onclick = async () => {
selectPgpKeyUi('#pgp-add-contact').onclick = async () => {
try {
const result = await window.daybreakMail.pgp.addContact({});
if (!result.canceled) status(result.keyChangedFrom?.length ? 'Key added. Warning: you already had a different key for this address. Verify the new fingerprint.' : 'Public key added. Verify its fingerprint with the owner, then mark it as verified.');
@@ -252,8 +257,8 @@ function openEncryptionKeys() {
} catch (error) { status(error.message || 'Could not import the key.'); }
};
async function renderKeyServers() {
const list = $('#pgp-server-list');
const selector = $('#pgp-publish-server');
const list = selectPgpKeyUi('#pgp-server-list');
const selector = selectPgpKeyUi('#pgp-publish-server');
const selectedId = selector.value;
try { keyServers = await window.daybreakMail.pgp.keyServers(); }
catch (error) { list.textContent = error.message || 'Could not load key servers.'; return; }
@@ -321,14 +326,14 @@ function openEncryptionKeys() {
list.append(row);
});
}
$('#pgp-find-key').onclick = async () => {
const button = $('#pgp-find-key');
const results = $('#pgp-found-keys');
selectPgpKeyUi('#pgp-find-key').onclick = async () => {
const button = selectPgpKeyUi('#pgp-find-key');
const results = selectPgpKeyUi('#pgp-found-keys');
button.disabled = true;
results.textContent = 'Searching…';
try {
const found = await window.daybreakMail.pgp.findKey({
email: $('#pgp-search-email').value
email: selectPgpKeyUi('#pgp-search-email').value
});
results.replaceChildren();
found.keys.forEach(key => {
@@ -374,39 +379,37 @@ function openEncryptionKeys() {
status(error.message || 'Could not search the key server.');
} finally { button.disabled = false; }
};
$('#pgp-add-server').onclick = async () => {
const button = $('#pgp-add-server');
selectPgpKeyUi('#pgp-add-server').onclick = async () => {
const button = selectPgpKeyUi('#pgp-add-server');
button.disabled = true;
try {
const result = await window.daybreakMail.pgp.addKeyServer({
name: $('#pgp-server-name').value,
url: $('#pgp-server-url').value,
protocol: $('#pgp-server-protocol').value
name: selectPgpKeyUi('#pgp-server-name').value,
url: selectPgpKeyUi('#pgp-server-url').value,
protocol: selectPgpKeyUi('#pgp-server-protocol').value
});
if (result.canceled) return;
$('#pgp-server-name').value = '';
$('#pgp-server-url').value = '';
selectPgpKeyUi('#pgp-server-name').value = '';
selectPgpKeyUi('#pgp-server-url').value = '';
await renderKeyServers();
status('Custom key server added.');
} catch (error) { status(error.message || 'Could not add the key server.'); }
finally { button.disabled = false; }
};
window.daybreakMail.pgp.getSettings().then(settings => { $('#pgp-cache-minutes').value = settings.cacheMinutes; }).catch(() => {});
$('#pgp-save-cache').onclick = async () => {
try { const settings = await window.daybreakMail.pgp.setSettings({ cacheMinutes: Number($('#pgp-cache-minutes').value) }); status(`Passphrases are remembered for ${settings.cacheMinutes} minute(s).`); }
window.daybreakMail.pgp.getSettings().then(settings => { selectPgpKeyUi('#pgp-cache-minutes').value = settings.cacheMinutes; }).catch(() => {});
selectPgpKeyUi('#pgp-save-cache').onclick = async () => {
try { const settings = await window.daybreakMail.pgp.setSettings({ cacheMinutes: Number(selectPgpKeyUi('#pgp-cache-minutes').value) }); status(`Passphrases are remembered for ${settings.cacheMinutes} minute(s).`); }
catch (error) { status(error.message || 'Could not save the setting.'); }
};
$('#pgp-forget').onclick = async () => { await window.daybreakMail.pgp.forgetPassphrases(); status('Remembered passphrases were cleared.'); };
selectPgpKeyUi('#pgp-forget').onclick = async () => { await window.daybreakMail.pgp.forgetPassphrases(); status('Remembered passphrases were cleared.'); };
void renderKeyServers();
void renderContacts();
void render();
}
if (typeof openPreferences === 'function') {
const baseOpenPreferences = openPreferences;
openPreferences = function () {
window.epMailFeatures.productivity.wrapOpenPreferences(baseOpenPreferences => function () {
baseOpenPreferences();
const fields = $('#modal-root .compose-fields');
const fields = selectPgpKeyUi('#modal-root .compose-fields');
if (!fields || !pgpAvailable()) return;
const button = document.createElement('button');
button.type = 'button';
@@ -415,5 +418,4 @@ if (typeof openPreferences === 'function') {
button.textContent = 'Encryption keys…';
button.onclick = openEncryptionKeys;
fields.append(button);
};
}
});