Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { forgetHistory } from "./helpers/history.ts";
import { commandGoogleOauth } from "./helpers/google.ts";
import useTools from "./helpers/useTools.ts";
import { includesUser } from "./utils/users.ts";

export async function handleForget(ctx: Context) {
forgetHistory(ctx.chat!.id);
Expand Down Expand Up @@ -111,7 +112,7 @@ export async function commandAddTool(
// check admin
const { user } = getActionUserMsg(ctx);
const username = user?.username || "without_username";
if (!user || !config.adminUsers?.includes(username)) return;
if (!user || !includesUser(config.adminUsers, username)) return;

let chatConfig: ConfigChatType | undefined;
if (ctx.chat?.type === "private") {
Expand Down Expand Up @@ -193,7 +194,8 @@ export async function getToolsInfo(

// check access when privateUsers is set
if (agentConfig.privateUsers) {
const isPrivateUser = agentConfig.privateUsers.includes(
const isPrivateUser = includesUser(
agentConfig.privateUsers,
msg.from?.username || "without_username",
);
if (!isPrivateUser) return false;
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/gpt/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import useLangfuse from "../useLangfuse.ts";
import { isAdminUser } from "../../telegram/send.ts";
import { useConfig } from "../../config.ts";
import { requestGptAnswer } from "./llm.ts";
import { includesUser } from "../../utils/users.ts";
import { publishMqttProgress } from "../../mqtt.ts";

export function prettifyKeyValue(
Expand Down Expand Up @@ -496,7 +497,8 @@ export async function resolveChatTools(
);
if (!agentConfig) return false;
if (agentConfig.privateUsers) {
const isPrivateUser = agentConfig.privateUsers.includes(
const isPrivateUser = includesUser(
agentConfig.privateUsers,
msg.from?.username || "without_username",
);
if (!isPrivateUser) return false;
Expand Down
3 changes: 2 additions & 1 deletion src/telegram/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Context } from "telegraf";
import { User } from "@telegraf/types/manage";
import { useConfig } from "../config.ts";
import { log } from "../helpers.ts";
import { includesUser } from "../utils/users.ts";
import {
ChatParamsType,
CompletionParamsType,
Expand All @@ -16,7 +17,7 @@ function isAccessAllowed(chatConfig: ConfigChatType, ctxChat: Chat) {
...(useConfig().adminUsers ?? []),
];
const username = privateChat.username || "without_username";
return allowedUsers.includes(username);
return includesUser(allowedUsers, username);
}

function getChatConfig(
Expand Down
14 changes: 10 additions & 4 deletions src/telegram/send.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Message } from "telegraf/types";
import { useBot } from "../bot.ts";
import { useConfig } from "../config.ts";
import { includesUser } from "../utils/users.ts";
import { ConfigChatButtonType, ConfigChatType } from "../types.ts";
import { Context, Markup, Input } from "telegraf";
import { User } from "@telegraf/types/manage";
Expand Down Expand Up @@ -208,7 +209,7 @@ export async function sendTelegramMessage(

export function isAdminUser(msg: Message.TextMessage): boolean {
if (!msg.from?.username) return false;
return (useConfig().adminUsers || []).includes(msg.from.username);
return includesUser(useConfig().adminUsers, msg.from.username);
}

export function buildButtonRows(buttons: ConfigChatButtonType[]) {
Expand Down Expand Up @@ -252,9 +253,14 @@ export function isOurUser(
const chatPrivateUsers = chatConfig?.privateUsers || [];
const isOurUser =
username &&
[chatPrivateUsers, useConfig().privateUsers, useConfig().adminUsers]
.flat()
.includes(username);
includesUser(
[
...chatPrivateUsers,
...(useConfig().privateUsers || []),
...(useConfig().adminUsers || []),
],
username,
);
return isOurUser;
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function includesUser(
list: readonly string[] | undefined,
username: string | undefined,
): boolean {
if (!username || !list) return false;
const name = username.toLowerCase();
return list.some((u) => u.toLowerCase() === name);
}
10 changes: 10 additions & 0 deletions tests/telegram/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ describe("getCtxChatMsg", () => {
expect(msg?.text).toBe("hi");
});

it("ignores case when checking private users", () => {
mockUseConfig.mockReturnValue({
chats: [{ ...baseChat, privateUsers: ["User"] }],
privateUsers: [],
});
const ctx = createCtx({ message: createMsg("user") });
const { chat } = getCtxChatMsg(ctx);
expect(chat).toMatchObject(baseChat);
});

it("returns undefined chat when user not allowed", () => {
mockUseConfig.mockReturnValue({
chats: [{ ...baseChat, privateUsers: ["other"] }],
Expand Down
7 changes: 7 additions & 0 deletions tests/telegram/send.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ describe("isAdminUser", () => {
expect(isAdminUser(msg)).toBe(false);
});

it("ignores case", () => {
const msg = {
from: { username: "AdMiN" },
} as unknown as Message.TextMessage;
expect(isAdminUser(msg)).toBe(true);
});

it("returns false when username missing", () => {
const msg = { from: {} } as unknown as Message.TextMessage;
expect(isAdminUser(msg)).toBe(false);
Expand Down