Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
197 lines
6.9 KiB
Go
197 lines
6.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"html/template"
|
|
"io"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func userSpaceTarget(raw string) (username, semantic string, isRaw bool) {
|
|
semantic = raw
|
|
if strings.HasPrefix(semantic, "/v/raw/path:") {
|
|
semantic = strings.TrimPrefix(semantic, "/v/raw/path:")
|
|
isRaw = true
|
|
}
|
|
if !store.IsUserSpacePath(semantic) {
|
|
return "", semantic, isRaw
|
|
}
|
|
return store.UserSpaceUsername(semantic), semantic, isRaw
|
|
}
|
|
|
|
func (s *Server) withUserPublicNavigation(r *http.Request, username string) (*http.Request, error) {
|
|
pages, err := s.store.PublicUserSpaceNavigation(r.Context(), s.domainUUIDForRequest(r), username)
|
|
if err != nil {
|
|
return r, err
|
|
}
|
|
return s.withPublishedNavigation(r, pages)
|
|
}
|
|
|
|
func publicUserNavigationUsername(r *http.Request) string {
|
|
var scope string
|
|
switch {
|
|
case r.URL.Path == "/v/children":
|
|
scope = r.URL.Query().Get("path")
|
|
case strings.HasPrefix(r.URL.Path, "/v/tree/"):
|
|
scope = "/" + strings.TrimPrefix(r.URL.Path, "/v/tree/")
|
|
case strings.HasPrefix(r.URL.Path, "/v/browser/"):
|
|
scope = "/" + strings.TrimPrefix(r.URL.Path, "/v/browser/")
|
|
default:
|
|
return ""
|
|
}
|
|
return store.UserSpaceUsername(path.Clean(scope))
|
|
}
|
|
|
|
func (s *Server) servePublicUserNavigation(w http.ResponseWriter, r *http.Request) bool {
|
|
username := publicUserNavigationUsername(r)
|
|
if username == "" {
|
|
return false
|
|
}
|
|
publicRequest, err := s.withUserPublicNavigation(r, username)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "user navigation unavailable", http.StatusServiceUnavailable)
|
|
return true
|
|
}
|
|
snapshot := publicNavigationFor(publicRequest)
|
|
root := "/u/" + username
|
|
if snapshot == nil || !snapshot.branches[root] {
|
|
return false
|
|
}
|
|
s.renderPublicNavigationRoute(w, publicRequest)
|
|
return true
|
|
}
|
|
|
|
func (s *Server) servePublicUserSpace(w http.ResponseWriter, r *http.Request) bool {
|
|
username, semantic, raw := userSpaceTarget(r.URL.Path)
|
|
if username == "" {
|
|
return false
|
|
}
|
|
root := "/u/" + username
|
|
if strings.HasPrefix(semantic, root+"/_/") {
|
|
if semantic == root+"/_/avatar" && !raw {
|
|
return s.servePublicUserAvatar(w, r, username)
|
|
}
|
|
return false
|
|
}
|
|
owner, err := s.store.PrincipalByUsername(s.domainUUIDForRequest(r), username)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "user space unavailable", http.StatusServiceUnavailable)
|
|
return true
|
|
}
|
|
policy, err := s.store.UserSpacePolicy(r.Context(), s.domainUUIDForRequest(r), owner.UUID)
|
|
if err != nil || !policy.DomainAllowsPublic {
|
|
return false
|
|
}
|
|
if semantic == root && !raw {
|
|
if policy.ProfileVisibility != "public" && policy.RootVisibility != "public" {
|
|
return false
|
|
}
|
|
return s.servePublicUserProfile(w, r, owner, policy)
|
|
}
|
|
v, err := s.store.PublicUserSpaceRevision(r.Context(), s.domainUUIDForRequest(r), username, semantic, s.revisionLanguageCandidates(r))
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "user publication unavailable", http.StatusServiceUnavailable)
|
|
return true
|
|
}
|
|
publicRequest, err := s.withUserPublicNavigation(r, username)
|
|
if err != nil {
|
|
http.Error(w, "user navigation unavailable", http.StatusServiceUnavailable)
|
|
return true
|
|
}
|
|
if current, ok := publicNavigationFor(publicRequest).pages[semantic]; !ok || current.UUID != v.UUID {
|
|
http.Error(w, "publication changed; retry", http.StatusServiceUnavailable)
|
|
return true
|
|
}
|
|
return s.serveAnonymousRevision(w, publicRequest, semantic, raw, v)
|
|
}
|
|
|
|
func (s *Server) servePublicUserAvatar(w http.ResponseWriter, r *http.Request, username string) bool {
|
|
owner, err := s.store.PrincipalByUsername(s.domainUUIDForRequest(r), username)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
policy, err := s.store.UserSpacePolicy(r.Context(), s.domainUUIDForRequest(r), owner.UUID)
|
|
if err != nil || !policy.DomainAllowsPublic || policy.AvatarVisibility != "public" {
|
|
return false
|
|
}
|
|
contentType, data, err := s.store.PrincipalAvatar(owner.UUID)
|
|
if err != nil || len(data) == 0 {
|
|
http.NotFound(w, r)
|
|
return true
|
|
}
|
|
w.Header().Set("Content-Type", contentType)
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.Header().Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'self'")
|
|
w.Header().Add("Vary", "Cookie, Authorization")
|
|
if r.Method != http.MethodHead {
|
|
_, _ = w.Write(data)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Server) servePublicUserProfile(w http.ResponseWriter, r *http.Request, owner *store.Principal, policy store.UserSpacePolicy) bool {
|
|
publicRequest, err := s.withUserPublicNavigation(r, owner.Username)
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
http.Error(w, "user navigation unavailable", http.StatusServiceUnavailable)
|
|
return true
|
|
}
|
|
if publicNavigationFor(publicRequest) == nil {
|
|
publicRequest = publicRequest.WithContext(context.WithValue(publicRequest.Context(), anonymousProjectionKey{}, true))
|
|
publicRequest = publicRequest.WithContext(context.WithValue(publicRequest.Context(), publicNavigationKey{}, &publicNavigationSnapshot{pages: map[string]store.Revision{}, branches: map[string]bool{}}))
|
|
}
|
|
display := *owner
|
|
if policy.ProfileVisibility != "public" {
|
|
display.DisplayName = owner.Username
|
|
display.Email = ""
|
|
}
|
|
display.HasAvatar = owner.HasAvatar && policy.AvatarVisibility == "public"
|
|
rendered := ""
|
|
root := "/u/" + owner.Username
|
|
if policy.RootVisibility == "public" {
|
|
if revision, revisionErr := s.store.PublicUserSpaceRevision(publicRequest.Context(), s.domainUUIDForRequest(publicRequest), owner.Username, root, s.revisionLanguageCandidates(publicRequest)); revisionErr == nil {
|
|
if view := revisionContentView(revision, root); view.Kind == "html" {
|
|
reader, openErr := s.store.OpenDocumentContext(publicRequest.Context(), revision.DocumentHash)
|
|
if openErr == nil {
|
|
body, readErr := io.ReadAll(io.LimitReader(reader, maxProjectionInput+1))
|
|
reader.Close()
|
|
if readErr == nil {
|
|
rendered = s.safeHTMLProjection(publicRequest, string(body), root, true)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
snapshot := publicNavigationFor(publicRequest)
|
|
contact, contactErr := s.store.ProfileContactForViewer(publicRequest.Context(), s.domainUUIDForRequest(publicRequest), owner.UUID, "")
|
|
if contactErr != nil {
|
|
http.Error(w, "contact unavailable", http.StatusServiceUnavailable)
|
|
return true
|
|
}
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
s.renderWithContext(w, publicRequest, "profile.gohtml", map[string]any{
|
|
"Principal": display, "Editing": false, "PublicProfile": true,
|
|
"Contact": contact, "ContactHref": profileContactHref(contact),
|
|
"Handle": owner.Username + "@" + profileHost(publicRequest), "RenderedHTML": template.HTML(rendered),
|
|
"PublicHasChildren": snapshot != nil && snapshot.hasChildren(root),
|
|
"PublicChildrenHref": cityPageHref("/v/browser" + root), "Locale": s.getLocale(s.domainUUIDForRequest(publicRequest)),
|
|
})
|
|
return true
|
|
}
|