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.
138 lines
3.9 KiB
Go
138 lines
3.9 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/bark"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// barkLate is how long after its time of day a summary is still pushed: a
|
|
// computer asleep then sends it on waking, and one woken the next evening
|
|
// leaves the day be.
|
|
const barkLate = 6 * time.Hour
|
|
|
|
// barkRetry is how long a push that failed waits before it is tried again.
|
|
const barkRetry = 15 * time.Minute
|
|
|
|
// barkTarget is where Settings sends pushes, and whether anywhere.
|
|
func barkTarget(s settings.Settings) bark.Target {
|
|
return bark.Target{Server: s.BarkServer, Key: s.BarkKey}
|
|
}
|
|
|
|
// barkPush sends one alert's words to Bark when it is set, without making
|
|
// the alert wait for it. The key is never logged.
|
|
func barkPush(title, body string) {
|
|
t := barkTarget(settings.Load())
|
|
if !t.On() {
|
|
return
|
|
}
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
if err := bark.Push(ctx, t, title, body); err != nil {
|
|
log.Println("bark:", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// barkDaily keeps the day the summary was last pushed, in memory and in a
|
|
// file beside the alerts' marks, so a restart doesn't push it again.
|
|
type barkDaily struct {
|
|
path string
|
|
mu sync.Mutex
|
|
sent string
|
|
retryAt time.Time
|
|
}
|
|
|
|
func (d *barkDaily) lastDay() string {
|
|
if d.sent != "" {
|
|
return d.sent
|
|
}
|
|
var m struct {
|
|
Day string `json:"day"`
|
|
}
|
|
if b, err := os.ReadFile(d.path); err == nil {
|
|
json.Unmarshal(b, &m)
|
|
}
|
|
return m.Day
|
|
}
|
|
|
|
// step pushes the day's summary when it is due at now: Settings has a key
|
|
// and a time, the time has come and is not more than barkLate past, and none
|
|
// was pushed today. A failed push is asked again after barkRetry, and a
|
|
// summary that can't be pushed in barkLate is left. It reports whether it
|
|
// pushed.
|
|
func (d *barkDaily) step(now time.Time, s settings.Settings, build func() (string, string), send func(title, body string) error) bool {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
if !barkTarget(s).On() || s.BarkDaily == "" {
|
|
return false
|
|
}
|
|
h, m, ok := settings.Clock(s.BarkDaily)
|
|
if !ok {
|
|
return false
|
|
}
|
|
y, mo, dd := now.Date()
|
|
at := time.Date(y, mo, dd, h, m, 0, 0, now.Location())
|
|
day := now.Format("2006-01-02")
|
|
if now.Before(at) || now.Sub(at) > barkLate || d.lastDay() == day || now.Before(d.retryAt) {
|
|
return false
|
|
}
|
|
title, body := build()
|
|
if err := send(title, body); err != nil {
|
|
log.Println("bark:", err)
|
|
d.retryAt = now.Add(barkRetry)
|
|
return false
|
|
}
|
|
d.sent = day
|
|
if b, err := json.Marshal(map[string]string{"day": day}); err == nil {
|
|
os.MkdirAll(filepath.Dir(d.path), 0o755)
|
|
os.WriteFile(d.path, append(b, '\n'), 0o600)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// barkSummary reads today's spend and the allowances now, and says them.
|
|
func barkSummary(ctx context.Context, now time.Time) (title, body string) {
|
|
s := settings.Load()
|
|
cx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
qs := provider.Quotas(cx)
|
|
cancel()
|
|
pct := s.UsageAlert
|
|
if pct <= 0 {
|
|
pct = barkNearPct
|
|
}
|
|
currency, rate := trayCurrency()
|
|
return barkDailyText(trayLang(s.Lang, systemLang), trayTotalSum(usage.Today), currency, rate,
|
|
provider.NearLimits(qs, pct, s.BalanceAlert, now), s.BalanceAlert, s.QuotaLeft, now)
|
|
}
|
|
|
|
// watchBarkDaily pushes the daily summary at the time Settings names, until
|
|
// ctx ends.
|
|
func watchBarkDaily(ctx context.Context) {
|
|
d := &barkDaily{path: filepath.Join(settings.Dir(), "bark-daily.json")}
|
|
t := time.NewTicker(time.Minute)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case now := <-t.C:
|
|
d.step(now, settings.Load(), func() (string, string) { return barkSummary(ctx, now) }, func(title, body string) error {
|
|
cx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
|
defer cancel()
|
|
return bark.Push(cx, barkTarget(settings.Load()), title, body)
|
|
})
|
|
}
|
|
}
|
|
}
|