Lists that were cut off by a fixed LIMIT, or could grow without bound, are paged by keyset: the cursor holds the sort key and an ID, so pages neither skip nor repeat items as others are added. The audit logs had a timestamp-only cursor that could skip entries with equal times. The API returns at most 100 items per list (limit takes up to 500) with next_cursor and prev_cursor, passed back as after and before. The console shows Newer/Older (or Previous/Next) links and To the start, without JavaScript. Paged: incidents (optionally of one monitor), a monitor's results and evidence, a checker's results, the audit logs, members, maintenance windows, firing Alertmanager alerts, the monitors API and the admin lists of status pages and organizations, which move to their own admin pages. A monitor's page shows its latest incidents and results and links to all of them. The dashboard caps every group at 50 of its own monitors with a link to a paged view of the group, and gains a search by name or target. Lists filtered by permission after reading may show fewer items on a page. Closes #116 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
312 lines
9.0 KiB
Go
312 lines
9.0 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
|
)
|
|
|
|
// Platform admins moderate public status pages and the organizations and
|
|
// accounts behind them: suspending hides or stops them and can be undone,
|
|
// deleting cannot. Suspended pages leave status page servers when these
|
|
// next ask for them, within half a minute.
|
|
|
|
type moderatedPage struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Published bool `json:"published"`
|
|
Domain string `json:"custom_domain,omitempty"`
|
|
SuspendedAt *time.Time `json:"suspended_at"`
|
|
Reason string `json:"suspended_reason,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
Org moderatedOrg `json:"org"`
|
|
Owner *moderatedUser `json:"owner"`
|
|
}
|
|
|
|
type moderatedOrg struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Slug string `json:"slug"`
|
|
Name string `json:"name"`
|
|
SuspendedAt *time.Time `json:"suspended_at"`
|
|
Reason string `json:"suspended_reason,omitempty"`
|
|
}
|
|
|
|
type moderatedUser struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Email string `json:"email"`
|
|
SuspendedAt *time.Time `json:"suspended_at"`
|
|
Reason string `json:"suspended_reason,omitempty"`
|
|
// Admin marks platform admins, who cannot be suspended or deleted.
|
|
Admin bool `json:"admin,omitempty"`
|
|
}
|
|
|
|
func (s *Server) moderatedPages(ctx context.Context, search string, pg pageReq) ([]moderatedPage, pageInfo, error) {
|
|
var rows []store.AdminListStatusPagesRow
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
rows, err = q.AdminListStatusPages(ctx, store.AdminListStatusPagesParams{
|
|
Search: truncate(strings.TrimSpace(search), 200), MaxRows: pg.fetch(),
|
|
AfterT: pg.afterT(), AfterID: pg.afterID(), BeforeT: pg.beforeT(), BeforeID: pg.beforeID(),
|
|
})
|
|
return err
|
|
})
|
|
rows, info := paginate(pg, rows, func(r store.AdminListStatusPagesRow) cursor { return cursor{T: r.CreatedAt, ID: r.ID} })
|
|
out := make([]moderatedPage, len(rows))
|
|
for i, r := range rows {
|
|
p := &out[i]
|
|
p.ID, p.Slug, p.Title, p.Published, p.SuspendedAt, p.Reason, p.CreatedAt = r.ID, r.Slug, r.Title, r.Published, r.SuspendedAt, r.SuspendedReason, r.CreatedAt
|
|
if r.CustomDomain != nil && r.DomainVerifiedAt != nil {
|
|
p.Domain = *r.CustomDomain
|
|
}
|
|
p.Org = moderatedOrg{r.OrgID, r.OrgSlug, r.OrgName, r.OrgSuspendedAt, r.OrgSuspendedReason}
|
|
if r.OwnerID != uuid.Nil {
|
|
p.Owner = &moderatedUser{r.OwnerID, r.OwnerEmail, r.OwnerDisabledAt, r.OwnerDisabledReason, r.OwnerIsAdmin}
|
|
}
|
|
}
|
|
return out, info, err
|
|
}
|
|
|
|
func (s *Server) adminListStatusPages(c *gin.Context) {
|
|
pg, ok := parsePage(c, apiPageSize)
|
|
if !ok {
|
|
return
|
|
}
|
|
pages, info, err := s.moderatedPages(c.Request.Context(), c.Query("q"), pg)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status_pages": pages, "next_cursor": info.Next, "prev_cursor": info.Prev})
|
|
}
|
|
|
|
type suspensionRequest struct {
|
|
Suspended bool `json:"suspended"`
|
|
Reason string `json:"reason" binding:"max=500"`
|
|
}
|
|
|
|
// suspension binds a suspension request and the target ID.
|
|
func suspension(c *gin.Context) (uuid.UUID, suspensionRequest, bool) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return uuid.Nil, suspensionRequest{}, false
|
|
}
|
|
var req suspensionRequest
|
|
if !bind(c, &req) {
|
|
return uuid.Nil, req, false
|
|
}
|
|
req.Reason = strings.TrimSpace(req.Reason)
|
|
auditDetail(c, "suspended", req.Suspended)
|
|
if req.Reason != "" {
|
|
auditDetail(c, "reason", req.Reason)
|
|
}
|
|
return id, req, true
|
|
}
|
|
|
|
// moderated finishes a moderation request: 404 for unknown targets.
|
|
func moderated(c *gin.Context, err error) bool {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return false
|
|
}
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Server) adminSuspendStatusPage(c *gin.Context) {
|
|
id, req, ok := suspension(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
row, err := q.SuspendStatusPage(ctx, store.SuspendStatusPageParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
|
|
auditDetail(c, "slug", row.Slug)
|
|
return err
|
|
})
|
|
if moderated(c, err) {
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func (s *Server) adminDeleteStatusPage(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
page, err := q.GetStatusPage(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
auditDetail(c, "slug", page.Slug)
|
|
_, err = q.DeleteStatusPage(ctx, id)
|
|
return err
|
|
})
|
|
if moderated(c, err) {
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func (s *Server) adminSuspendOrg(c *gin.Context) {
|
|
id, req, ok := suspension(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
row, err := q.SuspendOrg(ctx, store.SuspendOrgParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
auditDetail(c, "slug", row.Slug)
|
|
if !req.Suspended {
|
|
return q.RestartHeartbeatWait(ctx, id)
|
|
}
|
|
return nil
|
|
})
|
|
if !moderated(c, err) {
|
|
return
|
|
}
|
|
// Monitors leave or rejoin the schedule with its next reconciliation.
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) adminDeleteOrg(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
org, err := q.GetOrg(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
auditDetail(c, "slug", org.Slug)
|
|
auditDetail(c, "name", org.Name)
|
|
return purgeOrg(ctx, q, id)
|
|
})
|
|
if moderated(c, err) {
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
// moderatedUser loads the target account, refusing platform admins: they
|
|
// would otherwise lock each other, or themselves, out.
|
|
func (s *Server) moderatedUser(c *gin.Context) (store.User, bool) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return store.User{}, false
|
|
}
|
|
u, err := s.loadUser(c.Request.Context(), id)
|
|
if !moderated(c, err) {
|
|
return u, false
|
|
}
|
|
auditDetail(c, "email", u.Email)
|
|
if u.IsAdmin {
|
|
fail(c, http.StatusConflict, "target_is_admin")
|
|
return u, false
|
|
}
|
|
return u, true
|
|
}
|
|
|
|
func (s *Server) adminSuspendUser(c *gin.Context) {
|
|
id, req, ok := suspension(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
u, ok := s.moderatedUser(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
_, err := q.SuspendUser(ctx, store.SuspendUserParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
|
|
return err
|
|
})
|
|
if !moderated(c, err) {
|
|
return
|
|
}
|
|
if req.Suspended {
|
|
if err := s.sessions.DeleteAllForUser(ctx, u.ID); err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) adminDeleteUser(c *gin.Context) {
|
|
u, ok := s.moderatedUser(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
shared, orgsDeleted, err := s.eraseAccount(ctx, u)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
if len(shared) > 0 {
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"error": "owns_shared_orgs", "message": i18n.T(ctx, "error.user_owns_shared_orgs"), "organizations": shared,
|
|
})
|
|
return
|
|
}
|
|
auditDetail(c, "organizations_deleted", orgsDeleted)
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) moderationView(c *gin.Context, pg pageReq) (web.ModerationView, error) {
|
|
pages, info, err := s.moderatedPages(c.Request.Context(), c.Query("q"), pg)
|
|
v := web.ModerationView{Search: strings.TrimSpace(c.Query("q")), Pager: pager(c, info), Next: c.Request.URL.RequestURI()}
|
|
state := func(at *time.Time, reason string) string {
|
|
switch {
|
|
case at == nil:
|
|
return ""
|
|
case reason == "":
|
|
return "-"
|
|
}
|
|
return reason
|
|
}
|
|
for _, p := range pages {
|
|
u := strings.TrimSuffix(s.cfg.StatusPageURL, "/") + "/" + p.Slug
|
|
if p.Domain != "" {
|
|
u = "https://" + p.Domain + "/"
|
|
}
|
|
mp := web.ModeratedPage{
|
|
ID: p.ID.String(), Title: p.Title, Slug: p.Slug, URL: u, Created: p.CreatedAt.Format(time.DateOnly),
|
|
Published: p.Published, Suspended: state(p.SuspendedAt, p.Reason),
|
|
Org: web.ModeratedTarget{ID: p.Org.ID.String(), Name: p.Org.Name, Detail: p.Org.Slug, Suspended: state(p.Org.SuspendedAt, p.Org.Reason)},
|
|
}
|
|
if p.Domain != "" {
|
|
mp.Slug = p.Domain
|
|
}
|
|
if p.Owner != nil {
|
|
mp.Owner = &web.ModeratedTarget{ID: p.Owner.ID.String(), Name: p.Owner.Email, Suspended: state(p.Owner.SuspendedAt, p.Owner.Reason), Admin: p.Owner.Admin}
|
|
}
|
|
v.Pages = append(v.Pages, mp)
|
|
}
|
|
return v, err
|
|
}
|