Skip to content

Commit 77157ae

Browse files
author
Mishig
authored
Revert "[Mongo] count messages (aggregate) only when needed (#863)" (#865)
This reverts commit c8e89ef.
1 parent cd9f246 commit 77157ae

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/routes/+layout.server.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
4646
});
4747
}
4848

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+
4969
const enableAssistants = ENABLE_ASSISTANTS === "true";
5070

5171
const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
@@ -83,29 +103,6 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
83103

84104
const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
85105

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-
109106
return {
110107
conversations: conversations.map((conv) => {
111108
if (settings?.hideEmojiOnSidebar) {

0 commit comments

Comments
 (0)