66 lines
3.0 KiB
Go
66 lines
3.0 KiB
Go
package metadata
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// appendRevisionOutbox requires the Domain write lock. Sequence allocation uses
|
|
// small per-type watermarks, not MAX over the ever-growing event journal.
|
|
// This writes pending events only; delivery/leases remain a separate port.
|
|
func (r *Repository) appendRevisionOutbox(ctx context.Context, tx *sql.Tx, domain, actor string, v Revision) error {
|
|
payload, err := json.Marshal(map[string]string{"revision_uuid": v.UUID, "object_uuid": v.ObjectUUID, "document_hash": v.DocumentHash, "content_type": v.ContentType, "filename": v.Filename})
|
|
if err != nil {
|
|
return ErrInvalid
|
|
}
|
|
return r.appendOutbox(ctx, tx, domain, actor, "revision.created", "revision", v.UUID, string(payload))
|
|
}
|
|
|
|
func (r *Repository) appendPageOutbox(ctx context.Context, tx *sql.Tx, domain, path, object string) error {
|
|
payload, err := json.Marshal(map[string]string{"path": path, "object_uuid": object})
|
|
if err != nil {
|
|
return ErrInvalid
|
|
}
|
|
return r.appendOutbox(ctx, tx, domain, "", "page.created", "page", object, string(payload))
|
|
}
|
|
|
|
// Caller holds the Domain lock; metadata and the pending event commit together.
|
|
func (r *Repository) appendOutbox(ctx context.Context, tx *sql.Tx, domain, actor, eventType, targetKind, targetUUID, payload string) error {
|
|
var last int64
|
|
if err := tx.QueryRowContext(ctx, `SELECT COALESCE(MAX(last_sequence),0) FROM activity_outbox_heads WHERE domain_uuid=`+r.p(1), domain).Scan(&last); err != nil {
|
|
return r.failure(err)
|
|
}
|
|
if last == math.MaxInt64 {
|
|
return ErrInvalid
|
|
}
|
|
now := time.Now().UnixMilli()
|
|
_, err := tx.ExecContext(ctx, `INSERT INTO activity_outbox(uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,next_attempt_ms,created_at_ms) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`,`+r.p(5)+`,`+r.p(6)+`,`+r.p(7)+`,`+r.p(8)+`,`+r.p(9)+`,`+r.p(10)+`,`+r.p(11)+`)`, uuid.NewString(), domain, last+1, eventType, actor, targetKind, targetUUID, payload, eventType+":"+targetUUID, now, now)
|
|
if err != nil {
|
|
return r.failure(err)
|
|
}
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO activity_outbox_heads(domain_uuid,event_type,last_sequence) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`) ON CONFLICT(domain_uuid,event_type) DO UPDATE SET last_sequence=excluded.last_sequence`, domain, eventType, last+1)
|
|
return r.failure(err)
|
|
}
|
|
|
|
func (r *Repository) revisionReferences(ctx context.Context, domain, revisionUUID string) ([]string, error) {
|
|
rows, err := r.database.QueryContext(ctx, `SELECT document_hash FROM revision_document_refs WHERE revision_uuid=`+r.p(1)+` AND EXISTS (SELECT 1 FROM revisions r WHERE r.uuid=`+r.p(2)+` AND (`+r.objectDomainScope("r.object_uuid", r.p(3))+`)) ORDER BY document_hash`, revisionUUID, revisionUUID, domain)
|
|
if err != nil {
|
|
return nil, r.failure(err)
|
|
}
|
|
defer rows.Close()
|
|
var hashes []string
|
|
for rows.Next() {
|
|
var h string
|
|
if err := rows.Scan(&h); err != nil {
|
|
return nil, r.failure(err)
|
|
}
|
|
hashes = append(hashes, h)
|
|
}
|
|
return hashes, r.failure(rows.Err())
|
|
}
|