Files
magpie/internal/provider/checkin.go
T
yetone 0c7e6d834d provider: Trae CN's daily check-in (每日签到) is pressed once a Beijing day for each Trae CN account, as WorkBuddy's is (#694, Hu9956: TRAE cn也有每天签到送100积分活动,这个会考虑支持吗) — Trae CN is served only by its community plugin (trae-cn, no built-in, no mover), so magpie asks Trae through the plugin's fetch, which from trae 0.1.4 (magpie-community/plugins, branch trae-checkin) sends api.trae.cn's /trae/api/ pages as the account (Cloud-IDE-JWT renewed when near its end, its device id): POST /trae/api/v2/ug/checkin_credits/status with {}, and /claim with {} only while enable is set and checked_in isn't. These are the endpoints autumnsentiment/Trae2api-cn (the relay the plugin's protocol came from) and qilimixingkong/trae-checkin both call; an unauthenticated POST to /status answers 200 {code:1001, enable:false, checked_in:false} and a made-up sibling path 404s. Answers: not enabled → inactive, checked_in → done with its credits, a claim → claimed +credits(+extra_credits), 9095 (this device is in today) → not eligible for the day (no other device id is sent to get round it, as qilimixingkong's does), 1001 or the plugin's X-Magpie-Sign-In: expired → failed, sign in again; 9074 and anything else → failed and tried again; a plugin before 0.1.4 (400, not JSON) → failed, update it. The day loop and its file are WorkBuddy's, made generic: checkin.go's checkiner (accounts as checkinAcct{key, do}) holds checkinNow, which wbCheckiner now wraps; keepCheckedIn and wbCheckinLoop.tick take a checkinRunner and a log label; withCheckins goes through markCheckins over checkinCards. Trae's is kept in trae-checkin.json by trae-cn|uid; settings.TraeCheckin (traeCheckin) is its own switch; the gateway runs KeepTraeCheckedIn beside KeepWorkBuddyCheckedIn; WithCheckins marks Trae CN cards too, with SubscriptionQuota.CheckinBy and WorkBuddyCheckin.By "trae" (never kept), so the Usage card's row uses POST /api/settings/trae-checkin and /api/usage/trae-checkin and its first Trae card holds Trae's own Auto check-in and Check in now; Settings' Usage part gains a Trae CN tab (traeList) shown while an account is signed in or the switch is on; magpie accounts checkin and the TUI's c check Trae CN accounts in too (the footer says daily check-in). Tests: trae_checkin_test.go (once a day: status then claim, nothing sent again that day, done the next; inactive, 9095 held for the day, 9074 retried, expired, an old plugin; Trae cards marked, another plugin's not), TestTUIChecksTraeIn, usage-checkin.test.cjs's Trae CN card (its own switch and press, WorkBuddy's untouched) and settings-groups.test.cjs's Trae CN tab; the WorkBuddy check-in tests pass unchanged. go vet ./internal/..., builds (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass; the GUI tests pass on WebKit (Playwright's Chromium here fails to start, icudtl.dat missing, for the old tests too). Not tried with a real Trae CN account: none is signed in to the owner's magpie (no trae-cn in plugin-auth.json), so the authenticated status and claim answers' fields are as both open-source clients read them, not seen here.
2026-10-04 03:54:23 +08:00

89 lines
2.5 KiB
Go

package provider
// A daily check-in, whichever vendor's: WorkBuddy's (workbuddy_checkin.go)
// and Trae CN's (trae_checkin.go). Each account in use is checked in once
// a Beijing day; what came of it is kept in the vendor's file by its key,
// so a restart doesn't ask again (see workbuddy_checkin.go for when a
// failure is tried again).
import (
"context"
"encoding/json"
"log"
"sync"
"time"
)
// checkinAcct is an account checked in each day.
type checkinAcct struct {
User string
On bool
key string // its entry in the file; "" for one that can't be told apart
do func(context.Context) WorkBuddyCheckin
}
// checkiner checks the accounts it lists in, keeping what came of it at
// path; mu keeps two check-ins (the loop and a press) from asking for one
// account at once.
type checkiner struct {
path string
now func() time.Time
mu *sync.Mutex
by string // the vendor, WorkBuddyCheckin.By; "" for WorkBuddy
label string // the vendor in the log
accounts func() []checkinAcct
}
// checkinRunner is a check-in the daily loop runs.
type checkinRunner interface {
checkinNow(ctx context.Context, soon bool) []WorkBuddyCheckin
clock() time.Time
}
func (c checkiner) clock() time.Time { return c.now() }
// checkinNow checks each account in use in for today that isn't yet, and
// keeps what came of it; soon, a failure is tried again without waiting
// out wbCheckinRetry. It says how every account stands, those it didn't
// ask as last kept.
func (c checkiner) checkinNow(ctx context.Context, soon bool) []WorkBuddyCheckin {
c.mu.Lock()
defer c.mu.Unlock()
st := readCheckins(c.path)
now := c.now()
day := wbCheckinDay(now)
var out []WorkBuddyCheckin
changed := false
// one account signed in twice (the built-in and the plugin) is
// checked in once
asked := map[string]bool{}
for _, a := range c.accounts() {
if !a.On || a.key == "" || asked[a.key] {
continue
}
asked[a.key] = true
prev, seen := st[a.key]
if seen && prev.settled(day, now, soon) {
prev.User, prev.By = a.User, c.by
out = append(out, prev)
continue
}
r := a.do(ctx)
r.Day, r.At = day, now
if r.Offline && seen && prev.Day == day && prev.Offline {
r.Tries = prev.Tries + 1
}
st[a.key], changed = r, true
r.User, r.By, r.Asked = a.User, c.by, true
out = append(out, r)
}
if changed {
if b, err := json.MarshalIndent(st, "", " "); err == nil {
if err := writePrivate(c.path, append(b, '\n')); err != nil {
log.Printf("%s check-in: %v", c.label, err)
}
}
}
return out
}