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.
161 lines
4.6 KiB
Go
161 lines
4.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
// Public indexes use live publication grants. Registered titles inherit their
|
|
// Page's visibility; other registration metadata remains private.
|
|
func (s *Server) servePublicPeriodical(w http.ResponseWriter, r *http.Request) bool {
|
|
if r.Method != "GET" && r.Method != "HEAD" {
|
|
return false
|
|
}
|
|
hasCredential := len(r.Header.Values("Authorization")) != 0
|
|
for _, cookie := range r.Cookies() {
|
|
if cookie.Name == authCookieName {
|
|
hasCredential = true
|
|
}
|
|
}
|
|
domain := s.domainUUIDForRequest(r)
|
|
if hasCredential {
|
|
if _, err := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r)); err != nil {
|
|
return false
|
|
}
|
|
}
|
|
kind, op := periodicalRoute(r.URL.Path)
|
|
article := publicPeriodicalArticlePath(r.URL.Path)
|
|
feed := r.URL.Path == "/rss.xml"
|
|
if r.URL.Path == "/" || article || feed {
|
|
kind, op = s.homeLayout(domain), "view"
|
|
}
|
|
if op != "view" || (kind != "blog" && kind != "diary") {
|
|
return false
|
|
}
|
|
projection, err := s.store.PeriodicalProjection(r.Context(), domain, kind)
|
|
if err != nil {
|
|
http.Error(w, "projection unavailable", 503)
|
|
return true
|
|
}
|
|
if projection == nil {
|
|
return false
|
|
}
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
w.Header().Set("Referrer-Policy", "no-referrer")
|
|
for _, id := range []string{domain, projection.Source} {
|
|
on, err := s.store.PeriodicalEnabled(r.Context(), id, kind)
|
|
if err != nil {
|
|
http.Error(w, "projection unavailable", 503)
|
|
return true
|
|
}
|
|
if !on {
|
|
http.NotFound(w, r)
|
|
return true
|
|
}
|
|
}
|
|
selectionRequest := r.Clone(r.Context())
|
|
if article || feed {
|
|
selectionRequest.URL.Path = "/"
|
|
}
|
|
month, selected, err := periodicalSelection(selectionRequest, kind)
|
|
if err != nil {
|
|
http.Error(w, "invalid diary/blog date", 400)
|
|
return true
|
|
}
|
|
q := r.URL.Query()
|
|
if q.Has("own") || q.Has("mine") || q.Get("category") != "" {
|
|
s.authRequired(w, r)
|
|
return true
|
|
}
|
|
if q.Has("month") || q.Has("date") {
|
|
http.Redirect(w, r, periodicalURL(periodicalDatePath(kind, month, selected), q), http.StatusFound)
|
|
return true
|
|
}
|
|
host, err := s.store.GetDomainEffectiveHost(projection.Source)
|
|
if err != nil || host == "" {
|
|
http.Error(w, "projection unavailable", 503)
|
|
return true
|
|
}
|
|
pages, err := s.store.PublicNavigation(r.Context(), projection.Source)
|
|
if err != nil {
|
|
http.Error(w, "publication unavailable", 503)
|
|
return true
|
|
}
|
|
entries := []store.PeriodicalEntry{}
|
|
titles, err := s.store.PublicPeriodicalTitles(r.Context(), projection.Source, kind, pages)
|
|
if err != nil {
|
|
http.Error(w, "publication unavailable", 503)
|
|
return true
|
|
}
|
|
revisions := map[string]store.Revision{}
|
|
prefix := strings.TrimSuffix(projection.Prefix, "/") + "/"
|
|
for _, page := range pages {
|
|
if !strings.HasPrefix(page.Path, prefix) {
|
|
continue
|
|
}
|
|
parts := strings.Split(strings.TrimPrefix(page.Path, prefix), "/")
|
|
if len(parts) < 3 || len(parts) > 4 {
|
|
continue
|
|
}
|
|
date := strings.Join(parts[:3], "-")
|
|
parsed, err := time.Parse("2006-01-02", date)
|
|
if err != nil || parsed.Year() < 1 {
|
|
continue
|
|
}
|
|
if _, exists := revisions[page.Path]; exists {
|
|
continue
|
|
}
|
|
revisions[page.Path] = page.Revision
|
|
title := date
|
|
if len(parts) > 3 {
|
|
title = path.Base(page.Path)
|
|
}
|
|
if registered := strings.TrimSpace(titles[page.Path]); registered != "" {
|
|
title = registered
|
|
}
|
|
entries = append(entries, store.PeriodicalEntry{Path: page.Path, Date: date, Title: title})
|
|
}
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
if entries[i].Date == entries[j].Date {
|
|
return entries[i].Path < entries[j].Path
|
|
}
|
|
return entries[i].Date > entries[j].Date
|
|
})
|
|
// A small, bounded fallback for articles without registered titles.
|
|
for i := range entries {
|
|
e := &entries[i]
|
|
v := revisions[e.Path]
|
|
if i >= 20 || titles[e.Path] != "" || !strings.HasPrefix(v.ContentType, "text/html") {
|
|
continue
|
|
}
|
|
reader, err := s.store.OpenDocumentContext(r.Context(), v.DocumentHash)
|
|
if err != nil {
|
|
http.Error(w, "published document unavailable", 503)
|
|
return true
|
|
}
|
|
body, err := io.ReadAll(io.LimitReader(reader, 64<<10))
|
|
reader.Close()
|
|
if err != nil {
|
|
http.Error(w, "published document unavailable", 503)
|
|
return true
|
|
}
|
|
if title, _ := socialHTML(string(body)); title != "" {
|
|
e.Title = title
|
|
}
|
|
}
|
|
r = r.WithContext(context.WithValue(r.Context(), anonymousProjectionKey{}, true))
|
|
if s.homeLayout(domain) != kind {
|
|
s.renderPeriodical(w, r, domain, projection.Source, "//"+host, kind, month, selected, true, projection, &store.Principal{}, entries)
|
|
return true
|
|
}
|
|
s.renderPublicPeriodical(w, r, kind, month, selected, host, projection, entries, revisions, article, feed)
|
|
return true
|
|
}
|