Mishig commited on
Commit
77157ae
1 Parent(s): cd9f246

Revert "[Mongo] count messages (aggregate) only when needed (#863)" (#865)

Browse files

This reverts commit c8e89ef85fffa7ec6252e83ba8423c33a1225aca.

Files changed (1) hide show
  1. src/routes/+layout.server.ts +20 -23
src/routes/+layout.server.ts CHANGED
@@ -46,6 +46,26 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
46
  });
47
  }
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  const enableAssistants = ENABLE_ASSISTANTS === "true";
50
 
51
  const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
@@ -83,29 +103,6 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
83
 
84
  const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
85
 
86
- const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt(MESSAGES_BEFORE_LOGIN) : 0;
87
-
88
- let loginRequired = conversations.length > messagesBeforeLogin;
89
-
90
- if (!loginRequired && messagesBeforeLogin && requiresUser && !locals.user) {
91
- // get the number of messages where `from === "assistant"` across all conversations.
92
- const totalMessages =
93
- (
94
- await collections.conversations
95
- .aggregate([
96
- { $match: authCondition(locals), "messages.from": "assistant" },
97
- { $project: { messages: 1 } },
98
- { $limit: messagesBeforeLogin + 1 },
99
- { $unwind: "$messages" },
100
- { $match: { "messages.from": "assistant" } },
101
- { $count: "messages" },
102
- ])
103
- .toArray()
104
- )[0]?.messages ?? 0;
105
-
106
- loginRequired = totalMessages > messagesBeforeLogin;
107
- }
108
-
109
  return {
110
  conversations: conversations.map((conv) => {
111
  if (settings?.hideEmojiOnSidebar) {
 
46
  });
47
  }
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
  const enableAssistants = ENABLE_ASSISTANTS === "true";
70
 
71
  const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
 
103
 
104
  const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  return {
107
  conversations: conversations.map((conv) => {
108
  if (settings?.hideEmojiOnSidebar) {