nsarrazin HF staff commited on
Commit
87c6937
1 Parent(s): b8c0a1d

IP based rate limit

Browse files
src/routes/conversation/[id]/+server.ts CHANGED
@@ -46,10 +46,21 @@ export async function POST({ request, fetch, locals, params, getClientAddress })
46
  throw error(429, "Exceeded number of messages before login");
47
  }
48
 
49
- const nEvents = await collections.messageEvents.countDocuments({ userId });
 
 
 
 
 
 
 
 
 
 
50
 
51
- if (RATE_LIMIT != "" && nEvents > parseInt(RATE_LIMIT)) {
52
- throw error(429, ERROR_MESSAGES.rateLimited);
 
53
  }
54
 
55
  const model = models.find((m) => m.id === conv.model);
 
46
  throw error(429, "Exceeded number of messages before login");
47
  }
48
 
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
+ }
60
 
61
+ if (nEvents > parseInt(RATE_LIMIT)) {
62
+ throw error(429, ERROR_MESSAGES.rateLimited);
63
+ }
64
  }
65
 
66
  const model = models.find((m) => m.id === conv.model);