387 lines
19 KiB
Go
387 lines
19 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"pageswiki/internal/schutzaria"
|
|
)
|
|
|
|
var ErrPublicationImpact = errors.New("city.publication.impact_error")
|
|
|
|
// Publication is an explicit, bounded approval of current immutable content,
|
|
// not a permission to publish future revisions or newly created descendants.
|
|
type PublicationEntry struct {
|
|
Path string `json:"path"`
|
|
Object string `json:"object_uuid"`
|
|
Revision string `json:"revision_uuid"`
|
|
Hash string `json:"sha256"`
|
|
Language string `json:"lang"`
|
|
}
|
|
type LocalityPublication struct {
|
|
PublicationVersion int64 `json:"publication_version"`
|
|
Epoch int64 `json:"epoch"`
|
|
TopologyVersion int64 `json:"topology_version"`
|
|
Enabled bool `json:"enabled"`
|
|
Eligible bool `json:"eligible"`
|
|
ImpactToken string `json:"impact_token"`
|
|
Entries []PublicationEntry `json:"entries"`
|
|
Excluded []string `json:"excluded"`
|
|
}
|
|
type PublicationChange struct {
|
|
PublicationVersion int64 `json:"publication_version"`
|
|
Epoch int64 `json:"epoch"`
|
|
TopologyVersion int64 `json:"topology_version"`
|
|
ImpactToken string `json:"impact_token"`
|
|
Enabled bool `json:"enabled"`
|
|
Confirm bool `json:"confirm"`
|
|
}
|
|
|
|
func (s *Store) ensurePublicationSchema() error {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
return s.ensurePostgreSQLPublication(context.Background())
|
|
}
|
|
for _, query := range []string{
|
|
`CREATE TABLE IF NOT EXISTS locality_publication_versions(domain_uuid TEXT NOT NULL,node_kind TEXT NOT NULL,node_uuid TEXT NOT NULL,version INTEGER NOT NULL,PRIMARY KEY(domain_uuid,node_kind,node_uuid))`,
|
|
`CREATE TABLE IF NOT EXISTS locality_publication_manifests(domain_uuid TEXT NOT NULL,node_kind TEXT NOT NULL,node_uuid TEXT NOT NULL,epoch INTEGER NOT NULL,topology_version INTEGER NOT NULL,actor_uuid TEXT NOT NULL,created_at_ms INTEGER NOT NULL,PRIMARY KEY(domain_uuid,node_kind,node_uuid))`,
|
|
`CREATE TABLE IF NOT EXISTS locality_publication_entries(domain_uuid TEXT NOT NULL,node_kind TEXT NOT NULL,node_uuid TEXT NOT NULL,path TEXT NOT NULL,object_uuid TEXT NOT NULL,revision_uuid TEXT NOT NULL,document_hash TEXT NOT NULL,lang TEXT NOT NULL,PRIMARY KEY(domain_uuid,node_kind,node_uuid,path,lang))`,
|
|
`CREATE INDEX IF NOT EXISTS idx_publication_path ON locality_publication_entries(domain_uuid,path)`,
|
|
`CREATE INDEX IF NOT EXISTS idx_publication_object ON locality_publication_entries(object_uuid,lang)`,
|
|
`CREATE INDEX IF NOT EXISTS idx_publication_revision ON locality_publication_entries(revision_uuid)`,
|
|
`CREATE TABLE IF NOT EXISTS locality_auto_publication(domain_uuid TEXT NOT NULL,node_kind TEXT NOT NULL,node_uuid TEXT NOT NULL,mode TEXT NOT NULL CHECK(mode IN ('inherit','required','automatic')),scope_path TEXT NOT NULL,actor_uuid TEXT NOT NULL,version INTEGER NOT NULL,PRIMARY KEY(domain_uuid,node_kind,node_uuid))`,
|
|
} {
|
|
if _, err := s.DB.Exec(query); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.ensureNavigationSchema()
|
|
}
|
|
|
|
func installPublicationGuards(ctx context.Context, tx *sql.Tx) error {
|
|
statements := []string{
|
|
`CREATE TRIGGER IF NOT EXISTS auto_publication_config_update BEFORE UPDATE ON config WHEN OLD.key='authorization_mode' AND (NEW.key!=OLD.key OR NEW.domain_uuid!=OLD.domain_uuid OR NEW.value!='enforcing') AND EXISTS(SELECT 1 FROM locality_auto_publication WHERE domain_uuid=OLD.domain_uuid AND mode='automatic') BEGIN SELECT RAISE(ABORT,'publication_requires_enforcing'); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS auto_publication_config_delete BEFORE DELETE ON config WHEN OLD.key='authorization_mode' AND EXISTS(SELECT 1 FROM locality_auto_publication WHERE domain_uuid=OLD.domain_uuid AND mode='automatic') BEGIN SELECT RAISE(ABORT,'publication_requires_enforcing'); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS auto_publication_config_insert BEFORE INSERT ON config WHEN NEW.key='authorization_mode' AND NEW.value!='enforcing' AND EXISTS(SELECT 1 FROM locality_auto_publication WHERE domain_uuid=NEW.domain_uuid AND mode='automatic') BEGIN SELECT RAISE(ABORT,'publication_requires_enforcing'); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_revision_insert AFTER INSERT ON revisions BEGIN DELETE FROM locality_publication_entries WHERE object_uuid=NEW.object_uuid AND lang=COALESCE(NULLIF(NEW.lang,''),'und'); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_revision_update AFTER UPDATE ON revisions BEGIN DELETE FROM locality_publication_entries WHERE revision_uuid=OLD.uuid; END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_revision_delete AFTER DELETE ON revisions BEGIN DELETE FROM locality_publication_entries WHERE revision_uuid=OLD.uuid; END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_path_insert AFTER INSERT ON paths BEGIN DELETE FROM locality_publication_entries WHERE object_uuid=NEW.object_uuid; END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_path_delete AFTER DELETE ON paths BEGIN DELETE FROM locality_publication_entries WHERE object_uuid=OLD.object_uuid; END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_path_update AFTER UPDATE ON paths BEGIN DELETE FROM locality_publication_entries WHERE object_uuid IN (OLD.object_uuid,NEW.object_uuid); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_config_update BEFORE UPDATE ON config WHEN OLD.key='authorization_mode' AND (NEW.key!=OLD.key OR NEW.domain_uuid!=OLD.domain_uuid OR NEW.value!='enforcing') AND EXISTS(SELECT 1 FROM locality_publication_manifests WHERE domain_uuid=OLD.domain_uuid) BEGIN SELECT RAISE(ABORT,'publication_requires_enforcing'); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_config_delete BEFORE DELETE ON config WHEN OLD.key='authorization_mode' AND EXISTS(SELECT 1 FROM locality_publication_manifests WHERE domain_uuid=OLD.domain_uuid) BEGIN SELECT RAISE(ABORT,'publication_requires_enforcing'); END`,
|
|
`CREATE TRIGGER IF NOT EXISTS publication_config_insert BEFORE INSERT ON config WHEN NEW.key='authorization_mode' AND NEW.value!='enforcing' AND EXISTS(SELECT 1 FROM locality_publication_manifests WHERE domain_uuid=NEW.domain_uuid) BEGIN SELECT RAISE(ABORT,'publication_requires_enforcing'); END`,
|
|
}
|
|
for _, table := range []string{"zones", "spots"} {
|
|
for _, op := range []string{"INSERT", "UPDATE", "DELETE"} {
|
|
ref := "OLD"
|
|
if op == "INSERT" {
|
|
ref = "NEW"
|
|
}
|
|
statements = append(statements, `CREATE TRIGGER IF NOT EXISTS publication_`+table+`_`+strings.ToLower(op)+` AFTER `+op+` ON `+table+` BEGIN DELETE FROM locality_publication_manifests WHERE domain_uuid=`+ref+`.domain_uuid; DELETE FROM locality_publication_entries WHERE domain_uuid=`+ref+`.domain_uuid; END`)
|
|
}
|
|
}
|
|
for _, op := range []string{"INSERT", "UPDATE", "DELETE"} {
|
|
ref := "OLD"
|
|
if op == "INSERT" {
|
|
ref = "NEW"
|
|
}
|
|
statements = append(statements, `CREATE TRIGGER IF NOT EXISTS publication_street_spots_`+strings.ToLower(op)+` AFTER `+op+` ON street_spots BEGIN DELETE FROM locality_publication_manifests WHERE domain_uuid=(SELECT domain_uuid FROM spots WHERE uuid=`+ref+`.spot_uuid); DELETE FROM locality_publication_entries WHERE domain_uuid=(SELECT domain_uuid FROM spots WHERE uuid=`+ref+`.spot_uuid); END`)
|
|
}
|
|
for _, q := range statements {
|
|
if _, err := tx.ExecContext(ctx, q); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) publicationImpact(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string, g CityGovernance) (LocalityPublication, error) {
|
|
return s.publicationImpactBounded(ctx, tx, domain, actor, kind, id, g, 512, "")
|
|
}
|
|
|
|
func (s *Store) publicationImpactBounded(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string, g CityGovernance, limit int, onlyPath string) (LocalityPublication, error) {
|
|
p := LocalityPublication{PublicationVersion: g.PublicationVersion, Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Entries: []PublicationEntry{}, Excluded: []string{}}
|
|
var mode string
|
|
if err := tx.QueryRowContext(ctx, `SELECT value FROM config WHERE domain_uuid=`+s.sqlParam(1)+` AND key='authorization_mode'`, domain).Scan(&mode); err != nil {
|
|
return p, err
|
|
}
|
|
if mode != "enforcing" {
|
|
return p, ErrAccessDenied
|
|
}
|
|
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM locality_publication_manifests m WHERE domain_uuid=`+s.sqlParam(1)+` AND node_kind=`+s.sqlParam(2)+` AND node_uuid=`+s.sqlParam(3)+` AND epoch=`+s.sqlParam(4)+` AND topology_version=`+s.sqlParam(5)+` AND EXISTS(SELECT 1 FROM locality_publication_entries e WHERE e.domain_uuid=m.domain_uuid AND e.node_kind=m.node_kind AND e.node_uuid=m.node_uuid))`, domain, kind, id, g.Epoch, g.TopologyVersion).Scan(&p.Enabled); err != nil {
|
|
return p, err
|
|
}
|
|
// A Domain ceiling is never a publication grant.
|
|
if kind == "domain" || !g.DraftEffectivePublic {
|
|
return p, nil
|
|
}
|
|
scope := ""
|
|
table := "zones"
|
|
if kind == "spot" {
|
|
table = "spots"
|
|
}
|
|
if err := tx.QueryRowContext(ctx, `SELECT path_prefix FROM `+table+` WHERE domain_uuid=`+s.sqlParam(1)+` AND uuid=`+s.sqlParam(2)+``, domain, id).Scan(&scope); err != nil {
|
|
return p, err
|
|
}
|
|
policy, err := s.loadReadPathPolicy(cityReadQuery{ctx, tx}, domain, actor)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
// Literal segment predicates: '%' and '_' are valid names, never wildcards.
|
|
rows, err := tx.QueryContext(ctx, `SELECT p.path,p.object_uuid,
|
|
(EXISTS(SELECT 1 FROM locality_access_policies a WHERE a.domain_uuid=p.domain_uuid AND `+s.pathScopeSQL("a.scope_path", "p.path")+`)
|
|
OR EXISTS(SELECT 1 FROM locality_publication_policies a JOIN zones z ON z.uuid=a.node_uuid AND z.domain_uuid=a.domain_uuid AND z.state!='archived' WHERE a.domain_uuid=p.domain_uuid AND a.node_kind IN ('quartier','street') AND a.policy='authenticated_acl' AND `+s.pathScopeSQL("z.path_prefix", "p.path")+`)
|
|
OR EXISTS(SELECT 1 FROM locality_publication_policies a JOIN spots z ON z.uuid=a.node_uuid AND z.domain_uuid=a.domain_uuid AND z.state!='archived' WHERE a.domain_uuid=p.domain_uuid AND a.node_kind='spot' AND a.policy='authenticated_acl' AND `+s.pathScopeSQL("z.path_prefix", "p.path")+`)
|
|
OR EXISTS(SELECT 1 FROM paths other WHERE other.object_uuid=p.object_uuid AND (other.domain_uuid!=p.domain_uuid OR other.path!=p.path)))
|
|
FROM paths p CROSS JOIN (SELECT CAST(`+s.sqlParam(1)+` AS TEXT) AS scope) input WHERE p.domain_uuid=`+s.sqlParam(2)+` AND `+s.pathScopeSQL("input.scope", "p.path")+` AND (CAST(`+s.sqlParam(3)+` AS TEXT)='' OR p.path=`+s.sqlParam(4)+`) ORDER BY p.path LIMIT `+s.sqlParam(5), scope, domain, onlyPath, onlyPath, limit+1)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
type candidate struct {
|
|
path, object string
|
|
excluded bool
|
|
}
|
|
candidates := []candidate{}
|
|
for rows.Next() {
|
|
var c candidate
|
|
if err = rows.Scan(&c.path, &c.object, &c.excluded); err != nil {
|
|
break
|
|
}
|
|
candidates = append(candidates, c)
|
|
}
|
|
if err == nil {
|
|
err = rows.Err()
|
|
}
|
|
rows.Close()
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
if len(candidates) > limit {
|
|
return p, ErrPublicationImpact
|
|
}
|
|
objects := map[string]string{}
|
|
args := []any{}
|
|
for _, c := range candidates {
|
|
if !policy.Allows(c.path) {
|
|
return p, ErrAccessDenied
|
|
}
|
|
if c.excluded || !PublicSemanticPath(c.path) {
|
|
p.Excluded = append(p.Excluded, c.path)
|
|
continue
|
|
}
|
|
objects[c.object] = c.path
|
|
args = append(args, c.object)
|
|
}
|
|
if len(args) > 0 {
|
|
// Read all scoped language heads in one metadata-only window query,
|
|
// rather than repeating an authorization/head query for every Page.
|
|
marks := s.sqlValues(len(args))
|
|
rows, err = tx.QueryContext(ctx, `SELECT object_uuid,uuid,document_hash,lang FROM (
|
|
SELECT object_uuid,uuid,COALESCE(document_hash,'') AS document_hash,COALESCE(NULLIF(lang,''),'und') AS lang,
|
|
ROW_NUMBER() OVER(PARTITION BY object_uuid,COALESCE(NULLIF(lang,''),'und') ORDER BY `+s.revisionHeadOrder("")+`) AS head
|
|
FROM revisions WHERE object_uuid IN (`+marks+`)) heads WHERE head=1 ORDER BY object_uuid,lang LIMIT `+fmt.Sprint(limit*4+1), args...)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
counts := map[string]int{}
|
|
for rows.Next() {
|
|
var e PublicationEntry
|
|
if err = rows.Scan(&e.Object, &e.Revision, &e.Hash, &e.Language); err != nil {
|
|
break
|
|
}
|
|
e.Path = objects[e.Object]
|
|
counts[e.Object]++
|
|
if len(e.Hash) != 64 || counts[e.Object] > 32 || len(p.Entries) >= limit*4 {
|
|
err = ErrPublicationImpact
|
|
break
|
|
}
|
|
p.Entries = append(p.Entries, e)
|
|
}
|
|
if err == nil {
|
|
err = rows.Err()
|
|
}
|
|
rows.Close()
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
}
|
|
sort.Slice(p.Entries, func(i, j int) bool {
|
|
a, b := p.Entries[i], p.Entries[j]
|
|
if a.Path != b.Path {
|
|
return a.Path < b.Path
|
|
}
|
|
return a.Language < b.Language
|
|
})
|
|
p.Eligible = len(p.Entries) > 0
|
|
if limit != 512 {
|
|
return p, nil
|
|
}
|
|
// Include actor/domain/node identity to make approvals non-transferable.
|
|
b, _ := json.Marshal(struct {
|
|
Domain, Actor, Kind, ID string
|
|
Epoch, Topology int64
|
|
PublicationVersion int64
|
|
Entries []PublicationEntry
|
|
Excluded []string
|
|
}{domain, actor, kind, id, g.Epoch, g.TopologyVersion, g.PublicationVersion, p.Entries, p.Excluded})
|
|
hash := sha256.Sum256(b)
|
|
p.ImpactToken = hex.EncodeToString(hash[:])
|
|
return p, nil
|
|
}
|
|
|
|
func (s *Store) LocalityPublication(ctx context.Context, domain, actor, kind, id string, change *PublicationChange) (_ LocalityPublication, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("LocalityPublication", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
defer cancel()
|
|
options := s.policyReadOptions()
|
|
if change != nil {
|
|
options = &sql.TxOptions{Isolation: sql.LevelReadCommitted}
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, options)
|
|
if err != nil {
|
|
return LocalityPublication{}, err
|
|
}
|
|
defer tx.Rollback()
|
|
if change != nil {
|
|
if err = s.lockPolicyWrite(ctx, tx); err != nil {
|
|
return LocalityPublication{}, err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_epochs(domain_uuid,version) VALUES(`+s.sqlParam(1)+`,0) ON CONFLICT(domain_uuid) DO UPDATE SET version=city_governance_epochs.version`, domain); err != nil {
|
|
return LocalityPublication{}, err
|
|
}
|
|
}
|
|
g, err := s.governanceSnapshot(ctx, tx, domain, actor, kind, id)
|
|
if err != nil {
|
|
return LocalityPublication{}, err
|
|
}
|
|
if change != nil && (change.Epoch != g.Epoch || change.TopologyVersion != g.TopologyVersion || change.PublicationVersion != g.PublicationVersion) {
|
|
return LocalityPublication{}, ErrGovernanceConflict
|
|
}
|
|
// Revocation must work even when content is now unreadable or over budget.
|
|
p := LocalityPublication{PublicationVersion: g.PublicationVersion, Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Entries: []PublicationEntry{}, Excluded: []string{}}
|
|
if change == nil || change.Enabled {
|
|
p, err = s.publicationImpact(ctx, tx, domain, actor, kind, id, g)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
}
|
|
if change == nil {
|
|
return p, nil
|
|
}
|
|
if change.Enabled && (!change.Confirm || !p.Eligible || change.ImpactToken == "" || change.ImpactToken != p.ImpactToken) {
|
|
return p, ErrPublicationImpact
|
|
}
|
|
var old bool
|
|
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM locality_publication_manifests WHERE domain_uuid=`+s.sqlParam(1)+` AND node_kind=`+s.sqlParam(2)+` AND node_uuid=`+s.sqlParam(3)+`)`, domain, kind, id).Scan(&old); err != nil {
|
|
return p, err
|
|
}
|
|
for _, table := range []string{"locality_publication_entries", "locality_publication_manifests"} {
|
|
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain_uuid=`+s.sqlParam(1)+` AND node_kind=`+s.sqlParam(2)+` AND node_uuid=`+s.sqlParam(3)+``, domain, kind, id); err != nil {
|
|
return p, err
|
|
}
|
|
}
|
|
if change.Enabled {
|
|
if err = s.installPublicationGuards(ctx, tx); err != nil {
|
|
return p, err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_manifests VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`)`, domain, kind, id, g.Epoch, g.TopologyVersion, actor, time.Now().UnixMilli()); err != nil {
|
|
return p, err
|
|
}
|
|
for _, e := range p.Entries {
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_entries VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, domain, kind, id, e.Path, e.Object, e.Revision, e.Hash, e.Language); err != nil {
|
|
return p, err
|
|
}
|
|
}
|
|
}
|
|
if !change.Enabled {
|
|
if err = s.requireManualPublication(ctx, tx, domain, actor, kind, id); err != nil {
|
|
return p, err
|
|
}
|
|
}
|
|
p.Enabled = change.Enabled
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_versions VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,1) ON CONFLICT(domain_uuid,node_kind,node_uuid) DO UPDATE SET version=locality_publication_versions.version+1`, domain, kind, id); err != nil {
|
|
return p, err
|
|
}
|
|
p.PublicationVersion++
|
|
after, _ := json.Marshal(map[string]any{"publication": change.Enabled, "impact_token": p.ImpactToken, "entries": len(p.Entries)})
|
|
before, _ := json.Marshal(map[string]bool{"publication": old})
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_events(domain_uuid,epoch,actor_uuid,node_kind,node_uuid,before_json,after_json,created_at_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, domain, g.Epoch, actor, kind, id, string(before), string(after), time.Now().UnixMilli()); err != nil {
|
|
return p, err
|
|
}
|
|
// Reconfirmation needs a fresh impact token, even if content is unchanged.
|
|
p.Eligible = false
|
|
p.ImpactToken = ""
|
|
return p, tx.Commit()
|
|
}
|
|
|
|
// Explicitly exclude every application/user/group/editor namespace. Public
|
|
// projections must never turn an arbitrary reserved handler into a document.
|
|
func PublicSemanticPath(path string) bool {
|
|
return schutzaria.PublicSemanticPath(path)
|
|
}
|
|
|
|
// Lookup is metadata-only and Path-indexed. The manifest is fenced by current
|
|
// governance/topology epochs, live binding, and the current language head.
|
|
func (s *Store) AnonymousRevision(ctx context.Context, domain, path string, languages []string) (_ *Revision, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("AnonymousRevision", resultErr) }()
|
|
if !PublicSemanticPath(path) {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
|
|
if err != nil {
|
|
return nil, s.sqlError("anonymous publication", err)
|
|
}
|
|
defer tx.Rollback()
|
|
rows, err := tx.QueryContext(ctx, `SELECT DISTINCT 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
|
|
`+s.anonymousRevisionFromSQL()+` AND e.path=`+s.sqlParam(2)+`
|
|
ORDER BY r.created_at_ms DESC,r.uuid LIMIT 33`, domain, path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := []*Revision{}
|
|
for rows.Next() {
|
|
v := &Revision{}
|
|
if err = rows.Scan(&v.UUID, &v.ObjectUUID, &v.DocumentHash, &v.Language, &v.ContentType, &v.Filename, &v.CreatedAtMS); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
items = append(items, v)
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
rows.Close()
|
|
if len(items) == 0 {
|
|
pages, e := s.automaticPublicPagesTx(ctx, tx, domain, path)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
for i := range pages {
|
|
items = append(items, &pages[i].Revision)
|
|
}
|
|
}
|
|
if len(items) == 0 || len(items) > 32 {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
for _, lang := range languages {
|
|
for _, v := range items {
|
|
if v.Language == lang {
|
|
return v, nil
|
|
}
|
|
}
|
|
}
|
|
return items[0], nil
|
|
}
|