Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: sendVia validation, disabled account guard, structured errors, CDP resilience

Adam Eivy8349d60323a3
diff --git a/server/routes/messages.js b/server/routes/messages.js
index 950da17577..5b29dc3bc0 100644
--- a/server/routes/messages.js
+++ b/server/routes/messages.js
@@ -153,9 +153,11 @@ router.post('/drafts', asyncHandler(async (req, res) => {
const data = validateRequest(createDraftSchema, req.body);
const account = await messageAccounts.getAccount(data.accountId);
if (!account) return res.status(404).json({ error: 'Account not found' });
- if (!data.sendVia) {
- data.sendVia = account.provider || (account.type === 'gmail' ? 'mcp' : 'playwright');
+ const derivedSendVia = account.type === 'gmail' ? 'mcp' : 'playwright';
+ if (data.sendVia && data.sendVia !== derivedSendVia) {
+ return res.status(400).json({ error: `sendVia "${data.sendVia}" conflicts with account type "${account.type}" (expected "${derivedSendVia}")` });
}
+ data.sendVia = derivedSendVia;
const draft = await messageDrafts.createDraft(data);
req.app.get('io')?.emit('messages:draft:created', { draftId: draft.id });
res.status(201).json(draft);
@@ -206,8 +208,7 @@ router.post('/drafts/:id/send', asyncHandler(async (req, res) => {
const io = req.app.get('io');
const result = await messageSender.sendDraft(req.params.id, io);
if (!result.success) {
- const status = result.error?.includes('not found') ? 404 : 400;
- return res.status(status).json({ error: result.error });
+ return res.status(result.status).json({ code: result.code, error: result.error });
}
res.json(result);
}));
diff --git a/server/routes/messages.test.js b/server/routes/messages.test.js
index c4d2ddbbdf..152ae74a95 100644
--- a/server/routes/messages.test.js
+++ b/server/routes/messages.test.js
@@ -441,7 +441,7 @@ describe('Messages Routes', () => {
});
it('should return 404 if draft not found', async () => {
- messageSender.sendDraft.mockResolvedValue({ success: false, error: 'Draft not found' });
+ messageSender.sendDraft.mockResolvedValue({ success: false, status: 404, code: 'DRAFT_NOT_FOUND', error: 'Draft not found' });
const response = await request(app).post(`/api/messages/drafts/${DRAFT_UUID_2}/send`);
@@ -449,7 +449,7 @@ describe('Messages Routes', () => {
});
it('should return 400 for non-not-found errors', async () => {
- messageSender.sendDraft.mockResolvedValue({ success: false, error: 'Draft not approved' });
+ messageSender.sendDraft.mockResolvedValue({ success: false, status: 400, code: 'INVALID_STATUS', error: 'Draft not approved' });
const response = await request(app).post(`/api/messages/drafts/${DRAFT_UUID}/send`);
diff --git a/server/services/messagePlaywrightSync.js b/server/services/messagePlaywrightSync.js
index 6b95e981b8..f873905df9 100644
--- a/server/services/messagePlaywrightSync.js
+++ b/server/services/messagePlaywrightSync.js
@@ -110,7 +110,7 @@ export async function updateSelectors(provider, selectors) {
*/
export async function launchProvider(accountType) {
const url = accountType === 'teams' ? TEAMS_URL : OUTLOOK_URL;
- const page = await findOrOpenPage(url);
+ const page = await findOrOpenPage(url).catch(() => null);
if (!page) return { success: false, error: 'Failed to open browser tab — is portos-browser running?' };
console.log(`📧 Launched ${accountType} in CDP browser: ${page.url}`);
return { success: true, url: page.url, pageId: page.id, title: page.title };
diff --git a/server/services/messageSender.js b/server/services/messageSender.js
index 2d03f55902..6c224aa302 100644
--- a/server/services/messageSender.js
+++ b/server/services/messageSender.js
@@ -1,13 +1,24 @@
import { getDraft, updateDraft } from './messageDrafts.js';
import { getAccount } from './messageAccounts.js';
+const ACCOUNT_TYPE_TO_SEND_VIA = {
+ gmail: 'mcp',
+ outlook: 'playwright',
+ teams: 'playwright'
+};
+
export async function sendDraft(draftId, io) {
const draft = await getDraft(draftId);
- if (!draft) return { success: false, error: 'Draft not found' };
- if (draft.status !== 'approved') return { success: false, error: `Draft status is "${draft.status}", must be "approved"` };
+ if (!draft) return { success: false, status: 404, code: 'DRAFT_NOT_FOUND', error: 'Draft not found' };
+ if (draft.status !== 'approved') return { success: false, status: 400, code: 'INVALID_STATUS', error: `Draft status is "${draft.status}", must be "approved"` };
const account = await getAccount(draft.accountId);
- if (!account) return { success: false, error: 'Account not found' };
+ if (!account) return { success: false, status: 404, code: 'ACCOUNT_NOT_FOUND', error: 'Account not found' };
+
+ const expectedSendVia = ACCOUNT_TYPE_TO_SEND_VIA[account.type];
+ if (draft.sendVia !== expectedSendVia) {
+ return { success: false, status: 400, code: 'SEND_VIA_MISMATCH', error: `sendVia "${draft.sendVia}" does not match account type "${account.type}" (expected "${expectedSendVia}")` };
+ }
await updateDraft(draftId, { status: 'sending' });
console.log(`📧 Sending draft "${draft.subject}" via ${draft.sendVia}`);
@@ -23,7 +34,7 @@ export async function sendDraft(draftId, io) {
const result = await dispatch().catch(async (error) => {
console.error(`📧 Draft send threw for "${draft.subject}": ${error.message}`);
- return { success: false, error: error.message };
+ return { success: false, status: 502, code: 'SEND_FAILED', error: error.message };
});
if (result?.success) {
@@ -35,7 +46,7 @@ export async function sendDraft(draftId, io) {
await updateDraft(draftId, { status: 'failed' }).catch(() => {});
const errorMsg = result?.error ?? 'Unknown error sending draft';
console.log(`📧 Draft send failed: ${errorMsg}`);
- return { success: false, error: errorMsg };
+ return { success: false, status: result?.status ?? 500, code: result?.code ?? 'SEND_FAILED', error: errorMsg };
}
return result;
diff --git a/server/services/messageSync.js b/server/services/messageSync.js
index bac21efe70..6c74567814 100644
--- a/server/services/messageSync.js
+++ b/server/services/messageSync.js
@@ -84,6 +84,7 @@ export async function syncAccount(accountId, io) {
const account = await getAccount(accountId);
if (!account) return { error: 'Account not found' };
+ if (!account.enabled) return { error: 'Account is disabled', status: 400 };
syncLocks.set(accountId, true);
io?.emit('messages:sync:started', { accountId });
@@ -98,6 +99,8 @@ export async function syncAccount(accountId, io) {
} else if (account.type === 'outlook' || account.type === 'teams') {
const { syncPlaywright } = await import('./messagePlaywrightSync.js');
providerResult = await syncPlaywright(account, cache, io);
+ } else {
+ throw new Error(`Unsupported account type: ${account.type}`);
}
// Support structured result { messages, status } or plain array