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

@@ -29,6 +29,7 @@ import {
buildTelegramGroupPeerId, buildTelegramGroupPeerId,
buildTelegramParentPeer, buildTelegramParentPeer,
resolveTelegramForumThreadId, resolveTelegramForumThreadId,
resolveTelegramGroupAllowFromContext,
} from "./bot/helpers.js"; } from "./bot/helpers.js";
import { migrateTelegramGroupConfig } from "./group-migration.js"; import { migrateTelegramGroupConfig } from "./group-migration.js";
import { resolveTelegramInlineButtonsScope } from "./inline-buttons.js"; import { resolveTelegramInlineButtonsScope } from "./inline-buttons.js";
@@ -327,17 +328,15 @@ export const registerTelegramHandlers = ({
const messageThreadId = callbackMessage.message_thread_id; const messageThreadId = callbackMessage.message_thread_id;
const isForum = callbackMessage.chat.is_forum === true; const isForum = callbackMessage.chat.is_forum === true;
const resolvedThreadId = resolveTelegramForumThreadId({ const groupAllowContext = await resolveTelegramGroupAllowFromContext({
chatId,
isForum, isForum,
messageThreadId, messageThreadId,
groupAllowFrom,
resolveTelegramGroupConfig,
}); });
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId); const { resolvedThreadId, storeAllowFrom, groupConfig, topicConfig, effectiveGroupAllow } =
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []); groupAllowContext;
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
const effectiveGroupAllow = normalizeAllowFromWithStore({
allowFrom: groupAllowOverride ?? groupAllowFrom,
storeAllowFrom,
});
const effectiveDmAllow = normalizeAllowFromWithStore({ const effectiveDmAllow = normalizeAllowFromWithStore({
allowFrom: telegramCfg.allowFrom, allowFrom: telegramCfg.allowFrom,
storeAllowFrom, storeAllowFrom,
@@ -357,7 +356,7 @@ export const registerTelegramHandlers = ({
); );
return; return;
} }
if (typeof groupAllowOverride !== "undefined") { if (groupAllowContext.hasGroupAllowOverride) {
const allowed = const allowed =
senderId && senderId &&
isSenderAllowed({ isSenderAllowed({
@@ -698,18 +697,21 @@ export const registerTelegramHandlers = ({
const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup"; const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup";
const messageThreadId = msg.message_thread_id; const messageThreadId = msg.message_thread_id;
const isForum = msg.chat.is_forum === true; const isForum = msg.chat.is_forum === true;
const resolvedThreadId = resolveTelegramForumThreadId({ const groupAllowContext = await resolveTelegramGroupAllowFromContext({
chatId,
isForum, isForum,
messageThreadId, messageThreadId,
groupAllowFrom,
resolveTelegramGroupConfig,
}); });
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []); const {
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId); resolvedThreadId,
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
const effectiveGroupAllow = normalizeAllowFromWithStore({
allowFrom: groupAllowOverride ?? groupAllowFrom,
storeAllowFrom, storeAllowFrom,
}); groupConfig,
const hasGroupAllowOverride = typeof groupAllowOverride !== "undefined"; topicConfig,
effectiveGroupAllow,
hasGroupAllowOverride,
} = groupAllowContext;
if (isGroup) { if (isGroup) {
if (groupConfig?.enabled === false) { if (groupConfig?.enabled === false) {

View File

@@ -29,7 +29,6 @@ import { resolveTelegramCustomCommands } from "../config/telegram-custom-command
import { danger, logVerbose } from "../globals.js"; import { danger, logVerbose } from "../globals.js";
import { getChildLogger } from "../logging.js"; import { getChildLogger } from "../logging.js";
import { getAgentScopedMediaLocalRoots } from "../media/local-roots.js"; import { getAgentScopedMediaLocalRoots } from "../media/local-roots.js";
import { readChannelAllowFromStore } from "../pairing/pairing-store.js";
import { import {
executePluginCommand, executePluginCommand,
getPluginCommandSpecs, getPluginCommandSpecs,
@@ -53,7 +52,7 @@ import {
buildTelegramGroupFrom, buildTelegramGroupFrom,
buildTelegramGroupPeerId, buildTelegramGroupPeerId,
buildTelegramParentPeer, buildTelegramParentPeer,
resolveTelegramForumThreadId, resolveTelegramGroupAllowFromContext,
resolveTelegramThreadSpec, resolveTelegramThreadSpec,
} from "./bot/helpers.js"; } from "./bot/helpers.js";
import { buildInlineKeyboard } from "./send.js"; import { buildInlineKeyboard } from "./send.js";
@@ -155,18 +154,21 @@ async function resolveTelegramCommandAuth(params: {
const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup"; const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup";
const messageThreadId = (msg as { message_thread_id?: number }).message_thread_id; const messageThreadId = (msg as { message_thread_id?: number }).message_thread_id;
const isForum = (msg.chat as { is_forum?: boolean }).is_forum === true; const isForum = (msg.chat as { is_forum?: boolean }).is_forum === true;
const resolvedThreadId = resolveTelegramForumThreadId({ const groupAllowContext = await resolveTelegramGroupAllowFromContext({
chatId,
isForum, isForum,
messageThreadId, messageThreadId,
groupAllowFrom,
resolveTelegramGroupConfig,
}); });
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []); const {
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId); resolvedThreadId,
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
const effectiveGroupAllow = normalizeAllowFromWithStore({
allowFrom: groupAllowOverride ?? groupAllowFrom,
storeAllowFrom, storeAllowFrom,
}); groupConfig,
const hasGroupAllowOverride = typeof groupAllowOverride !== "undefined"; topicConfig,
effectiveGroupAllow,
hasGroupAllowOverride,
} = groupAllowContext;
const senderIdRaw = msg.from?.id; const senderIdRaw = msg.from?.id;
const senderId = senderIdRaw ? String(senderIdRaw) : ""; const senderId = senderIdRaw ? String(senderIdRaw) : "";
const senderUsername = msg.from?.username ?? ""; const senderUsername = msg.from?.username ?? "";

View File

@@ -1,6 +1,13 @@
import type { Chat, Message, MessageOrigin, User } from "@grammyjs/types"; import type { Chat, Message, MessageOrigin, User } from "@grammyjs/types";
import type { TelegramGroupConfig, TelegramTopicConfig } from "../../config/types.js";
import type { TelegramStreamMode } from "./types.js"; import type { TelegramStreamMode } from "./types.js";
import { formatLocationText, type NormalizedLocation } from "../../channels/location.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; const TELEGRAM_GENERAL_TOPIC_ID = 1;
@@ -9,6 +16,50 @@ export type TelegramThreadSpec = {
scope: "dm" | "forum" | "none"; 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. * Resolve the thread ID for Telegram forum topics.
* For non-forum groups, returns undefined even if messageThreadId is present * For non-forum groups, returns undefined even if messageThreadId is present