Fix TypeScript errors: Update User type system across all storage implementations

This commit is contained in:
katelya
2025-09-05 15:59:55 +08:00
parent d83e2c6f42
commit 142c780b50
9 changed files with 574 additions and 22 deletions
+30 -3
View File
@@ -3,7 +3,7 @@
import { Redis } from '@upstash/redis';
import { AdminConfig } from './admin.types';
import { EpisodeSkipConfig, Favorite, IStorage, PlayRecord, UserSettings } from './types';
import { EpisodeSkipConfig, Favorite, IStorage, PlayRecord, User, UserSettings } from './types';
// 搜索历史最大条数
const SEARCH_HISTORY_LIMIT = 20;
@@ -244,14 +244,41 @@ export class UpstashRedisStorage implements IStorage {
}
// ---------- 获取全部用户 ----------
async getAllUsers(): Promise<string[]> {
async getAllUsers(): Promise<User[]> {
const keys = await withRetry(() => this.client.keys('u:*:pwd'));
return keys
const ownerUsername = process.env.USERNAME || 'admin';
const usernames = keys
.map((k) => {
const match = k.match(/^u:(.+?):pwd$/);
return match ? ensureString(match[1]) : undefined;
})
.filter((u): u is string => typeof u === 'string');
// 获取用户创建时间并构造 User 对象
const users = await Promise.all(
usernames.map(async (username) => {
// 尝试获取用户创建时间,如果没有则使用空字符串
const createdAtKey = `u:${username}:created_at`;
let created_at = '';
try {
const timestamp = await withRetry(() => this.client.get(createdAtKey));
if (timestamp && typeof timestamp === 'number') {
created_at = new Date(timestamp).toISOString();
}
} catch (err) {
// 忽略错误,使用空字符串
}
return {
username,
role: username === ownerUsername ? 'owner' : 'user',
created_at
};
})
);
return users;
}
// ---------- 管理员配置 ----------