Files
magpie/internal/gui/assets/checkinnow.js
T
yetone 0ebe6c72c9 providers: each account row in the editor has its own Check in now, when the check-in reaches it and it isn't in today (xwoffice_91592 on Discord)
xwoffice_91592 asked for 立即签到 on each account in the provider editor
sheet (Trae CN, a plugin's subscription, one row per account with its
Credits line). The editor's rows read GET /api/login/usage, which carried
no check-in. It now passes those cards through provider.LoginCheckins,
which marks them as the Usage page's cards are (WithCheckins: checkins,
checkinBy, checkin, checkinOff) and adds checkinKey, the account's key
in its vendor's check-in (markCheckins sets it).

The row's button is Settings' Check in now for one account (xiaowu_95660),
not a fork. ciAcctFill in checkinnow.js presses ciNowSend with
{key, user} and checkinBy, so the same POST /api/usage/checkin-account,
the same status-line success and failure, then the row's usage is read
again. When to show it is checkinDue, taken out of checkinRow so the
Usage card's press and the row decide alike: the check-in reaches it, it
isn't off, and it isn't in today (a failed day, an "again" answer, or a
Qoder campaign not open yet still count as due). It sits at the end of
the account's usage line, small (.ci-acct), and wraps whole there rather
than being cut. app.js is 51 bytes smaller (comments cut); checkinnow.js
is lazy and not in the core.

Run (mote, worktree at origin/main 14f6d10de plus this):
- go test -run 'TestLoginCheckins|TestTraeCheckin|TestPluginCheckin|Checkin'
  ./internal/provider/: ok. GOOS=linux and GOOS=windows go vet -tags
  nogui ./internal/gui/ ./internal/provider/: ok. gofmt -l clean.
- New editor-checkin-account.test.cjs: 4 Trae CN accounts (due, in
  today, failed today, no check-in), en zh zh-TW ja de, 1000/560 desktop
  and 390 web, chromium and webkit: ok. It checks the translated label and
  tooltip, the placement (not cut, no sideways scroll), the press body
  {by, key}, the status line, the re-read, and that nothing scrolls.
- The editor-*, account-*, checkin-*, plugin-checkin,
  qoder-checkin-recheck, usage-card-* and i18n-keys files, both engines:
  634 of 635 ok. The red is account-eye's Usage card in chromium zh-TW,
  '' !== 'none', which is #5052; run alone it passed 30/30 twice.
- TestCoreScriptBudget: red at 1,533,950 > 1,533,802, which is #5139 (red
  on main at 1,534,001 before this); this change lowers it by 51.
- Rebased on c03dee7e3 (no core script or check-in file changed there):
  editor-checkin-account, i18n-keys and checkin-now-account, both
  engines, 28 of 28 ok; the Checkin provider tests ok; budget the same
  1,533,950.
- Broken by hand: no CheckinKey in markCheckins made
  TestLoginCheckinsPressOneAccount red.

Not tried: a real Trae CN account, WebView2 or WebKitGTK. The whole GUI
suite hadn't run before the push: another session's whole suite held
mote.
2026-11-04 18:49:11 +08:00

112 lines
5.1 KiB
JavaScript

