Spaces:
Build error
Build error
Revert "support rate limiting based on user IP (#342)"
Browse filesThis reverts commit 77677573c5f5a05c1c45d481089fd49e14709b10.
src/lib/types/MessageEvent.ts
CHANGED
@@ -3,5 +3,4 @@ import type { User } from "./User";
|
|
3 |
|
4 |
export interface MessageEvent extends Pick<Timestamps, "createdAt"> {
|
5 |
userId: User["_id"] | User["sessionId"];
|
6 |
-
ip?: string;
|
7 |
}
|
|
|
3 |
|
4 |
export interface MessageEvent extends Pick<Timestamps, "createdAt"> {
|
5 |
userId: User["_id"] | User["sessionId"];
|
|
|
6 |
}
|
src/routes/conversation/[id]/+server.ts
CHANGED
@@ -17,7 +17,7 @@ import { error } from "@sveltejs/kit";
|
|
17 |
import { ObjectId } from "mongodb";
|
18 |
import { z } from "zod";
|
19 |
|
20 |
-
export async function POST({ request, fetch, locals, params
|
21 |
const id = z.string().parse(params.id);
|
22 |
const convId = new ObjectId(id);
|
23 |
const date = new Date();
|
@@ -45,21 +45,10 @@ export async function POST({ request, fetch, locals, params, getClientAddress })
|
|
45 |
throw error(429, "Exceeded number of messages before login");
|
46 |
}
|
47 |
|
48 |
-
|
49 |
-
let nEvents = 0;
|
50 |
-
if (locals.user?._id) {
|
51 |
-
// if logged in do rate limiting based on user id
|
52 |
-
nEvents = await collections.messageEvents.countDocuments({ userId });
|
53 |
-
} else {
|
54 |
-
// do rate limiting based on session id but also ip address
|
55 |
-
const nEventsIp = await collections.messageEvents.countDocuments({ ip: getClientAddress() });
|
56 |
-
const nEventsSession = await collections.messageEvents.countDocuments({ userId });
|
57 |
-
nEvents = Math.max(nEventsIp, nEventsSession);
|
58 |
-
}
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
}
|
63 |
}
|
64 |
|
65 |
const model = models.find((m) => m.id === conv.model);
|
@@ -162,7 +151,6 @@ export async function POST({ request, fetch, locals, params, getClientAddress })
|
|
162 |
await collections.messageEvents.insertOne({
|
163 |
userId: userId,
|
164 |
createdAt: new Date(),
|
165 |
-
ip: getClientAddress(),
|
166 |
});
|
167 |
|
168 |
await collections.conversations.updateOne(
|
|
|
17 |
import { ObjectId } from "mongodb";
|
18 |
import { z } from "zod";
|
19 |
|
20 |
+
export async function POST({ request, fetch, locals, params }) {
|
21 |
const id = z.string().parse(params.id);
|
22 |
const convId = new ObjectId(id);
|
23 |
const date = new Date();
|
|
|
45 |
throw error(429, "Exceeded number of messages before login");
|
46 |
}
|
47 |
|
48 |
+
const nEvents = await collections.messageEvents.countDocuments({ userId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
if (RATE_LIMIT != "" && nEvents > parseInt(RATE_LIMIT)) {
|
51 |
+
throw error(429, ERROR_MESSAGES.rateLimited);
|
|
|
52 |
}
|
53 |
|
54 |
const model = models.find((m) => m.id === conv.model);
|
|
|
151 |
await collections.messageEvents.insertOne({
|
152 |
userId: userId,
|
153 |
createdAt: new Date(),
|
|
|
154 |
});
|
155 |
|
156 |
await collections.conversations.updateOne(
|