|
| 1 | +import { v } from "convex/values"; |
| 2 | +import { query, mutation } from "./_generated/server"; |
| 3 | + |
| 4 | +const MESSAGE_LIMIT = 3; |
| 5 | +const RESET_PERIOD_MS = 24 * 60 * 60 * 1000; // 24 hours |
| 6 | + |
| 7 | +export const checkRateLimit = query({ |
| 8 | + args: { fingerprint: v.string() }, |
| 9 | + handler: async (ctx, args) => { |
| 10 | + const record = await ctx.db |
| 11 | + .query("rateLimits") |
| 12 | + .withIndex("by_fingerprint", (q) => q.eq("fingerprint", args.fingerprint)) |
| 13 | + .first(); |
| 14 | + |
| 15 | + const now = Date.now(); |
| 16 | + |
| 17 | + if (!record) { |
| 18 | + return { |
| 19 | + allowed: true, |
| 20 | + remaining: MESSAGE_LIMIT, |
| 21 | + resetTime: now + RESET_PERIOD_MS, |
| 22 | + }; |
| 23 | + } |
| 24 | + |
| 25 | + // Check if reset period has passed |
| 26 | + if (now - record.lastResetTime >= RESET_PERIOD_MS) { |
| 27 | + return { |
| 28 | + allowed: true, |
| 29 | + remaining: MESSAGE_LIMIT, |
| 30 | + resetTime: now + RESET_PERIOD_MS, |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + const remaining = Math.max(0, MESSAGE_LIMIT - record.messageCount); |
| 35 | + return { |
| 36 | + allowed: remaining > 0, |
| 37 | + remaining, |
| 38 | + resetTime: record.lastResetTime + RESET_PERIOD_MS, |
| 39 | + }; |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +export const incrementRateLimit = mutation({ |
| 44 | + args: { fingerprint: v.string() }, |
| 45 | + handler: async (ctx, args) => { |
| 46 | + const now = Date.now(); |
| 47 | + const record = await ctx.db |
| 48 | + .query("rateLimits") |
| 49 | + .withIndex("by_fingerprint", (q) => q.eq("fingerprint", args.fingerprint)) |
| 50 | + .first(); |
| 51 | + |
| 52 | + if (!record) { |
| 53 | + // Create new record |
| 54 | + await ctx.db.insert("rateLimits", { |
| 55 | + fingerprint: args.fingerprint, |
| 56 | + messageCount: 1, |
| 57 | + lastResetTime: now, |
| 58 | + }); |
| 59 | + return { |
| 60 | + allowed: true, |
| 61 | + remaining: MESSAGE_LIMIT - 1, |
| 62 | + resetTime: now + RESET_PERIOD_MS, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + // Check if reset period has passed |
| 67 | + if (now - record.lastResetTime >= RESET_PERIOD_MS) { |
| 68 | + // Reset the counter |
| 69 | + await ctx.db.patch(record._id, { |
| 70 | + messageCount: 1, |
| 71 | + lastResetTime: now, |
| 72 | + }); |
| 73 | + return { |
| 74 | + allowed: true, |
| 75 | + remaining: MESSAGE_LIMIT - 1, |
| 76 | + resetTime: now + RESET_PERIOD_MS, |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + // Check if limit exceeded |
| 81 | + if (record.messageCount >= MESSAGE_LIMIT) { |
| 82 | + return { |
| 83 | + allowed: false, |
| 84 | + remaining: 0, |
| 85 | + resetTime: record.lastResetTime + RESET_PERIOD_MS, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + // Increment counter |
| 90 | + const newCount = record.messageCount + 1; |
| 91 | + await ctx.db.patch(record._id, { |
| 92 | + messageCount: newCount, |
| 93 | + }); |
| 94 | + |
| 95 | + return { |
| 96 | + allowed: true, |
| 97 | + remaining: MESSAGE_LIMIT - newCount, |
| 98 | + resetTime: record.lastResetTime + RESET_PERIOD_MS, |
| 99 | + }; |
| 100 | + }, |
| 101 | +}); |
0 commit comments