Files
magpie/internal/gui/key_replace.go
T
yetone 1502433978 gui: a key's value can be changed in place, keeping its name, place and settings, and its rest is lifted (#1869)
A key rotated at its vendor had to be removed and added again, which
lost its name, its place in the order, its weight, protocol, models,
proxy and concurrency, and any gateway key restricted to it. As decided
on #1869, in multi-key mode the row now has a Change key action, with a
double-click on the row as a shortcut.

- provider.ReplaceKey(id, ref, key) sets the key's new value and moves
  what providers.json keys by the key's fingerprint (AccountModels,
  AccountProxies, AccountConcurrency) to the new one. An empty value, an
  unknown key and a value another key of the provider already holds are
  refused. The same value is a no-op.
- access.ReplaceAccount moves gateway keys' Accounts entries from
  provider/old to provider/new, under the caller-keys lock.
- gateway.Server.UnrestAll lifts a rest key and every per-model rest
  under it. The GUI's keys/replace lifts the key's rest (provider#key, and
  the provider's own rest key when it is the only key on), so the new
  value is asked from the next request.
- app.js: a pencil beside the key's copy button opens a password field in
  place of the masked value. Enter saves, Escape leaves it, and a
  double-click on the row away from its controls opens it too. The button
  has its own class (.key-edit) with the copy button's look.
- i18n: four strings in zh, zh-TW, ja and de.
- providers-accounts.md: ReplaceKey in the Keys table.

Past requests keep the old fingerprint in history, under the same name.
A rest is lifted only in the gateway this magpie serves.

Run:
- go test ./internal/gateway ./internal/provider ./internal/access
  ./internal/gui -count=1: ok. New TestReplaceKeyKeepsItsPlace,
  TestKeysReplaceKeepsTheKeyAndLiftsItsRest and
  TestUnrestAllLiftsAKeysModels each fail with their change taken out.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows.
- key-replace.test.cjs (new): en/zh/zh-TW/ja/de, chromium and webkit,
  1100 and 420px. Checks the post, order, note, no sideways scroll, and
  no scroll on change or on double-click plus Escape.
- With key-copy, rest-try-now and key-import: 96/96.
- 72 key*/routing*/rest* GUI files at concurrency 6: 863/871. The 8 reds
  were key-copy, whose button.copy locator matched the pencil; that's why
  it has its own class now, and key-copy passes.
- Under load, about one run in 300 saw the editor body go back to the top
  after the copy button's double-click, with no script scrolling it. That
  is #1504 (findings added there). The test checks scroll before that
  step.
2026-10-13 07:30:06 +08:00

48 lines
1.3 KiB
Go

package gui
import (
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
)
// replaceKey puts a new value in place of one of a provider's keys
// (#1869), keeping its name, place and settings (provider.ReplaceKey).
// A gateway key held to the key is held to it still (access.ReplaceAccount),
// and the key's rests are lifted, as its new value is yet to be refused:
// the ones it had by its old fingerprint, and the provider's own while it
// was the one key on (provider.RestKey).
func replaceKey(id, ref, key string) error {
p, err := provider.Find(id)
if err != nil {
return err
}
var rest []string
for _, k := range p.KeyList() {
if k.ID == ref {
rest = append(rest, p.ID+"#"+ref)
if r := p.RestKey(k); k.On && r != rest[0] {
rest = append(rest, r)
}
}
}
was, now, err := provider.ReplaceKey(id, ref, key)
if err != nil {
return err
}
if _, err := access.ReplaceAccount(p.ID, was, now); err != nil {
return err
}
liftRests(rest)
return nil
}
// liftRests lifts every rest of each key named, in the gateway this magpie
// serves; one served by another magpie keeps them. Swapped in tests.
var liftRests = func(keys []string) {
if gw := served.Load(); gw != nil {
for _, k := range keys {
gw.UnrestAll(k)
}
}
}