Refractoring Stage 6.6 Migrated productivity.js and shortcuts.js mail-state access and render/preferences hooks to the mail feature API.
This commit is contained in:
+28
-28
@@ -1,8 +1,8 @@
|
||||
// Reading and sending OpenPGP mail (desktop). Inert where there is no key store.
|
||||
const mailUi = window.epMailFeatures.mail;
|
||||
const $ = mailUi.select;
|
||||
const $$ = mailUi.selectAll;
|
||||
const toast = mailUi.toast;
|
||||
const pgpMailUi = window.epMailFeatures.mail;
|
||||
const selectPgpMailUi = pgpMailUi.select;
|
||||
const selectAllPgpMailUi = pgpMailUi.selectAll;
|
||||
const toastPgpMailUi = pgpMailUi.toast;
|
||||
const pgpApi = () => window.daybreakMail?.pgp;
|
||||
|
||||
function pgpAskPassphrase(title, detail) {
|
||||
@@ -56,14 +56,14 @@ async function pgpAddAttachedKey(key) {
|
||||
if (!confirm(`Add this public key for ${emails}?\nFingerprint: ${pgpFormatFingerprint(key.fingerprint)}\n\nCompare the fingerprint with the owner before relying on it.`)) return;
|
||||
try {
|
||||
const result = await pgpApi().addContact({ armoredKey: key.armoredKey, source: 'message' });
|
||||
toast(result.keyChangedFrom?.length ? 'Key added. Warning: you already had a different key for this address.' : 'Public key added to your contacts');
|
||||
toastPgpMailUi(result.keyChangedFrom?.length ? 'Key added. Warning: you already had a different key for this address.' : 'Public key added to your contacts');
|
||||
} catch (error) {
|
||||
toast(error.message || 'Could not add the key');
|
||||
toastPgpMailUi(error.message || 'Could not add the key');
|
||||
}
|
||||
}
|
||||
|
||||
function pgpRenderReaderState(message) {
|
||||
const reader = $('#reader-pane');
|
||||
const reader = selectPgpMailUi('#reader-pane');
|
||||
const header = reader.querySelector('.reader-header');
|
||||
if (!header) return;
|
||||
reader.querySelectorAll('.pgp-status, .pgp-banner').forEach(node => node.remove());
|
||||
@@ -80,7 +80,7 @@ function pgpRenderReaderState(message) {
|
||||
reader.querySelectorAll('[data-pgp-key]').forEach(button => {
|
||||
button.onclick = () => pgpAddAttachedKey(keys[Number(button.dataset.pgpKey)]);
|
||||
});
|
||||
const unlock = $('#pgp-unlock');
|
||||
const unlock = selectPgpMailUi('#pgp-unlock');
|
||||
if (unlock) unlock.onclick = () => pgpUnlockMessage(message, true);
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ async function pgpUnlockMessage(message, interactive) {
|
||||
try {
|
||||
result = await pgpApi().decryptMessage({ folder: message.sourceFolder || 'Inbox', uid: message.uid, passphrase });
|
||||
} catch (error) {
|
||||
toast(error.message || 'Could not open this message');
|
||||
toastPgpMailUi(error.message || 'Could not open this message');
|
||||
return;
|
||||
}
|
||||
if (result.ok) {
|
||||
@@ -99,7 +99,7 @@ async function pgpUnlockMessage(message, interactive) {
|
||||
body: result.body, html: result.html, links: result.links, attachments: result.attachments, attachment: result.attachment, size: result.size,
|
||||
snippet: 'Encrypted message', pgpDecrypted: true, pgpResult: { signatures: result.signatures, attachedKeys: result.attachedKeys }
|
||||
});
|
||||
if (mailUi.state.activeMessage === message.id) mailUi.openMessage(message.id);
|
||||
if (pgpMailUi.state.activeMessage === message.id) pgpMailUi.openMessage(message.id);
|
||||
return;
|
||||
}
|
||||
if (result.code === 'PASSPHRASE_REQUIRED' || result.code === 'BAD_PASSPHRASE') {
|
||||
@@ -108,16 +108,16 @@ async function pgpUnlockMessage(message, interactive) {
|
||||
if (passphrase === null) return;
|
||||
continue;
|
||||
}
|
||||
const text = $('#pgp-banner-text');
|
||||
const text = selectPgpMailUi('#pgp-banner-text');
|
||||
if (text) text.textContent = result.message;
|
||||
else toast(result.message);
|
||||
else toastPgpMailUi(result.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mailUi.wrapOpenMessage(baseOpenMessage => function (id) {
|
||||
pgpMailUi.wrapOpenMessage(baseOpenMessage => function (id) {
|
||||
baseOpenMessage(id);
|
||||
const message = mailUi.state.messages.find(item => item.id === id);
|
||||
const message = pgpMailUi.state.messages.find(item => item.id === id);
|
||||
if (!message || !pgpApi()) return;
|
||||
pgpRenderReaderState(message);
|
||||
if (message.encrypted && !message.pgpDecrypted && !message.pgpAutoTried) {
|
||||
@@ -127,30 +127,30 @@ mailUi.wrapOpenMessage(baseOpenMessage => function (id) {
|
||||
});
|
||||
|
||||
// Compose: encrypt/sign options.
|
||||
mailUi.wrapShowCompose(baseShowCompose => function (reply, forward = false) {
|
||||
pgpMailUi.wrapShowCompose(baseShowCompose => function (reply, forward = false) {
|
||||
baseShowCompose(reply, forward);
|
||||
const footer = $('#modal-root .compose-footer');
|
||||
const footer = selectPgpMailUi('#modal-root .compose-footer');
|
||||
if (!footer || !pgpApi()) return;
|
||||
const box = document.createElement('div');
|
||||
box.className = 'pgp-compose';
|
||||
box.innerHTML = `<label><input type="checkbox" id="pgp-encrypt"> Encrypt</label><label><input type="checkbox" id="pgp-sign"> Sign</label><small id="pgp-hint"></small>`;
|
||||
footer.prepend(box);
|
||||
let touched = false;
|
||||
const encrypt = $('#pgp-encrypt');
|
||||
const sign = $('#pgp-sign');
|
||||
const encrypt = selectPgpMailUi('#pgp-encrypt');
|
||||
const sign = selectPgpMailUi('#pgp-sign');
|
||||
if (reply?.encrypted) { encrypt.checked = true; sign.checked = true; touched = true; }
|
||||
if (touched) encrypt.dataset.touched = '1';
|
||||
encrypt.addEventListener('change', () => { touched = true; encrypt.dataset.touched = '1'; });
|
||||
let timer = null;
|
||||
const refresh = async () => {
|
||||
const to = $('#compose-to')?.value || '';
|
||||
const cc = $('#compose-cc')?.value || '';
|
||||
const hint = $('#pgp-hint');
|
||||
const to = selectPgpMailUi('#compose-to')?.value || '';
|
||||
const cc = selectPgpMailUi('#compose-cc')?.value || '';
|
||||
const hint = selectPgpMailUi('#pgp-hint');
|
||||
if (!hint) return;
|
||||
if (!to.trim()) { hint.textContent = ''; return; }
|
||||
try {
|
||||
const status = await pgpApi().checkSend({ to, cc, sign: sign.checked });
|
||||
if (!$('#pgp-hint')) return;
|
||||
if (!selectPgpMailUi('#pgp-hint')) return;
|
||||
if (status.missing.length) hint.textContent = `No public key for ${status.missing.join(', ')}`;
|
||||
else {
|
||||
hint.textContent = status.recipients.some(item => item.trust === 'unknown') ? 'Keys found; some are not verified yet' : 'Keys found for all recipients';
|
||||
@@ -159,7 +159,7 @@ mailUi.wrapShowCompose(baseShowCompose => function (reply, forward = false) {
|
||||
} catch { hint.textContent = ''; }
|
||||
};
|
||||
const schedule = () => { clearTimeout(timer); timer = setTimeout(refresh, 300); };
|
||||
['#compose-to', '#compose-cc'].forEach(selector => $(selector)?.addEventListener('input', schedule));
|
||||
['#compose-to', '#compose-cc'].forEach(selector => selectPgpMailUi(selector)?.addEventListener('input', schedule));
|
||||
sign.addEventListener('change', schedule);
|
||||
schedule();
|
||||
});
|
||||
@@ -167,12 +167,12 @@ mailUi.wrapShowCompose(baseShowCompose => function (reply, forward = false) {
|
||||
// Called by the send handler. Returns null (plain mail), false (cancelled) or send options.
|
||||
async function prepareSend(to, cc) {
|
||||
if (!pgpApi()) return null;
|
||||
let encrypt = Boolean($('#pgp-encrypt')?.checked);
|
||||
const sign = Boolean($('#pgp-sign')?.checked);
|
||||
if (!encrypt && !sign && $('#pgp-encrypt')?.dataset.touched === '1') return null;
|
||||
let encrypt = Boolean(selectPgpMailUi('#pgp-encrypt')?.checked);
|
||||
const sign = Boolean(selectPgpMailUi('#pgp-sign')?.checked);
|
||||
if (!encrypt && !sign && selectPgpMailUi('#pgp-encrypt')?.dataset.touched === '1') return null;
|
||||
const status = await pgpApi().checkSend({ to, cc, sign });
|
||||
// The default-on update is debounced, so a quick send could skip encryption even though keys exist.
|
||||
if (!encrypt && !status.missing.length && status.recipients.length && $('#pgp-encrypt')?.dataset.touched !== '1') {
|
||||
if (!encrypt && !status.missing.length && status.recipients.length && selectPgpMailUi('#pgp-encrypt')?.dataset.touched !== '1') {
|
||||
if (confirm('Public keys are available for all recipients.\n\nSend this message WITHOUT encryption?')) {
|
||||
if (!sign) return null;
|
||||
} else return false;
|
||||
@@ -186,7 +186,7 @@ async function prepareSend(to, cc) {
|
||||
}
|
||||
let passphrase = null;
|
||||
if (sign) {
|
||||
if (!status.signer) { toast('Create or import an encryption key for your address before signing'); return false; }
|
||||
if (!status.signer) { toastPgpMailUi('Create or import an encryption key for your address before signing'); return false; }
|
||||
if (!status.signer.unlocked) {
|
||||
passphrase = await pgpAskPassphrase('Sign message', `Enter the passphrase for ${status.signer.email} to sign this message.`);
|
||||
if (passphrase === null) return false;
|
||||
|
||||
Reference in New Issue
Block a user