Files
magpie/internal/gui/whatsnew.go
T
tobitege 21702d01ff Add German translation; harden paths against unset env vars (#770)
* Add German translation and harden paths against unset env vars

Add a German locale to the GUI, tray menu, usage alerts and website home page, and refuse to start when the home folder is unset or relative so nothing is written under the working folder; the pi plugin no longer adopts the user's PI_CODING_AGENT_DIR, and an inherited APPIMAGE no longer selects portable mode. Scripts refuse to run from the wrong folder or empty a shared out folder, and a new internal/testenv sandboxes every test package from the user's real files, which also fixes the Claude picker and Termux installer tests on Windows.

* Makefile: know magpie's folder by its go.mod, so paths with spaces work

The check compared $(CURDIR) with $(dir $(abspath …)), which make splits on spaces, so a checkout under 'My Projects' or 'John Doe' refused every target. It now reads go.mod relative to the working folder and wants magpie's module there: make -C and a symlinked Makefile work, make -f from another folder still stops before any recipe runs.

* i18n: German for the strings main added since, with a parity test

75 strings main added (group order, patterns, slow-start wait, usage caps, RTK savings …) get their German, and two that main dropped go. gui-de.test.cjs checks, as gui-ja does, that de has every zh string with its placeholders and tags, and that the window reads in German when picked or on a German system.

* appdir: ignore relative folder variables when reading, keep them for children

CheckEnv no longer unsets anything. magpie reads the XDG folders, APPDATA/LOCALAPPDATA and every agent's folder variable through appdir.Getenv/LookupEnv, which pass over a relative value; the environment stays as it is, so the agents magpie starts get the variable as the user set it. That covers what proc.UserPath lends from the login shell too, which is set after main's check and reached the desktop app unchecked. On Windows a path rooted on the current drive (\Users\x, Git Bash's /c/x) counts as usable, and magpie version and help answer without a home, before run makes any folder.

* Sandbox the tests main added since

internal/wslrun gets testenv's TestMain, and four tests main added that set HOME alone set their home with testenv.SetHome, so on Windows they don't read the runner's profile through USERPROFILE.

* agentenv: PI_CONFIG_DIR and OPENCODE_DB take relative names on purpose

omp puts $PI_CONFIG_DIR under the home and OpenCode a relative $OPENCODE_DB in its data folder, so neither depends on the working folder: appdir no longer passes them over.

* i18n: German for the Library's multi-select and System One's decision API

The 19 strings main added since (#791's skill multi-select, the decision API snippets) get their German; the one main replaced (four APIs → five APIs) goes.

* appdir: tidy the import into magpie's group; say what Go makes of /c/x

The appdir import joins the group of magpie's own imports where a file has one. rooted's comment now says that Go takes a Git Bash path like /c/x as \c\x on the current drive, not C:\x.
2026-10-04 23:30:56 +08:00

265 lines
8.7 KiB
Go

package gui
import (
"context"
"encoding/json"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// whatsNew shows what changed once magpie runs a newer release than it last
// did (a Discord user: to see whether their issue was fixed). The version it
// last ran is kept in magpie's own folder; as it starts on a newer one, the
// notes of every release since are asked of the site (where the update feed
// is) the first time a window or magpie web's page asks, and shown once.
// Offline, nothing is shown. Settings can open them again. The notes are
// in the page's language, Chinese or English, where a release has them in
// it (freecss on Discord: they should follow Settings' language).
//
// The dialog comes up by itself only on the first start of a version the
// user didn't ask for from inside magpie (#525: releases come many a day,
// and a dialog after every "Restart to update" is in the way): a restart
// into an update leaves a note of the version it installs, and that version
// starting finds it and keeps quiet; the notes are still a click away in
// Settings. The dialog's "Don't show again today" keeps it quiet for the
// rest of the local day.
type whatsNew struct {
mu sync.Mutex
after string // the version last run, "" for the current one's notes alone
pending bool // an upgrade whose notes are still to be shown
quiet bool // the upgrade was the user's own restart to update: not shown by itself
shown bool // a page has shown them
notes map[string][]update.Note // by language
}
var news = &whatsNew{}
// started is when this process began, to tell magpie's folder from before
// it apart from one this run just made.
var started = time.Now()
const lastRunFile = "last-version"
// updatedInAppFile holds the version a restart to update installed, for
// that version's first start to keep its notes to Settings (#525).
const updatedInAppFile = "updated-in-app"
// quietDayFile holds the local day ("2006-01-02") the dialog's "Don't show
// again today" was ticked on.
const quietDayFile = "whatsnew-quiet-day"
// today is the local day, a variable for tests.
var today = func() string { return time.Now().Format("2006-01-02") }
// updatedInApp notes that a restart to update is installing version.
func updatedInApp(version string) {
if version == "" {
return
}
dir := settings.Dir()
if err := os.MkdirAll(dir, 0o755); err == nil {
err = os.WriteFile(filepath.Join(dir, updatedInAppFile), []byte(version+"\n"), 0o644)
if err != nil {
log.Println("updated in app:", err)
}
}
}
// quietToday is whether "Don't show again today" was ticked today.
func quietToday() bool {
b, err := os.ReadFile(filepath.Join(settings.Dir(), quietDayFile))
return err == nil && strings.TrimSpace(string(b)) == today()
}
// setQuietToday ticks (or unticks) "Don't show again today".
func setQuietToday(on bool) error {
f := filepath.Join(settings.Dir(), quietDayFile)
if !on {
if err := os.Remove(f); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
return err
}
return os.WriteFile(f, []byte(today()+"\n"), 0o644)
}
// start reads the version last run and puts this one in its place.
func (n *whatsNew) start() {
dir := settings.Dir()
b, _ := os.ReadFile(filepath.Join(dir, lastRunFile))
last := strings.TrimSpace(string(b))
after, show := update.ShowNotes(last, Version, last == "" && usedBefore(dir))
// a restart to update into this version: its notes wait in Settings
mark := filepath.Join(dir, updatedInAppFile)
m, err := os.ReadFile(mark)
inApp := err == nil && strings.TrimSpace(string(m)) == Version
if err == nil && (inApp || update.Released(Version)) {
os.Remove(mark) // read once; another version's is stale
}
n.mu.Lock()
n.after, n.pending, n.quiet = after, show, inApp
n.mu.Unlock()
if !update.Released(Version) || last == Version {
return // a build from source leaves the release it came after
}
if err := os.MkdirAll(dir, 0o755); err == nil {
err = os.WriteFile(filepath.Join(dir, lastRunFile), []byte(Version+"\n"), 0o644)
if err != nil {
log.Println("last version:", err)
}
}
}
// usedBefore is whether magpie's folder held its files before this run: a
// magpie from before the version was kept, not a fresh install.
func usedBefore(dir string) bool {
for _, f := range []string{"settings.json", "providers.json"} {
if fi, err := os.Stat(filepath.Join(dir, f)); err == nil && fi.ModTime().Before(started) {
return true
}
}
return false
}
type whatsNewJSON struct {
Show bool `json:"show"`
Current string `json:"current"`
Releases []update.Note `json:"releases"`
// Error: the notes couldn't be had (the site down, or GitHub failing
// it), as opposed to releases that have none (#661); URL is the
// current version's release page, where they can be read instead
Error string `json:"error,omitempty"`
URL string `json:"url,omitempty"`
}
// get is what a page shows: after an upgrade, the notes since, once (a
// page showing them says so with seen); asked again (all, from Settings),
// those or the current version's alone.
func (n *whatsNew) get(ctx context.Context, all bool, lang string) whatsNewJSON {
n.mu.Lock()
after, pending, shown := n.after, n.pending, n.shown
if !pending {
after = ""
}
notes := n.notes[lang]
// shown by itself: not after a restart to update, nor on a day it was
// asked not to be
popup := pending && !shown && !n.quiet && !quietToday()
n.mu.Unlock()
j := whatsNewJSON{Current: Version, Releases: []update.Note{}}
if (!popup && !all) || !update.Released(Version) {
return j
}
j.URL = update.ReleasePage(Version)
if notes == nil {
var err error
notes, err = fetchNotes(ctx, after, Version, lang)
if err != nil {
j.Error = err.Error()
}
if len(notes) > 0 { // kept: they don't change while magpie runs
n.mu.Lock()
if n.notes == nil {
n.notes = map[string][]update.Note{}
}
n.notes[lang] = notes
n.mu.Unlock()
}
}
if len(notes) > 0 {
j.Releases = notes
}
j.Show = popup && len(j.Releases) > 0
return j
}
func (n *whatsNew) seen() {
n.mu.Lock()
n.shown = true
n.mu.Unlock()
}
// fetchNotes asks the site for the notes since after; a site without the
// list yet, or one that can't be reached, leaves the update feed, whose
// newest release has its notes, for when that is the current one. The
// error says the notes couldn't be had, which the page tells apart from
// releases without notes (#661): the list failed and the feed had none.
func fetchNotes(ctx context.Context, after, upto, lang string) ([]update.Note, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
notes, failed := update.NotesBetween(ctx, after, upto, lang)
if failed == nil && len(notes) > 0 {
return notes, nil
}
if failed != nil {
log.Println("release notes:", failed)
}
rel, err := update.LatestIn(ctx, lang)
if err != nil {
return nil, failed
}
notes = update.Between([]update.Note{{Version: rel.Version, Notes: rel.Notes, URL: rel.URL}}, after, upto)
if len(notes) == 0 {
return nil, failed
}
return notes, nil
}
// pageLang is the language a page asks in, its own (?lang=, zh or en), or
// without one the app's, as the tray menu has it, in the languages release
// notes come in.
func pageLang(r *http.Request) string {
if l := askedLang(r); l != "" {
return l
}
return notesLang(trayLang(settings.Load().Lang, systemLang))
}
// askedLang is the language a page names (?lang=): zh for any Chinese, en
// for anything else, "" for none.
func askedLang(r *http.Request) string {
switch l := strings.ToLower(r.URL.Query().Get("lang")); {
case strings.HasPrefix(l, "zh"):
return "zh"
case l != "":
return "en"
}
return ""
}
func whatsNewRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/whatsnew", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, news.get(r.Context(), r.URL.Query().Get("all") != "", pageLang(r)))
})
mux.HandleFunc("POST /api/whatsnew/seen", func(rw http.ResponseWriter, r *http.Request) {
news.seen()
rw.WriteHeader(http.StatusNoContent)
})
// today: the dialog's "Don't show again today", ticked or unticked
mux.HandleFunc("POST /api/whatsnew/today", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
On bool `json:"on"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
if err := setQuietToday(in.On); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.WriteHeader(http.StatusNoContent)
})
}