mirror of
https://github.com/GabrielVidal1/ask-form.git
synced 2026-12-02 02:07:27 +01:00
A linked machine keeps the accounts it took (chat-link.json) and refuses any other, so a later code can't bring old links back; ask-form link names the app. Every chat form takes no files and has quotas (on a machine too); cloud forms add a size and total cap, 200 answers for multi, and a note on their page never to type a password there; creates are serialized so parallel calls can't pass a quota. ask-form-relay takedown removes a cloud form or an account. Calls to a machine past the tunnel's size fail early. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
535 lines
16 KiB
Go
535 lines
16 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"math"
|
|
"net/url"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/GabrielVidal1/ask-form/internal/brand"
|
|
"github.com/GabrielVidal1/ask-form/internal/form"
|
|
"github.com/GabrielVidal1/ask-form/internal/i18n"
|
|
"github.com/GabrielVidal1/ask-form/internal/store"
|
|
)
|
|
|
|
// pageField is one field as the answer page draws it.
|
|
type pageField struct {
|
|
form.Field
|
|
Name, OtherName string
|
|
DOMID string
|
|
Value string // single-valued fields
|
|
Picked map[string]bool // radio / select / multiselect values
|
|
OtherPicked bool
|
|
OtherText string
|
|
Checked bool
|
|
Error string
|
|
Reenter bool // a secret to type again after an invalid send
|
|
FrameURL string // html block document
|
|
OptionFrames []string // per option: its mockup document, "" when none
|
|
FitContent bool // choice cards take the ratio of what they show
|
|
UploadURL string // file field: where its chunks go ("" in a demo: nothing is stored)
|
|
FileHint string // file field: "Images, PDF · up to 10 MB"
|
|
AttachName string // its text takes files (form.Attachable): the input of their refs
|
|
AttachURL string // …where they go ("" in a demo or a multi form: none)
|
|
AttachRefs string // …the refs sent so far (a page shown again with errors)
|
|
AttachBytes int64 // …and their limits
|
|
AttachFiles int
|
|
Num struct{ Min, Max, Step string }
|
|
ShowIfJSON string // the field's conditions, for the page script
|
|
RequiredIfJSON string
|
|
ListCols []listCol // list: the inputs of a row
|
|
Rows [][]string
|
|
Scale []int // rating: the numbers on offer
|
|
PrintScale []string // slider: its values to circle on paper (none: too many, a line instead)
|
|
ShowIfText string // its showIf in words, noted above it on paper
|
|
RangeStart string
|
|
RangeEnd string
|
|
// Read-only, with the answers in it (snapshot.go): a snapshot, or the
|
|
// respondent's own look back at what they sent. Inline: everything the
|
|
// page shows travels in it (the snapshot file); else frames and
|
|
// pictures load from the form's links, as on the answer page.
|
|
Snap, Inline bool
|
|
FrameDoc string // html block: its document, for srcdoc
|
|
OptionDocs []string // per option: its mockup document, "" when none
|
|
OptionImages []template.URL // per option: its picture as a data: URI
|
|
SecretChars string // a secret given: its length
|
|
Files []snapFile // file field: what was uploaded; an attachable one: what was attached
|
|
}
|
|
|
|
// listCol is one input of a list row: the list's single text, or one column of
|
|
// a table.
|
|
type listCol struct {
|
|
Name, Label, Type, Placeholder string
|
|
Options []string
|
|
}
|
|
|
|
type pageGroup struct {
|
|
Title string
|
|
ID string // anchor the desktop section list links to
|
|
Nav string // its entry there: the title, else "Section N"
|
|
Fields []pageField
|
|
}
|
|
|
|
// pageBrand is the machine's brand as a page draws it: its stylesheet in the
|
|
// page's <style nonce>, its logo and name above the title.
|
|
type pageBrand struct {
|
|
CSS template.CSS
|
|
Logo template.URL // a data: URI (brand.Check)
|
|
Name string
|
|
}
|
|
|
|
func pageLook(b brand.Brand) pageBrand {
|
|
return pageBrand{CSS: template.CSS(b.PageCSS()), Logo: template.URL(b.Logo), Name: b.Name}
|
|
}
|
|
|
|
type formPage struct {
|
|
ID, Title string
|
|
Base string // URL prefix of this form: /f/<id>, or /examples/<name> for a demo
|
|
Description template.HTML
|
|
Groups []pageGroup
|
|
CatchAll *pageField
|
|
Decline *pageField // the note a decline carries (none in a multi form)
|
|
CSRF string
|
|
Errors []pageError
|
|
Nonce string
|
|
PageCSS template.CSS
|
|
Expires string
|
|
Paged bool
|
|
Demo *demoInfo // set on an example: nothing sent is stored
|
|
Multi bool // open to everyone with the link
|
|
Count int // a multi form's answers so far
|
|
ShowCount bool // …shown to the respondent
|
|
HasRequired bool // a question is required: paper says what * means
|
|
Hosted bool // a relay's cloud form, made from a chat: the anti-phishing note
|
|
Brand pageBrand
|
|
}
|
|
|
|
// demoInfo is what an example page shows on top of the form. Preview: the
|
|
// page is `ask-form brand preview`'s.
|
|
type demoInfo struct {
|
|
Index, Spec string
|
|
Preview bool
|
|
}
|
|
|
|
type pageError struct{ DOMID, Label, Msg string }
|
|
|
|
// buildPage lays a form out for the answer page: fields filled from values
|
|
// (the defaults on first load, what was sent after an invalid submit),
|
|
// grouped into pages the way the ai-agent pages them.
|
|
func buildPage(st store.State, base string, values url.Values, errs map[string]string, csrf, nonce string) formPage {
|
|
lang := i18n.Of(st.Lang)
|
|
p := formPage{
|
|
ID: st.ID, Title: st.Title, Base: base, Description: form.MarkdownHTML(st.Description),
|
|
CSRF: csrf, Nonce: nonce, Expires: st.ExpiresAt.UTC().Format("2006-01-02 15:04 UTC"),
|
|
Multi: st.Multi, Count: len(st.Responses), ShowCount: st.Multi && !st.HideCount,
|
|
Hosted: st.Meta[MetaHosted] == "1",
|
|
}
|
|
var css strings.Builder
|
|
fields := make([]pageField, 0, len(st.Fields))
|
|
for i, f := range st.Fields {
|
|
pf := pageField{
|
|
Field: f, Name: form.InputName(f.Key), OtherName: form.OtherName(f.Key),
|
|
DOMID: fmt.Sprintf("q%d", i), Picked: map[string]bool{}, Error: errs[f.Key],
|
|
}
|
|
fill(&pf, values)
|
|
if f.Type == form.Widget && values == nil {
|
|
pf.Value = form.DefaultJSON(f) // the JSON the widget starts from
|
|
}
|
|
pf.Reenter = values != nil && f.Type == form.Secret
|
|
pf.ShowIfJSON, pf.RequiredIfJSON = condJSON(f.ShowIf), condJSON(f.RequiredIf)
|
|
pf.ShowIfText = form.CondText(lang, f.ShowIf, st.Fields)
|
|
p.HasRequired = p.HasRequired || f.Required
|
|
switch f.Type {
|
|
case form.List:
|
|
pf.ListCols, pf.Rows = listRows(f, pf.Name, values)
|
|
case form.Rating:
|
|
for n := int(*f.Min); n <= int(*f.Max); n++ {
|
|
pf.Scale = append(pf.Scale, n)
|
|
}
|
|
case form.Slider:
|
|
pf.PrintScale = sliderScale(f)
|
|
case form.Daterange:
|
|
if values != nil {
|
|
pf.RangeStart, pf.RangeEnd = values.Get(pf.Name+form.RangeStart), values.Get(pf.Name+form.RangeEnd)
|
|
} else if d, ok := f.Default.(map[string]any); ok {
|
|
pf.RangeStart, pf.RangeEnd = fmt.Sprint(d["start"]), fmt.Sprint(d["end"])
|
|
}
|
|
}
|
|
if f.Type == form.HTML || f.Type == form.Widget {
|
|
pf.FrameURL = fmt.Sprintf("%s/frame/%s", base, f.Key)
|
|
h := f.Height
|
|
if h <= 0 {
|
|
h = 240
|
|
}
|
|
fmt.Fprintf(&css, "#%s .frame{height:%dpx}", pf.DOMID, h)
|
|
if f.Height > 0 {
|
|
fmt.Fprintf(&css, "#%s .frame{resize:none}", pf.DOMID)
|
|
}
|
|
}
|
|
if f.Type == form.File {
|
|
if strings.HasPrefix(base, Prefix) {
|
|
pf.UploadURL = fmt.Sprintf("%s/upload/%s", base, f.Key)
|
|
}
|
|
pf.FileHint = fileHint(lang, f)
|
|
}
|
|
if f.Attachable() && strings.HasPrefix(base, Prefix) && !st.Multi && !NoUploads(st.Meta) {
|
|
pf.AttachName, pf.AttachURL = form.AttachName(f.Key), fmt.Sprintf("%s/upload/%s", base, f.Key)
|
|
pf.AttachRefs = values.Get(pf.AttachName)
|
|
pf.AttachBytes, pf.AttachFiles = form.AttachMaxMB*1_000_000, form.AttachMaxFiles
|
|
}
|
|
if f.Visual() {
|
|
pf.OptionFrames = make([]string, len(f.Options))
|
|
for n, o := range f.Options {
|
|
if o.HTML != "" {
|
|
pf.OptionFrames[n] = fmt.Sprintf("%s/frame/%s/%d", base, f.Key, n)
|
|
}
|
|
}
|
|
if f.Columns > 0 {
|
|
// From the width the grid gets 2 columns at (app.css); a phone keeps 1.
|
|
fmt.Fprintf(&css, "@media (min-width:560px){#%s .cards{grid-template-columns:repeat(%d,minmax(0,1fr))}}", pf.DOMID, f.Columns)
|
|
}
|
|
aspect := form.CardAspect(f.Aspect)
|
|
pf.FitContent = aspect == form.AspectAuto
|
|
if !pf.FitContent {
|
|
// One of CardAspect's few values: safe inside CSS.
|
|
fmt.Fprintf(&css, "#%s .visual{aspect-ratio:%s}", pf.DOMID, aspect)
|
|
}
|
|
}
|
|
if f.Min != nil {
|
|
pf.Num.Min = form.FormatNumber(*f.Min)
|
|
}
|
|
if f.Max != nil {
|
|
pf.Num.Max = form.FormatNumber(*f.Max)
|
|
}
|
|
if f.Step != nil {
|
|
pf.Num.Step = form.FormatNumber(*f.Step)
|
|
} else if f.Type == form.Number {
|
|
pf.Num.Step = "any"
|
|
}
|
|
if pf.Error != "" {
|
|
p.Errors = append(p.Errors, pageError{DOMID: pf.DOMID, Label: f.Label, Msg: pf.Error})
|
|
}
|
|
fields = append(fields, pf)
|
|
}
|
|
if !st.Multi {
|
|
p.Decline = declineNote(st, base, values, errs)
|
|
if p.Decline.Error != "" {
|
|
p.Errors = append(p.Errors, pageError{DOMID: "decline", Label: i18n.T(lang, "Decline this form"), Msg: p.Decline.Error})
|
|
}
|
|
}
|
|
p.PageCSS = template.CSS(css.String())
|
|
p.Groups, p.CatchAll = groupFields(fields)
|
|
p.Paged = len(p.Groups) > 1
|
|
for i := range p.Groups {
|
|
g := &p.Groups[i]
|
|
g.ID, g.Nav = fmt.Sprintf("g%d", i+1), g.Title
|
|
if g.Nav == "" {
|
|
g.Nav = i18n.T(lang, "Section %d", i+1)
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
// declineNote is the box a respondent who declines can write in and attach
|
|
// files to (form.DeclineNote).
|
|
func declineNote(st store.State, base string, values url.Values, errs map[string]string) *pageField {
|
|
f := form.DeclineNote(st.Fields)
|
|
pf := &pageField{Field: f, Name: form.InputName(f.Key), DOMID: "decline-note", Error: errs[f.Key], Value: values.Get(form.InputName(f.Key))}
|
|
if strings.HasPrefix(base, Prefix) && !NoUploads(st.Meta) {
|
|
pf.AttachName, pf.AttachURL = form.AttachName(f.Key), fmt.Sprintf("%s/upload/%s", base, f.Key)
|
|
pf.AttachRefs = values.Get(pf.AttachName)
|
|
pf.AttachBytes, pf.AttachFiles = form.AttachMaxMB*1_000_000, form.AttachMaxFiles
|
|
}
|
|
return pf
|
|
}
|
|
|
|
// fill sets a field's current value: from the submitted values when there
|
|
// are any, else from the spec's default. A secret is never refilled.
|
|
func fill(pf *pageField, values url.Values) {
|
|
f := pf.Field
|
|
if values != nil {
|
|
if f.Type == form.Secret {
|
|
return
|
|
}
|
|
vals := values[pf.Name]
|
|
pf.OtherText = values.Get(pf.OtherName)
|
|
for _, v := range vals {
|
|
pf.Picked[v] = true
|
|
}
|
|
pf.OtherPicked = pf.Picked[form.OtherValue]
|
|
if len(vals) > 0 {
|
|
pf.Value = vals[0]
|
|
}
|
|
pf.Checked = pf.Value != ""
|
|
return
|
|
}
|
|
switch d := f.Default.(type) {
|
|
case nil:
|
|
if f.Type == form.Slider && f.Min != nil {
|
|
pf.Value = form.FormatNumber(*f.Min)
|
|
}
|
|
case bool:
|
|
pf.Checked = d
|
|
case float64:
|
|
pf.Value = form.FormatNumber(d)
|
|
case string:
|
|
pf.Value = d
|
|
pf.Checked = d == "true" || d == "on"
|
|
case []any:
|
|
for _, x := range d {
|
|
if s, ok := x.(string); ok {
|
|
pf.Picked[s] = true
|
|
}
|
|
}
|
|
}
|
|
if f.Choice() && pf.Value != "" {
|
|
if hasOption(f, pf.Value) {
|
|
pf.Picked[pf.Value] = true
|
|
} else {
|
|
// A default outside the options pre-fills "Other…".
|
|
pf.OtherPicked, pf.OtherText = true, pf.Value
|
|
pf.Picked[form.OtherValue] = true
|
|
}
|
|
}
|
|
}
|
|
|
|
// condJSON is a condition as the page script reads it ("" when there is none).
|
|
func condJSON(c *form.Cond) string {
|
|
if c == nil {
|
|
return ""
|
|
}
|
|
b, err := json.Marshal(c)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// minListRows is how many empty rows a list shows without a script to add
|
|
// more; with one, the page trims the blank ones down to what is needed.
|
|
const minListRows = 3
|
|
|
|
// listRows lays a list field out: the inputs of one row and the rows' values —
|
|
// what was sent (after an invalid submit), else the spec's default, padded
|
|
// with empty rows.
|
|
func listRows(f form.Field, name string, values url.Values) ([]listCol, [][]string) {
|
|
cols := []listCol{{Name: name, Label: f.Label, Type: form.ColText, Placeholder: f.Placeholder}}
|
|
if len(f.Cols) > 0 {
|
|
cols = cols[:0]
|
|
for _, c := range f.Cols {
|
|
cols = append(cols, listCol{Name: name + "." + c.Key, Label: c.Label, Type: c.Type, Placeholder: c.Placeholder, Options: c.Options})
|
|
}
|
|
}
|
|
var rows [][]string
|
|
switch {
|
|
case values != nil:
|
|
n := 0
|
|
for _, c := range cols {
|
|
if l := len(values[c.Name]); l > n {
|
|
n = l
|
|
}
|
|
}
|
|
for i := 0; i < n && i < 4*form.MaxListRows; i++ {
|
|
row := make([]string, len(cols))
|
|
for j, c := range cols {
|
|
if vs := values[c.Name]; i < len(vs) {
|
|
row[j] = vs[i]
|
|
}
|
|
}
|
|
rows = append(rows, row)
|
|
}
|
|
default:
|
|
items, _ := f.Default.([]any)
|
|
for _, it := range items {
|
|
row := make([]string, len(cols))
|
|
if m, ok := it.(map[string]any); ok && len(f.Cols) > 0 {
|
|
for j, c := range f.Cols {
|
|
if x, has := m[c.Key]; has {
|
|
row[j] = fmt.Sprint(x)
|
|
}
|
|
}
|
|
} else if s, ok := it.(string); ok && len(f.Cols) == 0 {
|
|
row[0] = s
|
|
}
|
|
rows = append(rows, row)
|
|
}
|
|
}
|
|
want := minListRows
|
|
if f.Min != nil && int(*f.Min) > want {
|
|
want = int(*f.Min)
|
|
}
|
|
for len(rows) < want {
|
|
rows = append(rows, make([]string, len(cols)))
|
|
}
|
|
return cols, rows
|
|
}
|
|
|
|
// maxPrintScale is how many values a slider prints as numbers to circle; one
|
|
// with more prints as a line to mark.
|
|
const maxPrintScale = 11
|
|
|
|
// sliderScale is a slider's values, min to max by step, when there are few
|
|
// enough to circle on paper.
|
|
func sliderScale(f form.Field) []string {
|
|
if f.Min == nil || f.Max == nil {
|
|
return nil
|
|
}
|
|
step := 1.0
|
|
if f.Step != nil && *f.Step > 0 {
|
|
step = *f.Step
|
|
}
|
|
n := int(math.Floor((*f.Max-*f.Min)/step+1e-9)) + 1
|
|
if n < 2 || n > maxPrintScale {
|
|
return nil
|
|
}
|
|
out := make([]string, n)
|
|
for i := range out {
|
|
v := *f.Min + float64(i)*step
|
|
out[i] = form.FormatNumber(math.Round(v*1e9) / 1e9)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func hasOption(f form.Field, v string) bool {
|
|
for _, o := range f.Options {
|
|
if o.Value == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// groupFields pages the fields by their `group`: a question without one
|
|
// stays in the group of the question before it; a display block without one
|
|
// joins the question after it (its context), else the one before. Groups
|
|
// keep the order of their first appearance. The catch-all is pinned last.
|
|
func groupFields(fields []pageField) ([]pageGroup, *pageField) {
|
|
var catchAll *pageField
|
|
var rest []pageField
|
|
for i := range fields {
|
|
if fields[i].CatchAll {
|
|
f := fields[i]
|
|
catchAll = &f
|
|
continue
|
|
}
|
|
rest = append(rest, fields[i])
|
|
}
|
|
eff := make([]string, len(rest))
|
|
cur := ""
|
|
for i, f := range rest {
|
|
if f.Display() {
|
|
continue
|
|
}
|
|
if f.Group != "" {
|
|
cur = f.Group
|
|
}
|
|
eff[i] = cur
|
|
}
|
|
for i, f := range rest {
|
|
if !f.Display() {
|
|
continue
|
|
}
|
|
if f.Group != "" {
|
|
eff[i] = f.Group
|
|
continue
|
|
}
|
|
eff[i] = ""
|
|
found := false
|
|
for j := i + 1; j < len(rest); j++ {
|
|
if !rest[j].Display() {
|
|
eff[i], found = eff[j], true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
for j := i - 1; j >= 0; j-- {
|
|
if !rest[j].Display() {
|
|
eff[i] = eff[j]
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
var groups []pageGroup
|
|
index := map[string]int{}
|
|
for i, f := range rest {
|
|
g, ok := index[eff[i]]
|
|
if !ok {
|
|
g = len(groups)
|
|
index[eff[i]] = g
|
|
groups = append(groups, pageGroup{Title: eff[i]})
|
|
}
|
|
groups[g].Fields = append(groups[g].Fields, f)
|
|
}
|
|
if len(groups) == 0 {
|
|
groups = []pageGroup{{}}
|
|
}
|
|
return groups, catchAll
|
|
}
|
|
|
|
// recapItem is one answered question on the closed page.
|
|
type recapItem struct{ Label, Text string }
|
|
|
|
func recap(lang string, fields []form.Field, answers map[string]any) []recapItem {
|
|
var out []recapItem
|
|
for _, f := range fields {
|
|
if f.Display() {
|
|
continue
|
|
}
|
|
var parts, files []string
|
|
var attached []form.FileInfo
|
|
if raw, ok := answers[form.FilesKey(f.Key)]; ok && f.Attachable() {
|
|
b, _ := json.Marshal(raw) // []FileInfo fresh from a submit, []any once stored
|
|
json.Unmarshal(b, &attached)
|
|
for _, a := range attached {
|
|
files = append(files, a.Name+" ("+form.HumanSizeIn(lang, a.Size)+")")
|
|
}
|
|
}
|
|
if v, ok := answers[f.Key]; ok {
|
|
// Files sent alone answer with their names: said once is enough.
|
|
t := form.AnswerTextIn(lang, f, v)
|
|
// A single choice's "Other…" text next to its option.
|
|
if o, ok := answers[form.OtherKey(f.Key)].(string); ok && o != "" && (len(attached) == 0 || o != form.FileNames(attached)) {
|
|
t += ", " + o
|
|
}
|
|
if t != "" && (len(attached) == 0 || t != form.FileNames(attached)) {
|
|
parts = append(parts, t)
|
|
}
|
|
}
|
|
if len(files) > 0 {
|
|
parts = append(parts, "📎 "+strings.Join(files, ", "))
|
|
}
|
|
if len(parts) > 0 {
|
|
out = append(out, recapItem{Label: f.Label, Text: strings.Join(parts, " · ")})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// fileHint is the line under a file field's drop area: what it takes.
|
|
func fileHint(lang string, f form.Field) string {
|
|
var parts []string
|
|
if f.Accept != "" {
|
|
names := map[string]string{"image/*": "images", "video/*": "videos", "audio/*": "audio", "text/*": "text files",
|
|
"application/pdf": "PDF", ".pdf": "PDF"}
|
|
var kinds []string
|
|
for _, t := range strings.Split(f.Accept, ",") {
|
|
if n, ok := names[t]; ok {
|
|
t = i18n.T(lang, n)
|
|
}
|
|
if !slices.Contains(kinds, t) {
|
|
kinds = append(kinds, t)
|
|
}
|
|
}
|
|
parts = append(parts, strings.Join(kinds, ", "))
|
|
}
|
|
parts = append(parts, i18n.T(lang, "up to %s MB", form.FormatNumber(f.MaxMB)))
|
|
if f.Multiple {
|
|
parts = append(parts, i18n.T(lang, "at most %d files", f.FileLimit()))
|
|
}
|
|
return strings.Join(parts, " · ")
|
|
}
|