mirror of
https://github.com/sune-org/chatroom.git
synced 2026-01-13 16:17:59 +00:00
strict
This commit is contained in:
10
index.js
10
index.js
@@ -55,13 +55,13 @@ export class ChatsuneDurableObject {
|
||||
}
|
||||
case 'NEW_MESSAGE': {
|
||||
if (!server.username) return;
|
||||
const now = Date.now(), timestamps = (this.rateLimiter.get(server.ip) || []).filter(t => now - t < 10000);
|
||||
if (timestamps.length >= 5) return server.send(JSON.stringify({ type: 'ERROR', payload: 'Rate limit exceeded.' }));
|
||||
const now = Date.now(), timestamps = (this.rateLimiter.get(server.ip) || []).filter(t => now - t < 20000);
|
||||
if (timestamps.length >= 3) return server.send(JSON.stringify({ type: 'ERROR', payload: 'Rate limit exceeded.' }));
|
||||
this.rateLimiter.set(server.ip, [...timestamps, now]);
|
||||
|
||||
const modRes = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key=${this.env.GOOGLE_KEY}`, { method: 'POST', body: JSON.stringify({ contents: [{ parts: [{ text: `You're a lenient chat moderator for a good UX. Reply 'yes' if a message is appropriate. Message: ${data.payload.text}` }] }] }) });
|
||||
const modBody = await modRes.text();
|
||||
if (modRes.status !== 429 && !modBody.includes('429') && !modBody.toLowerCase().includes('yes')) return server.send(JSON.stringify({ type: 'ERROR', payload: 'Message rejected by moderator.' }));
|
||||
const modRes = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key=${this.env.GOOGLE_KEY}`, { method: 'POST', body: JSON.stringify({ contents: [{ parts: [{ text: `if this message is appropiate respond only with yes, if its not respond with anything but yes. message: ${data.payload.text}` }] }] }) });
|
||||
const modData = await modRes.json();
|
||||
if (modData.candidates?.[0]?.content?.parts?.[0]?.text.trim().toLowerCase() !== 'yes') return server.send(JSON.stringify({ type: 'ERROR', payload: 'Message rejected by moderator.' }));
|
||||
|
||||
const messagePayload = { author: { name: server.username }, text: data.payload.text, timestamp: new Date().toISOString() };
|
||||
this.messages.push(messagePayload);
|
||||
|
||||
Reference in New Issue
Block a user