post

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package post defines the content object at the center of the CMS.

Index

Constants

View Source
const TypePost = "post"

TypePost is the name of the built-in post type.

Variables

View Source
var ErrConflict = errors.New("post: conflicting update")

ErrConflict reports that the post changed after the update was prepared.

View Source
var ErrInvalidAuthor = errors.New("post: invalid author")

ErrInvalidAuthor reports that a post carries no author.

View Source
var ErrInvalidOrder = errors.New("post: invalid order")

ErrInvalidOrder reports that a sort direction is not one the CMS sorts in.

View Source
var ErrInvalidOrderBy = errors.New("post: invalid orderby")

ErrInvalidOrderBy reports that a sort column is not one the CMS sorts by.

View Source
var ErrInvalidStatus = errors.New("post: invalid status")

ErrInvalidStatus reports that a status value is not one the CMS stores.

View Source
var ErrInvalidTransition = errors.New("post: invalid status transition")

ErrInvalidTransition reports that a status change is not allowed.

View Source
var ErrInvalidType = errors.New("post: invalid type")

ErrInvalidType reports that a post type is not registered.

View Source
var ErrNotFound = errors.New("post: not found")

ErrNotFound reports that no post exists for the requested ID.

View Source
var ErrRevisionNotFound = errors.New("post: revision not found")

ErrRevisionNotFound reports that no revision exists for the requested ID.

View Source
var ErrSlugTaken = errors.New("post: slug taken")

ErrSlugTaken reports that a post type already holds the requested slug.

Functions

func Register

func Register(t Type)

Register adds a post type to the registry and panics on an invalid one.

func Slugify

func Slugify(title string) string

Slugify returns title as a URL slug, or [untitledSlug] when it holds no slug characters.

Types

type Filter

type Filter struct {
	Type    string
	Status  Status
	Search  string
	OrderBy OrderBy
	Order   Order
	Page    int
	PerPage int
}

Filter narrows a post listing.

type Order

type Order string

Order names a sort direction.

const (
	OrderAsc  Order = "asc"
	OrderDesc Order = "desc"
)

The directions a listing can be sorted in.

func ParseOrder

func ParseOrder(raw string) (Order, error)

ParseOrder returns the direction named by raw, or ErrInvalidOrder.

type OrderBy

type OrderBy string

OrderBy names a column a listing can be sorted by.

const (
	OrderByDate  OrderBy = "date"
	OrderByTitle OrderBy = "title"
)

The columns a listing can be sorted by.

func ParseOrderBy

func ParseOrderBy(raw string) (OrderBy, error)

ParseOrderBy returns the column named by raw, or ErrInvalidOrderBy.

type Post

type Post struct {
	ID          uuid.UUID
	Type        string
	Slug        string
	Title       string
	Content     string
	Excerpt     string
	Status      Status
	AuthorID    uuid.UUID
	PublishedAt *time.Time
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

Post is a content object holding Gutenberg-serialized block HTML.

func New

func New(postType, title string, authorID uuid.UUID) (Post, error)

New returns a draft Post of the given type, slugged after its title.

func (*Post) Restore

func (p *Post) Restore() error

Restore returns a trashed post to draft, or reports ErrInvalidTransition.

func (*Post) Transition

func (p *Post) Transition(to Status) error

Transition moves the post to the given status, stamping PublishedAt on the first publication and returning ErrInvalidTransition when disallowed.

type Revision

type Revision struct {
	ID        uuid.UUID
	PostID    uuid.UUID
	Kind      RevisionKind
	AuthorID  uuid.UUID
	Title     string
	Content   string
	Excerpt   string
	CreatedAt time.Time
}

Revision is a snapshot of a post's editable content.

func NewRevision

func NewRevision(p Post, kind RevisionKind, authorID uuid.UUID) (Revision, error)

NewRevision returns a snapshot of the post's editable content, credited to the given author.

type RevisionKind

type RevisionKind string

RevisionKind distinguishes an update snapshot from a per-author autosave.

const (
	RevisionKindRevision RevisionKind = "revision"
	RevisionKindAutosave RevisionKind = "autosave"
)

The kinds of revision a post carries.

type Status

type Status string

Status is the publication state of a post.

const (
	StatusDraft     Status = "draft"
	StatusPending   Status = "pending"
	StatusPrivate   Status = "private"
	StatusScheduled Status = "scheduled"
	StatusPublished Status = "published"
	StatusTrash     Status = "trash"
)

The statuses a post may hold. Scheduled is stored but unreachable until a publish worker exists.

func ParseStatus

func ParseStatus(value string) (Status, error)

ParseStatus returns the Status named by value, or ErrInvalidStatus.

type Store

type Store interface {
	Create(ctx context.Context, p Post) (Post, error)
	ByID(ctx context.Context, id uuid.UUID) (Post, error)
	PublishedBySlug(ctx context.Context, postType, slug string) (Post, error)
	List(ctx context.Context, f Filter) ([]Post, int, error)
	Update(ctx context.Context, p Post, expectedUpdatedAt time.Time, snapshot *Revision, revisionCap int) (Post, error)
	Trash(ctx context.Context, id uuid.UUID, updatedAt time.Time) (Post, error)
	Restore(ctx context.Context, id uuid.UUID, updatedAt time.Time) (Post, error)
	Delete(ctx context.Context, id uuid.UUID) error
	Counts(ctx context.Context, postType string) (map[Status]int, error)
	Revisions(ctx context.Context, postID uuid.UUID) ([]Revision, error)
	RevisionByID(ctx context.Context, postID, revisionID uuid.UUID) (Revision, error)
	DeleteRevision(ctx context.Context, postID, revisionID uuid.UUID) error
	SaveAutosave(ctx context.Context, autosave Revision) (Revision, error)
	Autosave(ctx context.Context, postID, authorID uuid.UUID) (Revision, error)
	DeleteAutosave(ctx context.Context, postID, authorID uuid.UUID) error
}

Store persists posts and their revisions.

type Type

type Type struct {
	Name         string
	Label        string
	Hierarchical bool
	Revisions    bool
	// RevisionCap bounds the revisions kept per post. Zero keeps every revision.
	RevisionCap int
}

Type describes a kind of content the CMS stores.

func TypeByName

func TypeByName(name string) (Type, bool)

TypeByName returns the registered type of the given name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL