import { env } from "cloudflare:workers"; function inbox(userId: string) { return env.USER_INBOX.get(env.USER_INBOX.idFromName(userId)); } export function unreadNotifications(userId: string): Promise { return inbox(userId).unread(userId); } /** Call after inserting or marking notifications for these users; failures only delay the badge by the TTL. */ export async function invalidateUnread(userIds: Iterable): Promise { await Promise.all( [...new Set(userIds)].map(async (userId) => { try { await inbox(userId).invalidate(); } catch (error) { console.error("Inbox invalidate failed", userId, error); } }), ); } /** Per-user hourly email budget, held in the user's inbox object rather than a D1 upsert per email. */ export function takeInboxEmailBudget(userId: string, limit: number, windowMs: number): Promise { return inbox(userId).takeEmailBudget(limit, windowMs); } export function userInbox(userId: string) { return inbox(userId); }