(userId: string)
| 51 | const proposeHistory = new Map<string, number[]>(); |
| 52 | |
| 53 | function checkProposeRateLimit(userId: string): { ok: true } | { ok: false; retryAfterMs: number } { |
| 54 | if (userId.startsWith('system:')) return { ok: true }; |
| 55 | const now = Date.now(); |
| 56 | const cutoff = now - PROPOSE_WINDOW_MS; |
| 57 | const history = (proposeHistory.get(userId) ?? []).filter(t => t > cutoff); |
| 58 | if (history.length >= PROPOSE_MAX_PER_WINDOW) { |
| 59 | return { ok: false, retryAfterMs: history[0] + PROPOSE_WINDOW_MS - now }; |
| 60 | } |
| 61 | history.push(now); |
| 62 | proposeHistory.set(userId, history); |
| 63 | // Opportunistic GC — if we've accumulated entries for many users, |
| 64 | // prune ones with no recent activity. Cheap: runs once per call when |
| 65 | // the map is large. |
| 66 | if (proposeHistory.size > 1000) { |
| 67 | for (const [key, entries] of proposeHistory) { |
| 68 | const recent = entries.filter(t => t > cutoff); |
| 69 | if (recent.length === 0) proposeHistory.delete(key); |
| 70 | else proposeHistory.set(key, recent); |
| 71 | } |
| 72 | } |
| 73 | return { ok: true }; |
| 74 | } |
| 75 | |
| 76 | interface ContentAuthor { |
| 77 | user_id: string; |
no test coverage detected