diff --git a/src/database/server/models/message.ts b/src/database/server/models/message.ts index 7fa7a477df22..543b71a742ce 100644 --- a/src/database/server/models/message.ts +++ b/src/database/server/models/message.ts @@ -294,6 +294,16 @@ export class MessageModel { return result[0].count; }; + hasMoreThanN = async (n: number): Promise => { + const result = await this.db + .select({ id: messages.id }) + .from(messages) + .where(eq(messages.userId, this.userId)) + .limit(n + 1); + + return result.length > n; + }; + // **************** Create *************** // create = async ( diff --git a/src/database/server/models/session.ts b/src/database/server/models/session.ts index 74a4ae87dc39..2a39ba4127bd 100644 --- a/src/database/server/models/session.ts +++ b/src/database/server/models/session.ts @@ -95,6 +95,16 @@ export class SessionModel { return result[0].count; }; + hasMoreThanN = async (n: number): Promise => { + const result = await this.db + .select({ id: sessions.id }) + .from(sessions) + .where(eq(sessions.userId, this.userId)) + .limit(n + 1); + + return result.length > n; + }; + // **************** Create *************** // create = async ({ diff --git a/src/server/routers/lambda/user.ts b/src/server/routers/lambda/user.ts index f7522004940b..6c8f6a19a794 100644 --- a/src/server/routers/lambda/user.ts +++ b/src/server/routers/lambda/user.ts @@ -58,16 +58,17 @@ export const userRouter = router({ } const messageModel = new MessageModel(serverDB, ctx.userId); - const messageCount = await messageModel.count(); + const hasMoreThan4Messages = await messageModel.hasMoreThanN(4); const sessionModel = new SessionModel(serverDB, ctx.userId); - const sessionCount = await sessionModel.count(); + const hasAnyMessages = await messageModel.hasMoreThanN(0); + const hasExtraSession = await sessionModel.hasMoreThanN(1); return { - canEnablePWAGuide: messageCount >= 4, - canEnableTrace: messageCount >= 4, + canEnablePWAGuide: hasMoreThan4Messages, + canEnableTrace: hasMoreThan4Messages, // 有消息,或者创建过助手,则认为有 conversation - hasConversation: messageCount > 0 || sessionCount > 1, + hasConversation: hasAnyMessages || hasExtraSession, // always return true for community version isOnboard: state.isOnboarded || true,