ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m28s
ci / test (pull_request) Successful in 12m39s
ci / build (pull_request) Successful in 16m10s
ci / image (pull_request) Successful in 5m20s
Deleting an organization removed its check results and aggregates in the same transaction as the request. With gigabytes of results that took longer than the request may last, it was cut off and rolled back, so a large organization could not be deleted at all. The organization and everything with a foreign key on it now go in the request; its check data is queued in org_purges. Every API instance purges the queue a chunk at a time, each chunk in a transaction of its own, right after a deletion and every minute. Raw results go first, so the aggregates' refresh does not rebuild what was purged. A lease lets another instance take over a purge whose instance stopped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
306 lines
8.7 KiB
Go
306 lines
8.7 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/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) {
|
|
s.wakeOrgPurges()
|
|
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()
|
|
orgsDeleted, err := s.eraseAccount(ctx, u)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
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
|
|
}
|