Skip to content

Commit e9f74d2

Browse files
committed
Inbox: add bulk mark-read endpoint
1 parent 5a6fcbc commit e9f74d2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

worker/src/routes/inbox.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,46 @@ inboxRoutes.get('/', async (c) => {
4747
});
4848
});
4949

50+
/**
51+
* POST /api/inbox/mark-read
52+
* Mark emails as read.
53+
* Body: { ids?: string[], folder?: 'inbox'|'sent' }
54+
* - If ids provided: mark those ids as read for this handle.
55+
* - Else: mark all unread emails in the folder as read (default inbox).
56+
*/
57+
inboxRoutes.post('/mark-read', async (c) => {
58+
const auth = c.get('auth');
59+
if (!auth.handle) return c.json({ error: 'No email registered for this wallet' }, 403);
60+
61+
let body: { ids?: string[]; folder?: string } = {};
62+
try {
63+
body = await c.req.json();
64+
} catch {
65+
body = {};
66+
}
67+
68+
const folder = body.folder || 'inbox';
69+
const ids = Array.isArray(body.ids) ? body.ids.filter(Boolean) : null;
70+
71+
if (ids && ids.length > 0) {
72+
// Mark selected ids
73+
const qs = ids.map(() => '?').join(',');
74+
const stmt = `UPDATE emails SET read = 1 WHERE handle = ? AND id IN (${qs})`;
75+
await c.env.DB.prepare(stmt).bind(auth.handle, ...ids).run();
76+
} else {
77+
// Mark all unread in folder
78+
await c.env.DB.prepare(
79+
'UPDATE emails SET read = 1 WHERE handle = ? AND folder = ? AND read = 0'
80+
).bind(auth.handle, folder).run();
81+
}
82+
83+
const unreadResult = await c.env.DB.prepare(
84+
'SELECT COUNT(*) as unread FROM emails WHERE handle = ? AND folder = ? AND read = 0'
85+
).bind(auth.handle, folder).first<{ unread: number }>();
86+
87+
return c.json({ success: true, folder, unread: unreadResult?.unread || 0 });
88+
});
89+
5090
/**
5191
* GET /api/inbox/:id
5292
* Read a specific email with body and attachment metadata

0 commit comments

Comments
 (0)