Phase 6: Mini App
A Telegram Mini App served by the core under /app/, embedded in the binary. - API under /app/api/ with initData validated (HMAC, age) on every call - Pages: Home, Queue, Buy (target, product, invoice), History, Groups, group detail (Overview, Settings, Wallet, Usage) and Me - Every action runs through the same service, billing and scheduler code as the chat, so limits, prices and admin checks are decided on the server - Texts come from the catalog over the API; layout mirrors for RTL - Front end in web/miniapp (Vite, React, TypeScript); the build is committed and guarded by a source hash test; the Docker image rebuilds it - MINIAPP_ENABLED with an https PUBLIC_URL; entry points from /app, group settings and the over-limit offer - Browser test (make miniapp-e2e) against a core in test mode - Fix: each download attempt gets its own scratch directory, so a retry cannot lose its file to the cleanup of the failed attempt Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
51371da0e0
commit
4f11f939a0
@@ -34,6 +34,9 @@ type Bundle struct {
|
||||
names []string
|
||||
matcher language.Matcher
|
||||
log *slog.Logger
|
||||
// raw keeps each catalog as parsed TOML, for exporting the Mini App's
|
||||
// messages to the browser.
|
||||
raw map[string]map[string]any
|
||||
|
||||
mu sync.Mutex
|
||||
localizers map[string]*goi18n.Localizer
|
||||
@@ -51,6 +54,7 @@ func Load(fsys fs.FS, log *slog.Logger) (*Bundle, error) {
|
||||
log: log,
|
||||
localizers: map[string]*goi18n.Localizer{},
|
||||
printers: map[string]*message.Printer{},
|
||||
raw: map[string]map[string]any{},
|
||||
}
|
||||
b.bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
|
||||
files, err := fs.Glob(fsys, "*.toml")
|
||||
@@ -71,6 +75,10 @@ func Load(fsys fs.FS, log *slog.Logger) (*Bundle, error) {
|
||||
if mf.Tag == language.English {
|
||||
hasDefault = true
|
||||
}
|
||||
var doc map[string]any
|
||||
if err := toml.Unmarshal(data, &doc); err == nil {
|
||||
b.raw[mf.Tag.String()] = doc
|
||||
}
|
||||
}
|
||||
if !hasDefault {
|
||||
return nil, fmt.Errorf("i18n: the %s catalog is missing", Default)
|
||||
@@ -108,6 +116,7 @@ func (b *Bundle) Reload(fsys fs.FS) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.bundle, b.tags, b.names, b.matcher = fresh.bundle, fresh.tags, fresh.names, fresh.matcher
|
||||
b.raw = fresh.raw
|
||||
b.localizers = map[string]*goi18n.Localizer{}
|
||||
b.printers = map[string]*message.Printer{}
|
||||
return nil
|
||||
@@ -227,3 +236,88 @@ func (b *Bundle) Has(lang, id string) bool {
|
||||
})
|
||||
return err == nil && gotTag == tag
|
||||
}
|
||||
|
||||
// rtlLangs are the languages written right to left.
|
||||
var rtlLangs = map[string]bool{"ar": true, "he": true, "fa": true, "ur": true, "ps": true, "sd": true, "yi": true, "dv": true, "ckb": true, "ug": true}
|
||||
|
||||
// Dir returns the text direction of a language: "rtl" or "ltr".
|
||||
func Dir(lang string) string {
|
||||
base, _, _ := strings.Cut(strings.ToLower(lang), "-")
|
||||
if rtlLangs[base] {
|
||||
return "rtl"
|
||||
}
|
||||
return "ltr"
|
||||
}
|
||||
|
||||
var pluralForms = map[string]bool{"zero": true, "one": true, "two": true, "few": true, "many": true, "other": true}
|
||||
|
||||
// Section exports one top-level table of the catalogs for a client that
|
||||
// renders text itself, such as the Mini App. Keys are flattened with dots.
|
||||
// A value is a string, or an object of plural forms. Placeholders are
|
||||
// rewritten from {{.Name}} to {Name}. Languages fall back the same way as
|
||||
// everywhere else: the exact one, its base language, then English.
|
||||
func (b *Bundle) Section(lang, table string) map[string]any {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
out := map[string]any{}
|
||||
chain := []string{Default}
|
||||
if base, _, ok := strings.Cut(lang, "-"); ok && base != Default {
|
||||
chain = append(chain, base)
|
||||
}
|
||||
if lang != Default {
|
||||
chain = append(chain, lang)
|
||||
}
|
||||
for _, l := range chain {
|
||||
doc, ok := b.raw[l]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if t, ok := doc[table].(map[string]any); ok {
|
||||
flatten("", t, out)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func flatten(prefix string, in map[string]any, out map[string]any) {
|
||||
for k, v := range in {
|
||||
key := k
|
||||
if prefix != "" {
|
||||
key = prefix + "." + k
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
out[key] = clientTemplate(x)
|
||||
case map[string]any:
|
||||
if isPlural(x) {
|
||||
forms := map[string]string{}
|
||||
for form, text := range x {
|
||||
if s, ok := text.(string); ok {
|
||||
forms[form] = clientTemplate(s)
|
||||
}
|
||||
}
|
||||
out[key] = forms
|
||||
} else {
|
||||
flatten(key, x, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isPlural(m map[string]any) bool {
|
||||
if len(m) == 0 {
|
||||
return false
|
||||
}
|
||||
for k, v := range m {
|
||||
if _, ok := v.(string); !ok || !pluralForms[k] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// clientTemplate turns {{.Name}} into {Name}.
|
||||
func clientTemplate(s string) string {
|
||||
s = strings.ReplaceAll(s, "{{.", "{")
|
||||
return strings.ReplaceAll(s, "}}", "}")
|
||||
}
|
||||
|
||||
@@ -144,3 +144,59 @@ func TestFormatting(t *testing.T) {
|
||||
t.Errorf("Percent = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSectionExportsForTheMiniApp(t *testing.T) {
|
||||
fsys := fstest.MapFS{
|
||||
"en.toml": {Data: []byte(`
|
||||
[app]
|
||||
hello = "Hi {{.Name}}"
|
||||
only_english = "English only"
|
||||
[app.tab]
|
||||
home = "Home"
|
||||
[app.files]
|
||||
one = "{{.Count}} file"
|
||||
other = "{{.Count}} files"
|
||||
[other]
|
||||
secret = "not exported"
|
||||
`)},
|
||||
"es.toml": {Data: []byte(`
|
||||
[app]
|
||||
hello = "Hola {{.Name}}"
|
||||
[app.tab]
|
||||
home = "Inicio"
|
||||
[app.files]
|
||||
one = "{{.Count}} archivo"
|
||||
other = "{{.Count}} archivos"
|
||||
`)},
|
||||
}
|
||||
b, err := i18n.Load(fsys, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := b.Section("es-PY", "app")
|
||||
if got["hello"] != "Hola {Name}" || got["tab.home"] != "Inicio" {
|
||||
t.Fatalf("section = %v", got)
|
||||
}
|
||||
// A message missing in the language comes from English, like in chat.
|
||||
if got["only_english"] != "English only" {
|
||||
t.Fatalf("no fallback to English: %v", got["only_english"])
|
||||
}
|
||||
forms, ok := got["files"].(map[string]string)
|
||||
if !ok || forms["one"] != "{Count} archivo" || forms["other"] != "{Count} archivos" {
|
||||
t.Fatalf("plural forms = %v", got["files"])
|
||||
}
|
||||
if _, leaked := got["secret"]; leaked || len(b.Section("es", "missing")) != 0 {
|
||||
t.Fatal("a section must hold its own table only")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDir(t *testing.T) {
|
||||
for lang, want := range map[string]string{
|
||||
"en": "ltr", "es-PY": "ltr", "nl": "ltr", "en-XA": "ltr",
|
||||
"ar": "rtl", "ar-EG": "rtl", "he": "rtl", "fa-IR": "rtl", "ur": "rtl", "": "ltr",
|
||||
} {
|
||||
if got := i18n.Dir(lang); got != want {
|
||||
t.Errorf("Dir(%q) = %q, want %q", lang, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user