Add per-page backgrounds to canvas pages
Test and deploy / test (push) Successful in 1m56s
Test and deploy / deploy (push) Successful in 1m0s

Each canvas page key (the landing page, /stp, shrines and other admin-made
pages) can have its own background: a colour, an image and how the image
fills the window. Admins set it from a Background button beside Edit layout,
with live preview and image upload or drop; DefaultLayout paints it behind
the whole window and falls back to the halftone.

Backed by a new Background GraphQL type keyed by page key, validated
server-side, hidden with admin-only pages, and moved or deleted along with
its page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-15 00:41:19 +01:00
co-authored by Claude Opus 5.5
parent 967b464779
commit e54aaac181
16 changed files with 1236 additions and 32 deletions
+6
View File
@@ -88,3 +88,9 @@ models:
fields:
deletedAt:
resolver: false
Background:
model:
- adam-french.co.uk/backend/models.Background
fields:
deletedAt:
resolver: false
+63
View File
@@ -0,0 +1,63 @@
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"
"adam-french.co.uk/backend/graph/model"
"adam-french.co.uk/backend/models"
"gorm.io/gorm"
)
// SetBackground is the resolver for the setBackground field.
func (r *mutationResolver) SetBackground(ctx context.Context, page string, input model.BackgroundInput) (*models.Background, error) {
// One row per page: replaced in place if there is one, so its id stays
// stable for the store on the front end.
bg, err := backgroundFrom(page, input)
if err != nil {
return nil, err
}
var existing models.Background
err = r.Store.DB.Where("page = ?", page).First(&existing).Error
switch {
case err == nil:
bg.ID, bg.CreatedAt = existing.ID, existing.CreatedAt
err = r.Store.DB.Save(&bg).Error
case errors.Is(err, gorm.ErrRecordNotFound):
err = r.Store.DB.Create(&bg).Error
}
if err != nil {
return nil, err
}
return &bg, nil
}
// ClearBackground is the resolver for the clearBackground field.
func (r *mutationResolver) ClearBackground(ctx context.Context, page string) (bool, error) {
res := r.Store.DB.Where("page = ?", page).Delete(&models.Background{})
return res.RowsAffected > 0, res.Error
}
// Background is the resolver for the background field.
func (r *queryResolver) Background(ctx context.Context, page string) (*models.Background, error) {
// Null, not an error, when the page has none: that is the default look.
// Hidden along with an adminOnly page, like its boxes.
hidden, err := pageHiddenFrom(ctx, r.Store.DB, page)
if err != nil || hidden {
return nil, err
}
var bg models.Background
err = r.Store.DB.Where("page = ?", page).First(&bg).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &bg, nil
}
+53
View File
@@ -0,0 +1,53 @@
package graph
// Hand-written helpers for background.resolvers.go, kept out of the resolver
// file for the same reason as box_helpers.go.
import (
"errors"
"regexp"
"strings"
"adam-french.co.uk/backend/graph/model"
"adam-french.co.uk/backend/models"
)
// The SPA puts these values straight into a style binding, as
// `background-color` and `url("<image>")`. Vue sets them through the CSSOM,
// so nothing can escape into other properties, but a quote or bracket in the
// image could still end the url() early — hence the narrow shapes.
var (
// A hex colour, a colour name, or one of the colour functions — named,
// so that url() and friends don't pass as a "function".
bgColorPattern = regexp.MustCompile(`^(#[0-9a-fA-F]{3,8}|[a-zA-Z]+|(rgba?|hsla?|hwb|lab|lch|oklab|oklch)\([0-9a-z.,%/ -]*\))$`)
// A site path (an upload, /img/...) or an http(s) URL, with no quotes,
// brackets, backslashes or spaces.
bgImagePattern = regexp.MustCompile(`^(/|https?://)[^\s"'()\\<>]*$`)
)
const maxBgImageLen = 1000
var bgFits = map[string]bool{"cover": true, "contain": true, "tile": true}
// backgroundFrom validates input into a Background for page.
func backgroundFrom(page string, input model.BackgroundInput) (models.Background, error) {
bg := models.Background{
Page: page,
Color: strings.TrimSpace(input.Color),
Image: strings.TrimSpace(input.Image),
Fit: input.Fit,
}
if !validPageKey(page) {
return bg, errors.New("a background needs a valid page")
}
if bg.Color != "" && (len(bg.Color) > 64 || !bgColorPattern.MatchString(bg.Color)) {
return bg, errors.New(`colour must be a CSS colour such as #1b110e, black or rgb(0 0 0 / 50%)`)
}
if bg.Image != "" && (len(bg.Image) > maxBgImageLen || !bgImagePattern.MatchString(bg.Image)) {
return bg, errors.New("image must be a path on this site (e.g. /uploads/...) or an http(s) URL, without spaces or quotes")
}
if !bgFits[bg.Fit] {
return bg, errors.New(`fit must be "cover", "contain" or "tile"`)
}
return bg, nil
}
+33
View File
@@ -0,0 +1,33 @@
package graph
import (
"testing"
"adam-french.co.uk/backend/graph/model"
)
func TestBackgroundFrom(t *testing.T) {
for _, c := range []struct {
page string
input model.BackgroundInput
ok bool
}{
{"index", model.BackgroundInput{Color: "#102030", Fit: "cover"}, true},
{"shrines/gto", model.BackgroundInput{Image: "/uploads/abc.gif", Fit: "tile"}, true},
{"stp", model.BackgroundInput{Color: "rgb(0 0 0 / 50%)", Image: "https://example.com/a.png", Fit: "contain"}, true},
{"index", model.BackgroundInput{Color: "rebeccapurple", Fit: "cover"}, true},
{"index", model.BackgroundInput{Fit: "cover"}, true}, // plain theme colour
{"Index", model.BackgroundInput{Fit: "cover"}, false}, // bad page key
{"index", model.BackgroundInput{Fit: "stretch"}, false}, // unknown fit
{"index", model.BackgroundInput{Color: "red; color: blue", Fit: "cover"}, false},
{"index", model.BackgroundInput{Color: "url(/x.png)", Fit: "cover"}, false}, // an image in disguise
{"index", model.BackgroundInput{Image: `/x.png") , url("/y`, Fit: "cover"}, false},
{"index", model.BackgroundInput{Image: "javascript:alert(1)", Fit: "cover"}, false},
{"index", model.BackgroundInput{Image: "uploads/relative.png", Fit: "cover"}, false},
} {
_, err := backgroundFrom(c.page, c.input)
if (err == nil) != c.ok {
t.Errorf("backgroundFrom(%q, %+v) = %v, want ok=%v", c.page, c.input, err, c.ok)
}
}
}
+639 -23
View File
@@ -56,6 +56,15 @@ type ComplexityRoot struct {
User func(childComplexity int) int
}
Background struct {
Color func(childComplexity int) int
Fit func(childComplexity int) int
ID func(childComplexity int) int
Image func(childComplexity int) int
Page func(childComplexity int) int
UpdatedAt func(childComplexity int) int
}
Bookmark struct {
Category func(childComplexity int) int
CreatedAt func(childComplexity int) int
@@ -112,28 +121,30 @@ type ComplexityRoot struct {
}
Mutation struct {
CreateActivity func(childComplexity int, input model.CreateActivityInput) int
CreateBookmark func(childComplexity int, input model.CreateBookmarkInput) int
CreateBox func(childComplexity int, input model.CreateBoxInput) int
CreateFavorite func(childComplexity int, input model.CreateFavoriteInput) int
CreatePage func(childComplexity int, input model.CreatePageInput) int
CreatePlace func(childComplexity int, input model.CreatePlaceInput) int
CreatePost func(childComplexity int, input model.CreatePostInput) int
CreateUser func(childComplexity int, input model.CreateUserInput) int
DeleteBookmark func(childComplexity int, id int) int
DeleteBox func(childComplexity int, id int) int
DeletePage func(childComplexity int, id int) int
DeletePlace func(childComplexity int, id int) int
DeletePost func(childComplexity int, id int) int
DeleteUser func(childComplexity int, id int) int
Login func(childComplexity int, input model.LoginInput) int
Logout func(childComplexity int) int
RefreshToken func(childComplexity int) int
SetUserAdmin func(childComplexity int, id int, admin bool) int
UpdateBox func(childComplexity int, id int, input model.UpdateBoxInput) int
UpdatePage func(childComplexity int, id int, input model.UpdatePageInput) int
UpdatePlace func(childComplexity int, id int, input model.UpdatePlaceInput) int
UpdatePost func(childComplexity int, id int, input model.UpdatePostInput) int
ClearBackground func(childComplexity int, page string) int
CreateActivity func(childComplexity int, input model.CreateActivityInput) int
CreateBookmark func(childComplexity int, input model.CreateBookmarkInput) int
CreateBox func(childComplexity int, input model.CreateBoxInput) int
CreateFavorite func(childComplexity int, input model.CreateFavoriteInput) int
CreatePage func(childComplexity int, input model.CreatePageInput) int
CreatePlace func(childComplexity int, input model.CreatePlaceInput) int
CreatePost func(childComplexity int, input model.CreatePostInput) int
CreateUser func(childComplexity int, input model.CreateUserInput) int
DeleteBookmark func(childComplexity int, id int) int
DeleteBox func(childComplexity int, id int) int
DeletePage func(childComplexity int, id int) int
DeletePlace func(childComplexity int, id int) int
DeletePost func(childComplexity int, id int) int
DeleteUser func(childComplexity int, id int) int
Login func(childComplexity int, input model.LoginInput) int
Logout func(childComplexity int) int
RefreshToken func(childComplexity int) int
SetBackground func(childComplexity int, page string, input model.BackgroundInput) int
SetUserAdmin func(childComplexity int, id int, admin bool) int
UpdateBox func(childComplexity int, id int, input model.UpdateBoxInput) int
UpdatePage func(childComplexity int, id int, input model.UpdatePageInput) int
UpdatePlace func(childComplexity int, id int, input model.UpdatePlaceInput) int
UpdatePost func(childComplexity int, id int, input model.UpdatePostInput) int
}
Page struct {
@@ -170,6 +181,7 @@ type ComplexityRoot struct {
Query struct {
Activities func(childComplexity int) int
Background func(childComplexity int, page string) int
Bookmarks func(childComplexity int) int
Boxes func(childComplexity int, page string) int
Favorites func(childComplexity int) int
@@ -263,6 +275,8 @@ type MutationResolver interface {
Login(ctx context.Context, input model.LoginInput) (*model.AuthPayload, error)
Logout(ctx context.Context) (bool, error)
RefreshToken(ctx context.Context) (*model.AuthPayload, error)
SetBackground(ctx context.Context, page string, input model.BackgroundInput) (*models.Background, error)
ClearBackground(ctx context.Context, page string) (bool, error)
CreateBookmark(ctx context.Context, input model.CreateBookmarkInput) (*models.Bookmark, error)
DeleteBookmark(ctx context.Context, id int) (*models.Bookmark, error)
CreateBox(ctx context.Context, input model.CreateBoxInput) (*models.Box, error)
@@ -285,6 +299,7 @@ type MutationResolver interface {
type QueryResolver interface {
Activities(ctx context.Context) ([]*models.Activity, error)
Me(ctx context.Context) (*models.User, error)
Background(ctx context.Context, page string) (*models.Background, error)
Bookmarks(ctx context.Context) ([]*models.Bookmark, error)
Boxes(ctx context.Context, page string) ([]*models.Box, error)
Favorites(ctx context.Context) ([]*models.Favorite, error)
@@ -370,6 +385,43 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
return e.ComplexityRoot.AuthPayload.User(childComplexity), true
case "Background.color":
if e.ComplexityRoot.Background.Color == nil {
break
}
return e.ComplexityRoot.Background.Color(childComplexity), true
case "Background.fit":
if e.ComplexityRoot.Background.Fit == nil {
break
}
return e.ComplexityRoot.Background.Fit(childComplexity), true
case "Background.id":
if e.ComplexityRoot.Background.ID == nil {
break
}
return e.ComplexityRoot.Background.ID(childComplexity), true
case "Background.image":
if e.ComplexityRoot.Background.Image == nil {
break
}
return e.ComplexityRoot.Background.Image(childComplexity), true
case "Background.page":
if e.ComplexityRoot.Background.Page == nil {
break
}
return e.ComplexityRoot.Background.Page(childComplexity), true
case "Background.updatedAt":
if e.ComplexityRoot.Background.UpdatedAt == nil {
break
}
return e.ComplexityRoot.Background.UpdatedAt(childComplexity), true
case "Bookmark.category":
if e.ComplexityRoot.Bookmark.Category == nil {
break
@@ -615,6 +667,17 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
return e.ComplexityRoot.Message.Private(childComplexity), true
case "Mutation.clearBackground":
if e.ComplexityRoot.Mutation.ClearBackground == nil {
break
}
args, err := ec.field_Mutation_clearBackground_args(ctx, rawArgs)
if err != nil {
return 0, false
}
return e.ComplexityRoot.Mutation.ClearBackground(childComplexity, args["page"].(string)), true
case "Mutation.createActivity":
if e.ComplexityRoot.Mutation.CreateActivity == nil {
break
@@ -792,6 +855,17 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
}
return e.ComplexityRoot.Mutation.RefreshToken(childComplexity), true
case "Mutation.setBackground":
if e.ComplexityRoot.Mutation.SetBackground == nil {
break
}
args, err := ec.field_Mutation_setBackground_args(ctx, rawArgs)
if err != nil {
return 0, false
}
return e.ComplexityRoot.Mutation.SetBackground(childComplexity, args["page"].(string), args["input"].(model.BackgroundInput)), true
case "Mutation.setUserAdmin":
if e.ComplexityRoot.Mutation.SetUserAdmin == nil {
break
@@ -995,6 +1069,17 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
}
return e.ComplexityRoot.Query.Activities(childComplexity), true
case "Query.background":
if e.ComplexityRoot.Query.Background == nil {
break
}
args, err := ec.field_Query_background_args(ctx, rawArgs)
if err != nil {
return 0, false
}
return e.ComplexityRoot.Query.Background(childComplexity, args["page"].(string)), true
case "Query.bookmarks":
if e.ComplexityRoot.Query.Bookmarks == nil {
break
@@ -1323,6 +1408,7 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
opCtx := graphql.GetOperationContext(ctx)
ec := newExecutionContext(opCtx, e, make(chan graphql.DeferredResult))
inputUnmarshalMap := graphql.BuildUnmarshalerMap(
ec.unmarshalInputBackgroundInput,
ec.unmarshalInputCreateActivityInput,
ec.unmarshalInputCreateBookmarkInput,
ec.unmarshalInputCreateBoxInput,
@@ -1410,7 +1496,7 @@ func newExecutionContext(
}
}
//go:embed "schema/activity.graphql" "schema/auth.graphql" "schema/bookmark.graphql" "schema/box.graphql" "schema/favorite.graphql" "schema/gitea.graphql" "schema/message.graphql" "schema/page.graphql" "schema/place.graphql" "schema/post.graphql" "schema/rowing.graphql" "schema/schema.graphql" "schema/spotify.graphql" "schema/steam.graphql" "schema/user.graphql"
//go:embed "schema/activity.graphql" "schema/auth.graphql" "schema/background.graphql" "schema/bookmark.graphql" "schema/box.graphql" "schema/favorite.graphql" "schema/gitea.graphql" "schema/message.graphql" "schema/page.graphql" "schema/place.graphql" "schema/post.graphql" "schema/rowing.graphql" "schema/schema.graphql" "schema/spotify.graphql" "schema/steam.graphql" "schema/user.graphql"
var sourcesFS embed.FS
func sourceData(filename string) string {
@@ -1424,6 +1510,7 @@ func sourceData(filename string) string {
var sources = []*ast.Source{
{Name: "schema/activity.graphql", Input: sourceData("schema/activity.graphql"), BuiltIn: false},
{Name: "schema/auth.graphql", Input: sourceData("schema/auth.graphql"), BuiltIn: false},
{Name: "schema/background.graphql", Input: sourceData("schema/background.graphql"), BuiltIn: false},
{Name: "schema/bookmark.graphql", Input: sourceData("schema/bookmark.graphql"), BuiltIn: false},
{Name: "schema/box.graphql", Input: sourceData("schema/box.graphql"), BuiltIn: false},
{Name: "schema/favorite.graphql", Input: sourceData("schema/favorite.graphql"), BuiltIn: false},
@@ -1470,6 +1557,24 @@ func (ec *executionContext) childFields_AuthPayload(ctx context.Context, field g
return nil, fmt.Errorf("no field named %q was found under type AuthPayload", field.Name)
}
func (ec *executionContext) childFields_Background(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
switch field.Name {
case "id":
return ec.fieldContext_Background_id(ctx, field)
case "updatedAt":
return ec.fieldContext_Background_updatedAt(ctx, field)
case "page":
return ec.fieldContext_Background_page(ctx, field)
case "color":
return ec.fieldContext_Background_color(ctx, field)
case "image":
return ec.fieldContext_Background_image(ctx, field)
case "fit":
return ec.fieldContext_Background_fit(ctx, field)
}
return nil, fmt.Errorf("no field named %q was found under type Background", field.Name)
}
func (ec *executionContext) childFields_Bookmark(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
switch field.Name {
case "id":
@@ -1880,6 +1985,20 @@ func (ec *executionContext) childFields___Type(ctx context.Context, field graphq
// region ***************************** args.gotpl *****************************
func (ec *executionContext) field_Mutation_clearBackground_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
arg0, err := graphql.ProcessArgField(ctx, rawArgs, "page",
func(ctx context.Context, v any) (string, error) {
return ec.unmarshalNString2string(ctx, v)
})
if err != nil {
return nil, err
}
args["page"] = arg0
return args, nil
}
func (ec *executionContext) field_Mutation_createActivity_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
@@ -2090,6 +2209,28 @@ func (ec *executionContext) field_Mutation_login_args(ctx context.Context, rawAr
return args, nil
}
func (ec *executionContext) field_Mutation_setBackground_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
arg0, err := graphql.ProcessArgField(ctx, rawArgs, "page",
func(ctx context.Context, v any) (string, error) {
return ec.unmarshalNString2string(ctx, v)
})
if err != nil {
return nil, err
}
args["page"] = arg0
arg1, err := graphql.ProcessArgField(ctx, rawArgs, "input",
func(ctx context.Context, v any) (model.BackgroundInput, error) {
return ec.unmarshalNBackgroundInput2adamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐBackgroundInput(ctx, v)
})
if err != nil {
return nil, err
}
args["input"] = arg1
return args, nil
}
func (ec *executionContext) field_Mutation_setUserAdmin_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
@@ -2214,6 +2355,20 @@ func (ec *executionContext) field_Query___type_args(ctx context.Context, rawArgs
return args, nil
}
func (ec *executionContext) field_Query_background_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
arg0, err := graphql.ProcessArgField(ctx, rawArgs, "page",
func(ctx context.Context, v any) (string, error) {
return ec.unmarshalNString2string(ctx, v)
})
if err != nil {
return nil, err
}
args["page"] = arg0
return args, nil
}
func (ec *executionContext) field_Query_boxes_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
@@ -2500,6 +2655,144 @@ func (ec *executionContext) fieldContext_AuthPayload_user(_ context.Context, fie
return fc, nil
}
func (ec *executionContext) _Background_id(ctx context.Context, field graphql.CollectedField, obj *models.Background) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Background_id(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.ID, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v uint) graphql.Marshaler {
return ec.marshalNID2uint(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Background_id(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Background", field, false, false, errors.New("field of type ID does not have child fields"))
}
func (ec *executionContext) _Background_updatedAt(ctx context.Context, field graphql.CollectedField, obj *models.Background) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Background_updatedAt(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.UpdatedAt, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v time.Time) graphql.Marshaler {
return ec.marshalNTime2timeᚐTime(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Background_updatedAt(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Background", field, false, false, errors.New("field of type Time does not have child fields"))
}
func (ec *executionContext) _Background_page(ctx context.Context, field graphql.CollectedField, obj *models.Background) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Background_page(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.Page, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v string) graphql.Marshaler {
return ec.marshalNString2string(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Background_page(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Background", field, false, false, errors.New("field of type String does not have child fields"))
}
func (ec *executionContext) _Background_color(ctx context.Context, field graphql.CollectedField, obj *models.Background) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Background_color(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.Color, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v string) graphql.Marshaler {
return ec.marshalNString2string(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Background_color(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Background", field, false, false, errors.New("field of type String does not have child fields"))
}
func (ec *executionContext) _Background_image(ctx context.Context, field graphql.CollectedField, obj *models.Background) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Background_image(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.Image, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v string) graphql.Marshaler {
return ec.marshalNString2string(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Background_image(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Background", field, false, false, errors.New("field of type String does not have child fields"))
}
func (ec *executionContext) _Background_fit(ctx context.Context, field graphql.CollectedField, obj *models.Background) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Background_fit(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.Fit, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v string) graphql.Marshaler {
return ec.marshalNString2string(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Background_fit(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Background", field, false, false, errors.New("field of type String does not have child fields"))
}
func (ec *executionContext) _Bookmark_id(ctx context.Context, field graphql.CollectedField, obj *models.Bookmark) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
@@ -3576,6 +3869,120 @@ func (ec *executionContext) fieldContext_Mutation_refreshToken(_ context.Context
return fc, nil
}
func (ec *executionContext) _Mutation_setBackground(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Mutation_setBackground(ctx, field)
},
func(ctx context.Context) (any, error) {
fc := graphql.GetFieldContext(ctx)
return ec.Resolvers.Mutation().SetBackground(ctx, fc.Args["page"].(string), fc.Args["input"].(model.BackgroundInput))
},
func(ctx context.Context, next graphql.Resolver) graphql.Resolver {
directive0 := next
directive1 := func(ctx context.Context) (any, error) {
if ec.Directives.Admin == nil {
var zeroVal *models.Background
return zeroVal, errors.New("directive admin is not implemented")
}
return ec.Directives.Admin(ctx, nil, directive0)
}
next = directive1
return next
},
func(ctx context.Context, selections ast.SelectionSet, v *models.Background) graphql.Marshaler {
return ec.marshalNBackground2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋmodelsᚐBackground(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Mutation_setBackground(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: "Mutation",
Field: field,
IsMethod: true,
IsResolver: true,
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.childFields_Background(ctx, field)
},
}
defer func() {
if r := recover(); r != nil {
err = ec.Recover(ctx, r)
ec.Error(ctx, err)
}
}()
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.field_Mutation_setBackground_args(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
return fc, err
}
return fc, nil
}
func (ec *executionContext) _Mutation_clearBackground(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Mutation_clearBackground(ctx, field)
},
func(ctx context.Context) (any, error) {
fc := graphql.GetFieldContext(ctx)
return ec.Resolvers.Mutation().ClearBackground(ctx, fc.Args["page"].(string))
},
func(ctx context.Context, next graphql.Resolver) graphql.Resolver {
directive0 := next
directive1 := func(ctx context.Context) (any, error) {
if ec.Directives.Admin == nil {
var zeroVal bool
return zeroVal, errors.New("directive admin is not implemented")
}
return ec.Directives.Admin(ctx, nil, directive0)
}
next = directive1
return next
},
func(ctx context.Context, selections ast.SelectionSet, v bool) graphql.Marshaler {
return ec.marshalNBoolean2bool(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Mutation_clearBackground(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: "Mutation",
Field: field,
IsMethod: true,
IsResolver: true,
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return nil, errors.New("field of type Boolean does not have child fields")
},
}
defer func() {
if r := recover(); r != nil {
err = ec.Recover(ctx, r)
ec.Error(ctx, err)
}
}()
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.field_Mutation_clearBackground_args(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
return fc, err
}
return fc, nil
}
func (ec *executionContext) _Mutation_createBookmark(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
@@ -5204,6 +5611,50 @@ func (ec *executionContext) fieldContext_Query_me(_ context.Context, field graph
return fc, nil
}
func (ec *executionContext) _Query_background(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Query_background(ctx, field)
},
func(ctx context.Context) (any, error) {
fc := graphql.GetFieldContext(ctx)
return ec.Resolvers.Query().Background(ctx, fc.Args["page"].(string))
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v *models.Background) graphql.Marshaler {
return ec.marshalOBackground2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋmodelsᚐBackground(ctx, selections, v)
},
true,
false,
)
}
func (ec *executionContext) fieldContext_Query_background(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: "Query",
Field: field,
IsMethod: true,
IsResolver: true,
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.childFields_Background(ctx, field)
},
}
defer func() {
if r := recover(); r != nil {
err = ec.Recover(ctx, r)
ec.Error(ctx, err)
}
}()
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.field_Query_background_args(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
return fc, err
}
return fc, nil
}
func (ec *executionContext) _Query_bookmarks(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
@@ -7705,6 +8156,50 @@ func (ec *executionContext) fieldContext___Type_isOneOf(_ context.Context, field
// region **************************** input.gotpl *****************************
func (ec *executionContext) unmarshalInputBackgroundInput(ctx context.Context, obj any) (model.BackgroundInput, error) {
var it model.BackgroundInput
if obj == nil {
return it, nil
}
asMap := map[string]any{}
for k, v := range obj.(map[string]any) {
asMap[k] = v
}
fieldsInOrder := [...]string{"color", "image", "fit"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
continue
}
switch k {
case "color":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("color"))
data, err := ec.unmarshalNString2string(ctx, v)
if err != nil {
return it, err
}
it.Color = data
case "image":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("image"))
data, err := ec.unmarshalNString2string(ctx, v)
if err != nil {
return it, err
}
it.Image = data
case "fit":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("fit"))
data, err := ec.unmarshalNString2string(ctx, v)
if err != nil {
return it, err
}
it.Fit = data
}
}
return it, nil
}
func (ec *executionContext) unmarshalInputCreateActivityInput(ctx context.Context, obj any) (model.CreateActivityInput, error) {
var it model.CreateActivityInput
if obj == nil {
@@ -8554,6 +9049,69 @@ func (ec *executionContext) _AuthPayload(ctx context.Context, sel ast.SelectionS
return out
}
var backgroundImplementors = []string{"Background"}
func (ec *executionContext) _Background(ctx context.Context, sel ast.SelectionSet, obj *models.Background) graphql.Marshaler {
fields := graphql.CollectFields(ec.OperationContext, sel, backgroundImplementors)
out := graphql.NewFieldSet(fields)
deferredFieldSet := graphql.NewFieldSet(nil)
deferLabelToView := make(map[string]*graphql.FieldSetView)
for i, field := range fields {
switch field.Name {
case "__typename":
out.Values[i] = graphql.MarshalString("Background")
case "id":
out.Values[i] = ec._Background_id(ctx, field, obj)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "updatedAt":
out.Values[i] = ec._Background_updatedAt(ctx, field, obj)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "page":
out.Values[i] = ec._Background_page(ctx, field, obj)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "color":
out.Values[i] = ec._Background_color(ctx, field, obj)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "image":
out.Values[i] = ec._Background_image(ctx, field, obj)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "fit":
out.Values[i] = ec._Background_fit(ctx, field, obj)
if out.Values[i] == graphql.Null {
out.Invalids++
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
out.Dispatch(ctx)
if out.Invalids > 0 {
return graphql.Null
}
atomic.AddInt32(&ec.Deferred, int32(min(len(deferLabelToView), math.MaxInt32)))
ec.ProcessDeferredGroup(graphql.DeferredGroup{
Defers: deferLabelToView,
Path: graphql.GetPath(ctx),
FieldSet: deferredFieldSet,
Context: ctx,
})
return out
}
var bookmarkImplementors = []string{"Bookmark"}
func (ec *executionContext) _Bookmark(ctx context.Context, sel ast.SelectionSet, obj *models.Bookmark) graphql.Marshaler {
@@ -9000,6 +9558,20 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "setBackground":
out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) {
return ec._Mutation_setBackground(ctx, field)
})
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "clearBackground":
out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) {
return ec._Mutation_clearBackground(ctx, field)
})
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "createBookmark":
out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) {
return ec._Mutation_createBookmark(ctx, field)
@@ -9424,6 +9996,28 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr
func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
}
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) })
case "background":
field := field
innerFunc := func(ctx context.Context, fs *graphql.FieldSet) (res graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
}
}()
res = ec._Query_background(ctx, field)
if res == graphql.RequiredNull {
atomic.AddUint32(&fs.Invalids, 1)
}
return res
}
rrm := func(ctx context.Context) graphql.Marshaler {
return ec.OperationContext.RootResolverMiddleware(ctx,
func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
}
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) })
case "bookmarks":
field := field
@@ -10808,6 +11402,21 @@ func (ec *executionContext) marshalNAuthPayload2ᚖadamᚑfrenchᚗcoᚗukᚋbac
return ec._AuthPayload(ctx, sel, v)
}
func (ec *executionContext) marshalNBackground2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋmodelsᚐBackground(ctx context.Context, sel ast.SelectionSet, v *models.Background) graphql.Marshaler {
if v == nil {
if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
graphql.AddErrorf(ctx, "the requested element is null which the schema does not allow")
}
return graphql.Null
}
return ec._Background(ctx, sel, v)
}
func (ec *executionContext) unmarshalNBackgroundInput2adamᚑfrenchᚗcoᚗukᚋbackendᚋgraphᚋmodelᚐBackgroundInput(ctx context.Context, v any) (model.BackgroundInput, error) {
res, err := ec.unmarshalInputBackgroundInput(ctx, v)
return res, graphql.ErrorOnPath(ctx, err)
}
func (ec *executionContext) marshalNBookmark2ᚕᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋmodelsᚐBookmarkᚄ(ctx context.Context, sel ast.SelectionSet, v []*models.Bookmark) graphql.Marshaler {
ret := graphql.MarshalSliceConcurrently(ctx, len(v), 0, false, func(ctx context.Context, i int) graphql.Marshaler {
fc := graphql.GetFieldContext(ctx)
@@ -11467,6 +12076,13 @@ func (ec *executionContext) marshalN__TypeKind2string(ctx context.Context, sel a
return res
}
func (ec *executionContext) marshalOBackground2ᚖadamᚑfrenchᚗcoᚗukᚋbackendᚋmodelsᚐBackground(ctx context.Context, sel ast.SelectionSet, v *models.Background) graphql.Marshaler {
if v == nil {
return graphql.Null
}
return ec._Background(ctx, sel, v)
}
func (ec *executionContext) unmarshalOBoolean2bool(ctx context.Context, v any) (bool, error) {
res, err := graphql.UnmarshalBoolean(v)
return res, graphql.ErrorOnPath(ctx, err)
+6
View File
@@ -12,6 +12,12 @@ type AuthPayload struct {
User *models.User `json:"user"`
}
type BackgroundInput struct {
Color string `json:"color"`
Image string `json:"image"`
Fit string `json:"fit"`
}
type CreateActivityInput struct {
Type string `json:"type"`
Name string `json:"name"`
+11 -4
View File
@@ -45,8 +45,9 @@ func (r *mutationResolver) CreatePage(ctx context.Context, input model.CreatePag
// 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.
// Partial update, like updateBox. A new slug moves the page's boxes and
// background with it in the same transaction, since both find their page
// by slug.
var page models.Page
if err := r.Store.DB.First(&page, id).Error; err != nil {
return nil, err
@@ -81,6 +82,9 @@ func (r *mutationResolver) UpdatePage(ctx context.Context, id int, input model.U
if err := tx.Model(&models.Box{}).Where("page = ?", oldSlug).Update("page", page.Slug).Error; err != nil {
return err
}
if err := tx.Model(&models.Background{}).Where("page = ?", oldSlug).Update("page", page.Slug).Error; err != nil {
return err
}
}
return tx.Save(&page).Error
})
@@ -92,8 +96,8 @@ func (r *mutationResolver) UpdatePage(ctx context.Context, id int, input model.U
// 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.
// The boxes and background 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
@@ -102,6 +106,9 @@ func (r *mutationResolver) DeletePage(ctx context.Context, id int) (*models.Page
if err := tx.Where("page = ?", page.Slug).Delete(&models.Box{}).Error; err != nil {
return err
}
if err := tx.Where("page = ?", page.Slug).Delete(&models.Background{}).Error; err != nil {
return err
}
return tx.Delete(&page).Error
})
if err != nil {
+37
View File
@@ -0,0 +1,37 @@
# The background behind a canvas page, keyed by the same page key as its
# boxes ("index" for the landing page, "stp", or a Page's slug), so the
# landing page and every shrine are set the same way. Edited from the
# canvas's "Background" button; see models.Background.
#
# Anyone may read it, as part of the page, except that an adminOnly page's
# background is null to everyone but an admin.
type Background {
id: ID!
updatedAt: Time!
page: String!
# A CSS colour, or "" for the theme's page colour.
color: String!
# An image URL, or "" for none.
image: String!
# "cover", "contain" or "tile".
fit: String!
}
input BackgroundInput {
color: String!
image: String!
fit: String!
}
extend type Query {
# Null when the page has none, i.e. shows the theme's default.
background(page: String!): Background
}
extend type Mutation {
# Creates or replaces the page's background.
setBackground(page: String!, input: BackgroundInput!): Background! @admin
# Back to the theme's default. True if there was one to remove.
clearBackground(page: String!): Boolean! @admin
}
+19
View File
@@ -205,3 +205,22 @@ type Page struct {
// AdminOnly hides the whole page, boxes included, from everyone else.
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, "stp", 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.
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.
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".
Fit string `gorm:"not null;default:cover" json:"fit"`
}
+1
View File
@@ -53,6 +53,7 @@ func migrateDatabase(db *gorm.DB) error {
&models.Place{},
&models.Box{},
&models.Page{},
&models.Background{},
)
if err != nil {
return err