Files
uptimesebi/internal/web/legal.go
T
Sebastian UnterschützandClaude Opus 5.5 2d4bd10c9b
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m8s
ci / test (pull_request) Successful in 13m45s
ci / build (pull_request) Successful in 19m0s
ci / image (pull_request) Successful in 6m5s
feat(notify): name regions and link the console in alerts; consent to subscribe
Alert mails and chat messages named regions by id. They now show the
region's name and mails link the monitor in the console.

Visitors of a status page accept the platform's terms and the page's
privacy policy, where set, before they can subscribe to it; the status
page server and the API both check it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-26 20:16:28 +01:00

111 lines
3.6 KiB
Go

package web
import (
"bytes"
"slices"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
)
// LegalKinds are the platform's legal documents, in footer order.
var LegalKinds = []string{"imprint", "privacy", "terms"}
// OrgLegalKinds are the documents an organization sets for its status
// pages; their visitors accept no terms.
var OrgLegalKinds = []string{"imprint", "privacy"}
// ConsentKinds are the platform documents every account accepts.
var ConsentKinds = []string{"privacy", "terms"}
// LegalTitleID names the catalog message of a document kind.
func LegalTitleID(kind string) string { return "legal." + kind }
// LegalLink points to a legal document, ours or elsewhere.
type LegalLink struct {
Kind string
Href string
External bool
}
// markdown renders untrusted input: goldmark drops raw HTML and links with
// dangerous schemes such as javascript: unless told otherwise. Images are
// shown as their alt text: on a status page an external image would hand
// every visitor's address to its host.
var markdown = goldmark.New(
goldmark.WithExtensions(extension.Table, extension.Linkify, extension.Strikethrough),
goldmark.WithRendererOptions(renderer.WithNodeRenderers(util.Prioritized(altTextImages{}, 100))),
)
type altTextImages struct{}
// RegisterFuncs implements renderer.NodeRenderer.
func (altTextImages) RegisterFuncs(r renderer.NodeRendererFuncRegisterer) {
r.Register(ast.KindImage, func(w util.BufWriter, src []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
// The children are the alt text and render as usual.
return ast.WalkContinue, nil
})
}
// RenderMarkdown converts src to HTML that is safe to embed.
func RenderMarkdown(src string) (string, error) {
var buf bytes.Buffer
if err := markdown.Convert([]byte(src), &buf); err != nil {
return "", err
}
return buf.String(), nil
}
// LegalLinks turns documents into links: external URLs as they are,
// Markdown documents to base+kind.
func LegalLinks(docs []agentapi.LegalDoc, base string) []LegalLink {
var out []LegalLink
for _, kind := range LegalKinds {
for _, d := range docs {
if d.Kind != kind {
continue
}
switch {
case d.URL != "":
out = append(out, LegalLink{Kind: kind, Href: d.URL, External: true})
case d.Markdown != "" && base != "":
out = append(out, LegalLink{Kind: kind, Href: base + kind})
}
}
}
return out
}
// PageLegalLinks returns the legal links of a status page's footer. The
// terms are not among them: visitors accept them only when subscribing.
func PageLegalLinks(s agentapi.PageSnapshot, base string) []LegalLink {
return slices.DeleteFunc(LegalLinks(s.Legal, base), func(l LegalLink) bool { return l.Kind == "terms" })
}
// SubscribeConsent returns the documents a visitor of a status page
// accepts by subscribing: the terms and the privacy policy, where set.
func SubscribeConsent(s agentapi.PageSnapshot, base string) []LegalLink {
return slices.DeleteFunc(LegalLinks(s.Legal, base), func(l LegalLink) bool { return l.Kind == "imprint" })
}
// ConsentLabelID names the message of the subscription checkbox for links.
func ConsentLabelID(links []LegalLink) string {
var terms, privacy bool
for _, l := range links {
terms = terms || l.Kind == "terms"
privacy = privacy || l.Kind == "privacy"
}
switch {
case terms && privacy:
return "page.subscribe.accept_both"
case terms:
return "page.subscribe.accept_terms"
}
return "page.subscribe.accept_privacy"
}