Skip to content

Commit 87c6937

Browse files
committed
IP based rate limit
1 parent b8c0a1d commit 87c6937

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,21 @@ export async function POST({ request, fetch, locals, params, getClientAddress })
4646
throw error(429, "Exceeded number of messages before login");
4747
}
4848

49-
const nEvents = await collections.messageEvents.countDocuments({ userId });
49+
if (RATE_LIMIT !== "") {
50+
let nEvents = 0;
51+
if (locals.user?._id) {
52+
// if logged in do rate limiting based on user id
53+
nEvents = await collections.messageEvents.countDocuments({ userId });
54+
} else {
55+
// do rate limiting based on session id but also ip address
56+
const nEventsIp = await collections.messageEvents.countDocuments({ ip: getClientAddress() });
57+
const nEventsSession = await collections.messageEvents.countDocuments({ userId });
58+
nEvents = Math.max(nEventsIp, nEventsSession);
59+
}
5060

51-
if (RATE_LIMIT != "" && nEvents > parseInt(RATE_LIMIT)) {
52-
throw error(429, ERROR_MESSAGES.rateLimited);
61+
if (nEvents > parseInt(RATE_LIMIT)) {
62+
throw error(429, ERROR_MESSAGES.rateLimited);
63+
}
5364
}
5465

5566
const model = models.find((m) => m.id === conv.model);

0 commit comments

Comments
 (0)