mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-11-11 11:57:57 +00:00
Observers publish to the broker with one shared MQTT login. Anyone who
has that login can publish under any observer's topic, and the ingestor
takes the observer id from the topic (`cmd/ingestor/main.go:1009`), so
the data in the analyzer DB is only as trustworthy as the shared
password. This PR gives each user a personal MQTT login, generated and
mailed by CoreScope, and lets the broker ask CoreScope whether a login
may connect and publish.
Closes #2319.
The feature is optional and off by default
(`userManagement.observerLogins.enabled`). It needs
`userManagement.enabled`. With it off, nothing changes.
## Changes in this PR
**Broker hooks.** `POST /api/mqtt-hook/emqx/authn` and `/authz`, for the
EMQX HTTP authenticator and authorizer.
- They check a shared secret header, `X-CoreScope-Hook-Secret`, in
constant time.
- A username CoreScope does not know gets `ignore`, so EMQX falls
through to its built-in database and existing users keep working.
- A personal login may only publish `meshcore/<iata>/<pubkey>/<type>`,
with a 64-hex pubkey that is unbound or bound to the same user.
- The first publish for an unbound pubkey binds that observer to the
user (first come wins), until the user owns 20 observers (admin
assignments count too).
- An admin can rebind any observer.
**Credentials.**
- Username `obs_` + 12 random `[a-z0-9]`, password 32 random
`[A-Za-z0-9]`, both from `crypto/rand`.
- The password is stored twice: SHA-256 for the auth check, and sealed
with AES-256-GCM (key from config or `CORESCOPE_OBSERVER_KEY`) so the
user can ask for the same credentials again.
- The UI and the API never show the password. It leaves the server only
in the mail.
**Mail.** One mail with four formats:
- the meshcoretomqtt TOML;
- the observer firmware CLI commands (Gessaman or Dutch-MeshCore, same
`mqttN` CLI), on slot 4: slots 1-2 (Gessaman, Let's Mesh US/EU) and 1-3
(Dutch-MeshCore, `MQTTDefaults.h:19-34`) hold default broker presets on
a fresh install, slot 4 is empty on both. `set mqtt4.server` gets the
bare host: the firmware picks the scheme from the port
(`MQTTBridge.cpp:2094-2145`), and builds before merge 32dd8ad
(2026-10-19) do not accept a scheme there (read from the source, not
tested on a device). A scheme is written only when the port would pick
the wrong one, for example TLS on 8884;
- the Home Assistant meshcore-ha broker form fields;
- the plain values.
Each block starts with a numbered title and one line on where it goes.
The account page can resend the mail, at most 3 times per 24 h.
**Account and admin UI.**
- The account page gets an "Observer MQTT login" section
(`#/account?section=observers`) with the username, the bound observers
and the resend button.
- The admin page gets an "Observer logins" tab
(`#/admin?tab=observer-logins`) that lists logins with revoke and
reissue, and every observer with its owner, "not migrated" or
"orphaned", with a reassign control.
**Notifications.** Creating a login adds `observer.offline.own` ("My
observers go offline") to the owner's notification events; mails go out
once the user has notifications switched on, and the user can untick it.
**Storage.** users.db schema v7 adds `mqtt_logins` and
`observer_bindings`. The server still writes only users.db through
`internal/users`, and the analyzer DB stays read-only.
**Docs.** `docs/observer-mqtt-logins.md` covers the EMQX setup (authn
and authz chains, the hook body templates, the closing `^obs_` deny rule
and why it is needed), how to enable it on a running EMQX through the
REST API step by step, with a check and a rollback per step, and how to
migrate observers off a shared login. `config.example.json` has the
block with placeholder values.
## Enabling it on an instance
Full steps in `docs/observer-mqtt-logins.md`. In short:
1. **CoreScope:** add `userManagement.observerLogins` to `config.json`
with `enabled: true`, a `hookSecret` (`openssl rand -hex 32`), an
`encryptionKey` (`openssl rand -base64 32`) and the `broker` host, port,
transport and TLS that observers should use. Restart. Keep the key safe:
without it, mails cannot be resent and logins must be reissued.
2. **EMQX, authorization:** add a file source with `{deny, {username,
{re, "^obs_"}}, all, ["#"]}.` and move it to the end, then add the HTTP
authorizer pointing at `/api/mqtt-hook/emqx/authz` (it lands at the
front).
3. **EMQX, authentication:** add the HTTP authenticator pointing at
`/api/mqtt-hook/emqx/authn` and move it to the front, before the
built-in database.
4. **Check:** the shared login still connects, an unknown `obs_` login
is refused, and `metrics.deny` on the authorizer stays 0 for existing
clients.
5. **Owners:** they request a login on their account page and paste the
block for their uploader. Each observer binds to them on its first
publish. An uploader that was refused before step 3 may need a restart.
6. **Later:** remove the shared login from the broker once enough
observers moved.
## Who can claim an observer
Any active account can request a login, and its first publish under an
unbound pubkey binds that observer, also when CoreScope already knew it
from the shared login (first come wins, a deliberate choice: owners
migrate without waiting for an admin). Observer pubkeys are public, and
registration cannot be closed, so with this feature on anyone with a
mail address can claim up to 20 unbound observers per account. Before,
publishing needed the shared password.
What limits it: each claim of a known observer mails the admins, an
admin can unbind or reassign it and revoke the login, and a bound
observer cannot be claimed by another login. The doc spells this out and
suggests assigning known observers to their owners before announcing the
feature. A stricter mode (admin approval of logins, or no auto-bind for
known observers) is not in this PR.
## Performance
`authorize()` runs for every publish that misses the EMQX authz cache.
It takes two short read locks, does map lookups and one topic split.
`BenchmarkAuthorizeBound` (2000 observers, 500 logins): 533.5 ns/op, 96
B/op, 2 allocs/op. The admin observers table renders once per load and
updates single rows after a change.
## Verification
- All 16 Go modules: `bash scripts/allmod.sh test ./...` green.
- Skipped `TestSaveGeoFilterPreservesFileMode` and
`TestWriteStatsAtomic_SymlinkAtDestIsReplaced`: both fail on Windows on
a clean master too.
- `sh test-all.sh` green.
- `tests/e2e/test-user-management-e2e.js` 36/36 locally. It covers:
request, mail, resend, revoke, reissue, reassign, no password in the
page or API body, and axe. The CI job for this suite gets the
`observerLogins` block with fake values in `deploy.yml`.
- Local EMQX 5.8.6 in Docker against a local CoreScope:
- a personal login connects and publishes, which binds the observer; a
second personal login is denied on that observer's topic, and subscribe
is denied;
- with CoreScope stopped, the built-in users still connect and publish,
and new personal-login connects are refused;
- the `^obs_` deny rule blocks an already-connected personal login while
CoreScope is down; without the rule, the same publish was delivered.
- Production: running on an EMQX 5.8.9 broker with about 50 clients
since 2026-10-25, set up through the REST API as in the doc. After the
hooks went in, the authorizer answered `ignore` for every existing login
(0 denies, 0 dropped messages), and 4 observers were bound to their
owners through personal logins on the same evening, with the shared
login still in use by the rest.
## Known behaviour
EMQX's HTTP client can mark the hook resource down for one health-check
interval (15 s measured) when CoreScope closes an idle keep-alive
connection just as a health check runs. During that window new
personal-login connects are refused and the uploader reconnects. I saw
it 3 times in about 25 minutes of runs with a forced 1-5 s idle timeout,
and could not trigger it on demand. EMQX 5.8.6 has no setting for it,
and 5.10.5 ships the same client code. The doc describes it and the
alarm to watch for.
## Not in this PR
- mosquitto (mosquitto-go-auth). EMQX only.
- A websocket path field: the mail does not carry one, the doc tells
operators to match the listener path.
- Disabling the shared login. That is an operator step after migration,
described in the doc.
- Tested on EMQX 5.8.6 (Docker) and 5.8.9 (production) only.
Note on cb9f4ff3: its message says the scheme problem was reported by an
operator. That report turned out to come from before the broker used the
hook, so it does not show this cause; the change rests on the firmware
source only.
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
430 lines
14 KiB
Go
430 lines
14 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/url"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/meshcore-analyzer/mailer"
|
|
"github.com/meshcore-analyzer/users"
|
|
)
|
|
|
|
// notifier evaluates node notifications on a timer and mails the changes
|
|
// (docs/specs/2026-10-07-node-notifications-design.md). authService.notify
|
|
// is nil unless userManagement.notifications.enabled.
|
|
type notifier struct {
|
|
a *authService
|
|
src notifySource
|
|
now func() time.Time
|
|
// ingestStale is the last tick's verdict, so the pause and the resume
|
|
// are each logged once; ingestResumedAt is when the last stale period
|
|
// ended (the offline grace, see notifyInput). In memory only: a restart
|
|
// inside the grace loses it. Only the loop goroutine touches them.
|
|
ingestStale bool
|
|
ingestResumedAt time.Time
|
|
// externalErr is the last tick's reason the externalAlerts feed was not
|
|
// used ("" when it was), so each new reason is logged once.
|
|
externalErr string
|
|
// adaptive caches the learned offline thresholds (offlineWindows).
|
|
adaptive map[string]adaptiveEntry
|
|
}
|
|
|
|
func newNotifier(a *authService, src notifySource, now func() time.Time) *notifier {
|
|
return ¬ifier{a: a, src: src, now: now}
|
|
}
|
|
|
|
// notifySendTimeout bounds one mail send inside a tick.
|
|
const notifySendTimeout = 30 * time.Second
|
|
|
|
// notifyIngestStaleAfter: when the newest packet in the store is older,
|
|
// offline checks pause (an MQTT or ingestor outage would otherwise mail
|
|
// every watcher "offline" and later "back online").
|
|
const notifyIngestStaleAfter = 30 * time.Minute
|
|
|
|
// checkIngest reports whether ingest is stale at now and logs when that
|
|
// verdict changes.
|
|
func (n *notifier) checkIngest(now time.Time) bool {
|
|
newest := n.src.newestPacket()
|
|
stale := newest.IsZero() || now.Sub(newest) > notifyIngestStaleAfter
|
|
switch {
|
|
case stale && !n.ingestStale:
|
|
since := "(no packets)"
|
|
if !newest.IsZero() {
|
|
since = newest.UTC().Format(time.RFC3339)
|
|
}
|
|
log.Printf("[notify] ingest stale since %s; offline checks paused", since)
|
|
case !stale && n.ingestStale:
|
|
n.ingestResumedAt = now
|
|
log.Printf("[notify] ingest fresh again; offline checks resumed")
|
|
}
|
|
n.ingestStale = stale
|
|
return stale
|
|
}
|
|
|
|
// loop evaluates every interval until stop closes. The first evaluation
|
|
// runs one interval after startup. A tick in flight sees its context
|
|
// cancelled when stop closes, so a slow mail provider does not hold up
|
|
// the shutdown.
|
|
func (n *notifier) loop(stop <-chan struct{}, every time.Duration) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go func() {
|
|
select {
|
|
case <-stop:
|
|
cancel()
|
|
case <-ctx.Done():
|
|
}
|
|
}()
|
|
t := time.NewTicker(every)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-stop:
|
|
return
|
|
case <-t.C:
|
|
n.safeTick(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
// safeTick runs one evaluation; a panic is logged and the next tick runs.
|
|
func (n *notifier) safeTick(ctx context.Context) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Printf("[notify] evaluation panicked, the next one runs as planned: %v", r)
|
|
}
|
|
}()
|
|
n.tick(ctx)
|
|
}
|
|
|
|
// tick reads all preferences, watches and states (one query each), the
|
|
// accounts and the analyzer snapshot they need, evaluates, deletes the
|
|
// dropped states and writes the new ones, and then mails each user's
|
|
// changes. Users without a preferences row and users with notifications
|
|
// off are evaluated too (the evaluator gives the first the defaults and
|
|
// keeps the second's states current without changes). A failed read or
|
|
// state write ends the tick without mail; the next tick starts over. A
|
|
// tick that evaluates logs one line with its counts, its duration and how
|
|
// long LastHeardMap held the packet store's read lock.
|
|
func (n *notifier) tick(ctx context.Context) {
|
|
if !n.src.ready() {
|
|
return
|
|
}
|
|
started := time.Now()
|
|
now := n.now()
|
|
stale := n.checkIngest(now)
|
|
st := n.a.st
|
|
prefs, err := st.AllNotifyPrefs()
|
|
if err != nil {
|
|
log.Printf("[notify] read preferences: %v", err)
|
|
return
|
|
}
|
|
stored, err := st.AllWatches()
|
|
if err != nil {
|
|
log.Printf("[notify] read watches: %v", err)
|
|
return
|
|
}
|
|
if len(prefs) == 0 && len(stored) == 0 {
|
|
return
|
|
}
|
|
states, err := st.AllNotifyStates()
|
|
if err != nil {
|
|
log.Printf("[notify] read states: %v", err)
|
|
return
|
|
}
|
|
// The analyzer snapshot is keyed by lowercase pubkey; normalise the
|
|
// watches once so every lookup and state subject uses that form.
|
|
watches := make([]users.NotifyWatch, 0, len(stored))
|
|
watchSeen := make(map[users.NotifyKey]bool, len(stored))
|
|
seen := map[string]bool{}
|
|
var pubkeys []string
|
|
for _, w := range stored {
|
|
w.Pubkey = strings.ToLower(w.Pubkey)
|
|
k := users.NotifyKey{UserID: w.UserID, Subject: w.Pubkey}
|
|
if watchSeen[k] {
|
|
continue
|
|
}
|
|
watchSeen[k] = true
|
|
watches = append(watches, w)
|
|
if !seen[w.Pubkey] {
|
|
seen[w.Pubkey] = true
|
|
pubkeys = append(pubkeys, w.Pubkey)
|
|
}
|
|
}
|
|
byUser := make(map[int64]users.NotifyPrefs, len(prefs))
|
|
var ids []int64
|
|
for _, p := range prefs {
|
|
byUser[p.UserID] = p
|
|
ids = append(ids, p.UserID)
|
|
}
|
|
noPrefs := map[int64]bool{}
|
|
for _, w := range watches {
|
|
if _, ok := byUser[w.UserID]; !ok && !noPrefs[w.UserID] {
|
|
noPrefs[w.UserID] = true
|
|
ids = append(ids, w.UserID)
|
|
}
|
|
}
|
|
accounts, err := st.UsersByID(ids)
|
|
if err != nil {
|
|
log.Printf("[notify] read accounts: %v", err)
|
|
return
|
|
}
|
|
wantExternal := false
|
|
if n.a.set.notify.external.url != "" {
|
|
for _, p := range prefs {
|
|
wantExternal = wantExternal || p.Has(users.NotifyNodeExternal)
|
|
}
|
|
}
|
|
wantForeign, wantObservers := false, false
|
|
for _, p := range prefs {
|
|
if accounts[p.UserID].Role != users.RoleAdmin {
|
|
continue
|
|
}
|
|
wantForeign = wantForeign || p.Has(users.NotifyForeignNew)
|
|
wantObservers = wantObservers || p.Has(users.NotifyObserverOffline)
|
|
}
|
|
nodes, err := n.src.nodes(pubkeys, wantForeign)
|
|
if err != nil {
|
|
log.Printf("[notify] read nodes: %v", err)
|
|
return
|
|
}
|
|
var owners map[string]int64
|
|
if n.a.obsIdx != nil {
|
|
for _, p := range prefs {
|
|
if p.Has(users.NotifyObserverOfflineOwn) {
|
|
wantObservers = true
|
|
owners = n.a.obsIdx.ownersSnapshot()
|
|
break
|
|
}
|
|
}
|
|
}
|
|
var observers []notifyObserver
|
|
if wantObservers {
|
|
if observers, err = n.src.observers(); err != nil {
|
|
log.Printf("[notify] read observers: %v", err)
|
|
return
|
|
}
|
|
}
|
|
var external *externalFeed
|
|
if wantExternal {
|
|
external = n.readExternal(now)
|
|
}
|
|
heard, lock := n.src.lastHeard(pubkeys)
|
|
var infra []string
|
|
for _, pk := range pubkeys {
|
|
if r := strings.ToLower(nodes[pk].Role); r == "repeater" || r == "room" {
|
|
infra = append(infra, pk)
|
|
}
|
|
}
|
|
res := evaluateNotifications(notifyInput{Now: now, Health: n.src.health(), LowMv: n.src.lowBatteryMv(),
|
|
Accounts: accounts, Prefs: prefs, Watches: watches, States: states, Nodes: nodes,
|
|
Heard: heard, SharedSeen: n.src.sharedPrefixSeen(infra), OfflineAfter: n.offlineWindows(pubkeys, now), NewestPacket: n.src.newestPacket(), Observers: observers, ObserverOwners: owners, IngestStale: stale, IngestResumedAt: n.ingestResumedAt, External: external})
|
|
if err := st.DeleteNotifyStates(res.Drop); err != nil {
|
|
log.Printf("[notify] delete dropped states, nothing mailed this time: %v", err)
|
|
return
|
|
}
|
|
if len(res.States) > 0 {
|
|
if err := st.WriteNotifyStates(res.States); err != nil {
|
|
log.Printf("[notify] write states, nothing mailed this time: %v", err)
|
|
return
|
|
}
|
|
}
|
|
mails := n.deliver(ctx, now, res.Changes, accounts, byUser)
|
|
changes := 0
|
|
for _, list := range res.Changes {
|
|
changes += len(list)
|
|
}
|
|
log.Printf("[notify] tick: users=%d changes=%d mails=%d took=%.2fms lock=%.2fms",
|
|
len(accounts), changes, mails, msFloat(time.Since(started)), msFloat(lock))
|
|
}
|
|
|
|
// readExternal fetches and validates the externalAlerts feed. It returns
|
|
// nil when the feed cannot be used (fetch failed, malformed, too old), so
|
|
// no node.external state changes; a dead feed never mails "resolved". The
|
|
// reason is logged when it changes, and once when the feed is usable again.
|
|
func (n *notifier) readExternal(now time.Time) *externalFeed {
|
|
body, err := n.src.externalFeed()
|
|
var f *externalFeed
|
|
if err == nil {
|
|
f, err = parseExternalFeed(body, now, n.a.set.notify.external.maxAge)
|
|
}
|
|
why := ""
|
|
if err != nil {
|
|
why = err.Error()
|
|
}
|
|
switch {
|
|
case why != "" && why != n.externalErr:
|
|
log.Printf("[notify] external alerts feed not used, node.external unchanged: %s", why)
|
|
case why == "" && n.externalErr != "":
|
|
log.Printf("[notify] external alerts feed usable again")
|
|
}
|
|
n.externalErr = why
|
|
if f != nil && f.Skipped > 0 {
|
|
log.Printf("[notify] external alerts feed: %d malformed alert(s) skipped", f.Skipped)
|
|
}
|
|
return f
|
|
}
|
|
|
|
func msFloat(d time.Duration) float64 { return float64(d.Microseconds()) / 1000 }
|
|
|
|
// notifySkipReason says why a user's changes are not mailed; "" means send.
|
|
// Skipped changes are already stored and are never mailed later.
|
|
func notifySkipReason(u users.User, p users.NotifyPrefs, userMails, totalMails int, ns notifySettings) string {
|
|
switch {
|
|
case u.Status != users.StatusActive:
|
|
return "account not active"
|
|
case u.EmailBouncing:
|
|
return "address bounces"
|
|
case !p.Enabled:
|
|
return "notifications off"
|
|
case userMails >= ns.perUserPerDay:
|
|
return "daily limit for this user reached"
|
|
case totalMails >= ns.maxMailsPerDay:
|
|
return "daily limit for this instance reached"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// deliver sends one mail per user with changes, within the limits counted
|
|
// from mail_log over the last 24 hours (this tick's mails included). A user
|
|
// with changes but no preferences row was evaluated with the defaults; the
|
|
// row (and so the unsubscribe token) is created before the first mail.
|
|
// Once ctx is cancelled (shutdown) the remaining users are not mailed;
|
|
// their changes are already stored, like any other skipped change. It
|
|
// returns the number of mails sent.
|
|
func (n *notifier) deliver(ctx context.Context, now time.Time, changes map[int64][]notifyChange,
|
|
accounts map[int64]users.User, prefs map[int64]users.NotifyPrefs) int {
|
|
if len(changes) == 0 {
|
|
return 0
|
|
}
|
|
total, perUser, err := n.a.st.NotifyMailCounts(now.Add(-24 * time.Hour))
|
|
if err != nil {
|
|
log.Printf("[notify] count mails, nothing mailed this time: %v", err)
|
|
return 0
|
|
}
|
|
sent := 0
|
|
ids := make([]int64, 0, len(changes))
|
|
for uid := range changes {
|
|
ids = append(ids, uid)
|
|
}
|
|
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
|
|
for i, uid := range ids {
|
|
if ctx.Err() != nil {
|
|
log.Printf("[notify] shutting down: %d user(s) with changes not mailed", len(ids)-i)
|
|
return sent
|
|
}
|
|
u := accounts[uid]
|
|
p, ok := prefs[uid]
|
|
if !ok {
|
|
p = users.DefaultNotifyPrefs(uid)
|
|
}
|
|
if why := notifySkipReason(u, p, perUser[uid], total, n.a.set.notify); why != "" {
|
|
log.Printf("[notify] user #%d: %d change(s) not mailed: %s", uid, len(changes[uid]), why)
|
|
continue
|
|
}
|
|
if !ok {
|
|
if p, err = n.a.st.NotifyPrefsFor(uid); err != nil {
|
|
log.Printf("[notify] user #%d: create preferences, %d change(s) not mailed: %v", uid, len(changes[uid]), err)
|
|
continue
|
|
}
|
|
}
|
|
sendCtx, cancel := context.WithTimeout(ctx, notifySendTimeout)
|
|
err := n.a.sendMail(sendCtx, &u, users.NotifyMailPurpose, n.a.notifyMail(u, p, changes[uid]))
|
|
cancel()
|
|
if err != nil {
|
|
continue // logged by sendMail; the changes stay recorded
|
|
}
|
|
total++
|
|
perUser[uid]++
|
|
sent++
|
|
}
|
|
return sent
|
|
}
|
|
|
|
// notifyMail is the one mail for all of a user's changes in one evaluation.
|
|
func (a *authService) notifyMail(u users.User, p users.NotifyPrefs, changes []notifyChange) mailer.Message {
|
|
base := a.set.baseURL.String()
|
|
lines := make([]mailLine, 0, len(changes))
|
|
for _, c := range changes {
|
|
lines = append(lines, mailLine{text: notifyChangeText(c, a.set.notify.external.label), url: notifySubjectURL(base, c)})
|
|
}
|
|
subject := fmt.Sprintf("%d changes on your watched nodes", len(changes))
|
|
if len(changes) == 1 {
|
|
subject = "1 change on your watched nodes"
|
|
}
|
|
msg := a.render(u.Email, u.DisplayName, "notify", mailContent{
|
|
subject: subject, greeting: "Hello " + u.DisplayName + ",",
|
|
paragraphs: []string{"Changes since the last check:"},
|
|
lines: lines,
|
|
actionLabel: "Manage notifications", actionURL: base + "/#/account?section=notifications",
|
|
footer: &mailLine{text: "You get this mail because you asked " + a.set.baseURL.Host +
|
|
" to watch these nodes. Turn all notification mails off with one click:", url: a.link("unsubscribe", p.UnsubToken)},
|
|
})
|
|
msg.Headers = map[string]string{
|
|
"List-Unsubscribe": "<" + base + "/api/notifications/unsubscribe?token=" + url.QueryEscape(p.UnsubToken) + ">",
|
|
"List-Unsubscribe-Post": "List-Unsubscribe=One-Click",
|
|
}
|
|
return msg
|
|
}
|
|
|
|
// notifyChangeText is one line of the mail: name, what happened, when (UTC).
|
|
// label names the externalAlerts source.
|
|
func notifyChangeText(c notifyChange, label string) string {
|
|
bad := c.To == users.NotifyBad
|
|
var what string
|
|
switch c.Event {
|
|
case users.NotifyNodeOffline:
|
|
what = "back online"
|
|
if bad {
|
|
what = "offline"
|
|
if !c.SharedSeen.IsZero() {
|
|
// #2260: the UI shows such a node as "uncertain".
|
|
what += " (nothing that can only be this node; its hop prefix, shared with other repeaters, was last in a path " +
|
|
c.SharedSeen.UTC().Format("2006-01-02 15:04 UTC") + ", possibly another node)"
|
|
}
|
|
}
|
|
case users.NotifyNodeBattery:
|
|
what = "battery recovered"
|
|
if bad {
|
|
what = "battery low"
|
|
}
|
|
if c.BatteryMv != nil {
|
|
what += fmt.Sprintf(" (%d mV)", *c.BatteryMv)
|
|
}
|
|
case users.NotifyNodeExternal:
|
|
_, key, _ := strings.Cut(c.Subject, "/")
|
|
what = label + " resolved (" + key + ")"
|
|
if bad {
|
|
what = label + ": " + c.Text
|
|
}
|
|
case users.NotifyForeignNew:
|
|
what = "new foreign node"
|
|
case users.NotifyObserverOffline, users.NotifyObserverOfflineOwn:
|
|
what = "observer back online"
|
|
if bad {
|
|
what = "observer offline"
|
|
}
|
|
}
|
|
return c.Name + ": " + what + ", " + c.At.UTC().Format("2006-01-02 15:04 UTC")
|
|
}
|
|
|
|
// notifySubjectURL links a change to its node or observer page, or an
|
|
// external alert to the link the feed gave it.
|
|
func notifySubjectURL(base string, c notifyChange) string {
|
|
switch c.Event {
|
|
case users.NotifyObserverOffline, users.NotifyObserverOfflineOwn:
|
|
return base + "/#/observers/" + url.PathEscape(c.Subject)
|
|
case users.NotifyNodeExternal:
|
|
if c.URL != "" {
|
|
return c.URL
|
|
}
|
|
pk, _, _ := strings.Cut(c.Subject, "/")
|
|
return base + "/#/nodes/" + url.PathEscape(pk)
|
|
}
|
|
return base + "/#/nodes/" + url.PathEscape(c.Subject)
|
|
}
|