oficina / base Public

The thread draws a person's files without scripts (Task 7)

Human

Prompt

Build Base's half of files in Ask, slice 1, task by task from the approved plan, tests first.

From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.

Summary

chat.html: a files row above a person's words (thumbnails linking to the image, then download pills with the size), all from /ask/attachments; no empty words for a files-only message. fileSize writes the size as Equipo's chips do (B, KB, MB to one place) in the page's locale with rastrillo's number. CSS in light and dark through the theme's variables.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev befeef8ae5598ea61367b98524944158be341bae parent 9e90ef2
7 files changed, +108 −5
  • internal/equipo/files.go +3 −3
  • internal/look/static/app.css +15 −0
  • internal/web/app.go +1 −1
  • internal/web/ask_files.go +32 −0
  • internal/web/files_internal_test.go +16 −0
  • internal/web/templates/partials/chat.html +5 −1
  • internal/webtest/ask_files_test.go +36 −0
diff --git a/internal/equipo/files.go b/internal/equipo/files.go
index 7889128..ca73b8c 100644
--- a/internal/equipo/files.go
+++ b/internal/equipo/files.go
@@ -2,9 +2,6 @@ package equipo
import (
"bytes"
- "regexp"
- "strings"
- "unicode/utf8"
"context"
"encoding/json"
"errors"
@@ -13,7 +10,10 @@ import (
"mime"
"net/http"
"net/url"
+ "regexp"
+ "strings"
"time"
+ "unicode/utf8"
)
// Attachment is one of a message's files, as Equipo's
diff --git a/internal/look/static/app.css b/internal/look/static/app.css
index 34e5135..a52d5f7 100644
--- a/internal/look/static/app.css
+++ b/internal/look/static/app.css
@@ -571,6 +571,21 @@ aside.ask-panel { position: absolute; top: 16px; right: 16px; bottom: 16px; widt
.card-link, .msg.coordinator a { color: var(--focus); text-decoration: underline; text-underline-offset: 3px; overflow-wrap: anywhere; }
/* The person's links keep their bubble's ink, underlined. */
.msg.person a { color: inherit; text-decoration: underline; text-underline-offset: 3px; overflow-wrap: anywhere; }
+/* A person's files above their words (ask-files): thumbnails, then a pill
+ per other file, each a link to Base's own copy. A lone picture shows
+ larger; several sit in a row of the same height. */
+.msg .files { display: flex; flex-wrap: wrap; gap: 6px; white-space: normal; }
+.msg .files:not(:last-child) { margin-bottom: 8px; }
+.msg .files a { text-decoration: none; color: inherit; }
+.msg .files .thumb { display: block; max-width: 100%; border-radius: 10px; overflow: hidden; line-height: 0; background: var(--chip); box-shadow: inset 0 0 0 1px var(--chip-edge); }
+.msg .files .thumb img { display: block; height: 112px; width: auto; max-width: 100%; object-fit: cover; }
+.msg .files .thumb:only-child img { height: auto; max-height: 240px; }
+.msg .files .file { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; max-width: 100%; padding: 0 12px 0 10px; border-radius: 12px;
+ background: var(--chip); box-shadow: inset 0 0 0 1px var(--chip-edge); font-size: 13.5px; line-height: 18px; }
+.msg .files .file:hover { box-shadow: inset 0 0 0 1px var(--accent-edge); }
+.file-mark { flex: none; width: 18px; height: 18px; stroke: currentColor; stroke-width: 1.6; stroke-linejoin: round; fill: none; }
+.file-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
+.file-size { flex: none; color: var(--haze); font-size: 12.5px; }
.card-link { display: inline-flex; align-items: center; min-height: 44px; font-weight: 500; }
.msg.card .card-state { margin: 0; color: var(--haze); font-size: 14px; }
.msg.card .choices + .card-state { margin-top: 10px; }
diff --git a/internal/web/app.go b/internal/web/app.go
index 6df5782..b37a994 100644
--- a/internal/web/app.go
+++ b/internal/web/app.go
@@ -85,7 +85,7 @@ func App(d *db.DB, cfg config.Config, mailer mail.Sender, inst Installer, logger
}
// newKey gives each chat form its own request key when the page is
// drawn; startedText says when a job started, as of then.
- pages, err := look.NewRenderer(sub, template.FuncMap{"newKey": equipo.NewKey,
+ pages, err := look.NewRenderer(sub, template.FuncMap{"newKey": equipo.NewKey, "fileSize": fileSize,
"startedText": func(at *time.Time) string { return startedText(*at, time.Now(), cfg.Timezone) }})
if err != nil {
return nil, err
diff --git a/internal/web/ask_files.go b/internal/web/ask_files.go
index 72b8fe3..80885c0 100644
--- a/internal/web/ask_files.go
+++ b/internal/web/ask_files.go
@@ -11,6 +11,8 @@ import (
"sync/atomic"
"time"
+ _ "amadan.net/rastrillo/rastrillo/locales/en" // pageLocale's marks
+ "amadan.net/rastrillo/rastrillo/number"
"github.com/go-chi/chi/v5"
"amadan.net/oficina/base/internal/equipo"
@@ -69,6 +71,36 @@ var (
// and only an image of Base's own may show.
const fileCSP = "default-src 'none'; img-src 'self'; style-src 'unsafe-inline'; sandbox"
+// pageLocale is the locale Base's pages are written in (layout.html's
+// lang), whose marks a file's size is written with; ask.js writes it in
+// the same (fileSize), from the page's lang.
+const pageLocale = "en"
+
+// fileSize is a file's size as the thread shows it: bytes under 1 KB,
+// whole KB under 1 MB, then MB to one place, rounding half up (1024 to
+// the K, as Equipo's own chips do). ask.js has the twin.
+func fileSize(n int64) string {
+ whole := func(v int64) string {
+ s, err := number.Format(pageLocale, number.Decimal(strconv.FormatInt(v, 10)), number.Options{})
+ if err != nil {
+ return strconv.FormatInt(v, 10)
+ }
+ return s
+ }
+ switch {
+ case n < 1024:
+ return whole(max(n, 0)) + " B"
+ case n < 1<<20:
+ return whole((n+512)/1024) + " KB"
+ }
+ tenths := (n*10 + 1<<19) >> 20
+ s, err := number.Format(pageLocale, number.Decimal(strconv.FormatInt(tenths/10, 10)+"."+strconv.FormatInt(tenths%10, 10)), number.Options{Places: 1})
+ if err != nil {
+ s = strconv.FormatInt(tenths/10, 10) + "." + strconv.FormatInt(tenths%10, 10)
+ }
+ return s + " MB"
+}
+
// askState is the member's live connection for a relayed call that reads
// no form. Without one it answers for the caller, as askBearer does.
func (a *app) askState(w http.ResponseWriter, r *http.Request) (askConn, bool) {
diff --git a/internal/web/files_internal_test.go b/internal/web/files_internal_test.go
new file mode 100644
index 0000000..9d48c15
--- /dev/null
+++ b/internal/web/files_internal_test.go
@@ -0,0 +1,16 @@
+package web
+
+import "testing"
+
+// A size reads as Equipo's chips write it: bytes, whole KB, then MB to
+// one place, half up.
+func TestFileSize(t *testing.T) {
+ for n, want := range map[int64]string{
+ 0: "0 B", 1023: "1,023 B", 1024: "1 KB", 1535: "1 KB", 1536: "2 KB", 839680: "820 KB",
+ 1 << 20: "1.0 MB", 1258291: "1.2 MB", 1310720: "1.3 MB", 10 << 20: "10.0 MB",
+ } {
+ if got := fileSize(n); got != want {
+ t.Errorf("%d: %q, want %q", n, got, want)
+ }
+ }
+}
diff --git a/internal/web/templates/partials/chat.html b/internal/web/templates/partials/chat.html
index 5bc96d1..dcc82cd 100644
--- a/internal/web/templates/partials/chat.html
+++ b/internal/web/templates/partials/chat.html
@@ -26,6 +26,10 @@
{{- if or (eq .State "ready") (eq .State "done")}}{{with .Go}}<a class="jv-go" href="{{.URL}}" target="_blank" rel="noopener">{{.Label}}</a>{{end}}{{end}}</section>{{end}}
{{/* A job's card that has ended, in its view: the question and how it ended, nothing to press. The panel's script draws the same (ask.js, addCard). */}}
{{define "ask-jobcard"}}<li class="jv-card" data-card="{{.ID}}" data-revision="{{.Revision}}" data-for="{{.Job}}"><p class="jv-q">{{.Question}}</p><p class="jv-result">{{.Result}}</p></li>{{end}}
+{{/* A person's files, above their words: thumbnails first, each a link to the image itself, then a pill per other file (its mark, name and size), a download. Both come from Base's own /ask/attachments, never Equipo's. Without scripts a thumbnail opens the image; the panel's script opens it in a viewer instead, and draws the same row (ask-attach.js, filesRow). */}}
+{{define "ask-files"}}<div class="files" role="group" aria-label="Attached files">{{range .}}{{if eq .Kind "image"}}<a class="thumb" href="/ask/attachments/{{.ID}}" data-attachment="{{.ID}}"><img src="/ask/attachments/{{.ID}}/preview" alt="{{.Name}}"{{if and .Width .Height}} width="{{.Width}}" height="{{.Height}}"{{end}}></a>{{end}}{{end}}{{range .}}{{if ne .Kind "image"}}<a class="file" href="/ask/attachments/{{.ID}}" download aria-label="{{.Name}}, {{fileSize .Size}}. Download">{{template "ask-file-mark"}}<span class="file-name">{{.Name}}</span><span class="file-size">{{fileSize .Size}}</span></a>{{end}}{{end}}</div>{{end}}
+{{/* A file's mark: a page with its corner folded. ask-attach.js has the same path (fileMark). */}}
+{{define "ask-file-mark"}}<svg class="file-mark" viewBox="0 0 24 24" aria-hidden="true"><path d="M14 3.5H7.5a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2V8zM14 3.5V8h4.5"/></svg>{{end}}
{{define "ask-body"}}<div class="ask-note" data-when="connect"{{if not (.Shows "connect")}} hidden{{end}}>
<p>Set up Ask Base to start asking.</p>
<form method="post" action="/ask/connect"><input type="hidden" name="next" value="{{.Next}}"><button class="btn" type="submit">Set up Ask Base</button></form>
@@ -43,7 +47,7 @@
</div>
<div class="ask-chat" data-when="ready norunner oldrunner"{{if not (.Shows "ready norunner oldrunner")}} hidden{{end}}>
<div class="log"><ol class="thread" role="log" aria-label="Conversation"{{with .Zone}} data-zone="{{.}}"{{end}} data-mark="{{asset "static/oficina/equipo.svg"}}">
-{{range .Messages}}{{if eq .Role "person"}}<li class="msg person" data-id="{{.ID}}">{{$.RichPerson .Text}}</li>{{else}}<li class="msg coordinator" data-id="{{.ID}}">{{$.Rich .Text}}{{range $.OffersAt .ID}}<div class="connect-offer" data-card="{{.ID}}" data-revision="{{.Revision}}">{{template "ask-offer" .}}</div>{{end}}</li>{{end}}
+{{range .Messages}}{{if eq .Role "person"}}<li class="msg person" data-id="{{.ID}}">{{with .Attachments}}{{template "ask-files" .}}{{end}}{{with .Text}}{{$.RichPerson .}}{{end}}</li>{{else}}<li class="msg coordinator" data-id="{{.ID}}">{{$.Rich .Text}}{{range $.OffersAt .ID}}<div class="connect-offer" data-card="{{.ID}}" data-revision="{{.Revision}}">{{template "ask-offer" .}}</div>{{end}}</li>{{end}}
{{end}}{{with .Notice}}<li class="ask-notice" data-after="{{$.NoticeAfter}}">{{.}}</li>
{{end}}{{range .Jobs}}{{template "ask-job" .}}
{{end}}{{range .LooseCards}}{{template "ask-card" .}}
diff --git a/internal/webtest/ask_files_test.go b/internal/webtest/ask_files_test.go
index 24808ee..5a9d2c4 100644
--- a/internal/webtest/ask_files_test.go
+++ b/internal/webtest/ask_files_test.go
@@ -719,3 +719,39 @@ func TestAskEventsCarryFiles(t *testing.T) {
}
t.Fatal("no message event")
}
+
+// Without scripts the thread shows a person's files above their words: a
+// thumbnail linking to the image, a pill downloading anything else, both
+// from Base; a message of files alone has no empty words.
+func TestAskThreadDrawsFiles(t *testing.T) {
+ a, f := newAskApp(t)
+ a.connectEquipo(f)
+ f.FilesReady(true)
+ f.Hold = true
+ shot := uploaded(t, a.upload("Screen <1>.png", "image/png", pngFile(t, 64, 48)))
+ csv := uploaded(t, a.upload("sales.csv", "text/csv", bytes.Repeat([]byte("1,2\n"), 300)))
+ if r := a.postJSON("/ask/messages", url.Values{"text": {"These two"}, "attachments": {shot, csv}}); r.Code != 200 {
+ t.Fatalf("send: %d %s", r.Code, r.Body)
+ }
+ lone := uploaded(t, a.upload("q.md", "text/markdown", []byte("# Q")))
+ if r := a.postJSON("/ask/messages", url.Values{"attachments": {lone}}); r.Code != 200 {
+ t.Fatalf("send: %d %s", r.Code, r.Body)
+ }
+ page := a.get("/ask")
+ for _, want := range []string{
+ `<div class="files" role="group" aria-label="Attached files"><a class="thumb" href="/ask/attachments/` + shot + `" data-attachment="` + shot + `"><img src="/ask/attachments/` + shot + `/preview" alt="Screen &lt;1&gt;.png" width="64" height="48"></a>`,
+ `<a class="file" href="/ask/attachments/` + csv + `" download aria-label="sales.csv, 1 KB. Download">`,
+ `<span class="file-name">sales.csv</span><span class="file-size">1 KB</span></a></div>These two</li>`,
+ `<li class="msg person" data-id="`,
+ `<span class="file-name">q.md</span><span class="file-size">3 B</span></a></div></li>`,
+ } {
+ if !strings.Contains(page.Body, want) {
+ t.Errorf("page lacks %s", want)
+ }
+ }
+ // Every file comes from Base: no address of Equipo's API, and nothing
+ // the page's img-src 'self' data: would refuse.
+ if strings.Contains(page.Body, "/api/v1/attachments") || strings.Contains(page.Body, "blob:") {
+ t.Error("the page links a file somewhere other than Base")
+ }
+}