package server import ( "encoding/json" "net/http" "strconv" "time" "github.com/google/uuid" "pageswiki/internal/store" ) func writeFeedHistoryJSON(w http.ResponseWriter, value any) { w.Header().Set("Cache-Control", "private, no-store") w.Header().Set("Content-Type", "application/json; charset=utf-8") _ = json.NewEncoder(w).Encode(value) } func (s *Server) handleFeedHistory(w http.ResponseWriter, r *http.Request, domain, actor, key string) bool { if r.Method != "GET" || (key != "actions" && key != "action/export") { return false } if key == "action/export" { a, e := s.store.FeedActionRecord(r.Context(), domain, actor, r.URL.Query().Get("request_id")) if e != nil { taskError(w, e) return true } id, _ := uuid.Parse(a.RequestID) w.Header().Set("Content-Disposition", `attachment; filename="pageswiki-feed-action-`+id.String()+`.json"`) w.Header().Set("X-Content-Type-Options", "nosniff") writeFeedHistoryJSON(w, map[string]any{"format": "pageswiki.feed-action", "version": 1, "exported_ms": time.Now().UnixMilli(), "domain_uuid": domain, "owner_uuid": actor, "action": a}) return true } q := r.URL.Query() query := store.FeedActionQuery{Source: q.Get("source"), Status: q.Get("status"), Before: -1, Key: q.Get("key")} if q.Has("before") { var e error query.Before, e = strconv.ParseInt(q.Get("before"), 10, 64) if e != nil || query.Before < 0 { taskError(w, store.ErrTaskInvalid) return true } } items, e := s.store.FeedActionHistory(r.Context(), domain, actor, query) if e != nil { taskError(w, e) return true } more := len(items) > 50 if more { items = items[:50] } writeFeedHistoryJSON(w, map[string]any{"actions": items, "more": more}) return true }