81 lines
3.3 KiB
TypeScript
81 lines
3.3 KiB
TypeScript
import { z } from 'zod';
|
|
|
|
// Preserve the exact server-supplied ID for routing, including domainless v12 IDs.
|
|
export const matrixRoomId = z.string().regex(/^!(?:[A-Za-z0-9_-]{43}|[^\s:]+:[^\s]+)$/, 'Invalid roomId');
|
|
|
|
const accountSchema = z.object({
|
|
homeServer: z.string().url(),
|
|
userId: z.string().regex(/^@.+:.+$/, 'userId 必须是 @user:server 形式'),
|
|
password: z.string().min(1),
|
|
});
|
|
|
|
const botSchema = z.object({
|
|
name: z.string().min(1),
|
|
account: z.string().min(1),
|
|
rooms: z.array(matrixRoomId).min(1),
|
|
config: z.record(z.any()).default({}),
|
|
});
|
|
|
|
export const roomSchema = z.object({
|
|
account: z.string().min(1),
|
|
roomId: matrixRoomId,
|
|
locale: z.enum(['zh-CN', 'en']).default('zh-CN'),
|
|
timeZone: z.string().default('Asia/Shanghai').refine((value) => {
|
|
try { new Intl.DateTimeFormat('en', { timeZone: value }); return true; }
|
|
catch { return false; }
|
|
}, 'Invalid timeZone'),
|
|
adminPowerLevel: z.number().int().min(1).default(100),
|
|
admins: z.array(z.string().regex(/^@.+:.+$/)).default([]),
|
|
plugins: z.record(z.object({
|
|
enabled: z.boolean().default(false), config: z.record(z.unknown()).default({}),
|
|
})).default({}),
|
|
});
|
|
export const rootConfigSchema = z.object({
|
|
accounts: z.record(accountSchema),
|
|
rooms: z.array(roomSchema).default([]),
|
|
bots: z.array(botSchema).default([]),
|
|
pluginDirectories: z.array(z.string().min(1)).default([]),
|
|
legacyCheckinTarget: z.object({ account: z.string(), roomId: matrixRoomId }).optional(),
|
|
}).transform((config, ctx) => {
|
|
if (config.rooms.length && config.bots.length) {
|
|
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Use rooms or legacy bots, not both' });
|
|
return z.NEVER;
|
|
}
|
|
const rooms = [...config.rooms];
|
|
for (const bot of config.bots) {
|
|
for (const roomId of bot.rooms) {
|
|
let room = rooms.find((r) => r.account === bot.account && r.roomId === roomId);
|
|
if (!room) {
|
|
room = roomSchema.parse({ account: bot.account, roomId,
|
|
adminPowerLevel: bot.config.adminPowerLevel, admins: bot.config.admins });
|
|
rooms.push(room);
|
|
}
|
|
if (room.plugins[bot.name]) {
|
|
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Duplicate plugin in legacy room' });
|
|
return z.NEVER;
|
|
}
|
|
const { mainRoom, adminPowerLevel, admins, ...options } = bot.config;
|
|
room.plugins[bot.name] = { enabled: true, config: options };
|
|
}
|
|
}
|
|
const keys = new Set<string>();
|
|
for (const room of rooms) {
|
|
const key = JSON.stringify([room.account, room.roomId]);
|
|
if (!config.accounts[room.account] || keys.has(key)) {
|
|
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Unknown account or duplicate room' });
|
|
return z.NEVER;
|
|
}
|
|
keys.add(key);
|
|
}
|
|
if (!rooms.length) ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'At least one managed room is required' });
|
|
const legacy = config.bots.filter((b) => b.name === 'checkin');
|
|
const inferred = legacy.length === 1 && legacy[0].rooms.length === 1
|
|
? { account: legacy[0].account, roomId: legacy[0].rooms[0] } : undefined;
|
|
return { ...config, rooms, legacyCheckinTarget: config.legacyCheckinTarget ?? inferred };
|
|
});
|
|
|
|
export type AccountConfig = z.infer<typeof accountSchema>;
|
|
export type BotConfig = z.infer<typeof botSchema>;
|
|
export type RootConfig = z.infer<typeof rootConfigSchema>;
|
|
export type RoomConfig = z.infer<typeof roomSchema>;
|