feat: add complete message listing and pagination
This commit is contained in:
1 parent
e84cae7f80
commit
85bbc9bbb4
6 files changed
+50
-13
No files matched your search
@@ -47,7 +47,7 @@ metadata:
|
||||
| 单据实体模型与字段元数据查询 | `hose bill schema` | [references/bill.md](references/bill.md) |
|
||||
| 企业通讯录检索 / 员工档案 / 防爆保护 | `hose contact search`, `hose contact list`, `hose contact get` | [references/contact.md](references/contact.md) |
|
||||
| 待办中心 / 已审批 / 我发起的 / 审批记录 / 抄送 | `hose task todo`, `hose task submitted`, `hose task records`, `hose task cc` | [references/task.md](references/task.md) |
|
||||
| 消息中心 / 未读统计 / 标记已读 / 类型过滤 | `hose message list`, `hose message count`, `hose message read` | [references/message.md](references/message.md) |
|
||||
| 消息中心 / 未读统计 / 标记已读 / 类型过滤 | `hose message list [--all]`, `hose message count`, `hose message read` | [references/message.md](references/message.md) |
|
||||
| 跨自建应用工作流聚合检索 | `hose workflow list`, `hose platform list` | [references/workflow.md](references/workflow.md) |
|
||||
| 特定任务节点扫描 (如脚本/Webhook) | `hose workflow find --task=code-js2` | [references/workflow.md](references/workflow.md) |
|
||||
| 工作流详情与开通切换 | `hose workflow get`, `hose workflow toggle` | [references/workflow.md](references/workflow.md) |
|
||||
@@ -99,6 +99,7 @@ hose bill schema # 查询单据实体元数
|
||||
# 4. 待办与消息
|
||||
hose task submitted # 查看我发起的单据
|
||||
hose message list --unread # 查看未读消息
|
||||
hose message list --all # 合并查看已读和未读消息
|
||||
hose message count # 查看未读数量
|
||||
hose message read <messageId> # 标记单条消息已读
|
||||
|
||||
|
||||
@@ -5,10 +5,11 @@
|
||||
```bash
|
||||
hose message list --unread
|
||||
hose message list --read
|
||||
hose message list --all
|
||||
hose message list --type=Approve
|
||||
hose message count
|
||||
hose message read <messageId>
|
||||
hose message read-all
|
||||
```
|
||||
|
||||
`message list --json` 返回消息 ID、标题、内容、消息类型、已读状态和原始字段;`--type` 在 CLI 层按 `messageType`、`msgType`、`msgTypeName` 或 `msgName` 做兼容过滤。
|
||||
`message list --json` 返回消息 ID、标题、内容、消息类型、已读状态和原始字段;`--all` 会并行拉取已读与未读页面并去重后按时间排序;`--type` 在 CLI 层按 `messageType`、`msgType`、`msgTypeName` 或 `msgName` 做兼容过滤。`message read-all` 会分页读取全部未读消息再逐条标记,避免只处理首 100 条。
|
||||
+29
-10
@@ -2,7 +2,7 @@ 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, readMessage } from "../../domains/message/tools.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 {
|
||||
@@ -44,10 +44,19 @@ export async function handleMessageCommand(parsed: CliParsedArgs, runtime: CliRu
|
||||
}
|
||||
|
||||
if (action === "read-all" || action === "mark-all-read") {
|
||||
const result = await listMessages(client, corpId, { start: 0, count: 100, read: false });
|
||||
for (const item of result.items) await readMessage(client, corpId, item.id);
|
||||
if (runtime.jsonMode) return { stdout: printJson({ corpId, requested: result.items.length, read: result.items.length }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
||||
return { stdout: `已标记 ${result.items.length} 条消息为已读。\n`, stderr: "", exitCode: 0 };
|
||||
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") {
|
||||
@@ -56,17 +65,27 @@ export async function handleMessageCommand(parsed: CliParsedArgs, runtime: CliRu
|
||||
|
||||
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);
|
||||
const start = numberOption(parsed.options, ["start", "offset"], 0);
|
||||
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 = await listMessages(client, corpId, { start, count, read: readFilter });
|
||||
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: readFilter, items }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
||||
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: `====== ${readFilter ? "已读" : "未读"}消息 (企业: ${corp.name} | 总数: ${type ? items.length : result.total} | 当前返回: ${items.length}) ======\n${items.length ? renderItems(items) : " (暂无数据)"}\n`,
|
||||
stdout: `====== ${all ? "全部" : readFilter ? "已读" : "未读"}消息 (企业: ${corp.name} | 总数: ${type ? items.length : result.total} | 当前返回: ${items.length}) ======\n${items.length ? renderItems(items) : " (暂无数据)"}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
|
||||
@@ -159,6 +159,7 @@ export function printHelp(): string {
|
||||
hose-use task submitted # 查看我发起的单据
|
||||
hose-use task cc --unread # 查看未读抄送
|
||||
hose-use message list --unread # 查看未读消息
|
||||
hose-use message list --all # 合并查看已读和未读消息
|
||||
hose-use message count # 查看未读消息数
|
||||
hose-use workflow list # 汇总查看全部工作流
|
||||
hose-use workflow find --task=code-js2 # 搜索包含自定义脚本的工作流
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { filterMessages, normalizeMessage } from "./tools.js";
|
||||
import { filterMessages, mergeMessages, normalizeMessage } from "./tools.js";
|
||||
|
||||
describe("Message Domain - 消息中心模型", () => {
|
||||
test("normalizes message fields and filters by type", () => {
|
||||
@@ -17,4 +17,13 @@ describe("Message Domain - 消息中心模型", () => {
|
||||
expect(filterMessages([item], "审批")).toHaveLength(1);
|
||||
expect(filterMessages([item], "Loan_Repayment")).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("merges read and unread pages by ID and newest time", () => {
|
||||
const older = normalizeMessage({ id: "msg-1", title: "旧标题", read: false, sortTime: 10 });
|
||||
const newer = normalizeMessage({ id: "msg-1", title: "新标题", read: true, sortTime: 20 });
|
||||
const second = normalizeMessage({ id: "msg-2", title: "第二条", read: false, sortTime: 15 });
|
||||
const merged = mergeMessages([older, second, newer]);
|
||||
expect(merged.map((item) => item.id)).toEqual(["msg-2", "msg-1"]);
|
||||
expect(merged[1]?.title).toBe("旧标题");
|
||||
});
|
||||
});
|
||||
@@ -49,3 +49,9 @@ export function filterMessages(items: MessageItem[], messageType?: string): Mess
|
||||
.filter(Boolean)
|
||||
.some((value) => String(value).toLowerCase() === target || String(value).toLowerCase().includes(target)));
|
||||
}
|
||||
|
||||
export function mergeMessages(items: MessageItem[]): MessageItem[] {
|
||||
return [...items]
|
||||
.filter((item, index, all) => all.findIndex((candidate) => candidate.id === item.id) === index)
|
||||
.sort((left, right) => (right.sortTime ?? right.createTime ?? 0) - (left.sortTime ?? left.createTime ?? 0));
|
||||
}
|
||||
Reference in new issue
Block a user