Refractoring Stage 3 Completed the mail-service refactoring stage and started IPC hardening
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
const { randomUUID } = require('node:crypto');
|
||||
|
||||
function applyQueuedActionToCache(account, input) {
|
||||
const folder = String(input.folder || 'Inbox');
|
||||
const cache = account.mailCache?.[folder];
|
||||
if (!cache) return;
|
||||
const index = cache.findIndex(message => Number(message.uid || message.id) === Number(input.uid));
|
||||
if (index < 0) return;
|
||||
const [message] = cache.splice(index, 1);
|
||||
if (input.action === 'star' || input.action === 'unread') {
|
||||
message[input.action] = Boolean(input.enabled);
|
||||
cache.splice(index, 0, message);
|
||||
return;
|
||||
}
|
||||
const target = input.action === 'archive' ? 'Archive' : input.action === 'delete' ? 'Trash' : input.action === 'inbox' ? 'Inbox' : null;
|
||||
if (!target) {
|
||||
cache.splice(index, 0, message);
|
||||
return;
|
||||
}
|
||||
message.sourceFolder = target;
|
||||
message.archived = target === 'Archive';
|
||||
message.deleted = target === 'Trash';
|
||||
account.mailCache[target] = account.mailCache[target] || [];
|
||||
account.mailCache[target] = [...account.mailCache[target].filter(item => Number(item.uid || item.id) !== Number(input.uid)), message].slice(-50);
|
||||
}
|
||||
|
||||
function createMailOutboxService({
|
||||
sendMailNow,
|
||||
applyMessageAction,
|
||||
updateAccountStore,
|
||||
isConnectivityFailure,
|
||||
createId = randomUUID,
|
||||
now = Date.now,
|
||||
logError = (...args) => console.error(...args)
|
||||
}) {
|
||||
async function flushMailOutbox(account) {
|
||||
const operations = [...(account.mailOutbox || [])];
|
||||
for (const operation of operations) {
|
||||
try {
|
||||
if (operation.type === 'send') await sendMailNow(account, operation.data);
|
||||
else if (operation.type === 'action') await applyMessageAction(account, operation.data);
|
||||
else throw new Error('Unsupported queued mail operation.');
|
||||
await updateAccountStore(store => {
|
||||
const saved = store.accounts.find(item => item.id === account.id);
|
||||
if (saved) saved.mailOutbox = (saved.mailOutbox || []).filter(item => item.id !== operation.id);
|
||||
});
|
||||
} catch (error) {
|
||||
if (isConnectivityFailure(error)) break;
|
||||
logError(`Could not complete queued mail operation ${operation.id}:`, error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function queueMessageAction(account, input) {
|
||||
await updateAccountStore(store => {
|
||||
const saved = store.accounts.find(item => item.id === account.id);
|
||||
if (!saved) throw new Error('That mail account is no longer available.');
|
||||
saved.mailOutbox = saved.mailOutbox || [];
|
||||
saved.mailOutbox = saved.mailOutbox.filter(item => !(item.type === 'action' &&
|
||||
Number(item.data?.uid) === Number(input.uid) && item.data?.folder === input.folder && item.data?.action === input.action));
|
||||
saved.mailOutbox.push({ id: createId(), type: 'action', data: input, createdAt: now() });
|
||||
applyQueuedActionToCache(saved, input);
|
||||
});
|
||||
return { ok: true, queued: true };
|
||||
}
|
||||
|
||||
return { flushMailOutbox, queueMessageAction };
|
||||
}
|
||||
|
||||
module.exports = { applyQueuedActionToCache, createMailOutboxService };
|
||||
Reference in New Issue
Block a user