refactor(auto-reply,telegram,config): extract guard and forum helpers

This commit is contained in:
Peter Steinberger
2026-03-02 23:47:14 +00:00
parent dc825e59f5
commit 32ecd6f579
7 changed files with 125 additions and 129 deletions

View File

@@ -1,16 +1,8 @@
import { describe, expect, it } from "vitest";
import { buildTelegramMessageContextForTest } from "./bot-message-context.test-harness.js";
import { TELEGRAM_FORUM_SERVICE_FIELDS } from "./forum-service-message.js";
describe("buildTelegramMessageContext implicitMention forum service messages", () => {
const TELEGRAM_FORUM_SERVICE_FIELDS = [
"forum_topic_created",
"forum_topic_edited",
"forum_topic_closed",
"forum_topic_reopened",
"general_forum_topic_hidden",
"general_forum_topic_unhidden",
] as const;
/**
* Build a group message context where the user sends a message inside a
* forum topic that has `reply_to_message` pointing to a message from the

View File

@@ -67,6 +67,7 @@ import {
} from "./bot/helpers.js";
import type { StickerMetadata, TelegramContext } from "./bot/types.js";
import { enforceTelegramDmAccess } from "./dm-access.js";
import { isTelegramForumServiceMessage } from "./forum-service-message.js";
import { evaluateTelegramGroupBaseAccess } from "./group-access.js";
import { resolveTelegramGroupPromptSettings } from "./group-config-helpers.js";
import {
@@ -867,31 +868,3 @@ export const buildTelegramMessageContext = async ({
export type TelegramMessageContext = NonNullable<
Awaited<ReturnType<typeof buildTelegramMessageContext>>
>;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Telegram forum-topic service-message fields (Bot API). */
const FORUM_SERVICE_FIELDS = [
"forum_topic_created",
"forum_topic_edited",
"forum_topic_closed",
"forum_topic_reopened",
"general_forum_topic_hidden",
"general_forum_topic_unhidden",
] as const;
/**
* Returns `true` when the message is a Telegram forum service message (e.g.
* "Topic created"). These auto-generated messages carry one of the
* `forum_topic_*` / `general_forum_topic_*` fields and should not count as
* regular bot replies for implicit-mention purposes.
*/
function isTelegramForumServiceMessage(msg: unknown): boolean {
if (!msg || typeof msg !== "object") {
return false;
}
const record = msg as Record<string, unknown>;
return FORUM_SERVICE_FIELDS.some((f) => record[f] != null);
}

View File

@@ -0,0 +1,23 @@
/** Telegram forum-topic service-message fields (Bot API). */
export const TELEGRAM_FORUM_SERVICE_FIELDS = [
"forum_topic_created",
"forum_topic_edited",
"forum_topic_closed",
"forum_topic_reopened",
"general_forum_topic_hidden",
"general_forum_topic_unhidden",
] as const;
/**
* Returns `true` when the message is a Telegram forum service message (e.g.
* "Topic created"). These auto-generated messages carry one of the
* `forum_topic_*` / `general_forum_topic_*` fields and should not count as
* regular bot replies for implicit-mention purposes.
*/
export function isTelegramForumServiceMessage(msg: unknown): boolean {
if (!msg || typeof msg !== "object") {
return false;
}
const record = msg as Record<string, unknown>;
return TELEGRAM_FORUM_SERVICE_FIELDS.some((field) => record[field] != null);
}