Files
stpweb/backend/models/models.go
adamfandClaude Opus 5.5 4530cb8dc4
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s
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]>
2026-11-24 20:49:03 +00:00

228 lines
9.9 KiB
Go

package models
import (
"time"
"gorm.io/gorm"
)
// 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, 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; gqlgen.yml hides it from GraphQL too.
Password []byte `json:"-"`
// Copied into the access token, so changes apply on the next refresh.
Admin bool `json:"admin"`
// 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"`
// 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 nil unless preloaded.
type Post struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Title string `gorm:"not null" json:"title"`
AuthorID uint `json:"-"`
Author *User `gorm:"foreignKey:AuthorID" json:"author"`
Content string `json:"content"`
}
// 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"`
// 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.
type Activity struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Type string `json:"type"`
Name string `json:"name"`
Link *string `json:"link"`
}
// Favorite is a "currently enjoying" entry on the home page.
type Favorite struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Type string `json:"type"`
Name string `json:"name"`
Link *string `json:"link"`
}
// 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"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Date time.Time `json:"date"`
Time uint64 `json:"time"`
Distance uint64 `json:"distance"`
TimePer500m float64 `json:"timePer500m"`
Calories float64 `json:"calories"`
}
// Bookmark is a link on the bookmarks page, grouped by Category.
type Bookmark struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Category string `gorm:"not null" json:"category"`
Name string `gorm:"not null" json:"name"`
Link string `gorm:"not null" json:"link"`
}
// 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"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Title string `gorm:"not null" json:"title"`
Location *string `json:"location"`
Notes *string `json:"notes"`
ImageURL *string `json:"imageUrl"`
Category *string `json:"category"`
Cost *string `json:"cost"`
Priority int `gorm:"default:0" json:"priority"`
Done bool `gorm:"default:false" json:"done"`
}
// 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"`
// 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"`
HTML *string `json:"html"`
Title *string `json:"title"`
X int `gorm:"not null;default:0" json:"x"`
Y int `gorm:"not null;default:0" json:"y"`
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: 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"`
// A CSS box-shadow, or "".
Shadow string `gorm:"not null;default:''" json:"shadow"`
HideOnMobile bool `gorm:"not null;default:false" json:"hideOnMobile"`
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 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"`
UpdatedAt time.Time `json:"updatedAt"`
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 bool `gorm:"not null;default:false" json:"adminOnly"`
}
// 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"`
// "" means the theme colour.
Color string `gorm:"not null;default:''" json:"color"`
Image string `gorm:"not null;default:''" json:"image"`
// "cover", "contain" or "tile".
Fit string `gorm:"not null;default:cover" json:"fit"`
}
// 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 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. 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"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Name string `gorm:"not null;index:idx_components_name,unique,where:deleted_at IS NULL" json:"name"`
Title *string `json:"title"`
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, 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; Body is JSON in views/CV/coverLetter.js's shape.
type CoverLetter 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_cover_letters_name,unique,where:deleted_at IS NULL" json:"name"`
Body string `gorm:"type:text;not null" json:"body"`
}
// 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"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
Text string `gorm:"type:text;not null" json:"text"`
Section string `gorm:"not null;default:''" json:"section"`
}