// Settings' Check in now for one account (xiaowu_95660: 手动签到一次):
// its row, the press and what it came to. app.js imports this when
// Settings draws a vendor's check-in (checkinNowRow), so it weighs nothing
// on the core.
// what each vendor's last press said, kept over Settings' redraws
const checkinNowSaid = {};
// ciNowFill draws Check in now into row, for vendor by's accounts: one
// account checked in once now, picked from the app menu when there are
// several, its switch and the account's left as they are; what came of it
// is said in the row.
window.ciNowFill = function (row, s, by) {
const accts = (s.checkinAccounts || []).filter((a) => (a.by || "") === by);
row.dataset.by = by;
const sub = el("div", "sub");
const who = el("div", "who");
who.append(el("div", "name", t("Check in now")), sub);
const box = el("div");
row.append(who, box);
const draw = () => {
const said = checkinNowSaid[by];
sub.textContent = said ? said.text : t("Checks one account in once now; the daily check-in's switches stay as they are");
sub.classList.toggle("err", !!said?.bad);
sub.classList.toggle("say", !!said); // what came of it: a state, kept in sight
const b = el("button", "text ci-now-go", t(accts.length > 1 ? "Check in…" : "Check in"));
b.type = "button";
b.disabled = !!said?.busy;
b.classList.toggle("busy", !!said?.busy);
if (accts.length > 1) b.setAttribute("aria-haspopup", "menu");
b.onclick = (e) => {
e.stopPropagation();
if (accts.length === 1) return send(accts[0]);
if (b.classList.contains("open")) return closeProtoMenu();
openProtoMenu(b, accts.map((a) => ({ v: a.key, name: a.user || a.key, note: "", literalName: true })), null,
(k) => send(accts.find((a) => a.key === k)), "Check in which account", "sess-menu", "right");
};
box.replaceChildren(b);
};
const send = (a) => ciNowSend(a, by, (o) => { checkinNowSaid[by] = o; if (row.isConnected) draw(); });
draw();
row.hidden = false;
};
// ciAcctFill puts an account's own Check in now in slot, on its row in
// the provider editor (xwoffice_91592): q is its reading, which carries
// its check-in (checkinBy, checkinKey). The press is Settings' one,
// said the same way, and the row's reading is asked again after.
window.ciAcctFill = function (slot, q, agent, user) {
const b = el("button", "text ci-acct", t("Check in now"));
b.type = "button";
b.title = t("Checks one account in once now; the daily check-in's switches stay as they are");
b.onclick = (e) => {
e.stopPropagation();
ciNowSend({ key: q.checkinKey, user }, q.checkinBy || "", (o) => {
b.disabled = !!o.busy;
b.classList.toggle("busy", !!o.busy);
if (o.busy) return;
if (loginUsage[agent]) loginUsage[agent].at = 0;
loginUsageOf(agent);
});
};
slot.replaceWith(b);
};
// ciNowSend checks account a of vendor by in once now and says, through
// say, how it went; no switch is changed.
window.ciNowSend = async function (a, by, say) {
if (!a) return;
const name = a.user || a.key;
say({ busy: true, text: t("Checking in {who}…", { who: name }) });
try {
const r = await api("usage/checkin-account", { by, key: a.key });
const said = checkinNowLine(r, name);
say(said);
status(said.text, said.bad ? "err" : "ok");
} catch (err) {
const text = t("Couldn't check in {who}: {why}", { who: name, why: t(err.message) });
say({ bad: true, text });
status(text, "err");
}
// the Usage cards' lines read again; Settings isn't drawn again under
// the button just pressed, which a redraw moved at 420px
loadQuotas();
};
// checkinNowLine is what one account's press came to: checked in now,
// already in today, not eligible or no event, its own app's, or failed,
// with the vendor's reason.
function checkinNowLine(r, who) {
const why = r?.msg ? " · " + r.msg : "";
switch (r?.outcome) {
case "claimed":
case "done":
if (r.again) return { text: t("{user}: {name} says already checked in; magpie asks again later", { user: who, name: checkinVendorName(r) }) };
if (r.outcome === "claimed" && r.asked) return { text: t("Checked in {who}", { who }) + (r.credit ? " +" + r.credit : "") + (r.streak ? " · " + t("{n}-day streak", { n: r.streak }) : "") };
return { text: t("{who} was already checked in today", { who }) };
case "ineligible":
return { text: t("{user} is not eligible", { user: who }) + why };
case "inactive":
return { text: t("{user}: no check-in event now", { user: who }) + why };
case "captcha":
return { text: t("{user} is asked for a captcha; check in in the app", { user: who }) };
case "own-app":
return { text: r.by === "trae"
? t("{user}: Trae CN refused this check-in (9074: too many taking part); check in in the Trae CN app", { user: who })
: t("{user}: {name} only gives check-in credits to its own app; check in in the {name} app", { user: who, name: checkinVendorName(r) }) };
default:
return { bad: true, text: t("Couldn't check in {who}: {why}", { who, why: r?.msg || t("Check-in failed") }) };
}
}