Add room-scoped Matrix plugins and bilingual configuration commands
This commit is contained in:
1 parent
061bfbd12a
commit
8a87c893fd
75 files changed
+2559
-1975
No files matched your search
+59
-2
@@ -1,5 +1,8 @@
|
||||
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 形式'),
|
||||
@@ -9,15 +12,69 @@ const accountSchema = z.object({
|
||||
const botSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
account: z.string().min(1),
|
||||
rooms: z.array(z.string().regex(/^!.+:.+$/, 'roomId 必须是 !id:server 形式')).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),
|
||||
bots: z.array(botSchema),
|
||||
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>;
|
||||
Reference in new issue
Block a user