feat: add CLI message center commands

This commit is contained in:
gitea committed 2026-09-05 14:05:05 +08:00
1 parent 0be1365289
commit 621c8cb7dc
11 files changed
+242 -4

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
import type { KyInstance } from "ky";
import type { MessageQueryOptions } from "./types.js";
export async function fetchMessagePage(
client: KyInstance,
corpId: string,
options: MessageQueryOptions = {}
): Promise<{ count: number; items: Record<string, unknown>[] }> {
const start = options.start ?? 0;
const count = options.count ?? 20;
const read = options.read ?? false;
const response = await client.get("api/message/v1/messages", {
searchParams: {
read: String(read),
start: String(start),
count: String(count),
corpId,
},
});
const json = (await response.json()) as { count?: number; items?: Record<string, unknown>[] };
return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] };
}
export async function markMessageRead(client: KyInstance, corpId: string, messageId: string): Promise<void> {
// The downstream route accepts the compound ID with its colon separators intact.
await client.put(`api/message/v1/messages/$${messageId}`, {
searchParams: { corpId },
json: { id: messageId },
});
}
+20
View File
@@ -0,0 +1,20 @@
import { describe, expect, test } from "bun:test";
import { filterMessages, normalizeMessage } from "./tools.js";
describe("Message Domain - 消息中心模型", () => {
test("normalizes message fields and filters by type", () => {
const item = normalizeMessage({
id: "msg-1",
title: "你的单据被驳回",
content: "单据:有效性。",
messageType: "INFO",
msgTypeName: "审批通知",
read: false,
sortTime: 1776052034000,
});
expect(item.id).toBe("msg-1");
expect(item.read).toBe(false);
expect(filterMessages([item], "审批")).toHaveLength(1);
expect(filterMessages([item], "Loan_Repayment")).toHaveLength(0);
});
});
+51
View File
@@ -0,0 +1,51 @@
import type { KyInstance } from "ky";
import { fetchMessagePage, markMessageRead } from "./api.js";
import type { MessageItem, MessagePageResult, MessageQueryOptions } from "./types.js";
export function normalizeMessage(raw: Record<string, unknown>): MessageItem {
return {
id: String(raw.id ?? ""),
title: typeof raw.title === "string" ? raw.title : undefined,
content: typeof raw.content === "string" ? raw.content : undefined,
messageType: typeof raw.messageType === "string" ? raw.messageType : undefined,
msgType: typeof raw.msgType === "string" ? raw.msgType : undefined,
msgTypeName: typeof raw.msgTypeName === "string" ? raw.msgTypeName : undefined,
msgName: typeof raw.msgName === "string" ? raw.msgName : undefined,
read: raw.read === true,
createTime: typeof raw.createTime === "number" ? raw.createTime : undefined,
updateTime: typeof raw.updateTime === "number" ? raw.updateTime : undefined,
sortTime: typeof raw.sortTime === "number" ? raw.sortTime : undefined,
sourceId: typeof raw.sourceId === "string" || raw.sourceId === null ? raw.sourceId : undefined,
sourceType: typeof raw.sourceType === "string" ? raw.sourceType : undefined,
sourceStatus: typeof raw.sourceStatus === "string" || raw.sourceStatus === null ? raw.sourceStatus : undefined,
url: typeof raw.url === "string" ? raw.url : undefined,
raw,
};
}
export async function listMessages(
client: KyInstance,
corpId: string,
options: MessageQueryOptions = {}
): Promise<MessagePageResult> {
const page = await fetchMessagePage(client, corpId, options);
return {
total: page.count,
start: options.start ?? 0,
count: options.count ?? 20,
read: options.read ?? false,
items: page.items.map(normalizeMessage),
};
}
export async function readMessage(client: KyInstance, corpId: string, messageId: string): Promise<void> {
await markMessageRead(client, corpId, messageId);
}
export function filterMessages(items: MessageItem[], messageType?: string): MessageItem[] {
if (!messageType) return items;
const target = messageType.toLowerCase();
return items.filter((item) => [item.messageType, item.msgType, item.msgTypeName, item.msgName]
.filter(Boolean)
.some((value) => String(value).toLowerCase() === target || String(value).toLowerCase().includes(target)));
}
+32
View File
@@ -0,0 +1,32 @@
export interface MessageQueryOptions {
start?: number;
count?: number;
read?: boolean;
}
export interface MessageItem {
id: string;
title?: string;
content?: string;
messageType?: string;
msgType?: string;
msgTypeName?: string;
msgName?: string;
read: boolean;
createTime?: number;
updateTime?: number;
sortTime?: number;
sourceId?: string | null;
sourceType?: string;
sourceStatus?: string | null;
url?: string;
raw: Record<string, unknown>;
}
export interface MessagePageResult {
total: number;
start: number;
count: number;
read: boolean;
items: MessageItem[];
}