Skip to content

Commit 105d8aa

Browse files
Mishigcoyotte508
andauthored
[Mongo] count messages (aggregate) only when needed V2 (#866)
fixes #863 needed 9e67e76 --------- Co-authored-by: Eliott C. <coyotte508@gmail.com>
1 parent 77157ae commit 105d8aa

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/routes/+layout.server.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,6 @@ 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-
6949
const enableAssistants = ENABLE_ASSISTANTS === "true";
7050

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

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

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+
106113
return {
107114
conversations: conversations.map((conv) => {
108115
if (settings?.hideEmojiOnSidebar) {

src/routes/conversation/[id]/+server.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,25 @@ export async function POST({ request, locals, params, getClientAddress }) {
7272
ip: getClientAddress(),
7373
});
7474

75+
const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt(MESSAGES_BEFORE_LOGIN) : 0;
76+
7577
// guest mode check
76-
if (
77-
!locals.user?._id &&
78-
requiresUser &&
79-
(MESSAGES_BEFORE_LOGIN ? parseInt(MESSAGES_BEFORE_LOGIN) : 0) > 0
80-
) {
78+
if (!locals.user?._id && requiresUser && messagesBeforeLogin) {
8179
const totalMessages =
8280
(
8381
await collections.conversations
8482
.aggregate([
85-
{ $match: authCondition(locals) },
83+
{ $match: { ...authCondition(locals), "messages.from": "assistant" } },
8684
{ $project: { messages: 1 } },
85+
{ $limit: messagesBeforeLogin + 1 },
8786
{ $unwind: "$messages" },
8887
{ $match: { "messages.from": "assistant" } },
8988
{ $count: "messages" },
9089
])
9190
.toArray()
9291
)[0]?.messages ?? 0;
9392

94-
if (totalMessages > parseInt(MESSAGES_BEFORE_LOGIN)) {
93+
if (totalMessages > messagesBeforeLogin) {
9594
throw error(429, "Exceeded number of messages before login");
9695
}
9796
}

0 commit comments

Comments
 (0)