Files
magpie/internal/provider/forget_account.go
T
yetone 89d305229b providers: a removed plugin subscription is named as its row was, and can be signed out for good (#694, mintonight: 点了移除,国际版账号没有消失,放在 plugin 里无法彻底删除;再添加回来还是旧账号) — Add a provider's "Removed from magpie — still signed in" listed a removed plugin account by its agent, which for every plugin's sign-in is "plugin", with the generic logo; and its only way out was Add it back, which brought the old account back, since Remove only hides an account and nothing signed it out. Now a removed account is named and drawn as its row was (accountLabel, shared with the provider row: a plugin's by the provider it signs in to, a moved built-in's by the built-in's logo; Exclusion.Name carries it to the CLI's list too). Its row in the add sheet opens the app's menu (openProtoMenu, the page left where it was): Add it back as before, or Sign out…, which asks first and posts provider/forget; the Providers page's reminder line has Sign out beside Add it back and Don't remind me. provider.ForgetAccount signs a plugin's accounts out of plugin-auth.json (the host's signOut, else under plugin-auth.json.lock), takes them out of logins.json and, for a moved built-in, out of the move's backup so moving back doesn't restore them; an agent's own sign-in that magpie only reads is hidden, as an account's Remove hides it; a built-in's saved accounts go through ForgetLogin, the one in use last. Signing in again then starts afresh. zh strings added. TestForgetRemovedPluginAccount (bun fake plugin: removed is named FakeCo, forgotten leaves plugin-auth.json, logins.json and the removed list empty, Add back brings nothing) and removed-forget.test.cjs (webkit, en/zh: the row named Qoder, the menu with no request and no scroll, Sign out asks then posts forget with the provider id, Add it back still posts show, the reminder's Sign out) fail on the old code and pass; add-sheet, click-scroll, plugin-two-subs and saved-signedout-remove still pass. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried with a real Qoder account; the plugins repo needs no change (the Qoder plugin keeps its sign-ins only in plugin-auth.json).
2026-10-03 23:09:13 +08:00

127 lines
3.6 KiB
Go

package provider
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// ForgetAccount signs a subscription's accounts out of magpie for good, the
// way past a removal (#694: a removed Qoder came back with its old account
// each time it was added back, and nothing took it away). A plugin's
// sign-ins leave plugin-auth.json (the plugin host's signOut when one runs,
// else under plugin-auth.json.lock) and logins.json, and a moved built-in's
// backup of them goes too, so moving back doesn't bring them either; a
// built-in's saved accounts go as each account's Remove takes them. An
// agent's own sign-in, which magpie only reads, is hidden as Remove hides
// it. Signing in again starts afresh.
func ForgetAccount(id string) error {
p, ok := find(Accounts(), strings.ToLower(strings.TrimSpace(id)))
if !ok || p.Account == nil {
return fmt.Errorf("no signed-in account %q", id)
}
if p.IsPlugin() {
pp, ok := PluginOf(p.ID)
if !ok {
pp = *p.Account.plugin
}
return forgetPluginAccounts(pp)
}
return forgetLogins(p.Account.Agent)
}
// forgetLogins forgets each of a built-in's accounts, the one in use first
// last, as the accounts list removes them one by one.
func forgetLogins(agent string) error {
ls := Logins(agent)
slices.SortStableFunc(ls, func(a, b Login) int {
switch {
case a.Active == b.Active:
return 0
case a.Active:
return 1
}
return -1
})
var errs []error
for _, l := range ls {
if err := ForgetLogin(agent, l.User); err != nil {
errs = append(errs, err)
}
}
ForgetAccounts()
return errors.Join(errs...)
}
// forgetPluginAccounts signs every account of the plugin's provider out:
// the ones magpie signed in to through it from plugin-auth.json, and the
// agent's own (a CLI's sign-in the plugin reads) hidden.
func forgetPluginAccounts(pp plugin.Provider) error {
agent := pluginAgent(pp)
var errs []error
keys, users := map[string]bool{}, map[string]bool{}
var owners []string
for _, l := range pluginLogins(pp) {
if l.Own {
owners = append(owners, l.User)
continue
}
keys[l.acct.Key], users[strings.ToLower(l.User)] = true, true
}
own := len(owners) > 0
// the agent's own sign-in first, while the list still has every
// account: hidden, as each account's Remove hides it
for _, u := range owners {
if err := forgetPluginLogin(pp, u); err != nil {
errs = append(errs, err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if !own {
// every sign-in kept under the provider's id, listed or not
if err := plugin.SignOut(ctx, pp.ID, ""); err != nil {
errs = append(errs, err)
}
} else {
for k := range keys {
if err := plugin.SignOut(ctx, pp.ID, k); err != nil {
errs = append(errs, err)
}
}
}
loginsMu.Lock()
ls := readLogins()
n := len(ls)
ls = slices.DeleteFunc(ls, func(l savedLogin) bool {
return l.Agent == agent && (!own || keys[l.Home]) && !l.own()
})
if len(ls) != n {
if err := writeLogins(ls); err != nil {
errs = append(errs, err)
}
}
loginsMu.Unlock()
if Moved(pp.ID) {
// moving back would bring the accounts forgotten back from the
// backup the move set aside
mv := movers[pp.ID]
err := setMigration(pp.ID, func(m *Migration) {
m.Accounts = slices.DeleteFunc(m.Accounts, func(a movedAccount) bool { return !a.Own && (!own || keys[a.Key]) })
m.Backup = slices.DeleteFunc(m.Backup, func(b savedLogin) bool {
return mv != nil && slices.Contains(mv.agents, b.Agent) && !b.own() && (!own || users[strings.ToLower(b.User)])
})
})
if err != nil {
errs = append(errs, err)
}
}
ForgetAccounts()
return errors.Join(errs...)
}