Documentation
¶
Overview ¶
Package pageblocks stores the content of the layout blocks a DM makes in place on a page: the Read-aloud box, the Table, the Steps list and the Map.
Block config belongs to the page's category and is shared by every page of it, so what a DM writes on one page cannot live there. Content is kept per (page, block) here instead. The service returns only what the viewer may see: a DM-only table column never reaches a viewer below Owner visibility, so a client bug cannot leak it.
The browser HTTP surface for page blocks. Handlers bind, call the service and shape the response; the service owns every rule, so a mis-wired route cannot widen access.
Hand-written SQL for the page_block_content table.
SECURITY: every statement filters on campaign_id as well as the page and block ids, so ids from another campaign never read or overwrite a document here.
Route registration for page blocks.
Index ¶
- Constants
- Variables
- func HeldCounts(d Checklist) (a, b int)
- func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, ...)
- type Checklist
- type ChecklistItem
- type Column
- type EntityDirectory
- type Handler
- type PageAccess
- type PageLink
- type ReadAloud
- type Repository
- type Row
- type SaveInput
- type Service
- type SketchPreview
- type Step
- type Steps
- type Stored
- type Table
- type Threshold
- type View
- type Viewer
Constants ¶
const ( KindReadAloud = "read_aloud" KindTable = "page_table" KindSteps = "page_steps" KindPageMap = "page_map" KindChecklist = "page_checklist" )
Block kinds. They equal the layout block types so the widget can ask for its content by the type it was placed as.
const ( SideA = "a" SideB = "b" )
Holder sides of a checklist item. An item nobody holds yet has "".
const ( MaxReadAloudChars = 4000 MaxColumns = 8 MaxRows = 60 MaxLabelChars = 80 MaxCellChars = 500 MaxSteps = 30 MaxStepTitleChars = 120 MaxStepTextChars = 1000 MaxIDChars = 64 // MaxBodyBytes is above the worst legal table (60 rows x 8 cells x 500 // four-byte characters) so a legal save is never refused, and low enough // that a junk body is. MaxBodyBytes = 1 << 20 )
Limits. They bound the stored document (and so a request body) and keep the widget's layout sane; the widget enforces the same numbers as a courtesy.
const ( MaxChecklistItems = 60 MaxChecklistName = 120 MaxSideNameChars = 40 MaxThresholds = 10 MaxThresholdNote = 500 MaxChecklistTitle = 120 )
Checklist limits. Sixty items is the table's row limit; ten thresholds is more than a story needs and keeps the DM's notes readable.
const PluginSlug = "pageblocks"
PluginSlug names this plugin in the registry and the health/migration bookkeeping.
Variables ¶
var MigrationsFS embed.FS
MigrationsFS contains the embedded SQL migration files so the table is created from the compiled binary regardless of the working directory (same pattern as the other plugins).
var StaticAssetsFS embed.FS
StaticAssetsFS holds the block widget script, served at /static/plugins/pageblocks/ and loaded only on pages that show one of the blocks.
Functions ¶
func HeldCounts ¶
HeldCounts returns how many items each side holds.
func RegisterRoutes ¶
func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService)
RegisterRoutes mounts the routes under the campaign group. Reads follow the page: anyone who can view it (a public-campaign guest included) may read, and the service hides what the viewer must not see. Writes need a signed-in member; the service then applies the page's own edit rule. CSRF is enforced by the global middleware.
Types ¶
type Checklist ¶
type Checklist struct {
Title string `json:"title"`
SideA string `json:"sideA"`
SideB string `json:"sideB"`
DMOnly bool `json:"dmOnly"`
Items []ChecklistItem `json:"items"`
Thresholds []Threshold `json:"thresholds"`
}
Checklist is the Checklist block's document: named items, each held by one of two sides the owner names. DMOnly hides the whole block from players.
type ChecklistItem ¶
type ChecklistItem struct {
ID string `json:"id"`
Name string `json:"name"`
EntityID string `json:"entityId,omitempty"`
EntityName string `json:"entityName,omitempty"`
EntityURL string `json:"entityUrl,omitempty"`
Holder string `json:"holder"`
DM bool `json:"dm"`
}
ChecklistItem is one named thing in a Checklist, held by side a, side b or nobody. EntityID optionally links it to a page; EntityName and EntityURL are added on read for the links the viewer may open, and ignored on save. DM items are removed for a viewer below Owner visibility.
type Column ¶
type Column struct {
ID string `json:"id"`
Label string `json:"label"`
DMOnly bool `json:"dmOnly"`
}
Column is one table column. DMOnly columns are removed, with their cells, for any viewer below Owner visibility.
type EntityDirectory ¶
type EntityDirectory interface {
// Access answers what the viewer may do with a page, using the same
// rules as the page itself. A page that is missing or in another
// campaign comes back with Exists false.
Access(ctx context.Context, campaignID, entityID string, v Viewer) (PageAccess, error)
// FilterViewable returns the pages among ids that belong to campaignID and
// that v may see, keyed by id. A missing, foreign or hidden id is simply
// absent, so the two cases cannot be told apart.
FilterViewable(ctx context.Context, campaignID string, ids []string, v Viewer) (map[string]PageLink, error)
// Live returns which of ids are pages of campaignID that still exist (not
// deleted or in the Trash), whoever is asking. It is how a link to a page
// that is gone is told apart from one the viewer may not open; the answer
// never reaches a viewer.
Live(ctx context.Context, campaignID string, ids []string) (map[string]bool, error)
// FindByName finds a page by the name it is written with, among the pages
// v may see. ok is false for an unknown name and for a hidden page alike.
FindByName(ctx context.Context, campaignID, name string, v Viewer) (link PageLink, ok bool, err error)
// FirstPageMapBlock returns the id of the first page_map block in the
// layout of the page's category. ok is false when the page is missing, in
// another campaign, or its layout has none.
FirstPageMapBlock(ctx context.Context, campaignID, entityID string) (blockID string, ok bool, err error)
}
EntityDirectory is owned by this plugin and implemented in internal/app over the entities service, so pageblocks never imports another plugin (plugin isolation).
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the block routes.
type PageAccess ¶
type PageAccess struct {
Exists bool
CanView bool
// CanEdit is the page's own edit rule: a Scribe or above who may edit
// this page.
CanEdit bool
}
PageAccess is one viewer's standing on one page.
type PageLink ¶
type PageLink struct {
ID string
Name string
// URL is the page's path, for the hover card and its link.
URL string
}
PageLink is a page a viewer may open, as a marker links to it.
type ReadAloud ¶
type ReadAloud struct {
Text string `json:"text"`
}
ReadAloud is the boxed text a DM reads to the table.
type Repository ¶
type Repository interface {
// Get returns the stored document; found is false when the block has no
// row yet.
Get(ctx context.Context, campaignID, entityID, blockID string) (s Stored, found bool, err error)
// Save writes the document if the stored version is still
// expectedVersion (0 means "no row yet") and bumps it. ok is false on a
// lost race. The kind of an existing row is never changed.
Save(ctx context.Context, campaignID, entityID, blockID, kind string, data []byte, expectedVersion int, userID string) (ok bool, err error)
}
Repository is the persistence boundary for block documents.
func NewRepository ¶
func NewRepository(db *sql.DB) Repository
NewRepository creates a MariaDB-backed repository.
type SaveInput ¶
type SaveInput struct {
Kind string `json:"kind"`
Data json.RawMessage `json:"data"`
Version *int `json:"version"`
}
SaveInput is a PUT body. Version is a pointer so a missing one is refused rather than read as "no row yet".
type Service ¶
type Service interface {
// Get returns the block as viewer v may see it. kindHint says which
// kind to answer with for a block that has no saved row; a saved row's
// own kind wins. A page that is missing or hidden from v is NotFound.
Get(ctx context.Context, campaignID, entityID, blockID, kindHint string, v Viewer) (*View, error)
// Put validates and saves a block, returning the saved view.
Put(ctx context.Context, campaignID, entityID, blockID string, v Viewer, in SaveInput) (*View, error)
// PageMapText is the page map as the line grammar (mapsketch.Grammar), as
// viewer v sees it: DM-only markers and links to pages v cannot open are
// left out. version is the one a save must carry. A block with no saved
// map reads as empty text at version 0.
PageMapText(ctx context.Context, campaignID, entityID, blockID string, v Viewer) (text string, version int, err error)
// EditorSketchText is PageMapText for the editor's "edit as text": it also
// requires that v may edit the page and may save everything on the map.
EditorSketchText(ctx context.Context, campaignID, entityID, blockID string, v Viewer) (text string, version int, err error)
// PreviewSketch parses text and resolves its page names for an editor,
// without saving. Warnings are the lines left out and the names that
// matched no page v can open.
PreviewSketch(ctx context.Context, campaignID, entityID, blockID string, v Viewer, text string) (*SketchPreview, error)
// SavePageMapText parses text, resolves its page names, validates, and
// replaces the whole map with the same permission rules as Put. A line
// that cannot be drawn is left out and reported in Result.Problems, as are
// page names that matched nothing (those lack the "Line N: " prefix).
SavePageMapText(ctx context.Context, campaignID, entityID, blockID string, v Viewer, text string) (mapsketch.Result, error)
// FirstPageMapBlock finds the first page_map block in the layout of the
// page's category.
FirstPageMapBlock(ctx context.Context, campaignID, entityID string) (blockID string, ok bool, err error)
}
Service owns the block content rules.
func NewService ¶
func NewService(repo Repository, entities EntityDirectory) Service
NewService builds the service.
type SketchPreview ¶
SketchPreview is what the editor gets back for text it typed: the map the text makes, and a plain sentence for everything that was left out or did not match. Nothing is saved.
type Step ¶
type Step struct {
ID string `json:"id"`
Title string `json:"title"`
Text string `json:"text"`
Done bool `json:"done"`
}
Step is one beat of the Steps block.
type Threshold ¶
type Threshold struct {
ID string `json:"id"`
Side string `json:"side"`
Count int `json:"count"`
Note string `json:"note"`
}
Threshold is a count of items one side may reach, with a note for the DM when it does. Thresholds are the DM's alone: they never reach a viewer below Owner visibility.
func ReachedThresholds ¶
ReachedThresholds returns the thresholds whose side holds at least their count, in the order the DM wrote them.
type View ¶
type View struct {
Kind string `json:"kind"`
Data any `json:"data"`
Version int `json:"version"`
CanEdit bool `json:"canEdit"`
}
View is what the widget receives: the document already filtered for the viewer, its version for the next save, and whether the viewer may save.
type Viewer ¶
type Viewer struct {
UserID string
// MemberRole is the real membership role (0 guest .. 3 owner).
MemberRole int
// VisibilityRole is the role used for what a viewer may see; a member the
// owner gave DM access counts as an owner there. It decides who sees a
// DM-only column.
VisibilityRole int
}
Viewer is who is asking, taken from the campaign context in the handler.