refactor: Add Telegram binding storage and auth

This commit is contained in:
weishu
2025-12-31 17:43:57 +08:00
parent a0b1bb1524
commit 8f7dcf848b
16 changed files with 406 additions and 202 deletions
-28
View File
@@ -13,7 +13,6 @@ import { readSettings, writeSettings, type Settings } from './web/cliApiToken'
export interface ServerSettings {
telegramBotToken: string | null
allowedChatIds: number[]
webappPort: number
webappUrl: string
corsOrigins: string[]
@@ -23,7 +22,6 @@ export interface ServerSettingsResult {
settings: ServerSettings
sources: {
telegramBotToken: 'env' | 'file' | 'default'
allowedChatIds: 'env' | 'file' | 'default'
webappPort: 'env' | 'file' | 'default'
webappUrl: 'env' | 'file' | 'default'
corsOrigins: 'env' | 'file' | 'default'
@@ -31,16 +29,6 @@ export interface ServerSettingsResult {
savedToFile: boolean
}
/**
* Parse comma-separated chat IDs from string
*/
function parseChatIds(str: string): number[] {
return str
.split(',')
.map(id => parseInt(id.trim(), 10))
.filter(id => !isNaN(id))
}
/**
* Parse and normalize CORS origins
*/
@@ -95,7 +83,6 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
let needsSave = false
const sources: ServerSettingsResult['sources'] = {
telegramBotToken: 'default',
allowedChatIds: 'default',
webappPort: 'default',
webappUrl: 'default',
corsOrigins: 'default',
@@ -115,20 +102,6 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
sources.telegramBotToken = 'file'
}
// allowedChatIds: env > file > []
let allowedChatIds: number[] = []
if (process.env.ALLOWED_CHAT_IDS) {
allowedChatIds = parseChatIds(process.env.ALLOWED_CHAT_IDS)
sources.allowedChatIds = 'env'
if (settings.allowedChatIds === undefined) {
settings.allowedChatIds = allowedChatIds
needsSave = true
}
} else if (settings.allowedChatIds !== undefined) {
allowedChatIds = settings.allowedChatIds
sources.allowedChatIds = 'file'
}
// webappPort: env > file > 3006
let webappPort = 3006
if (process.env.WEBAPP_PORT) {
@@ -185,7 +158,6 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
return {
settings: {
telegramBotToken,
allowedChatIds,
webappPort,
webappUrl,
corsOrigins,