Mishig coyotte508 HF staff commited on
Commit
c8e89ef
1 Parent(s): 8ffc49e

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

Browse files

* [Mongo] count messages (aggregate) only when needed

* more restrictive

* even more stricter

* Update src/routes/+layout.server.ts

Co-authored-by: Eliott C. <coyotte508@gmail.com>

* format

---------

Co-authored-by: Eliott C. <coyotte508@gmail.com>

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