AkurAI Build
Menu

Public repository / 054e5cda0171bd13facff3c16a38f2048a1052cc

AkurAI-Mail / src/lib/webmail/session.js

Latest change 10d19fe0941c186ec244d1f0e2af6b19200dcb0c - OLI-248: pure webmail logic as plain ES modules with JSDoc types; inline icon module by Ólafur Búi Ólafsson

/**
 * @typedef {{ authenticated?: boolean, admin?: boolean, email?: string, user?: string, mailbox?: string, method?: string }} AuthCheck
 * @typedef {{ id: string, status: string, sendAt: number, subject: string, to: string }} ScheduledSendView
 */
/**
 * Normalizes /api/auth/check into the signed-in webmail session.
 * @param {AuthCheck} check
 */
export function webmailSession(check) {
  return {
    signedIn: !!check.authenticated,
    email: check.email || check.user || check.mailbox || "",
    admin: check.admin ?? check.method === "password",
  };
}

/**
 * A scheduled send as the API lists it: the body is `payload`, older records say `message`.
 * @param {unknown} value
 * @returns {string}
 */
function recipients(value) {
  if (Array.isArray(value)) return value.filter((v) => typeof v === "string").join(", ");
  return typeof value === "string" ? value : "";
}

/**
 * @param {Record<string, any>} item
 * @returns {ScheduledSendView}
 */
export function scheduledSendView(item) {
  const body = item.payload ?? item.message;
  const message = body && typeof body === "object" ? body : {};
  return {
    id: String(item.id ?? ""),
    status: String(item.status ?? ""),
    sendAt: Number(item.send_at) || 0,
    subject: typeof message.subject === "string" ? message.subject : "",
    to: recipients(message.to),
  };
}