Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+50
-139
@@ -6,64 +6,29 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Package models holds the GORM entities, which are also the GraphQL object
|
||||
// types: gqlgen.yml maps each GraphQL type straight onto the struct here, so
|
||||
// graph/model/ only contains generated input and payload types. That means
|
||||
// these structs carry three sets of contracts at once — database columns,
|
||||
// JSON field names for the REST endpoints, and the shape resolvers return —
|
||||
// and renaming a field or changing a tag breaks at least one of them.
|
||||
//
|
||||
// Conventions across every model:
|
||||
// - There are no migration files; services.migrateDatabase runs AutoMigrate,
|
||||
// so a model must be listed there to get a table.
|
||||
// - A gorm.DeletedAt field means soft delete: Delete sets deleted_at, and
|
||||
// every subsequent query silently filters those rows out. Rows are never
|
||||
// actually removed, and a uniqueIndex still sees deleted rows, so a
|
||||
// soft-deleted username cannot be reused.
|
||||
// - IDs are uint here and the ID scalar in GraphQL. gqlgen.yml binds the
|
||||
// two directly (graphql.UintID), so an `ID uint` field needs no resolver.
|
||||
// On the wire an id is a quoted decimal string, "12".
|
||||
// These structs are also the GraphQL object types (gqlgen.yml), so a field or
|
||||
// tag change affects the DB, REST JSON and GraphQL at once.
|
||||
|
||||
// User is an account. There is no public sign-up: users are created by an
|
||||
// existing admin through the createUser mutation, or by the dev seeder.
|
||||
// User is an account, created only by an admin or the dev seeder.
|
||||
type User struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
Username string `gorm:"uniqueIndex" json:"username"`
|
||||
// bcrypt hash, never the plaintext. `json:"-"` keeps it out of REST
|
||||
// responses, and gqlgen.yml marks the GraphQL password field
|
||||
// resolver:false so it is not exposed there either.
|
||||
// bcrypt hash; gqlgen.yml hides it from GraphQL too.
|
||||
Password []byte `json:"-"`
|
||||
// Admin gates almost every mutation. It is copied into the access token,
|
||||
// so a change here only takes effect on the user's next token refresh.
|
||||
// Copied into the access token, so changes apply on the next refresh.
|
||||
Admin bool `json:"admin"`
|
||||
// Permissions names the gated services this non-admin user may use, one
|
||||
// key each from services.PermissionTypes (admins pass every check
|
||||
// regardless). Unlike Admin it is never copied into a token: the gate
|
||||
// reads it from the row, which services.Auth loads on every request
|
||||
// anyway, so a grant or revoke takes effect on the user's next request.
|
||||
// Stored as a JSON array in one text column; a row from before the
|
||||
// column existed reads as NULL, i.e. no permissions.
|
||||
// Keys from services.PermissionTypes. Read from the row on each request,
|
||||
// not the token, so grants apply immediately.
|
||||
Permissions []string `gorm:"serializer:json" json:"permissions"`
|
||||
// TokenVersion is the session generation counter. Every issued JWT
|
||||
// carries the value current at the time it was minted, and
|
||||
// services.Auth rejects a token whose value no longer matches, so
|
||||
// incrementing this column revokes every outstanding token for the user
|
||||
// at once (logout, password change). It is the only server-side
|
||||
// revocation mechanism there is.
|
||||
//
|
||||
// Pre-migration rows: AutoMigrate adds the column with DEFAULT 1, but a
|
||||
// row that somehow ends up at 0 is normalised to 1 by
|
||||
// services.tokenVersionOf, as is a token minted before the "tv" claim
|
||||
// existed. So the migration invalidates nobody's existing session, and
|
||||
// the first bump moves a user to 2 and invalidates everything older.
|
||||
// Every JWT carries this; bumping it revokes all of the user's tokens.
|
||||
// 0 and a missing claim are both treated as 1 (services.tokenVersionOf).
|
||||
TokenVersion uint `gorm:"not null;default:1" json:"-"`
|
||||
}
|
||||
|
||||
// Post is a blog entry. Author is loaded with Preload("Author") by the post
|
||||
// resolvers; without that preload it is nil rather than an error.
|
||||
// Post is a blog entry. Author is nil unless preloaded.
|
||||
type Post struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -75,27 +40,20 @@ type Post struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// Message is one chat message. The table is capped at 50 rows by the
|
||||
// WebSocket hub, which soft-deletes older ones on every insert.
|
||||
//
|
||||
// Two things to watch: Content is exposed as "text", not "content"; and
|
||||
// AuthorID is a per-process connection counter from the WebSocket hub, NOT a
|
||||
// User ID — chat is pseudonymous and the number is meaningless across
|
||||
// restarts.
|
||||
// Message is a chat message; the WebSocket hub keeps only the latest 50.
|
||||
// AuthorID is the hub's per-process connection number, not a User ID.
|
||||
type Message struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
Content string `json:"text"`
|
||||
AuthorID uint `json:"authorId"`
|
||||
FileURL string `json:"fileUrl,omitempty"`
|
||||
// Private messages are visible only to admins; both the WebSocket
|
||||
// broadcast and the messages query filter on it.
|
||||
// Visible only to admins.
|
||||
Private bool `gorm:"not null;default:false" json:"private"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
}
|
||||
|
||||
// Activity is a "currently doing" entry on the home page. Structurally
|
||||
// identical to Favorite but kept separate so the two lists can diverge.
|
||||
// Activity is a "currently doing" entry on the home page.
|
||||
type Activity struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -117,13 +75,9 @@ type Favorite struct {
|
||||
Link *string `json:"link"`
|
||||
}
|
||||
|
||||
// Rowing is one rowing machine session, created by uploading a photo of the
|
||||
// display (see handlers.CreateRowing).
|
||||
//
|
||||
// Date is the photo's EXIF capture time and acts as the deduplication key —
|
||||
// it is not the row's creation time. Time is total seconds; Distance is
|
||||
// metres; TimePer500m is seconds and derived from the other two; Calories is
|
||||
// an estimate computed from distance, not read off the machine.
|
||||
// Rowing is one rowing machine session read from a photo of the display.
|
||||
// Date is the photo's EXIF time and the dedup key. Time is seconds, Distance
|
||||
// metres; Calories is estimated from distance.
|
||||
type Rowing struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -146,9 +100,7 @@ type Bookmark struct {
|
||||
Link string `gorm:"not null" json:"link"`
|
||||
}
|
||||
|
||||
// Place is an entry on the places-to-go list. Creating or editing one needs
|
||||
// only a signed-in user, not an admin — the one exception to the
|
||||
// admin-writes-everything rule.
|
||||
// Place is a places-to-go entry; any signed-in user may edit it.
|
||||
type Place struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -164,20 +116,15 @@ type Place struct {
|
||||
Done bool `gorm:"default:false" json:"done"`
|
||||
}
|
||||
|
||||
// Box is one panel on a canvas page. Page names the canvas it belongs to:
|
||||
// "index" for the landing page, or a Page.Slug for an admin-created page
|
||||
// (the home page is the Page "stp") — it is a key, not a foreign key, so the
|
||||
// built-in landing canvas needs no Page row. Its position and size are in px on a
|
||||
// fixed 1400px-wide design canvas, which the page scales down on narrower
|
||||
// screens — so the stored numbers mean the same thing for every visitor. Kind "widget" renders a built-in Vue component named by Widget;
|
||||
// kind "html" renders HTML in a sandboxed iframe on the front end, which is
|
||||
// what makes storing admin-authored script here acceptable.
|
||||
// Box is one panel on a canvas. Page is a page key ("index" or a Page.Slug),
|
||||
// not a foreign key. Geometry is px on a fixed 1400px-wide canvas. "html"
|
||||
// boxes render in a sandboxed iframe.
|
||||
type Box struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
// default:stp is what the boxes that predate pages are migrated to.
|
||||
// Boxes from before pages existed migrate to "stp".
|
||||
Page string `gorm:"not null;default:stp;index" json:"page"`
|
||||
Kind string `gorm:"not null" json:"kind"`
|
||||
Widget *string `json:"widget"`
|
||||
@@ -188,30 +135,20 @@ type Box struct {
|
||||
W int `gorm:"not null;default:300" json:"w"`
|
||||
H int `gorm:"not null;default:200" json:"h"`
|
||||
Z int `gorm:"not null;default:0" json:"z"`
|
||||
// No gorm default on Framed: GORM omits zero values for defaulted columns
|
||||
// on insert, so default:true would turn an explicit false into true.
|
||||
// CreateBox applies the true default itself.
|
||||
// No gorm default: GORM skips zero values on insert, so default:true would
|
||||
// turn an explicit false into true. CreateBox defaults it.
|
||||
Framed bool `gorm:"not null" json:"framed"`
|
||||
// Shadow is a CSS box-shadow value drawn around the box, or "" for none.
|
||||
// It is independent of Framed, so an unframed box can still cast one.
|
||||
// A CSS box-shadow, or "".
|
||||
Shadow string `gorm:"not null;default:''" json:"shadow"`
|
||||
HideOnMobile bool `gorm:"not null;default:false" json:"hideOnMobile"`
|
||||
// AdminOnly boxes are never sent to anyone but an admin (the boxes
|
||||
// resolver filters them out), so they suit private notes.
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
// ComponentID links an html box to a Component, whose HTML it shows
|
||||
// instead of its own: the row's HTML is nil, and the resolvers fill it in
|
||||
// from the component before answering (graph/component_helpers.go). There
|
||||
// is deliberately no association field — with one, DB.Save(&box) would
|
||||
// write the component back too.
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
// The box shows the component's HTML (its own is nil). No association
|
||||
// field, or DB.Save(&box) would write the component back too.
|
||||
ComponentID *uint `gorm:"index" json:"componentId"`
|
||||
}
|
||||
|
||||
// Page is an admin-created canvas page, served by the SPA's catch-all route
|
||||
// at /<Slug> (e.g. "shrines/evangelion"). Its content is the Boxes whose Page
|
||||
// equals Slug. The unique index is partial so that a deleted page's slug can
|
||||
// be reused — a plain uniqueIndex would still collide with the soft-deleted
|
||||
// row.
|
||||
// Page is an admin-created canvas page at /<Slug>. The unique index is
|
||||
// partial so a soft-deleted slug can be reused.
|
||||
type Page struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -219,60 +156,40 @@ type Page struct {
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
Slug string `gorm:"not null;index:idx_pages_slug,unique,where:deleted_at IS NULL" json:"slug"`
|
||||
Title string `gorm:"not null" json:"title"`
|
||||
// AdminOnly hides the whole page, boxes included, from everyone else.
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
}
|
||||
|
||||
// Background is the page background behind one canvas (Page is the same page
|
||||
// key a Box has: "index" for the landing page, or a Page's slug), so
|
||||
// built-in canvases and admin-created pages are set the same way. A page with
|
||||
// no row shows the theme's halftone; deleting the row is "reset to default".
|
||||
// The unique index is partial for the same reason as Page's.
|
||||
// Background is a canvas page's background; no row means the theme default.
|
||||
type Background struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
Page string `gorm:"not null;index:idx_backgrounds_page,unique,where:deleted_at IS NULL" json:"page"`
|
||||
// Color is a CSS colour, or "" for the theme's page colour.
|
||||
// "" means the theme colour.
|
||||
Color string `gorm:"not null;default:''" json:"color"`
|
||||
// Image is a URL, or "" for none. Drawn over Color.
|
||||
Image string `gorm:"not null;default:''" json:"image"`
|
||||
// Fit is how Image fills the window: "cover", "contain" or "tile".
|
||||
// "cover", "contain" or "tile".
|
||||
Fit string `gorm:"not null;default:cover" json:"fit"`
|
||||
}
|
||||
|
||||
// Layout is a named copy of one canvas page's boxes and background, saved so
|
||||
// it can be loaded onto any page later (graph/layout_helpers.go). It is a
|
||||
// snapshot, not a link: loading it creates new boxes that belong to the
|
||||
// target page, so editing either page afterwards never touches the other or
|
||||
// the layout. The content is JSON rather than child rows because nothing
|
||||
// queries inside it — it is only ever written whole and read whole. The
|
||||
// unique index is partial for the same reason as Page's.
|
||||
// Layout is a saved snapshot of a page's boxes and background. Loading it
|
||||
// creates new boxes, so nothing stays linked. Stored as JSON because it is
|
||||
// only ever read and written whole.
|
||||
type Layout struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
Name string `gorm:"not null;index:idx_layouts_name,unique,where:deleted_at IS NULL" json:"name"`
|
||||
// SourcePage is the page key it was saved from, for the list only.
|
||||
SourcePage string `gorm:"not null" json:"sourcePage"`
|
||||
// Boxes is a JSON array of the boxes' content and geometry, without id
|
||||
// or page.
|
||||
Boxes string `gorm:"type:text;not null" json:"-"`
|
||||
// Background is the page's background as JSON, or nil if it had none.
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
Name string `gorm:"not null;index:idx_layouts_name,unique,where:deleted_at IS NULL" json:"name"`
|
||||
SourcePage string `gorm:"not null" json:"sourcePage"`
|
||||
// JSON array of box content and geometry, without id or page.
|
||||
Boxes string `gorm:"type:text;not null" json:"-"`
|
||||
Background *string `gorm:"type:text" json:"-"`
|
||||
}
|
||||
|
||||
// Component is a named HTML box that can be placed on any canvas page, either
|
||||
// as a copy (an ordinary box) or linked (a box with ComponentID set). Linked
|
||||
// boxes share only the HTML — editing it changes every one of them at once —
|
||||
// while title, geometry, frame and visibility stay each box's own, since
|
||||
// those are about where it sits on a page rather than what it is. Title, W,
|
||||
// H, Framed, Shadow and AdminOnly are the defaults a newly placed box starts
|
||||
// with.
|
||||
// Widgets are not components: a widget box is just a name, with nothing
|
||||
// worth sharing. The unique index is partial for the same reason as Page's.
|
||||
// Component is a named HTML box. Linked boxes share only its HTML; the other
|
||||
// fields are the defaults for a newly placed box.
|
||||
type Component struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -283,17 +200,13 @@ type Component struct {
|
||||
HTML string `gorm:"type:text;not null" json:"html"`
|
||||
W int `gorm:"not null" json:"w"`
|
||||
H int `gorm:"not null" json:"h"`
|
||||
// No gorm default on Framed, for the same reason as Box.Framed.
|
||||
// No gorm default, as for Box.Framed.
|
||||
Framed bool `gorm:"not null" json:"framed"`
|
||||
Shadow string `gorm:"not null;default:''" json:"shadow"`
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
}
|
||||
|
||||
// CoverLetter is a saved letter from the /cv Cover Letter tab. Body is the
|
||||
// whole letter as JSON (contact block, sections, paragraphs; the shape is
|
||||
// views/CV/coverLetter.js's) because only the frontend ever reads inside it —
|
||||
// the same reasoning as Layout.Boxes. The unique index is partial for the
|
||||
// same reason as Page's.
|
||||
// CoverLetter is a saved letter; Body is JSON in views/CV/coverLetter.js's shape.
|
||||
type CoverLetter struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -303,9 +216,7 @@ type CoverLetter struct {
|
||||
Body string `gorm:"type:text;not null" json:"body"`
|
||||
}
|
||||
|
||||
// CoverSnippet is a reusable paragraph in the cover-letter bank, dragged into
|
||||
// a letter as a copy. Section is the title of the section it was written for,
|
||||
// only used to group the bank; a snippet can be dropped into any section.
|
||||
// CoverSnippet is a reusable cover-letter paragraph; Section only groups the bank.
|
||||
type CoverSnippet struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
|
||||
Reference in New Issue
Block a user