2026-04-02 14:44:41 +02:00
|
|
|
package db
|
|
|
|
|
|
2026-07-19 15:01:54 +00:00
|
|
|
import (
|
2026-07-23 17:03:52 +02:00
|
|
|
"context"
|
2026-07-19 15:01:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
2026-08-28 06:54:32 +02:00
|
|
|
"github.com/J3vb/OwnCord/Server/db/dbgen"
|
2026-07-19 15:01:54 +00:00
|
|
|
)
|
2026-04-02 14:44:41 +02:00
|
|
|
|
|
|
|
|
// UpsertLockout inserts or replaces a rate-limit lockout entry.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) UpsertLockout(ctx context.Context, key string, expiresAt time.Time) error {
|
|
|
|
|
return d.q.UpsertLockout(ctx, dbgen.UpsertLockoutParams{
|
2026-07-19 15:01:54 +00:00
|
|
|
Key: key,
|
|
|
|
|
ExpiresAt: expiresAt.UTC().Format(time.RFC3339),
|
|
|
|
|
})
|
2026-04-02 14:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadActiveLockouts returns all lockouts that have not yet expired as
|
|
|
|
|
// parallel slices of keys and expiry times.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) LoadActiveLockouts(ctx context.Context) (keys []string, expiresAt []time.Time, err error) {
|
|
|
|
|
rows, err := d.q.LoadActiveLockouts(ctx, time.Now().UTC().Format(time.RFC3339))
|
2026-04-02 14:44:41 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
2026-07-19 15:01:54 +00:00
|
|
|
for _, r := range rows {
|
|
|
|
|
t, parseErr := time.Parse(time.RFC3339, r.ExpiresAt)
|
2026-04-02 14:44:41 +02:00
|
|
|
if parseErr != nil {
|
|
|
|
|
continue // skip unparseable rows
|
|
|
|
|
}
|
2026-07-19 15:01:54 +00:00
|
|
|
keys = append(keys, r.Key)
|
2026-04-02 14:44:41 +02:00
|
|
|
expiresAt = append(expiresAt, t)
|
|
|
|
|
}
|
2026-07-19 15:01:54 +00:00
|
|
|
return keys, expiresAt, nil
|
2026-04-02 14:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CleanupExpiredLockouts removes lockout rows whose expiry has passed.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) CleanupExpiredLockouts(ctx context.Context) error {
|
|
|
|
|
return d.q.CleanupExpiredLockouts(ctx, time.Now().UTC().Format(time.RFC3339))
|
2026-04-02 14:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteLockout removes a single lockout entry.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) DeleteLockout(ctx context.Context, key string) error {
|
|
|
|
|
return d.q.DeleteLockout(ctx, key)
|
2026-04-02 14:44:41 +02:00
|
|
|
}
|