refactor: use shared pairing store for telegram
This commit is contained in:
parent
ca92597e1f
commit
24fbafa9a7
23 changed files with 95 additions and 288 deletions
|
|
@ -247,10 +247,6 @@ vi.mock("../daemon/service.js", () => ({
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("../telegram/pairing-store.js", () => ({
|
|
||||||
readTelegramAllowFromStore: vi.fn().mockResolvedValue([]),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
||||||
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
||||||
|
|
|
||||||
|
|
@ -246,10 +246,6 @@ vi.mock("../daemon/service.js", () => ({
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("../telegram/pairing-store.js", () => ({
|
|
||||||
readTelegramAllowFromStore: vi.fn().mockResolvedValue([]),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
||||||
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
||||||
|
|
|
||||||
|
|
@ -246,10 +246,6 @@ vi.mock("../daemon/service.js", () => ({
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("../telegram/pairing-store.js", () => ({
|
|
||||||
readTelegramAllowFromStore: vi.fn().mockResolvedValue([]),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
||||||
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
||||||
|
|
|
||||||
|
|
@ -246,10 +246,6 @@ vi.mock("../daemon/service.js", () => ({
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("../telegram/pairing-store.js", () => ({
|
|
||||||
readTelegramAllowFromStore: vi.fn().mockResolvedValue([]),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
||||||
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
||||||
|
|
|
||||||
|
|
@ -246,10 +246,6 @@ vi.mock("../daemon/service.js", () => ({
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("../telegram/pairing-store.js", () => ({
|
|
||||||
readTelegramAllowFromStore: vi.fn().mockResolvedValue([]),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../pairing/pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
readChannelAllowFromStore: vi.fn().mockResolvedValue([]),
|
||||||
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
upsertChannelPairingRequest: vi.fn().mockResolvedValue({ code: "000000", created: false }),
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import { resolveChannelConfigWrites } from "../channels/plugins/config-writes.js
|
||||||
import { loadConfig } from "../config/config.js";
|
import { loadConfig } from "../config/config.js";
|
||||||
import { writeConfigFile } from "../config/io.js";
|
import { writeConfigFile } from "../config/io.js";
|
||||||
import { danger, logVerbose, warn } from "../globals.js";
|
import { danger, logVerbose, warn } from "../globals.js";
|
||||||
|
import { readChannelAllowFromStore } from "../pairing/pairing-store.js";
|
||||||
import { withTelegramApiErrorLogging } from "./api-logging.js";
|
import { withTelegramApiErrorLogging } from "./api-logging.js";
|
||||||
import { firstDefined, isSenderAllowed, normalizeAllowFromWithStore } from "./bot-access.js";
|
import { firstDefined, isSenderAllowed, normalizeAllowFromWithStore } from "./bot-access.js";
|
||||||
import { RegisterTelegramHandlerParams } from "./bot-native-commands.js";
|
import { RegisterTelegramHandlerParams } from "./bot-native-commands.js";
|
||||||
|
|
@ -21,7 +22,6 @@ import { resolveMedia } from "./bot/delivery.js";
|
||||||
import { resolveTelegramForumThreadId } from "./bot/helpers.js";
|
import { resolveTelegramForumThreadId } 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";
|
||||||
import { readTelegramAllowFromStore } from "./pairing-store.js";
|
|
||||||
import { buildInlineKeyboard } from "./send.js";
|
import { buildInlineKeyboard } from "./send.js";
|
||||||
|
|
||||||
export const registerTelegramHandlers = ({
|
export const registerTelegramHandlers = ({
|
||||||
|
|
@ -142,7 +142,7 @@ export const registerTelegramHandlers = ({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const storeAllowFrom = await readTelegramAllowFromStore().catch(() => []);
|
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []);
|
||||||
await processMessage(primaryEntry.ctx, allMedia, storeAllowFrom);
|
await processMessage(primaryEntry.ctx, allMedia, storeAllowFrom);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
runtime.error?.(danger(`media group handler failed: ${String(err)}`));
|
runtime.error?.(danger(`media group handler failed: ${String(err)}`));
|
||||||
|
|
@ -173,7 +173,7 @@ export const registerTelegramHandlers = ({
|
||||||
date: last.msg.date ?? first.msg.date,
|
date: last.msg.date ?? first.msg.date,
|
||||||
};
|
};
|
||||||
|
|
||||||
const storeAllowFrom = await readTelegramAllowFromStore().catch(() => []);
|
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []);
|
||||||
const baseCtx = first.ctx as { me?: unknown; getFile?: unknown } & Record<string, unknown>;
|
const baseCtx = first.ctx as { me?: unknown; getFile?: unknown } & Record<string, unknown>;
|
||||||
const getFile =
|
const getFile =
|
||||||
typeof baseCtx.getFile === "function" ? baseCtx.getFile.bind(baseCtx) : async () => ({});
|
typeof baseCtx.getFile === "function" ? baseCtx.getFile.bind(baseCtx) : async () => ({});
|
||||||
|
|
@ -248,7 +248,7 @@ export const registerTelegramHandlers = ({
|
||||||
messageThreadId,
|
messageThreadId,
|
||||||
});
|
});
|
||||||
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId);
|
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId);
|
||||||
const storeAllowFrom = await readTelegramAllowFromStore().catch(() => []);
|
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []);
|
||||||
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
|
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
|
||||||
const effectiveGroupAllow = normalizeAllowFromWithStore({
|
const effectiveGroupAllow = normalizeAllowFromWithStore({
|
||||||
allowFrom: groupAllowOverride ?? groupAllowFrom,
|
allowFrom: groupAllowOverride ?? groupAllowFrom,
|
||||||
|
|
@ -492,7 +492,7 @@ export const registerTelegramHandlers = ({
|
||||||
isForum,
|
isForum,
|
||||||
messageThreadId,
|
messageThreadId,
|
||||||
});
|
});
|
||||||
const storeAllowFrom = await readTelegramAllowFromStore().catch(() => []);
|
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []);
|
||||||
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId);
|
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId);
|
||||||
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
|
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
|
||||||
const effectiveGroupAllow = normalizeAllowFromWithStore({
|
const effectiveGroupAllow = normalizeAllowFromWithStore({
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ import { formatCliCommand } from "../cli/command-format.js";
|
||||||
import { readSessionUpdatedAt, resolveStorePath } from "../config/sessions.js";
|
import { readSessionUpdatedAt, resolveStorePath } from "../config/sessions.js";
|
||||||
import { logVerbose, shouldLogVerbose } from "../globals.js";
|
import { logVerbose, shouldLogVerbose } from "../globals.js";
|
||||||
import { recordChannelActivity } from "../infra/channel-activity.js";
|
import { recordChannelActivity } from "../infra/channel-activity.js";
|
||||||
|
import { upsertChannelPairingRequest } from "../pairing/pairing-store.js";
|
||||||
import { resolveAgentRoute } from "../routing/resolve-route.js";
|
import { resolveAgentRoute } from "../routing/resolve-route.js";
|
||||||
import { resolveThreadSessionKeys } from "../routing/session-key.js";
|
import { resolveThreadSessionKeys } from "../routing/session-key.js";
|
||||||
import { withTelegramApiErrorLogging } from "./api-logging.js";
|
import { withTelegramApiErrorLogging } from "./api-logging.js";
|
||||||
|
|
@ -52,7 +53,6 @@ import {
|
||||||
hasBotMention,
|
hasBotMention,
|
||||||
resolveTelegramForumThreadId,
|
resolveTelegramForumThreadId,
|
||||||
} from "./bot/helpers.js";
|
} from "./bot/helpers.js";
|
||||||
import { upsertTelegramPairingRequest } from "./pairing-store.js";
|
|
||||||
|
|
||||||
type TelegramMediaRef = {
|
type TelegramMediaRef = {
|
||||||
path: string;
|
path: string;
|
||||||
|
|
@ -252,11 +252,14 @@ export const buildTelegramMessageContext = async ({
|
||||||
}
|
}
|
||||||
| undefined;
|
| undefined;
|
||||||
const telegramUserId = from?.id ? String(from.id) : candidate;
|
const telegramUserId = from?.id ? String(from.id) : candidate;
|
||||||
const { code, created } = await upsertTelegramPairingRequest({
|
const { code, created } = await upsertChannelPairingRequest({
|
||||||
chatId: candidate,
|
channel: "telegram",
|
||||||
|
id: String(candidate),
|
||||||
|
meta: {
|
||||||
username: from?.username,
|
username: from?.username,
|
||||||
firstName: from?.first_name,
|
firstName: from?.first_name,
|
||||||
lastName: from?.last_name,
|
lastName: from?.last_name,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
if (created) {
|
if (created) {
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ vi.mock("../plugins/commands.js", () => ({
|
||||||
const deliverReplies = vi.hoisted(() => vi.fn(async () => {}));
|
const deliverReplies = vi.hoisted(() => vi.fn(async () => {}));
|
||||||
vi.mock("./bot/delivery.js", () => ({ deliverReplies }));
|
vi.mock("./bot/delivery.js", () => ({ deliverReplies }));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => []),
|
readChannelAllowFromStore: vi.fn(async () => []),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe("registerTelegramNativeCommands (plugin auth)", () => {
|
describe("registerTelegramNativeCommands (plugin auth)", () => {
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ import {
|
||||||
} from "../config/telegram-custom-commands.js";
|
} from "../config/telegram-custom-commands.js";
|
||||||
import { danger, logVerbose } from "../globals.js";
|
import { danger, logVerbose } from "../globals.js";
|
||||||
import { getChildLogger } from "../logging.js";
|
import { getChildLogger } from "../logging.js";
|
||||||
|
import { readChannelAllowFromStore } from "../pairing/pairing-store.js";
|
||||||
import {
|
import {
|
||||||
executePluginCommand,
|
executePluginCommand,
|
||||||
getPluginCommandSpecs,
|
getPluginCommandSpecs,
|
||||||
|
|
@ -49,7 +50,6 @@ import {
|
||||||
buildTelegramGroupPeerId,
|
buildTelegramGroupPeerId,
|
||||||
resolveTelegramForumThreadId,
|
resolveTelegramForumThreadId,
|
||||||
} from "./bot/helpers.js";
|
} from "./bot/helpers.js";
|
||||||
import { readTelegramAllowFromStore } from "./pairing-store.js";
|
|
||||||
import { buildInlineKeyboard } from "./send.js";
|
import { buildInlineKeyboard } from "./send.js";
|
||||||
|
|
||||||
const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
|
const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
|
||||||
|
|
@ -153,7 +153,7 @@ async function resolveTelegramCommandAuth(params: {
|
||||||
isForum,
|
isForum,
|
||||||
messageThreadId,
|
messageThreadId,
|
||||||
});
|
});
|
||||||
const storeAllowFrom = await readTelegramAllowFromStore().catch(() => []);
|
const storeAllowFrom = await readChannelAllowFromStore("telegram").catch(() => []);
|
||||||
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId);
|
const { groupConfig, topicConfig } = resolveTelegramGroupConfig(chatId, resolvedThreadId);
|
||||||
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
|
const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom);
|
||||||
const effectiveGroupAllow = normalizeAllowFromWithStore({
|
const effectiveGroupAllow = normalizeAllowFromWithStore({
|
||||||
|
|
|
||||||
|
|
@ -35,17 +35,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -36,17 +36,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
@ -357,8 +357,8 @@ describe("createTelegramBot", () => {
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: { telegram: { dmPolicy: "pairing" } },
|
channels: { telegram: { dmPolicy: "pairing" } },
|
||||||
});
|
});
|
||||||
readTelegramAllowFromStore.mockResolvedValue([]);
|
readChannelAllowFromStore.mockResolvedValue([]);
|
||||||
upsertTelegramPairingRequest.mockResolvedValue({
|
upsertChannelPairingRequest.mockResolvedValue({
|
||||||
code: "PAIRME12",
|
code: "PAIRME12",
|
||||||
created: true,
|
created: true,
|
||||||
});
|
});
|
||||||
|
|
@ -393,8 +393,8 @@ describe("createTelegramBot", () => {
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: { telegram: { dmPolicy: "pairing" } },
|
channels: { telegram: { dmPolicy: "pairing" } },
|
||||||
});
|
});
|
||||||
readTelegramAllowFromStore.mockResolvedValue([]);
|
readChannelAllowFromStore.mockResolvedValue([]);
|
||||||
upsertTelegramPairingRequest
|
upsertChannelPairingRequest
|
||||||
.mockResolvedValueOnce({ code: "PAIRME12", created: true })
|
.mockResolvedValueOnce({ code: "PAIRME12", created: true })
|
||||||
.mockResolvedValueOnce({ code: "PAIRME12", created: false });
|
.mockResolvedValueOnce({ code: "PAIRME12", created: false });
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -39,17 +39,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const useSpy = vi.fn();
|
const useSpy = vi.fn();
|
||||||
|
|
|
||||||
|
|
@ -88,9 +88,9 @@ vi.mock("./sticker-cache.js", () => ({
|
||||||
describeStickerImage: (...args: unknown[]) => describeStickerImageSpy(...args),
|
describeStickerImage: (...args: unknown[]) => describeStickerImageSpy(...args),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
|
|
|
||||||
|
|
@ -77,9 +77,9 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
|
|
|
||||||
|
|
@ -56,17 +56,17 @@ vi.mock("../config/sessions.js", async (importOriginal) => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const { readTelegramAllowFromStore, upsertTelegramPairingRequest } = vi.hoisted(() => ({
|
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(() => ({
|
||||||
readTelegramAllowFromStore: vi.fn(async () => [] as string[]),
|
readChannelAllowFromStore: vi.fn(async () => [] as string[]),
|
||||||
upsertTelegramPairingRequest: vi.fn(async () => ({
|
upsertChannelPairingRequest: vi.fn(async () => ({
|
||||||
code: "PAIRCODE",
|
code: "PAIRCODE",
|
||||||
created: true,
|
created: true,
|
||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("./pairing-store.js", () => ({
|
vi.mock("../pairing/pairing-store.js", () => ({
|
||||||
readTelegramAllowFromStore,
|
readChannelAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertChannelPairingRequest,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const { enqueueSystemEvent } = vi.hoisted(() => ({
|
const { enqueueSystemEvent } = vi.hoisted(() => ({
|
||||||
|
|
@ -569,8 +569,8 @@ describe("createTelegramBot", () => {
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: { telegram: { dmPolicy: "pairing" } },
|
channels: { telegram: { dmPolicy: "pairing" } },
|
||||||
});
|
});
|
||||||
readTelegramAllowFromStore.mockResolvedValue([]);
|
readChannelAllowFromStore.mockResolvedValue([]);
|
||||||
upsertTelegramPairingRequest.mockResolvedValue({
|
upsertChannelPairingRequest.mockResolvedValue({
|
||||||
code: "PAIRME12",
|
code: "PAIRME12",
|
||||||
created: true,
|
created: true,
|
||||||
});
|
});
|
||||||
|
|
@ -606,8 +606,8 @@ describe("createTelegramBot", () => {
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
channels: { telegram: { dmPolicy: "pairing" } },
|
channels: { telegram: { dmPolicy: "pairing" } },
|
||||||
});
|
});
|
||||||
readTelegramAllowFromStore.mockResolvedValue([]);
|
readChannelAllowFromStore.mockResolvedValue([]);
|
||||||
upsertTelegramPairingRequest
|
upsertChannelPairingRequest
|
||||||
.mockResolvedValueOnce({ code: "PAIRME12", created: true })
|
.mockResolvedValueOnce({ code: "PAIRME12", created: true })
|
||||||
.mockResolvedValueOnce({ code: "PAIRME12", created: false });
|
.mockResolvedValueOnce({ code: "PAIRME12", created: false });
|
||||||
|
|
||||||
|
|
@ -2335,7 +2335,7 @@ describe("createTelegramBot", () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
readTelegramAllowFromStore.mockResolvedValueOnce(["12345"]);
|
readChannelAllowFromStore.mockResolvedValueOnce(["12345"]);
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
||||||
|
|
@ -2378,7 +2378,7 @@ describe("createTelegramBot", () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
readTelegramAllowFromStore.mockResolvedValueOnce(["12345"]);
|
readChannelAllowFromStore.mockResolvedValueOnce(["12345"]);
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
||||||
|
|
@ -2422,7 +2422,7 @@ describe("createTelegramBot", () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
readTelegramAllowFromStore.mockResolvedValueOnce([]);
|
readChannelAllowFromStore.mockResolvedValueOnce([]);
|
||||||
|
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
||||||
|
|
|
||||||
|
|
@ -1,52 +0,0 @@
|
||||||
import fs from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { describe, expect, it } from "vitest";
|
|
||||||
import {
|
|
||||||
approveTelegramPairingCode,
|
|
||||||
listTelegramPairingRequests,
|
|
||||||
readTelegramAllowFromStore,
|
|
||||||
upsertTelegramPairingRequest,
|
|
||||||
} from "./pairing-store.js";
|
|
||||||
|
|
||||||
async function withTempStateDir<T>(fn: (stateDir: string) => Promise<T>) {
|
|
||||||
const previous = process.env.OPENCLAW_STATE_DIR;
|
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-pairing-"));
|
|
||||||
process.env.OPENCLAW_STATE_DIR = dir;
|
|
||||||
try {
|
|
||||||
return await fn(dir);
|
|
||||||
} finally {
|
|
||||||
if (previous === undefined) {
|
|
||||||
delete process.env.OPENCLAW_STATE_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_STATE_DIR = previous;
|
|
||||||
}
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("telegram pairing store", () => {
|
|
||||||
it("creates pairing request and approves it into allow store", async () => {
|
|
||||||
await withTempStateDir(async () => {
|
|
||||||
const created = await upsertTelegramPairingRequest({
|
|
||||||
chatId: "123456789",
|
|
||||||
username: "ada",
|
|
||||||
});
|
|
||||||
expect(created.code).toBeTruthy();
|
|
||||||
|
|
||||||
const list = await listTelegramPairingRequests();
|
|
||||||
expect(list).toHaveLength(1);
|
|
||||||
expect(list[0]?.chatId).toBe("123456789");
|
|
||||||
expect(list[0]?.code).toBe(created.code);
|
|
||||||
|
|
||||||
const approved = await approveTelegramPairingCode({ code: created.code });
|
|
||||||
expect(approved?.chatId).toBe("123456789");
|
|
||||||
|
|
||||||
const listAfter = await listTelegramPairingRequests();
|
|
||||||
expect(listAfter).toHaveLength(0);
|
|
||||||
|
|
||||||
const allow = await readTelegramAllowFromStore();
|
|
||||||
expect(allow).toContain("123456789");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,124 +0,0 @@
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
|
||||||
import {
|
|
||||||
addChannelAllowFromStoreEntry,
|
|
||||||
approveChannelPairingCode,
|
|
||||||
listChannelPairingRequests,
|
|
||||||
readChannelAllowFromStore,
|
|
||||||
upsertChannelPairingRequest,
|
|
||||||
} from "../pairing/pairing-store.js";
|
|
||||||
|
|
||||||
export type TelegramPairingListEntry = {
|
|
||||||
chatId: string;
|
|
||||||
username?: string;
|
|
||||||
firstName?: string;
|
|
||||||
lastName?: string;
|
|
||||||
code: string;
|
|
||||||
createdAt: string;
|
|
||||||
lastSeenAt: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const PROVIDER = "telegram" as const;
|
|
||||||
|
|
||||||
export async function readTelegramAllowFromStore(
|
|
||||||
env: NodeJS.ProcessEnv = process.env,
|
|
||||||
): Promise<string[]> {
|
|
||||||
return readChannelAllowFromStore(PROVIDER, env);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function addTelegramAllowFromStoreEntry(params: {
|
|
||||||
entry: string | number;
|
|
||||||
env?: NodeJS.ProcessEnv;
|
|
||||||
}): Promise<{ changed: boolean; allowFrom: string[] }> {
|
|
||||||
return addChannelAllowFromStoreEntry({
|
|
||||||
channel: PROVIDER,
|
|
||||||
entry: params.entry,
|
|
||||||
env: params.env,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function listTelegramPairingRequests(
|
|
||||||
env: NodeJS.ProcessEnv = process.env,
|
|
||||||
): Promise<TelegramPairingListEntry[]> {
|
|
||||||
const list = await listChannelPairingRequests(PROVIDER, env);
|
|
||||||
return list.map((r) => ({
|
|
||||||
chatId: r.id,
|
|
||||||
code: r.code,
|
|
||||||
createdAt: r.createdAt,
|
|
||||||
lastSeenAt: r.lastSeenAt,
|
|
||||||
username: r.meta?.username,
|
|
||||||
firstName: r.meta?.firstName,
|
|
||||||
lastName: r.meta?.lastName,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function upsertTelegramPairingRequest(params: {
|
|
||||||
chatId: string | number;
|
|
||||||
username?: string;
|
|
||||||
firstName?: string;
|
|
||||||
lastName?: string;
|
|
||||||
env?: NodeJS.ProcessEnv;
|
|
||||||
}): Promise<{ code: string; created: boolean }> {
|
|
||||||
return upsertChannelPairingRequest({
|
|
||||||
channel: PROVIDER,
|
|
||||||
id: String(params.chatId),
|
|
||||||
env: params.env,
|
|
||||||
meta: {
|
|
||||||
username: params.username,
|
|
||||||
firstName: params.firstName,
|
|
||||||
lastName: params.lastName,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function approveTelegramPairingCode(params: {
|
|
||||||
code: string;
|
|
||||||
env?: NodeJS.ProcessEnv;
|
|
||||||
}): Promise<{ chatId: string; entry?: TelegramPairingListEntry } | null> {
|
|
||||||
const res = await approveChannelPairingCode({
|
|
||||||
channel: PROVIDER,
|
|
||||||
code: params.code,
|
|
||||||
env: params.env,
|
|
||||||
});
|
|
||||||
if (!res) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const entry = res.entry
|
|
||||||
? {
|
|
||||||
chatId: res.entry.id,
|
|
||||||
code: res.entry.code,
|
|
||||||
createdAt: res.entry.createdAt,
|
|
||||||
lastSeenAt: res.entry.lastSeenAt,
|
|
||||||
username: res.entry.meta?.username,
|
|
||||||
firstName: res.entry.meta?.firstName,
|
|
||||||
lastName: res.entry.meta?.lastName,
|
|
||||||
}
|
|
||||||
: undefined;
|
|
||||||
return { chatId: res.id, entry };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function resolveTelegramEffectiveAllowFrom(params: {
|
|
||||||
cfg: OpenClawConfig;
|
|
||||||
env?: NodeJS.ProcessEnv;
|
|
||||||
}): Promise<{ dm: string[]; group: string[] }> {
|
|
||||||
const env = params.env ?? process.env;
|
|
||||||
const cfgAllowFrom = (params.cfg.channels?.telegram?.allowFrom ?? [])
|
|
||||||
.map((v) => String(v).trim())
|
|
||||||
.filter(Boolean)
|
|
||||||
.map((v) => v.replace(/^(telegram|tg):/i, ""))
|
|
||||||
.filter((v) => v !== "*");
|
|
||||||
const cfgGroupAllowFrom = (params.cfg.channels?.telegram?.groupAllowFrom ?? [])
|
|
||||||
.map((v) => String(v).trim())
|
|
||||||
.filter(Boolean)
|
|
||||||
.map((v) => v.replace(/^(telegram|tg):/i, ""))
|
|
||||||
.filter((v) => v !== "*");
|
|
||||||
const storeAllowFrom = await readTelegramAllowFromStore(env);
|
|
||||||
|
|
||||||
const dm = Array.from(new Set([...cfgAllowFrom, ...storeAllowFrom]));
|
|
||||||
const group = Array.from(
|
|
||||||
new Set([
|
|
||||||
...(cfgGroupAllowFrom.length > 0 ? cfgGroupAllowFrom : cfgAllowFrom),
|
|
||||||
...storeAllowFrom,
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
return { dm, group };
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue