@@ -46,26 +46,6 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
46
46
} ) ;
47
47
}
48
48
49
- // get the number of messages where `from === "assistant"` across all conversations.
50
- const totalMessages =
51
- (
52
- await collections . conversations
53
- . aggregate ( [
54
- { $match : authCondition ( locals ) } ,
55
- { $project : { messages : 1 } } ,
56
- { $unwind : "$messages" } ,
57
- { $match : { "messages.from" : "assistant" } } ,
58
- { $count : "messages" } ,
59
- ] )
60
- . toArray ( )
61
- ) [ 0 ] ?. messages ?? 0 ;
62
-
63
- const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt ( MESSAGES_BEFORE_LOGIN ) : 0 ;
64
-
65
- const userHasExceededMessages = messagesBeforeLogin > 0 && totalMessages > messagesBeforeLogin ;
66
-
67
- const loginRequired = requiresUser && ! locals . user && userHasExceededMessages ;
68
-
69
49
const enableAssistants = ENABLE_ASSISTANTS === "true" ;
70
50
71
51
const assistantActive = ! models . map ( ( { id } ) => id ) . includes ( settings ?. activeModel ?? "" ) ;
@@ -103,6 +83,33 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
103
83
104
84
const assistants = await collections . assistants . find ( { _id : { $in : assistantIds } } ) . toArray ( ) ;
105
85
86
+ const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt ( MESSAGES_BEFORE_LOGIN ) : 0 ;
87
+
88
+ let loginRequired = false ;
89
+
90
+ if ( requiresUser && ! locals . user && messagesBeforeLogin ) {
91
+ if ( conversations . length > messagesBeforeLogin ) {
92
+ loginRequired = true ;
93
+ } else {
94
+ // get the number of messages where `from === "assistant"` across all conversations.
95
+ const totalMessages =
96
+ (
97
+ await collections . conversations
98
+ . aggregate ( [
99
+ { $match : { ...authCondition ( locals ) , "messages.from" : "assistant" } } ,
100
+ { $project : { messages : 1 } } ,
101
+ { $limit : messagesBeforeLogin + 1 } ,
102
+ { $unwind : "$messages" } ,
103
+ { $match : { "messages.from" : "assistant" } } ,
104
+ { $count : "messages" } ,
105
+ ] )
106
+ . toArray ( )
107
+ ) [ 0 ] ?. messages ?? 0 ;
108
+
109
+ loginRequired = totalMessages > messagesBeforeLogin ;
110
+ }
111
+ }
112
+
106
113
return {
107
114
conversations : conversations . map ( ( conv ) => {
108
115
if ( settings ?. hideEmojiOnSidebar ) {
0 commit comments