refactor(telegram): share group allowFrom resolution

This commit is contained in:
Peter Steinberger
2026-02-16 02:26:51 +00:00
parent b88f377623
commit 35c5d2be5c
3 changed files with 82 additions and 27 deletions

View File

@@ -1,6 +1,13 @@
import type { Chat, Message, MessageOrigin, User } from "@grammyjs/types";
import type { TelegramGroupConfig, TelegramTopicConfig } from "../../config/types.js";
import type { TelegramStreamMode } from "./types.js";
import { formatLocationText, type NormalizedLocation } from "../../channels/location.js";
import { readChannelAllowFromStore } from "../../pairing/pairing-store.js";
import {
firstDefined,
normalizeAllowFromWithStore,
type NormalizedAllowFrom,
} from "../bot-access.js";
const TELEGRAM_GENERAL_TOPIC_ID = 1;
@@ -9,6 +16,50 @@ export type TelegramThreadSpec = {
scope: "dm" | "forum" | "none";
};
export async function resolveTelegramGroupAllowFromContext(params: {
chatId: string | number;
isForum?: boolean;
messageThreadId?: number | null;
groupAllowFrom?: Array<string | number>;
resolveTelegramGroupConfig: (
chatId: string | number,
messageThreadId?: number,
) => { groupConfig?: TelegramGroupConfig; topicConfig?: TelegramTopicConfig };
}): Promise<{
resolvedThreadId?: number;
storeAllowFrom: string[];
groupConfig?: TelegramGroupConfig;
topicConfig?: TelegramTopicConfig;
groupAllowOverride?: Array<string | number>;
effectiveGroupAllow: NormalizedAllowFrom;
hasGroupAllowOverride: boolean;
}> {
const resolvedThreadId = resolveTelegramForumThreadId({
isForum: params.isForum,
messageThreadId: params.messageThreadId,
});
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []);
const { groupConfig, topicConfig } = params.resolveTelegramGroupConfig(
params.chatId,
resolvedThreadId,
);
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
const effectiveGroupAllow = normalizeAllowFromWithStore({
allowFrom: groupAllowOverride ?? params.groupAllowFrom,
storeAllowFrom,
});
const hasGroupAllowOverride = typeof groupAllowOverride !== "undefined";
return {
resolvedThreadId,
storeAllowFrom,
groupConfig,
topicConfig,
groupAllowOverride,
effectiveGroupAllow,
hasGroupAllowOverride,
};
}
/**
* Resolve the thread ID for Telegram forum topics.
* For non-forum groups, returns undefined even if messageThreadId is present