Add GET /api/user/gdpr-export handler (GetGDPRExportHandler) with async background generation — navigation away doesn't cancel. 12h in-memory cache with 5-minute cleanup ticker. Three cache states: MISS (triggers generation), HIT (returns cached data), GENERATING (concurrent request while building). Route registered without RequireVerified middleware so unverified users can export. 25 tests covering: auth rejection, cache miss/hit/generating/expired states, anonymize_user child table scrubbing (social logins, saved cards, verification codes, time blockers, edit notes, notification prefs), export_all_user_data 16-section export, AnonymizeStaleGuestAccounts field scrubbing. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
|
|
"crussell/db"
|
|
"crussell/mw"
|
|
)
|
|
|
|
var (
|
|
gdprExportCache = make(map[string]*gdprCacheEntry)
|
|
gdprExportCacheMu sync.RWMutex
|
|
)
|
|
|
|
type gdprCacheEntry struct {
|
|
data json.RawMessage
|
|
expiresAt time.Time
|
|
generating bool
|
|
}
|
|
|
|
func init() {
|
|
go func() {
|
|
ticker := time.NewTicker(5 * time.Minute)
|
|
defer ticker.Stop()
|
|
for range ticker.C {
|
|
gdprExportCacheMu.Lock()
|
|
now := time.Now()
|
|
for k, v := range gdprExportCache {
|
|
if now.After(v.expiresAt) {
|
|
delete(gdprExportCache, k)
|
|
}
|
|
}
|
|
gdprExportCacheMu.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
|
|
// GET /api/user/gdpr-export
|
|
func GetGDPRExportHandler(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := mw.GetUserID(r.Context())
|
|
if !ok {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
gdprExportCacheMu.Lock()
|
|
if entry, found := gdprExportCache[userID]; found && time.Now().Before(entry.expiresAt) {
|
|
if entry.generating {
|
|
gdprExportCacheMu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("X-Cache", "GENERATING")
|
|
w.Write([]byte(`{"status":"generating"}`))
|
|
return
|
|
}
|
|
gdprExportCacheMu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("X-Cache", "HIT")
|
|
w.Write(entry.data)
|
|
return
|
|
}
|
|
|
|
gdprExportCache[userID] = &gdprCacheEntry{generating: true}
|
|
gdprExportCacheMu.Unlock()
|
|
|
|
go func() {
|
|
var result json.RawMessage
|
|
err := db.DB.QueryRow(context.Background(), `SELECT export_all_user_data($1)`, userID).Scan(&result)
|
|
if err != nil {
|
|
log.Printf("GDPR export failed for user %s: %v", userID, err)
|
|
gdprExportCacheMu.Lock()
|
|
delete(gdprExportCache, userID)
|
|
gdprExportCacheMu.Unlock()
|
|
return
|
|
}
|
|
|
|
gdprExportCacheMu.Lock()
|
|
gdprExportCache[userID] = &gdprCacheEntry{
|
|
data: result,
|
|
expiresAt: time.Now().Add(12 * time.Hour),
|
|
}
|
|
gdprExportCacheMu.Unlock()
|
|
}()
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("X-Cache", "MISS")
|
|
w.Write([]byte(`{"status":"generating"}`))
|
|
}
|