
























@@ -40,7 +40,26 @@ export {
4040};
41414242const TELEGRAM_GENERAL_TOPIC_ID = 1;
43-const telegramForumFlagByChatId = new Map<string, boolean>();
43+const TELEGRAM_FORUM_FLAG_CACHE_MAX_CHATS = 1024;
44+const TELEGRAM_FORUM_FLAG_CACHE_TTL_MS = 10 * 60_000;
45+const telegramForumFlagByChatId = new Map<string, { expiresAtMs: number; isForum: boolean }>();
46+47+function cacheTelegramForumFlag(chatId: string | number, isForum: boolean, nowMs = Date.now()) {
48+const cacheKey = String(chatId);
49+if (
50+!telegramForumFlagByChatId.has(cacheKey) &&
51+telegramForumFlagByChatId.size >= TELEGRAM_FORUM_FLAG_CACHE_MAX_CHATS
52+) {
53+const oldestKey = telegramForumFlagByChatId.keys().next().value;
54+if (oldestKey !== undefined) {
55+telegramForumFlagByChatId.delete(oldestKey);
56+}
57+}
58+telegramForumFlagByChatId.set(cacheKey, {
59+expiresAtMs: nowMs + TELEGRAM_FORUM_FLAG_CACHE_TTL_MS,
60+ isForum,
61+});
62+}
44634564function hadUnsafeTelegramText(raw: unknown, sanitized: string): boolean {
4665return typeof raw === "string" && raw.trim().length > 0 && sanitized.trim().length === 0;
@@ -67,19 +86,26 @@ export async function resolveTelegramForumFlag(params: {
6786getChat?: TelegramGetChat;
6887}): Promise<boolean> {
6988if (typeof params.isForum === "boolean") {
89+if (params.isGroup && params.chatType === "supergroup") {
90+cacheTelegramForumFlag(params.chatId, params.isForum);
91+}
7092return params.isForum;
7193}
7294if (!params.isGroup || params.chatType !== "supergroup" || !params.getChat) {
7395return false;
7496}
7597const cacheKey = String(params.chatId);
98+const nowMs = Date.now();
7699const cached = telegramForumFlagByChatId.get(cacheKey);
77-if (cached !== undefined) {
78-return cached;
100+if (cached && cached.expiresAtMs > nowMs) {
101+return cached.isForum;
102+}
103+if (cached) {
104+telegramForumFlagByChatId.delete(cacheKey);
79105}
80106try {
81107const resolved = extractTelegramForumFlag(await params.getChat(params.chatId)) === true;
82-telegramForumFlagByChatId.set(cacheKey, resolved);
108+cacheTelegramForumFlag(params.chatId, resolved, nowMs);
83109return resolved;
84110} catch {
85111return false;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。