Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
143 lines
5.9 KiB
Go
143 lines
5.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const MaxNavigationEntries = 20000
|
|
|
|
var ErrNavigationBudget = errors.New("navigation snapshot exceeds its bounded budget")
|
|
|
|
// A database-backed generation observes other processes and CLI writes too.
|
|
// It invalidates data caches, never caches a Principal's authorization result.
|
|
func (s *Store) ensureNavigationSchema() error {
|
|
if _, err := s.DB.Exec(`CREATE TABLE IF NOT EXISTS navigation_generation(id INTEGER PRIMARY KEY CHECK(id=1),version INTEGER NOT NULL); INSERT OR IGNORE INTO navigation_generation VALUES(1,1)`); err != nil {
|
|
return err
|
|
}
|
|
for _, table := range []string{"locality_auto_publication", "locality_publication_policies", "locality_access_policies", "locality_access_entries", "city_owner_bindings", "user_space_policies", "user_space_path_policies", "principals", "domain_memberships", "group_members", "groups", "group_permissions", "auth_roles", "auth_role_bindings", "auth_capability_grants", "campuses", "paths", "revisions", "domains", "config", "zones", "spots", "street_spots", "spot_pages", "spot_relations", "street_inbox_pages", "city_page_overrides", "domain_topology_versions", "city_governance_epochs", "locality_publication_entries", "locality_publication_manifests"} {
|
|
var exists bool
|
|
if err := s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name=?)`, table).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if !exists {
|
|
continue
|
|
}
|
|
for _, op := range []string{"INSERT", "UPDATE", "DELETE"} {
|
|
condition := ""
|
|
switch table {
|
|
case "principals", "domain_memberships", "group_members", "groups", "group_permissions", "auth_roles", "auth_role_bindings", "auth_capability_grants", "city_owner_bindings":
|
|
condition = " WHEN EXISTS(SELECT 1 FROM locality_auto_publication WHERE mode='automatic')"
|
|
}
|
|
q := fmt.Sprintf(`CREATE TRIGGER IF NOT EXISTS navigation_%s_%s AFTER %s ON %s%s BEGIN UPDATE navigation_generation SET version=version+1 WHERE id=1; END`, table, op, op, table, condition)
|
|
if _, err := s.DB.Exec(q); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) NavigationGeneration(ctx context.Context) (int64, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
var version int64
|
|
err := s.DB.QueryRowContext(ctx, `SELECT version FROM navigation_generation WHERE id=1`).Scan(&version)
|
|
return version, err
|
|
}
|
|
|
|
func (s *Store) NavigationPaths(ctx context.Context, domain string) ([]string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT path FROM paths WHERE domain_uuid=? ORDER BY path LIMIT ?`, domain, MaxNavigationEntries+1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []string{}
|
|
bytes := 0
|
|
for rows.Next() {
|
|
var p string
|
|
if err = rows.Scan(&p); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, p)
|
|
bytes += len(p)
|
|
if len(out) > MaxNavigationEntries || bytes > 4<<20 {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
type PublishedNavigationPage struct {
|
|
Path string
|
|
Revision Revision
|
|
}
|
|
|
|
func (s *Store) PublicNavigation(ctx context.Context, domain string) ([]PublishedNavigationPage, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
// Same live binding/head/epoch checks as AnonymousRevision. No canonical
|
|
// bodies, historical revisions, unapproved names or private child counts.
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT DISTINCT e.path,r.uuid,r.object_uuid,r.document_hash,COALESCE(NULLIF(r.lang,''),'und'),COALESCE(NULLIF(r.content_type,''),'text/html; charset=utf-8'),COALESCE(r.filename,''),r.created_at_ms `+anonymousRevisionFromSQL+`
|
|
ORDER BY e.path,r.created_at_ms DESC,r.uuid LIMIT ?`, domain, MaxNavigationEntries+1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []PublishedNavigationPage{}
|
|
bytes := 0
|
|
for rows.Next() {
|
|
var p PublishedNavigationPage
|
|
v := &p.Revision
|
|
if err = rows.Scan(&p.Path, &v.UUID, &v.ObjectUUID, &v.DocumentHash, &v.Language, &v.ContentType, &v.Filename, &v.CreatedAtMS); err != nil {
|
|
return nil, err
|
|
}
|
|
if !PublicSemanticPath(p.Path) {
|
|
continue
|
|
}
|
|
out = append(out, p)
|
|
bytes += len(p.Path) + len(v.Filename) + len(v.ContentType)
|
|
if len(out) > MaxNavigationEntries || bytes > 4<<20 {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
rows.Close()
|
|
automatic, err := s.automaticPublicPages(ctx, domain, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, p := range out {
|
|
seen[p.Path+"\x00"+p.Revision.Language] = true
|
|
}
|
|
for _, p := range automatic {
|
|
key := p.Path + "\x00" + p.Revision.Language
|
|
if seen[key] {
|
|
continue
|
|
}
|
|
seen[key] = true
|
|
out = append(out, p)
|
|
bytes += len(p.Path) + len(p.Revision.Filename) + len(p.Revision.ContentType)
|
|
if len(out) > MaxNavigationEntries || bytes > 4<<20 {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
const anonymousRevisionFromSQL = `FROM locality_publication_entries e JOIN locality_publication_manifests m ON m.domain_uuid=e.domain_uuid AND m.node_kind=e.node_kind AND m.node_uuid=e.node_uuid
|
|
JOIN paths p ON p.domain_uuid=e.domain_uuid AND p.path=e.path AND p.object_uuid=e.object_uuid
|
|
JOIN revisions r ON r.uuid=e.revision_uuid AND r.object_uuid=e.object_uuid AND r.document_hash=e.document_hash AND COALESCE(NULLIF(r.lang,''),'und')=e.lang
|
|
JOIN domains d ON d.uuid=e.domain_uuid AND d.enabled=1
|
|
WHERE e.domain_uuid=? AND m.epoch=COALESCE((SELECT version FROM city_governance_epochs WHERE domain_uuid=e.domain_uuid),0)
|
|
AND m.topology_version=COALESCE((SELECT version FROM domain_topology_versions WHERE domain_uuid=e.domain_uuid),1)
|
|
AND (SELECT value FROM config WHERE domain_uuid=e.domain_uuid AND key='authorization_mode')='enforcing'
|
|
AND r.uuid=(SELECT head.uuid FROM revisions head WHERE head.object_uuid=r.object_uuid AND COALESCE(NULLIF(head.lang,''),'und')=e.lang ORDER BY head.created_at_ms DESC,head.rowid DESC LIMIT 1)`
|