diff --git a/internal/gui/whatsnew.go b/internal/gui/whatsnew.go index d4862be31..e564689f9 100644 --- a/internal/gui/whatsnew.go +++ b/internal/gui/whatsnew.go @@ -12,6 +12,7 @@ import ( "strings" "sync" "time" + "unicode" "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/update" @@ -40,14 +41,57 @@ type whatsNew struct { 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 and channel (notesKey) - back map[string]backNotes // Settings' "this version and n before it", by language + // again: when notes kept by that key, some in English though asked in + // Chinese, are asked for again (see untranslated) + again map[string]time.Time + back map[string]backNotes // Settings' "this version and n before it", by language } +// notesAgain is how long notes some of which came without their +// translation are kept before the site is asked again: magpie-releases' +// notes.yml writes a release's Chinese notes each hour once DeepSeek +// answers (#2260), the site keeps an answer five minutes, and a page kept +// open, or opened again, should show them without a restart (yuu003348 on +// Discord: every release's notes in English). +const notesAgain = 10 * time.Minute + +// notesNow is the clock notesAgain is measured by, a variable for tests. +var notesNow = time.Now + +// untranslated is whether some of notes, asked in lang, came in English: +// a release the release workflow couldn't translate yet. Chinese is the +// only language the notes are translated into; a release's Chinese notes +// always hold Han, its English ones (almost) never. +func untranslated(notes []update.Note, lang string) bool { + if lang != "zh" { + return false + } + for _, r := range notes { + if !strings.ContainsFunc(r.Notes, func(c rune) bool { return unicode.Is(unicode.Han, c) }) { + return true + } + } + return false +} + +// askAgainAt is when notes had now in lang are asked for again, zero for +// never while magpie runs. +func askAgainAt(notes []update.Note, lang string) time.Time { + if untranslated(notes, lang) { + return notesNow().Add(notesAgain) + } + return time.Time{} +} + +// due is whether a time askAgainAt gave has come. +func due(at time.Time) bool { return !at.IsZero() && !notesNow().Before(at) } + // backNotes is the longest "this version and n before it" asked for in a // language: a shorter one is cut from it, a longer one asked for again. type backNotes struct { - n int - list []update.Note + n int + list []update.Note + again time.Time // askAgainAt: some came in English though asked in Chinese } var news = &whatsNew{} @@ -164,6 +208,7 @@ func (n *whatsNew) get(ctx context.Context, all bool, lang string) whatsNewJSON } key := notesKey(lang) notes := n.notes[key] + again := notes != nil && due(n.again[key]) // 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() @@ -173,20 +218,30 @@ func (n *whatsNew) get(ctx context.Context, all bool, lang string) whatsNewJSON return j } j.URL = update.ReleasePage(Version) - if notes == nil { - var err error - notes, err = fetchNotes(ctx, after, Version, lang) - if err != nil { + if notes == nil || again { + got, err := fetchNotes(ctx, after, Version, lang) + if err != nil && !again { j.Error = err.Error() } - if len(notes) > 0 { // kept: they don't change while magpie runs - n.mu.Lock() + n.mu.Lock() + if len(got) > 0 { + // kept: they don't change while magpie runs, but for a + // translation written since (askAgainAt) + notes = got if n.notes == nil { n.notes = map[string][]update.Note{} } - n.notes[key] = notes - n.mu.Unlock() + n.notes[key] = got } + if len(got) > 0 || again { + if n.again == nil { + n.again = map[string]time.Time{} + } + // asked again failing: the notes had are shown, and asked for + // again later + n.again[key] = askAgainAt(notes, lang) + } + n.mu.Unlock() } if len(notes) > 0 { j.Releases = notes @@ -220,20 +275,31 @@ func (n *whatsNew) before(ctx context.Context, count int, lang string) whatsNewJ n.mu.Lock() got, ok := n.back[lang] n.mu.Unlock() - if !ok || got.n < count { + again := ok && got.n >= count && due(got.again) + if !ok || got.n < count || again { ctx, cancel := context.WithTimeout(ctx, 15*time.Second) list, err := update.NotesBack(ctx, Version, count, lang) cancel() - if err != nil { + switch { + case err != nil && again: + // the range had stays, asked for again later + log.Println("release notes:", err) + n.mu.Lock() + if b, ok := n.back[lang]; ok && b.n == got.n { + b.again = notesNow().Add(notesAgain) + n.back[lang] = b + } + n.mu.Unlock() + case err != nil: log.Println("release notes:", err) j.Error = err.Error() - } else { - got, ok = backNotes{count, list}, true + default: + got, ok = backNotes{count, list, askAgainAt(list, lang)}, true n.mu.Lock() if n.back == nil { n.back = map[string]backNotes{} } - if n.back[lang].n < count { + if n.back[lang].n <= count { n.back[lang] = got } n.mu.Unlock() diff --git a/internal/gui/whatsnew_lang_test.go b/internal/gui/whatsnew_lang_test.go index f35f7b454..2ac88728d 100644 --- a/internal/gui/whatsnew_lang_test.go +++ b/internal/gui/whatsnew_lang_test.go @@ -109,3 +109,109 @@ func TestUpdateNotesInPageLang(t *testing.T) { t.Fatalf("asked in %q", asked) } } + +// Notes the release workflow couldn't translate yet (DeepSeek out of +// credit, #2260) come in English though asked in Chinese; once +// magpie-releases' notes.yml writes the Chinese, What's new shows it +// without a restart (yuu003348 on Discord: every release's notes in +// English). Notes all in Chinese, or asked in English, are kept for the +// run, and a failed ask again keeps the English had. +func TestWhatsNewAsksAgainForTranslation(t *testing.T) { + home := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", home) + dir := filepath.Join(home, "magpie") + os.MkdirAll(dir, 0o755) + os.WriteFile(filepath.Join(dir, lastRunFile), []byte("0.1.603\n"), 0o644) + var mu sync.Mutex + asked := map[string]int{} // by path and lang + translated, down := false, false + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + asked[r.URL.Query().Get("limit")+"|"+r.URL.Query().Get("lang")]++ + if down { + http.Error(w, "down", 502) + return + } + older := notesEn // a release from before the marker + newest := notesEn + "\n\n### Install\n\nDownload it." // DeepSeek turned away + if translated { + newest = notesBoth + } + json.NewEncoder(w).Encode(map[string]any{"releases": []update.Note{ + {Version: "0.1.604", Notes: newest}, + {Version: "0.1.603", Notes: older + "\n\n\n\n" + notesZh}, + }}) + })) + defer srv.Close() + t.Setenv("MAGPIE_NOTES_FEED", srv.URL) + t.Setenv("MAGPIE_UPDATE_FEED", "http://127.0.0.1:1/") + old, oldNow := Version, notesNow + defer func() { Version, notesNow = old, oldNow }() + now := time.Date(2026, 11, 2, 8, 0, 0, 0, time.UTC) + notesNow = func() time.Time { return now } + Version = "0.1.604" + n := &whatsNew{} + n.start() + count := func(k string) int { + mu.Lock() + defer mu.Unlock() + return asked[k] + } + + if j := n.get(context.Background(), true, "zh"); len(j.Releases) != 1 || j.Releases[0].Notes != notesEn { + t.Fatalf("zh untranslated: %+v", j) + } + if j := n.before(context.Background(), 10, "zh"); len(j.Releases) != 2 || j.Releases[0].Notes != notesEn || j.Releases[1].Notes != notesZh { + t.Fatalf("zh back untranslated: %+v", j) + } + n.get(context.Background(), true, "en") + n.before(context.Background(), 10, "en") + // within notesAgain: what was had, the site not asked + now = now.Add(notesAgain - time.Second) + n.get(context.Background(), true, "zh") + n.before(context.Background(), 10, "zh") + if count("|zh") != 1 || count("11|zh") != 1 { + t.Fatalf("asked again too soon: %v", asked) + } + // later, the site down: the English had, no error, asked again after + mu.Lock() + down = true + mu.Unlock() + now = now.Add(2 * time.Second) + if j := n.get(context.Background(), true, "zh"); j.Error != "" || len(j.Releases) != 1 || j.Releases[0].Notes != notesEn { + t.Fatalf("zh again, site down: %+v", j) + } + if j := n.before(context.Background(), 10, "zh"); j.Error != "" || len(j.Releases) != 2 { + t.Fatalf("zh back again, site down: %+v", j) + } + // the Chinese written since: shown, in both lists + mu.Lock() + down, translated = false, true + mu.Unlock() + now = now.Add(notesAgain) + if j := n.get(context.Background(), true, "zh"); len(j.Releases) != 1 || j.Releases[0].Notes != notesZh { + t.Fatalf("zh once translated: %+v", j) + } + if j := n.before(context.Background(), 10, "zh"); len(j.Releases) != 2 || j.Releases[0].Notes != notesZh { + t.Fatalf("zh back once translated: %+v", j) + } + // all in Chinese now, and English never needs it: kept for the run + before := map[string]int{"|zh": count("|zh"), "11|zh": count("11|zh")} + now = now.Add(24 * time.Hour) + n.get(context.Background(), true, "zh") + n.before(context.Background(), 10, "zh") + n.get(context.Background(), true, "en") + n.before(context.Background(), 10, "en") + if count("|zh") != before["|zh"] || count("11|zh") != before["11|zh"] || count("|en") != 1 || count("11|en") != 1 { + t.Fatalf("asked again with nothing to wait for: %v", asked) + } +} + +func TestUntranslated(t *testing.T) { + zh := []update.Note{{Notes: notesZh}} + en := []update.Note{{Notes: notesZh}, {Notes: notesEn}} + if untranslated(zh, "zh") || !untranslated(en, "zh") || untranslated(en, "en") || untranslated(nil, "zh") { + t.Fatal("untranslated") + } +}