@@ -49,19 +49,32 @@ inboxRoutes.get('/', async (c) => {
4949 LIMIT ? OFFSET ?`
5050 ) . bind ( auth . handle , limit , offset ) . all ( ) ;
5151 } else {
52- emails = await c . env . DB . prepare (
53- `SELECT e.id, e.folder, e.from_addr, e.to_addr, e.subject, e.snippet, e.size, e.read, e.created_at,
54- ab.amount_usdc as bond_amount, ab.status as bond_status, ab.response_deadline as bond_deadline,
55- ae.amount as attn_stake, ae.status as attn_status, ae.expires_at as attn_expires
56- FROM emails e
57- LEFT JOIN attention_bonds ab ON ab.email_id = e.id AND ab.status = 'active'
58- LEFT JOIN attn_escrow ae ON ae.email_id = e.id
59- WHERE e.handle = ? AND e.folder = ?
60- ORDER BY
61- CASE WHEN ae.amount IS NOT NULL AND ae.status = 'pending' THEN ae.amount ELSE 0 END DESC,
62- e.created_at DESC
63- LIMIT ? OFFSET ?`
64- ) . bind ( auth . handle , folder , limit , offset ) . all ( ) ;
52+ try {
53+ emails = await c . env . DB . prepare (
54+ `SELECT e.id, e.folder, e.from_addr, e.to_addr, e.subject, e.snippet, e.size, e.read, e.created_at,
55+ ab.amount_usdc as bond_amount, ab.status as bond_status, ab.response_deadline as bond_deadline,
56+ ae.amount as attn_stake, ae.status as attn_status, ae.expires_at as attn_expires
57+ FROM emails e
58+ LEFT JOIN attention_bonds ab ON ab.email_id = e.id AND ab.status = 'active'
59+ LEFT JOIN attn_escrow ae ON ae.email_id = e.id
60+ WHERE e.handle = ? AND e.folder = ?
61+ ORDER BY
62+ CASE WHEN ae.amount IS NOT NULL AND ae.status = 'pending' THEN ae.amount ELSE 0 END DESC,
63+ e.created_at DESC
64+ LIMIT ? OFFSET ?`
65+ ) . bind ( auth . handle , folder , limit , offset ) . all ( ) ;
66+ } catch ( _ ) {
67+ // attn_escrow table may not exist yet — fallback without ATTN join
68+ emails = await c . env . DB . prepare (
69+ `SELECT e.id, e.folder, e.from_addr, e.to_addr, e.subject, e.snippet, e.size, e.read, e.created_at,
70+ ab.amount_usdc as bond_amount, ab.status as bond_status, ab.response_deadline as bond_deadline
71+ FROM emails e
72+ LEFT JOIN attention_bonds ab ON ab.email_id = e.id AND ab.status = 'active'
73+ WHERE e.handle = ? AND e.folder = ?
74+ ORDER BY e.created_at DESC
75+ LIMIT ? OFFSET ?`
76+ ) . bind ( auth . handle , folder , limit , offset ) . all ( ) ;
77+ }
6578 }
6679
6780 const countResult = await c . env . DB . prepare (
0 commit comments