Files
magpie/internal/gui/assets/bark.js
T
yetone 71c4ef2d07 gui: Bark push target for every alert, and a daily spend summary (EV_John_Liu on X)
Settings > Usage has a Bark row: a server (default https://api.day.app, or
a self-hosted one) and the device key, then Test and Remove. Every alert
that goes through notifier.sendAs (allowance, balance, reset reminder, task
done, masking, sign-in) is also pushed to Bark when a key is set, whether or
not the system lets this computer notify. A second row, once a key is set,
turns on a daily summary at a time of day: today's cost and request count,
and each window or balance near its cap (the Allowance alert's share, else
80%), capped at 8 lines. It is pushed once a day (a restart does not resend;
a failed push is retried every 15 minutes; one more than 6 hours late is
left), in the page's language (en, zh, zh-TW, ja, de).

The device key is a key: the page only gets barkKeySet, never the key; it has
its own route (POST /api/settings/bark, key omitted keeps it, "" removes it),
a Settings save carries it (settingsapi.Merge), and a backup or sync without
keys leaves it out and keeps this machine's. No log line or error names it
(the Bark client replaces it with "..." in what the server or client says).
bark.js is a lazy module so app.js (budget) gains one line.

Run (mote, at the pushed tree before the rebase onto 6d7862769):
- go test ./internal/bark ./internal/settings ./internal/settingsapi
  ./internal/backup ./internal/davsync ./internal/provider: ok.
- go test -run 'Bark|AlertsGoTo|GitHubToken' ./internal/gui: ok. go vet
  darwin, GOOS=linux -tags nogui and GOOS=windows over gui, bark, settings:
  clean. gofmt -l: clean.
- GUI: bark.test.cjs (5 languages x 900/380px x chromium/webkit), i18n-keys,
  i18n-en-no-cjk, usage-alert: pass. Whole GUI suite started; reds so far are
  in account/agent/checkin tests that do not touch Settings or Bark, filed
  separately after this tag; old-webkit's Safari 15.0 :has() is #5919.
- All against fake servers; nothing reaches the network.
Broken by hand, each went red: no barkPush in sendAs
(TestAlertsGoToBark), BarkKeySet forced true / key shown (TestBarkSettingAndTest),
no same-day check in the daily step (TestBarkDailyStep).
Not tried: a real Bark device or the real api.day.app; win-run and wsl-run
(vet only for those OSes); a self-hosted Bark server.
2026-11-11 03:41:37 +08:00

60 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Settings › Usage's Bark rows (EV_John_Liu on X): the server and device key
// every alert also goes to, and a daily summary. app.js imports this when
// renderSettings draws them (renderBark), so it weighs nothing on the core.
// The page never has the key back: it is written here, and settings tell
// only that one is set.
let barkDraft = { server: "", key: "" };
window.barkPaint = function (s) {
const box = $("#barkBox");
const set = !!s.barkKeySet;
const save = (body, done) => writingPrefs(api("settings/bark", body))
.then((ns) => { prefs = ns; if (state) state.settings = ns; if (done) barkDraft = { server: "", key: "" }; renderSettings(); status(t("Saved"), "ok", 1500); })
.catch((e) => statusTErr(e));
box.replaceChildren();
if (set) {
const server = el("code", "bark-server", (s.barkServer || "https://api.day.app").replace(/^https?:\/\//, ""));
server.title = s.barkServer || "https://api.day.app";
const test = el("button", "text bark-test", t("Test"));
test.onclick = () => {
test.disabled = true;
api("bark/test", {}).then(() => status(t("Sent to Bark"), "ok", 1500), statusTErr).finally(() => { test.disabled = false; });
};
const x = el("button", "text bark-remove", t("Remove"));
x.onclick = () => save({ key: "", daily: "" });
box.append(server, el("code", "bark-key", "••••••••"), test, x);
} else {
const srv = input(barkDraft.server, "https://api.day.app", "url");
srv.className = "words bark-server-input";
srv.setAttribute("aria-label", t("Server"));
srv.oninput = () => { barkDraft.server = srv.value; };
const key = input(barkDraft.key, t("Device key"), "password");
key.className = "words bark-key-input";
key.setAttribute("aria-label", t("Device key"));
key.oninput = () => { barkDraft.key = key.value; };
const go = el("button", "text bark-save", t("Save"));
go.onclick = () => {
if (!key.value.trim()) return key.focus();
save({ server: srv.value.trim(), key: key.value.trim() }, true);
};
for (const i of [srv, key]) i.addEventListener("keydown", (e) => { if (e.key === "Enter") go.onclick(); });
box.append(srv, key, go);
}
// the daily summary, with its time of day, once there is a key
$("#barkDailyRow").hidden = !set;
const daily = $("#barkDailySegs");
daily.replaceChildren();
if (!set) return;
const at = s.barkDaily || "";
if (at) {
const i = input(at, "21:00", "time");
i.className = "at";
i.setAttribute("aria-label", t("Time of day"));
blurOnEnter(i);
i.onchange = () => { if (i.value && i.value !== at) save({ daily: i.value }); };
daily.append(i);
}
daily.append(segs([["off", t("Off")], ["on", t("On")]], at ? "on" : "off", (v) => save({ daily: v === "on" ? "21:00" : "" })));
};