Documentation
¶
Overview ¶
Package rumours is the Rumours page block: what the party has heard about a page, who said it, how sure they were, what the Director knows is true and what the party has decided to believe.
The two judgements are kept apart on purpose. The Director's truth is theirs alone and never leaves the service for anyone else; the party's belief is set by a Scribe or the Director and shown to everyone once marked. A Scribe can add rumours and mark what the party thinks but can never change the truth. The service decides what each viewer is told and who may write; the handler, the block and the script add no rule of their own.
Index ¶
Constants ¶
const ( TruthOpen = "open" TruthTrue = "true" TruthFalse = "false" )
What the Director knows about a rumour. Only the Director ever reads or sets it.
const ( BeliefNone = "" BeliefTrue = "true" BeliefFalse = "false" BeliefUnsure = "unsure" )
What the party has decided to believe. BeliefNone is a rumour nobody has marked yet.
const ( SureMin = 1 SureMax = 3 SureDefault = 2 )
How sure the person who said it was, as shown by the three dots.
const ( // QuoteMax is the longest rumour, in characters. QuoteMax = 280 // SourceMax is the longest "who said it", in characters. SourceMax = 80 // MaxRumours is how many rumours one page keeps. It is also the most a // read sends, so a page can never hold a rumour its block cannot show; // adding past it asks for some to be removed first. MaxRumours = 200 )
Bounds that keep a page's rumours a small thing. They are why the block can read a whole page in one query instead of paging it.
const BlockType = "rumours"
BlockType is the block's type in the page block registry.
const PluginSlug = "rumours"
PluginSlug names the Rumours block in the plugin migration bookkeeping and the health registry. It has a slug and a migrations directory of its own so a failed migration here leaves only the Rumours block out, and never marks another plugin degraded or runs another plugin's files.
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's script, served at /static/plugins/rumours/ and loaded on sight the first time a page shows the block.
Functions ¶
func RegisterRoutes ¶
func RegisterRoutes(e *echo.Echo, h *Handler, campaignSvc campaigns.CampaignService, authSvc auth.AuthService)
RegisterRoutes mounts the block's routes. Reads need a campaign member and the service narrows them further; writes are the Director's and Scribes'.
Types ¶
type AddInput ¶
type AddInput struct {
CampaignID string
EntityID string
Quote string
Who string
Sure int
Viewer Viewer
}
AddInput is a new rumour.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the Rumours block's JSON. It binds, checks the page is one the caller may open, and calls the service; every rule about who may see or write what is the service's.
func NewHandler ¶
NewHandler creates the Rumours handler.
type Pages ¶
type Pages interface {
Open(ctx context.Context, campaignID, entityID string, role int, userID string) error
}
Pages answers whether the caller may open a page. It is owned here and implemented in internal/app over the entities service, so this plugin never imports another plugin. A page that is missing, in another campaign or hidden from the caller is the same answer: not found.
type Repository ¶
type Repository interface {
// List returns the page's rumours, newest first, at most limit, and how
// many the page holds in all.
List(ctx context.Context, campaignID, entityID string, withTruth bool, limit int) ([]Rumour, int, error)
// Get returns one rumour of that page, or nil when it is not there.
Get(ctx context.Context, campaignID, entityID, id string, withTruth bool) (*Rumour, error)
// Insert adds a rumour unless the page already holds max; it also checks
// the page is in the campaign, under a lock that serialises two adds.
Insert(ctx context.Context, r Rumour, max int) error
// UpdateText changes what was said, who said it and how sure they were.
UpdateText(ctx context.Context, campaignID, entityID, id, quote, source string, sure int) (bool, error)
// SetTruth records the Director's verdict.
SetTruth(ctx context.Context, campaignID, entityID, id, truth string) (bool, error)
// SetBelief records what the party thinks and who marked it.
SetBelief(ctx context.Context, campaignID, entityID, id, belief, byUser string, byDM bool) (bool, error)
// Delete removes a rumour of that page; false when it was not there.
Delete(ctx context.Context, campaignID, entityID, id string) (bool, error)
}
Repository owns the page_rumours table. It decides nothing about who may do what, except one thing the SQL itself enforces: a read made without withTruth never selects the truth column, so a read for a Scribe or a player cannot carry it even if a caller forgets to drop it afterwards.
func NewRepository ¶
func NewRepository(db *sql.DB) Repository
NewRepository creates the MariaDB repository for rumours.
type Rumour ¶
type Rumour struct {
ID string
CampaignID string
EntityID string
Quote string
Source string
Sure int
Truth string
Belief string
BeliefBy string
BeliefName string
BeliefByDM bool
ByDM bool
CreatedBy string
AuthorName string
CreatedAt time.Time
}
Rumour is one stored rumour with the names of the people who touched it. Truth is empty when the read was made for someone who may not see it.
type RumourView ¶
type RumourView struct {
ID string `json:"id"`
Quote string `json:"quote"`
Who string `json:"who"`
Sure int `json:"sure"`
// When is the day it was heard, already worded for the page.
When string `json:"when"`
// AddedBy is "you", a person's name, or empty for a rumour the Director
// wrote (the Director's own rumours carry no byline).
AddedBy string `json:"addedBy,omitempty"`
// Belief is what the party thinks; empty until someone marks it.
Belief string `json:"belief"`
BeliefBy string `json:"beliefBy,omitempty"`
// Truth is only ever set in a view built for the Director.
Truth *string `json:"truth,omitempty"`
// CanEdit is whether this viewer may edit or remove this rumour.
CanEdit bool `json:"canEdit"`
}
RumourView is a rumour as one viewer may see it. Truth is a pointer so that for everyone but the Director the key is absent from the JSON altogether, not present and empty.
type Service ¶
type Service interface {
// View is the whole block for one viewer.
View(ctx context.Context, campaignID, entityID string, v Viewer) (*View, error)
// Add writes a rumour. Director and Scribes.
Add(ctx context.Context, in AddInput) (*RumourView, error)
// Update edits a rumour's words. Director, or the Scribe who wrote it.
Update(ctx context.Context, campaignID, entityID, rumourID string, in UpdateRumourInput, v Viewer) (*RumourView, error)
// SetTruth records what is true. Director only.
SetTruth(ctx context.Context, campaignID, entityID, rumourID, truth string, v Viewer) (*RumourView, error)
// SetBelief records what the party thinks. Director and Scribes.
SetBelief(ctx context.Context, campaignID, entityID, rumourID, belief string, v Viewer) (*RumourView, error)
// Remove deletes a rumour. Director, or the Scribe who wrote it.
Remove(ctx context.Context, campaignID, entityID, rumourID string, v Viewer) error
}
Service is the Rumours block's business logic. Every method takes the viewer and decides here what that viewer may be told and may do; handlers, the block and the script add no rule of their own.
The rules, in one place:
- Everyone who can open the page reads its rumours and what the party thinks. Only the Director is ever sent what is true.
- The Director and Scribes add rumours and set what the party thinks.
- Only the Director changes what is true.
- The Director edits or removes any rumour; a Scribe, only their own.
type UpdateRumourInput ¶
type UpdateRumourInput struct {
Quote patch.Field[string]
Who patch.Field[string]
Sure patch.Field[int]
}
UpdateRumourInput edits what was said, who said it or how sure they were. Each field is a patch.Field so a body naming one of them leaves the others as they are. All three are required columns, so an explicit null preserves. Which rumour, on which page, and who is asking travel beside it.
type View ¶
type View struct {
// Hidden means the block draws nothing for this viewer: a player on a page
// with no rumours has nothing to read and no way to add one.
Hidden bool `json:"-"`
// IsDM is true for the Director, the only viewer sent the truth.
IsDM bool `json:"isDM"`
// CanAdd and CanMark are what this viewer may do; the server re-checks
// both on every write.
CanAdd bool `json:"canAdd"`
CanMark bool `json:"canMark"`
// Total is how many rumours the page holds. When it exceeds the length of
// Rumours the read was cut off, and the block says so.
Total int `json:"total"`
Rumours []RumourView `json:"rumours"`
}
View is everything the block shows one viewer.
type Viewer ¶
Viewer is who is asking. Role is the campaign's visibility role (CampaignContext.VisibilityRole), so a granted co-Director counts as the Director; the service never compares it against anything else.
func ViewerOf ¶
func ViewerOf(cc *campaigns.CampaignContext, userID string) Viewer
ViewerOf is who is asking, from the campaign context. VisibilityRole is used as on the page itself, so a granted co-Director sees and may do what the owner does.