feat: reorganize settings and add synced appearance preferences

This commit is contained in:
NoSetViolin committed 2026-10-12 18:26:53 +09:00
1 parent 28c128d0c9
commit 8d750d19b0
30 files changed
+835 -98

No files matched your search

+23 -1
View File
@@ -37,13 +37,35 @@ describe("database", () => {
const userCols = botDb.db.prepare("PRAGMA table_info(users)").all() as Array<{ name: string }>;
const userColNames = userCols.map((c) => c.name).sort();
expect(userColNames).toEqual(["createdAt", "id", "passwordHash", "role", "updatedAt", "username"]);
expect(userColNames).toEqual(["accentColor", "createdAt", "id", "passwordHash", "role", "updatedAt", "username"]);
const sessionCols = botDb.db.prepare("PRAGMA table_info(sessions)").all() as Array<{ name: string }>;
const sessionColNames = sessionCols.map((c) => c.name).sort();
expect(sessionColNames).toEqual(["createdAt", "expiresAt", "id", "lastSeenAt", "userId"]);
});
it("migrates existing accounts and preserves their accent across database reopen", async () => {
const dir = mkdtempSync(join(tmpdir(), 'tsmb-accent-'));
const path = join(dir, 'test.db');
let db = createDatabase(path);
try {
const user = await createUserStore(db.db).createUser('alice', 'test-password', 'member');
db.db.exec('ALTER TABLE users DROP COLUMN accentColor');
db.close();
db = createDatabase(path);
const users = createUserStore(db.db);
expect(users.getAccentColor(user.id)).toBe('#335eea');
users.setAccentColor(user.id, '#0891b2');
db.close();
db = createDatabase(path);
expect(createUserStore(db.db).getAccentColor(user.id)).toBe('#0891b2');
expect(createUserStore(db.db).findById(user.id)?.username).toBe('alice');
} finally {
db.close();
rmSync(dir, { recursive: true, force: true });
}
});
it("creates user_audit table on init", () => {
const tables = botDb.db
.prepare("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
+3
View File
@@ -210,6 +210,9 @@ function migrateSchema(db: Database.Database): void {
if (!userColNames.includes("role")) {
db.exec("ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'admin'");
}
if (!userColNames.includes("accentColor")) {
db.exec("ALTER TABLE users ADD COLUMN accentColor TEXT NOT NULL DEFAULT '#335eea'");
}
const historyColumns = db.prepare("PRAGMA table_info(play_history)").all() as Array<{ name: string }>;
const historyColNames = historyColumns.map((c) => c.name);
+8
View File
@@ -22,6 +22,8 @@ export interface UserRow {
}
export interface UserStore {
getAccentColor(userId: string): string;
setAccentColor(userId: string, color: string): boolean;
countUsers(): number;
countAdmins(): number;
createUser(username: string, password: string, role: UserRole): Promise<UserRow>;
@@ -68,6 +70,12 @@ export function createUserStore(db: Database.Database): UserStore {
const deleteUserStmt = db.prepare("DELETE FROM users WHERE id = ?");
return {
getAccentColor(userId) {
return (db.prepare("SELECT accentColor FROM users WHERE id = ?").get(userId) as { accentColor: string } | undefined)?.accentColor ?? "#335eea";
},
setAccentColor(userId, color) {
return db.prepare("UPDATE users SET accentColor = ?, updatedAt = ? WHERE id = ? AND role != 'guest'").run(color, Date.now(), userId).changes > 0;
},
countUsers() {
return (countStmt.get() as { n: number }).n;
},