64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/maspend/hostora/internal/store"
|
|
)
|
|
|
|
// ---------- Log aktivitas ----------
|
|
//
|
|
// GET /api/activity?actor=&action=&limit=50&offset=0 — admin saja.
|
|
// Mengembalikan entri terbaru dulu, dengan filter aktor/aksi dan paginasi.
|
|
|
|
func (s *Server) handleActivity(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
writeErr(w, http.StatusMethodNotAllowed, "metode tidak diizinkan")
|
|
return
|
|
}
|
|
if !s.isAdmin(r) {
|
|
writeErr(w, http.StatusForbidden, "hanya admin")
|
|
return
|
|
}
|
|
actor := strings.TrimSpace(r.URL.Query().Get("actor"))
|
|
action := strings.TrimSpace(r.URL.Query().Get("action"))
|
|
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
if limit <= 0 || limit > 200 {
|
|
limit = 50
|
|
}
|
|
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
var all []store.ActivityEntry
|
|
s.store.View(func(d *store.Data) { all = d.Activity })
|
|
// Filter, terbaru dulu.
|
|
filtered := make([]store.ActivityEntry, 0, len(all))
|
|
for i := len(all) - 1; i >= 0; i-- {
|
|
e := all[i]
|
|
if actor != "" && e.Actor != actor {
|
|
continue
|
|
}
|
|
if action != "" && e.Action != action {
|
|
continue
|
|
}
|
|
filtered = append(filtered, e)
|
|
}
|
|
total := len(filtered)
|
|
if offset > total {
|
|
offset = total
|
|
}
|
|
end := offset + limit
|
|
if end > total {
|
|
end = total
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"total": total,
|
|
"limit": limit,
|
|
"offset": offset,
|
|
"entries": filtered[offset:end],
|
|
})
|
|
}
|