mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 04:51:25 +00:00
refactor(telegram): share group allowFrom resolution
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user