import { booleanOption, numberOption, stringOption } from "../parser.js"; import { printJson, printTable } from "../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js"; import { createEkbClient } from "../../core/auth/ekb.js"; import { filterMessages, listMessages, mergeMessages, readMessage } from "../../domains/message/tools.js"; import type { MessageItem } from "../../domains/message/types.js"; function formatTime(value: number | undefined): string { return value === undefined ? "-" : new Date(value).toISOString().replace("T", " ").slice(0, 16); } function renderItems(items: MessageItem[]): string { const headers = ["消息 ID", "时间", "类型", "标题", "内容", "状态"]; const rows = items.map((item) => [ item.id, formatTime(item.sortTime ?? item.createTime), item.msgTypeName || item.messageType || "-", item.title || "-", item.content || "-", item.read ? "已读" : "未读", ]); return printTable(headers, rows); } export async function handleMessageCommand(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const corp = runtime.selectedCorp; if (!corp) throw new Error("未指定或未找到有效企业。请运行 hose corps list 或指定 --corp="); const action = parsed.action || parsed.args[0] || "list"; const client = await createEkbClient(corp); const corpId = corp.corpId || corp.id; if (action === "count" || action === "unread") { const result = await listMessages(client, corpId, { start: 0, count: 1, read: false }); if (runtime.jsonMode) return { stdout: printJson({ corpId, unread: result.total }, runtime.outputOptions), stderr: "", exitCode: 0 }; return { stdout: `未读消息: ${result.total}\n`, stderr: "", exitCode: 0 }; } if (action === "read" || action === "mark-read") { const messageId = parsed.args[0] || stringOption(parsed.options, ["id", "message-id"]); if (!messageId) return { stdout: "", stderr: "用法: hose message read \n", exitCode: 1 }; await readMessage(client, corpId, messageId); if (runtime.jsonMode) return { stdout: printJson({ corpId, id: messageId, read: true }, runtime.outputOptions), stderr: "", exitCode: 0 }; return { stdout: `已标记消息为已读: ${messageId}\n`, stderr: "", exitCode: 0 }; } if (action === "read-all" || action === "mark-all-read") { const ids: string[] = []; let start = 0; let total = 0; do { const page = await listMessages(client, corpId, { start, count: 100, read: false }); total = page.total; ids.push(...page.items.map((item) => item.id)); start += page.items.length; if (page.items.length === 0) break; } while (start < total); for (const id of ids) await readMessage(client, corpId, id); if (runtime.jsonMode) return { stdout: printJson({ corpId, requested: ids.length, read: ids.length, totalUnreadBefore: total }, runtime.outputOptions), stderr: "", exitCode: 0 }; return { stdout: `已标记 ${ids.length} 条消息为已读。\n`, stderr: "", exitCode: 0 }; } if (action !== "list") { return { stdout: "", stderr: `未知消息子命令: ${action}。支持: list, count, read , read-all\n`, exitCode: 1 }; } const read = booleanOption(parsed.options, ["read"]); const unread = booleanOption(parsed.options, ["unread"]); const all = booleanOption(parsed.options, ["all"]); const readFilter = read && !unread; const count = numberOption(parsed.options, ["count", "limit", "n"], 20) ?? 20; const start = numberOption(parsed.options, ["start", "offset"], 0) ?? 0; const type = stringOption(parsed.options, ["type", "message-type"]); const result = all ? await (async () => { const [unreadPage, readPage] = await Promise.all([ listMessages(client, corpId, { start: 0, count: Math.max(100, start + count), read: false }), listMessages(client, corpId, { start: 0, count: Math.max(100, start + count), read: true }), ]); const merged = mergeMessages([...unreadPage.items, ...readPage.items]); return { total: unreadPage.total + readPage.total, start, count, read: false, items: merged.slice(start, start + count) }; })() : await listMessages(client, corpId, { start, count, read: readFilter }); const items = filterMessages(result.items, type); if (runtime.jsonMode) { return { stdout: printJson({ corpId, total: type ? items.length : result.total, start: result.start, count: result.count, read: all ? "all" : readFilter, items }, runtime.outputOptions), stderr: "", exitCode: 0 }; } return { stdout: `====== ${all ? "全部" : readFilter ? "已读" : "未读"}消息 (企业: ${corp.name} | 总数: ${type ? items.length : result.total} | 当前返回: ${items.length}) ======\n${items.length ? renderItems(items) : " (暂无数据)"}\n`, stderr: "", exitCode: 0, }; }