perf(telegram): cache forum metadata lookup · openclaw/openclaw@50e6c0a
obviyus
·
2026-04-23
·
via Recent Commits to openclaw:main
File tree
extensions/telegram/src/bot
| Original file line number | Diff line number | Diff line change |
|---|
@@ -52,13 +52,26 @@ describe("resolveTelegramForumFlag", () => {
|
52 | 52 | const getChat = vi.fn(async () => ({ is_forum: true })); |
53 | 53 | await expect( |
54 | 54 | resolveTelegramForumFlag({ |
55 | | -chatId: -100123, |
| 55 | +chatId: -100789, |
56 | 56 | chatType: "supergroup", |
57 | 57 | isGroup: true, |
58 | 58 | getChat, |
59 | 59 | }), |
60 | 60 | ).resolves.toBe(true); |
61 | | -expect(getChat).toHaveBeenCalledWith(-100123); |
| 61 | +expect(getChat).toHaveBeenCalledWith(-100789); |
| 62 | +}); |
| 63 | + |
| 64 | +it("reuses resolved forum metadata for later supergroup updates", async () => { |
| 65 | +const getChat = vi.fn(async () => ({ is_forum: true })); |
| 66 | +const params = { |
| 67 | +chatId: -100456, |
| 68 | +chatType: "supergroup" as const, |
| 69 | +isGroup: true, |
| 70 | + getChat, |
| 71 | +}; |
| 72 | +await expect(resolveTelegramForumFlag(params)).resolves.toBe(true); |
| 73 | +await expect(resolveTelegramForumFlag(params)).resolves.toBe(true); |
| 74 | +expect(getChat).toHaveBeenCalledTimes(1); |
62 | 75 | }); |
63 | 76 | |
64 | 77 | it("returns false when forum lookup is unavailable", async () => { |
@@ -67,7 +80,7 @@ describe("resolveTelegramForumFlag", () => {
|
67 | 80 | }); |
68 | 81 | await expect( |
69 | 82 | resolveTelegramForumFlag({ |
70 | | -chatId: -100123, |
| 83 | +chatId: -100999, |
71 | 84 | chatType: "supergroup", |
72 | 85 | isGroup: true, |
73 | 86 | getChat, |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -40,6 +40,7 @@ export {
|
40 | 40 | }; |
41 | 41 | |
42 | 42 | const TELEGRAM_GENERAL_TOPIC_ID = 1; |
| 43 | +const telegramForumFlagByChatId = new Map<string, boolean>(); |
43 | 44 | |
44 | 45 | function hadUnsafeTelegramText(raw: unknown, sanitized: string): boolean { |
45 | 46 | return typeof raw === "string" && raw.trim().length > 0 && sanitized.trim().length === 0; |
@@ -71,8 +72,15 @@ export async function resolveTelegramForumFlag(params: {
|
71 | 72 | if (!params.isGroup || params.chatType !== "supergroup" || !params.getChat) { |
72 | 73 | return false; |
73 | 74 | } |
| 75 | +const cacheKey = String(params.chatId); |
| 76 | +const cached = telegramForumFlagByChatId.get(cacheKey); |
| 77 | +if (cached !== undefined) { |
| 78 | +return cached; |
| 79 | +} |
74 | 80 | try { |
75 | | -return extractTelegramForumFlag(await params.getChat(params.chatId)) === true; |
| 81 | +const resolved = extractTelegramForumFlag(await params.getChat(params.chatId)) === true; |
| 82 | +telegramForumFlagByChatId.set(cacheKey, resolved); |
| 83 | +return resolved; |
76 | 84 | } catch { |
77 | 85 | return false; |
78 | 86 | } |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。