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.
98 lines
3.8 KiB
Go
98 lines
3.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// barkNearPct is the share of a window the daily summary calls near its end
|
|
// when Settings' Allowance alert is off (with it on, its own share).
|
|
const barkNearPct = 80
|
|
|
|
// barkNearMax is how many near-limit lines a push carries before "and N
|
|
// more": a push is read on a lock screen.
|
|
const barkNearMax = 8
|
|
|
|
// barkWords is the daily summary's and the test push's words, in the
|
|
// languages the notifications speak. %s is an amount, %d a count.
|
|
var barkWords = map[string]map[string]string{
|
|
"en": {
|
|
"title": "magpie · today", "spent": "Spent about %s today · %d requests", "calls": "%d requests today",
|
|
"none": "No requests yet today", "near": "Near the limit:", "clear": "No allowance is near its limit.",
|
|
"more": "…and %d more", "test": "Bark is set up: alerts and the daily summary come here.",
|
|
},
|
|
"zh": {
|
|
"title": "magpie · 今日", "spent": "今日约花费 %s · %d 次请求", "calls": "今日 %d 次请求",
|
|
"none": "今天还没有请求", "near": "额度快用完:", "clear": "没有额度接近上限。",
|
|
"more": "…另有 %d 项", "test": "Bark 已接通:提醒和每日汇总会发到这里。",
|
|
},
|
|
"zh-TW": {
|
|
"title": "magpie · 今日", "spent": "今日約花費 %s · %d 次請求", "calls": "今日 %d 次請求",
|
|
"none": "今天還沒有請求", "near": "額度快用完:", "clear": "沒有額度接近上限。",
|
|
"more": "…另有 %d 項", "test": "Bark 已接通:提醒和每日彙總會發到這裡。",
|
|
},
|
|
"ja": {
|
|
"title": "magpie · 今日", "spent": "今日の支出は約 %s · %d リクエスト", "calls": "今日 %d リクエスト",
|
|
"none": "今日はまだリクエストがありません", "near": "上限が近い枠:", "clear": "上限に近い枠はありません。",
|
|
"more": "…ほか %d 件", "test": "Bark につながりました。通知と毎日のまとめがここに届きます。",
|
|
},
|
|
"de": {
|
|
"title": "magpie · heute", "spent": "Heute etwa %s ausgegeben · %d Anfragen", "calls": "%d Anfragen heute",
|
|
"none": "Heute noch keine Anfragen", "near": "Nahe am Limit:", "clear": "Kein Kontingent ist nahe am Limit.",
|
|
"more": "…und %d weitere", "test": "Bark ist eingerichtet: Hinweise und die tägliche Übersicht kommen hierher.",
|
|
},
|
|
}
|
|
|
|
func barkWord(lang, key string, args ...any) string {
|
|
s, ok := barkWords[lang][key]
|
|
if !ok {
|
|
s = barkWords["en"][key]
|
|
}
|
|
if len(args) == 0 {
|
|
return s
|
|
}
|
|
return fmt.Sprintf(s, args...)
|
|
}
|
|
|
|
// barkTestText is the push Settings' Test sends.
|
|
func barkTestText(lang string) (title, body string) {
|
|
return "magpie", barkWord(lang, "test")
|
|
}
|
|
|
|
// barkDailyText is the once-a-day push (EV_John_Liu on X): what today cost
|
|
// and the windows and balances near their end, each as its alert would say
|
|
// it. near is what provider.NearLimits found, bal the balance alert's line.
|
|
func barkDailyText(lang string, today usage.Totals, currency string, rate float64, near []provider.QuotaAlert, bal float64, left bool, now time.Time) (title, body string) {
|
|
var lines []string
|
|
switch {
|
|
case today.Calls == 0:
|
|
lines = append(lines, barkWord(lang, "none"))
|
|
case today.Cost > 0:
|
|
c := usage.FormatCost(today.Cost, currency, rate)
|
|
if today.Unpriced > 0 {
|
|
c += "+"
|
|
}
|
|
lines = append(lines, barkWord(lang, "spent", c, today.Calls))
|
|
default:
|
|
lines = append(lines, barkWord(lang, "calls", today.Calls))
|
|
}
|
|
if len(near) == 0 {
|
|
lines = append(lines, barkWord(lang, "clear"))
|
|
} else {
|
|
lines = append(lines, barkWord(lang, "near"))
|
|
for i, a := range near {
|
|
if i == barkNearMax {
|
|
lines = append(lines, barkWord(lang, "more", len(near)-i))
|
|
break
|
|
}
|
|
t, b := alertText(lang, a, bal, left, now)
|
|
lines = append(lines, "· "+t+": "+b)
|
|
}
|
|
}
|
|
return barkWord(lang, "title"), strings.Join(lines, "\n")
|
|
}
|