Make the box canvas reusable: canvas pages, admin-only boxes, landing notes
The /stp canvas is now BoxCanvas.vue, keyed by a page. Boxes carry a
page ("stp", "index" for the landing page's notes overlay, or a Page
slug) and an adminOnly flag that the boxes resolver filters server-side.
A new Page type lets the admin create canvas pages in the browser,
served at /<slug> by the router's catch-all (CanvasPage.vue), starting
from a Blank, Shrine or Notes-board layout. Slugs that an SPA or nginx
route would answer first are refused. Renaming a page moves its boxes;
deleting it deletes them.
The GTO, Evangelion and Skipskipbenben placeholder routes are dropped
in favour of canvas pages listed on /shrines, which the landing page
now links to. The box editor gains an admin-only toggle and an
upload-and-insert button, and there are Note, Heading, Text, Gallery
and Video templates.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -82,3 +82,9 @@ models:
|
||||
fields:
|
||||
deletedAt:
|
||||
resolver: false
|
||||
Page:
|
||||
model:
|
||||
- adam-french.co.uk/backend/models.Page
|
||||
fields:
|
||||
deletedAt:
|
||||
resolver: false
|
||||
|
||||
@@ -18,6 +18,7 @@ func (r *mutationResolver) CreateBox(ctx context.Context, input model.CreateBoxI
|
||||
// comment on models.Box.Framed. Sizes not supplied fall back to the
|
||||
// same 300x200 the column defaults use.
|
||||
box := models.Box{
|
||||
Page: input.Page,
|
||||
Kind: input.Kind,
|
||||
Widget: input.Widget,
|
||||
HTML: input.HTML,
|
||||
@@ -37,6 +38,9 @@ func (r *mutationResolver) CreateBox(ctx context.Context, input model.CreateBoxI
|
||||
if input.HideOnMobile != nil {
|
||||
box.HideOnMobile = *input.HideOnMobile
|
||||
}
|
||||
if input.AdminOnly != nil {
|
||||
box.AdminOnly = *input.AdminOnly
|
||||
}
|
||||
if err := validateBox(&box); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -76,6 +80,9 @@ func (r *mutationResolver) UpdateBox(ctx context.Context, id int, input model.Up
|
||||
if input.HideOnMobile != nil {
|
||||
box.HideOnMobile = *input.HideOnMobile
|
||||
}
|
||||
if input.AdminOnly != nil {
|
||||
box.AdminOnly = *input.AdminOnly
|
||||
}
|
||||
if err := validateBox(&box); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -98,13 +105,27 @@ func (r *mutationResolver) DeleteBox(ctx context.Context, id int) (*models.Box,
|
||||
}
|
||||
|
||||
// Boxes is the resolver for the boxes field.
|
||||
func (r *queryResolver) Boxes(ctx context.Context) ([]*models.Box, error) {
|
||||
func (r *queryResolver) Boxes(ctx context.Context, page string) ([]*models.Box, error) {
|
||||
// Public: this is the page layout every visitor loads. Ordered by z so
|
||||
// DOM order matches stacking order, which keeps tab order sensible.
|
||||
//
|
||||
// What is admin-only is filtered here rather than hidden in the browser,
|
||||
// so a private note never reaches anyone else: an adminOnly page answers
|
||||
// with no boxes at all, and adminOnly boxes are left out of the rest.
|
||||
hidden, err := pageHiddenFrom(ctx, r.Store.DB, page)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if hidden {
|
||||
return []*models.Box{}, nil
|
||||
}
|
||||
query := r.Store.DB.Where("page = ?", page)
|
||||
if !IsAdminFromCtx(ctx) {
|
||||
query = query.Where("admin_only = ?", false)
|
||||
}
|
||||
var boxes []models.Box
|
||||
if err := r.Store.DB.Order("z ASC, id ASC").Find(&boxes).Error; err != nil {
|
||||
if err := query.Order("z ASC, id ASC").Find(&boxes).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ptrs(boxes), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@ func setInt(dst *int, src *int) {
|
||||
// rather than rejecting it, because a drag that overshoots the canvas edge
|
||||
// should land at the edge, not fail.
|
||||
func validateBox(box *models.Box) error {
|
||||
if !validPageKey(box.Page) {
|
||||
return errors.New("a box needs a valid page")
|
||||
}
|
||||
switch box.Kind {
|
||||
case "widget":
|
||||
if box.Widget == nil || *box.Widget == "" {
|
||||
|
||||
@@ -14,13 +14,15 @@ func TestValidateBox(t *testing.T) {
|
||||
box models.Box
|
||||
wantErr bool
|
||||
}{
|
||||
{"widget with a name", models.Box{Kind: "widget", Widget: str("Chat")}, false},
|
||||
{"widget without a name", models.Box{Kind: "widget"}, true},
|
||||
{"widget with an empty name", models.Box{Kind: "widget", Widget: str("")}, true},
|
||||
{"html with html", models.Box{Kind: "html", HTML: str("<p>hi</p>")}, false},
|
||||
{"html with empty html", models.Box{Kind: "html", HTML: str("")}, false},
|
||||
{"html without html", models.Box{Kind: "html"}, true},
|
||||
{"unknown kind", models.Box{Kind: "iframe", HTML: str("x")}, true},
|
||||
{"widget with a name", models.Box{Page: "stp", Kind: "widget", Widget: str("Chat")}, false},
|
||||
{"widget without a name", models.Box{Page: "stp", Kind: "widget"}, true},
|
||||
{"widget with an empty name", models.Box{Page: "stp", Kind: "widget", Widget: str("")}, true},
|
||||
{"html with html", models.Box{Page: "stp", Kind: "html", HTML: str("<p>hi</p>")}, false},
|
||||
{"html with empty html", models.Box{Page: "stp", Kind: "html", HTML: str("")}, false},
|
||||
{"html without html", models.Box{Page: "stp", Kind: "html"}, true},
|
||||
{"no page", models.Box{Kind: "widget", Widget: str("Chat")}, true},
|
||||
{"bad page", models.Box{Page: "Shrines/GTO", Kind: "widget", Widget: str("Chat")}, true},
|
||||
{"unknown kind", models.Box{Page: "stp", Kind: "iframe", HTML: str("x")}, true},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
err := validateBox(&c.box)
|
||||
@@ -32,7 +34,7 @@ func TestValidateBox(t *testing.T) {
|
||||
|
||||
// Geometry is clamped, not rejected: a drag past the canvas edge lands
|
||||
// at the edge, and a box can never shrink below a grabbable size.
|
||||
box := models.Box{Kind: "widget", Widget: str("Chat"), X: -30, Y: -1, W: 5, H: 0}
|
||||
box := models.Box{Page: "stp", Kind: "widget", Widget: str("Chat"), X: -30, Y: -1, W: 5, H: 0}
|
||||
if err := validateBox(&box); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
+969
-8
File diff suppressed because it is too large
Load Diff
@@ -25,6 +25,7 @@ type CreateBookmarkInput struct {
|
||||
}
|
||||
|
||||
type CreateBoxInput struct {
|
||||
Page string `json:"page"`
|
||||
Kind string `json:"kind"`
|
||||
Widget *string `json:"widget,omitempty"`
|
||||
HTML *string `json:"html,omitempty"`
|
||||
@@ -36,6 +37,7 @@ type CreateBoxInput struct {
|
||||
Z *int `json:"z,omitempty"`
|
||||
Framed *bool `json:"framed,omitempty"`
|
||||
HideOnMobile *bool `json:"hideOnMobile,omitempty"`
|
||||
AdminOnly *bool `json:"adminOnly,omitempty"`
|
||||
}
|
||||
|
||||
type CreateFavoriteInput struct {
|
||||
@@ -44,6 +46,12 @@ type CreateFavoriteInput struct {
|
||||
Link *string `json:"link,omitempty"`
|
||||
}
|
||||
|
||||
type CreatePageInput struct {
|
||||
Slug string `json:"slug"`
|
||||
Title string `json:"title"`
|
||||
AdminOnly *bool `json:"adminOnly,omitempty"`
|
||||
}
|
||||
|
||||
type CreatePlaceInput struct {
|
||||
Title string `json:"title"`
|
||||
Location *string `json:"location,omitempty"`
|
||||
@@ -138,6 +146,13 @@ type UpdateBoxInput struct {
|
||||
Z *int `json:"z,omitempty"`
|
||||
Framed *bool `json:"framed,omitempty"`
|
||||
HideOnMobile *bool `json:"hideOnMobile,omitempty"`
|
||||
AdminOnly *bool `json:"adminOnly,omitempty"`
|
||||
}
|
||||
|
||||
type UpdatePageInput struct {
|
||||
Slug *string `json:"slug,omitempty"`
|
||||
Title *string `json:"title,omitempty"`
|
||||
AdminOnly *bool `json:"adminOnly,omitempty"`
|
||||
}
|
||||
|
||||
type UpdatePlaceInput struct {
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
package graph
|
||||
|
||||
// This file will be automatically regenerated based on the schema, any resolver
|
||||
// implementations
|
||||
// will be copied through when generating and any unknown code will be moved to the end.
|
||||
// Code generated by github.com/99designs/gqlgen version v0.17.95
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"adam-french.co.uk/backend/graph/model"
|
||||
"adam-french.co.uk/backend/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreatePage is the resolver for the createPage field.
|
||||
func (r *mutationResolver) CreatePage(ctx context.Context, input model.CreatePageInput) (*models.Page, error) {
|
||||
page := models.Page{
|
||||
Slug: input.Slug,
|
||||
Title: strings.TrimSpace(input.Title),
|
||||
}
|
||||
if input.AdminOnly != nil {
|
||||
page.AdminOnly = *input.AdminOnly
|
||||
}
|
||||
if err := validateSlug(page.Slug); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if page.Title == "" {
|
||||
return nil, errors.New("a page needs a title")
|
||||
}
|
||||
taken, err := slugTaken(r.Store.DB, page.Slug, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if taken {
|
||||
return nil, errors.New("there is already a page at that address")
|
||||
}
|
||||
if err := r.Store.DB.Create(&page).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &page, nil
|
||||
}
|
||||
|
||||
// UpdatePage is the resolver for the updatePage field.
|
||||
func (r *mutationResolver) UpdatePage(ctx context.Context, id int, input model.UpdatePageInput) (*models.Page, error) {
|
||||
// Partial update, like updateBox. A new slug moves the page's boxes with
|
||||
// it in the same transaction, since boxes find their page by slug.
|
||||
var page models.Page
|
||||
if err := r.Store.DB.First(&page, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
oldSlug := page.Slug
|
||||
if input.Slug != nil {
|
||||
page.Slug = *input.Slug
|
||||
}
|
||||
if input.Title != nil {
|
||||
page.Title = strings.TrimSpace(*input.Title)
|
||||
}
|
||||
if input.AdminOnly != nil {
|
||||
page.AdminOnly = *input.AdminOnly
|
||||
}
|
||||
if page.Title == "" {
|
||||
return nil, errors.New("a page needs a title")
|
||||
}
|
||||
if page.Slug != oldSlug {
|
||||
if err := validateSlug(page.Slug); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
taken, err := slugTaken(r.Store.DB, page.Slug, page.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if taken {
|
||||
return nil, errors.New("there is already a page at that address")
|
||||
}
|
||||
}
|
||||
err := r.Store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if page.Slug != oldSlug {
|
||||
if err := tx.Model(&models.Box{}).Where("page = ?", oldSlug).Update("page", page.Slug).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Save(&page).Error
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &page, nil
|
||||
}
|
||||
|
||||
// DeletePage is the resolver for the deletePage field.
|
||||
func (r *mutationResolver) DeletePage(ctx context.Context, id int) (*models.Page, error) {
|
||||
// The boxes go too: left behind, they would reappear on any page later
|
||||
// created at the same address.
|
||||
var page models.Page
|
||||
if err := r.Store.DB.First(&page, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err := r.Store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("page = ?", page.Slug).Delete(&models.Box{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(&page).Error
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &page, nil
|
||||
}
|
||||
|
||||
// Pages is the resolver for the pages field.
|
||||
func (r *queryResolver) Pages(ctx context.Context) ([]*models.Page, error) {
|
||||
// Public, for page indexes such as /shrines; adminOnly pages are only
|
||||
// listed for an admin.
|
||||
query := r.Store.DB.Order("slug ASC")
|
||||
if !IsAdminFromCtx(ctx) {
|
||||
query = query.Where("admin_only = ?", false)
|
||||
}
|
||||
var pages []models.Page
|
||||
if err := query.Find(&pages).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ptrs(pages), nil
|
||||
}
|
||||
|
||||
// Page is the resolver for the page field.
|
||||
func (r *queryResolver) Page(ctx context.Context, slug string) (*models.Page, error) {
|
||||
// Null, not an error, for a missing page: the SPA's catch-all route asks
|
||||
// this for every unknown path and shows its 404 on null. An adminOnly
|
||||
// page reads as missing to everyone else, so its existence isn't leaked.
|
||||
var page models.Page
|
||||
err := r.Store.DB.Where("slug = ?", slug).First(&page).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if page.AdminOnly && !IsAdminFromCtx(ctx) {
|
||||
return nil, nil
|
||||
}
|
||||
return &page, nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package graph
|
||||
|
||||
// Hand-written helpers for page.resolvers.go and box.resolvers.go, kept out
|
||||
// of the resolver files for the same reason as box_helpers.go.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"adam-french.co.uk/backend/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// pageKeyPattern is the shape of a box's page key and of a page slug: lower
|
||||
// case words joined by hyphens, in one or more /-separated segments. It is
|
||||
// URL-safe as it stands, so the front end can use a slug as a path without
|
||||
// escaping it.
|
||||
var pageKeyPattern = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*(/[a-z0-9]+(-[a-z0-9]+)*)*$`)
|
||||
|
||||
const maxSlugLen = 100
|
||||
|
||||
// reservedFirstSegments are paths a page could never be reached at, because
|
||||
// nginx or one of the SPA's own routes answers them first (see
|
||||
// nginx/nginx_routes.conf.template and vue/src/router/index.js), plus the
|
||||
// built-in canvases' page keys, whose boxes a page of the same slug would
|
||||
// share. "shrines" is deliberately absent: shrine pages live under it.
|
||||
var reservedFirstSegments = map[string]bool{
|
||||
"stp": true, "stp2": true, "index": true, "admin": true, "cv": true,
|
||||
"automata": true, "hotels": true, "api": true, "py": true, "radio": true,
|
||||
"gitea": true, "notes": true, "sb": true, "jobs": true, "tracker": true,
|
||||
"usaco": true, "uploads": true, "img": true, "fonts": true, "pdf": true,
|
||||
"sound": true, "assets": true, "internal": true,
|
||||
}
|
||||
|
||||
// reservedSlugs are individual paths the SPA routes itself.
|
||||
var reservedSlugs = map[string]bool{
|
||||
"shrines": true,
|
||||
"shrines/demoman": true,
|
||||
}
|
||||
|
||||
func validPageKey(key string) bool {
|
||||
return len(key) <= maxSlugLen && pageKeyPattern.MatchString(key)
|
||||
}
|
||||
|
||||
// validateSlug reports why slug can't be a page's address, or nil.
|
||||
func validateSlug(slug string) error {
|
||||
if !validPageKey(slug) {
|
||||
return errors.New("a page address is lower-case letters, digits and hyphens, in /-separated parts, e.g. shrines/evangelion")
|
||||
}
|
||||
first, _, _ := strings.Cut(slug, "/")
|
||||
if reservedFirstSegments[first] || reservedSlugs[slug] {
|
||||
return errors.New("that address is already used by the site")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// pageHiddenFrom reports whether the page with this key is an adminOnly page
|
||||
// and the caller is not an admin. A key with no Page row (a built-in canvas,
|
||||
// or a page that doesn't exist) is not hidden.
|
||||
func pageHiddenFrom(ctx context.Context, db *gorm.DB, key string) (bool, error) {
|
||||
if IsAdminFromCtx(ctx) {
|
||||
return false, nil
|
||||
}
|
||||
var count int64
|
||||
err := db.Model(&models.Page{}).Where("slug = ? AND admin_only = ?", key, true).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// slugTaken reports whether a live page other than exceptID has this slug.
|
||||
// Checked up front so the admin gets a sentence rather than a constraint
|
||||
// violation; the partial unique index still backs it up.
|
||||
func slugTaken(db *gorm.DB, slug string, exceptID uint) (bool, error) {
|
||||
var count int64
|
||||
err := db.Model(&models.Page{}).Where("slug = ? AND id <> ?", slug, exceptID).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package graph
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateSlug(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
slug string
|
||||
ok bool
|
||||
}{
|
||||
{"notes-to-self", true},
|
||||
{"shrines/evangelion", true},
|
||||
{"shrines/neon-genesis/rei2", true},
|
||||
{"", false},
|
||||
{"Shrines/GTO", false}, // upper case
|
||||
{"shrines/", false}, // empty segment
|
||||
{"/shrines/gto", false}, // leading slash
|
||||
{"shrines//gto", false}, // empty segment
|
||||
{"shrines/gto-", false}, // trailing hyphen
|
||||
{"shrines/../admin", false}, // dots
|
||||
{"shrines", false}, // the SPA's own index
|
||||
{"shrines/demoman", false}, // an existing SPA route
|
||||
{"admin/pages", false}, // under an SPA route
|
||||
{"jobs/x", false}, // under an nginx route
|
||||
{"stp", false}, // a built-in canvas's key
|
||||
{"index", false}, // likewise
|
||||
{"stp-fan-page", true}, // only whole segments are reserved
|
||||
} {
|
||||
err := validateSlug(c.slug)
|
||||
if (err == nil) != c.ok {
|
||||
t.Errorf("validateSlug(%q) = %v, want ok=%v", c.slug, err, c.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
# Boxes on the home page canvas (/stp). Anyone may read the layout — it is
|
||||
# the page — but only an admin may change it. x/y/w/h are px on a fixed
|
||||
# 1400px-wide design canvas; see models.Box.
|
||||
# Boxes on a canvas page: the home page (/stp, page "stp"), the landing
|
||||
# page's overlay (page "index") or an admin-created Page (page = its slug).
|
||||
# Anyone may read a page's layout — it is the page — but only an admin may
|
||||
# change it, and adminOnly boxes are only ever returned to an admin. x/y/w/h
|
||||
# are px on a fixed 1400px-wide design canvas; see models.Box.
|
||||
#
|
||||
# kind is "widget" (a built-in Vue component named by `widget`) or "html"
|
||||
# (`html` rendered in a sandboxed iframe on the front end).
|
||||
@@ -9,6 +11,7 @@ type Box {
|
||||
id: ID!
|
||||
createdAt: Time!
|
||||
updatedAt: Time!
|
||||
page: String!
|
||||
kind: String!
|
||||
widget: String
|
||||
html: String
|
||||
@@ -20,9 +23,11 @@ type Box {
|
||||
z: Int!
|
||||
framed: Boolean!
|
||||
hideOnMobile: Boolean!
|
||||
adminOnly: Boolean!
|
||||
}
|
||||
|
||||
input CreateBoxInput {
|
||||
page: String!
|
||||
kind: String!
|
||||
widget: String
|
||||
html: String
|
||||
@@ -34,6 +39,7 @@ input CreateBoxInput {
|
||||
z: Int
|
||||
framed: Boolean
|
||||
hideOnMobile: Boolean
|
||||
adminOnly: Boolean
|
||||
}
|
||||
|
||||
input UpdateBoxInput {
|
||||
@@ -47,10 +53,11 @@ input UpdateBoxInput {
|
||||
z: Int
|
||||
framed: Boolean
|
||||
hideOnMobile: Boolean
|
||||
adminOnly: Boolean
|
||||
}
|
||||
|
||||
extend type Query {
|
||||
boxes: [Box!]!
|
||||
boxes(page: String!): [Box!]!
|
||||
}
|
||||
|
||||
extend type Mutation {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# Admin-created canvas pages. A page is just a slug and a title; its content
|
||||
# is the boxes whose `page` is the slug (see box.graphql). The SPA serves it
|
||||
# at /<slug> through its catch-all route, so a slug may not start with a path
|
||||
# the site already uses — see validateSlug in page_helpers.go.
|
||||
#
|
||||
# adminOnly pages, and their boxes, are invisible to everyone but an admin:
|
||||
# `page` answers null and `pages` leaves them out.
|
||||
|
||||
type Page {
|
||||
id: ID!
|
||||
createdAt: Time!
|
||||
updatedAt: Time!
|
||||
slug: String!
|
||||
title: String!
|
||||
adminOnly: Boolean!
|
||||
}
|
||||
|
||||
input CreatePageInput {
|
||||
slug: String!
|
||||
title: String!
|
||||
adminOnly: Boolean
|
||||
}
|
||||
|
||||
input UpdatePageInput {
|
||||
slug: String
|
||||
title: String
|
||||
adminOnly: Boolean
|
||||
}
|
||||
|
||||
extend type Query {
|
||||
pages: [Page!]!
|
||||
page(slug: String!): Page
|
||||
}
|
||||
|
||||
extend type Mutation {
|
||||
createPage(input: CreatePageInput!): Page! @admin
|
||||
# Renaming a page moves its boxes with it.
|
||||
updatePage(id: ID!, input: UpdatePageInput!): Page! @admin
|
||||
# Deletes the page's boxes too.
|
||||
deletePage(id: ID!): Page! @admin
|
||||
}
|
||||
+42
-19
@@ -156,29 +156,52 @@ type Place struct {
|
||||
Done bool `gorm:"default:false" json:"done"`
|
||||
}
|
||||
|
||||
// Box is one panel on the home page canvas (/stp). 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;
|
||||
// Box is one panel on a canvas page. Page names the canvas it belongs to:
|
||||
// "stp" for the home page, "index" for the landing page's overlay, or a
|
||||
// Page.Slug for an admin-created page — it is a key, not a foreign key, so the
|
||||
// built-in canvases need 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.
|
||||
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"`
|
||||
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"`
|
||||
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.
|
||||
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 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.
|
||||
Framed bool `gorm:"not null" json:"framed"`
|
||||
HideOnMobile bool `gorm:"not null;default:false" json:"hideOnMobile"`
|
||||
Framed bool `gorm:"not null" json:"framed"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// 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.
|
||||
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 hides the whole page, boxes included, from everyone else.
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ func migrateDatabase(db *gorm.DB) error {
|
||||
&models.Bookmark{},
|
||||
&models.Place{},
|
||||
&models.Box{},
|
||||
&models.Page{},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user