Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

⚡️ perf: skip user count on api key checks (#15385)

Rylan Cai5b25b8d8bb41
diff --git a/apps/cli/src/auth/apiKey.ts b/apps/cli/src/auth/apiKey.ts
index e6eac3be923..7b53adb9e7a 100644
--- a/apps/cli/src/auth/apiKey.ts
+++ b/apps/cli/src/auth/apiKey.ts
@@ -13,7 +13,7 @@ interface CurrentUserResponse {
export async function getUserIdFromApiKey(apiKey: string, serverUrl?: string): Promise<string> {
const normalizedServerUrl = normalizeUrl(serverUrl) || resolveServerUrl();
- const response = await fetch(`${normalizedServerUrl}/api/v1/users/me`, {
+ const response = await fetch(`${normalizedServerUrl}/api/v1/users/me?includeCount=0`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
@@ -23,7 +23,9 @@ export async function getUserIdFromApiKey(apiKey: string, serverUrl?: string): P
try {
body = (await response.json()) as CurrentUserResponse;
} catch {
- throw new Error(`Failed to parse response from ${normalizedServerUrl}/api/v1/users/me.`);
+ throw new Error(
+ `Failed to parse response from ${normalizedServerUrl}/api/v1/users/me?includeCount=0.`,
+ );
}
if (!response.ok || body?.success === false) {
diff --git a/packages/openapi/src/controllers/user.controller.ts b/packages/openapi/src/controllers/user.controller.ts
index 92761b5d89e..b16f9949d39 100644
--- a/packages/openapi/src/controllers/user.controller.ts
+++ b/packages/openapi/src/controllers/user.controller.ts
@@ -21,10 +21,13 @@ export class UserController extends BaseController {
*/
async getCurrentUser(c: Context): Promise<Response> {
try {
+ const includeCountQuery = c.req.query('includeCount');
+ const includeCount = includeCountQuery !== '0' && includeCountQuery !== 'false';
+
// Get database connection and create service instance
const db = await this.getDatabase();
const userService = new UserService(db, this.getUserId(c));
- const userInfo = await userService.getCurrentUser();
+ const userInfo = await userService.getCurrentUser(includeCount);
return this.success(c, userInfo, 'User info retrieved successfully');
} catch (error) {
diff --git a/packages/openapi/src/services/user.service.ts b/packages/openapi/src/services/user.service.ts
index 2787a22f868..228a15d07f0 100644
--- a/packages/openapi/src/services/user.service.ts
+++ b/packages/openapi/src/services/user.service.ts
@@ -33,7 +33,7 @@ export class UserService extends BaseService {
* @param userId User ID
* @returns User info and role info
*/
- private async getUserWithRoles(userId: string): Promise<UserWithRoles> {
+ private async getUserWithRoles(userId: string, includeCount = true): Promise<UserWithRoles> {
// Use subquery approach to avoid complex GROUP BY
const user = await this.db.query.users.findFirst({
where: eq(users.id, userId),
@@ -43,6 +43,19 @@ export class UserService extends BaseService {
throw this.createNotFoundError('用户不存在');
}
+ if (!includeCount) {
+ const userRoleResults = await this.db
+ .select({ roles })
+ .from(userRoles)
+ .innerJoin(roles, eq(userRoles.roleId, roles.id))
+ .where(eq(userRoles.userId, userId));
+
+ return {
+ ...user,
+ roles: userRoleResults.map((r) => r.roles),
+ };
+ }
+
// Fetch roles and message count in parallel for better efficiency
const [userRoleResults, messageCountResult] = await Promise.all([
this.db
@@ -65,11 +78,11 @@ export class UserService extends BaseService {
* Get the currently logged-in user info
* @returns User info
*/
- async getCurrentUser(): ServiceResult<UserWithRoles> {
+ async getCurrentUser(includeCount = true): ServiceResult<UserWithRoles> {
this.log('info', '获取当前登录用户信息及角色信息');
// Query basic user info
- return this.getUserWithRoles(this.userId!);
+ return this.getUserWithRoles(this.userId!, includeCount);
}
/**