mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-11-22 15:58:07 +00:00
Follow-up to #2415, see #2414. ## Situation A user can claim repeaters, but no page lists the ones they claimed. Claims show on each node page ("Maintained by"), in the admin user detail and in the "Download my data" export. A maintainer of several repeaters has to remember them and open each node page to see whether one went quiet. ## Change **`GET /api/account/repeaters`** (browser session; device tokens answer 403) returns the caller's claims. For each one: - `pubkey` and `name`. - `known`: false once the node left the analyzer database. The claim stays. - `role` and `claimedAt`. - `lastHeard`: the newer of the last advert and the last transmission certainly from the node, the same sources as the node page's Last Heard. - `lastRelayed`: when its hop prefix last relayed. Only used to mark a silent repeater as uncertain, since the prefix can be shared. - `status`: the saved repeater status, or null. - `comaintainers`: the other active claimers' display names. The list is sorted by name, with unnamed repeaters after, by pubkey. It's built on the existing `RepeaterClaimsByUser`, `NotifyNodes` and `LastHeardMap`. **My account, My repeaters**, a new section after Companions: - Silent repeaters come first. Health uses `getNodeStatus`, so the threshold matches the node pages and map (`infraSilentMs`): - green "heard 2h ago"; - yellow "last heard …, its prefix still relays"; - red "silent, last heard 184d ago" or "never heard"; - grey "no longer in the database". - Each row links to the node page and shows the saved status ("no details yet" without one), the claim date and "also maintained by …". - *Unclaim* asks for confirmation, then uses the existing `DELETE /api/repeaters/{pubkey}/claim`. - The empty state says how to claim. - `#/account?section=repeaters` deep-links to the section, like `section=notifications`. Editing stays on the node page, so there's only one editor. The admin user detail and the export are unchanged. Docs: `openapi.go` and the Repeater info section of `docs/user-guide/accounts.md`. ## Notes - A mobile client will want the same list. #2429 proposes device-token scopes for repeater claims. If it lands, letting a `repeater:claim` token read this route is a one-line addition to the bearer route table, and I'd rather do that there than here. - The two PRs touch nearby lines in `openapi.go`, `repeater_handlers.go` (route registration) and `public/account.js`. Whichever merges second needs a small rebase. ## Tests - `cmd/server/my_repeaters_test.go`: - empty list; - ordering (named first, case-insensitive); - unheard, heard and departed nodes; - saved status; - co-maintainers, excluding the caller and disabled users; - another user's view; - unclaim removes the row; - anonymous gets 401 and a device token 403; - no analyzer DB. - `tests/unit/test-user-management-ui.js`: health sort and chips, escaping, the short-pubkey fallback, status and co-maintainer lines, Unclaim on every row, and the empty state. - `tests/e2e/test-user-management-e2e.js`: - the claimed repeater appears with its status and node link; - axe on the section; - Unclaim: dismissing the confirm keeps the claim, accepting empties the list. Run locally: - `make test`: all 16 modules OK. - `sh test-all.sh` and `node tests/e2e/test-lint-network-calls.js`: pass, run in `node:24-bookworm`. - `npx eslint public/*.js`: 0 errors, the same 87 warnings as master. - `test-user-management-e2e.js` against local e2etest servers with the CI config: 46/46. - Manually in Chrome, light and dark: heard, silent and co-maintained rows, and the deep link. Not run: the full Playwright shard matrix from CI. --------- Co-authored-by: Adam <196335+anieto@users.noreply.github.com>
146 lines
4.8 KiB
Go
146 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/meshcore-analyzer/users"
|
|
)
|
|
|
|
// myRepeaterJSON is one repeater the caller claimed, for My account, My
|
|
// repeaters: enough to spot one that went quiet without opening each node.
|
|
type myRepeaterJSON struct {
|
|
Pubkey string `json:"pubkey"`
|
|
Name string `json:"name"` // "" when unnamed or in neither nodes nor inactive_nodes
|
|
Known bool `json:"known"` // false when the node is in neither nodes nor inactive_nodes
|
|
// Inactive: retention moved the node to inactive_nodes (silent longer
|
|
// than retention.nodeDays); name, role and lastHeard come from there.
|
|
Inactive bool `json:"inactive"`
|
|
Role string `json:"role"` // as stored, lowercase; "" when not known
|
|
// ClaimedAt is the caller's own claim.
|
|
ClaimedAt string `json:"claimedAt"`
|
|
// LastHeard is the newer of the node's last advert and the last
|
|
// transmission certainly from it (the node page's Last Heard); null when
|
|
// neither is known.
|
|
LastHeard *string `json:"lastHeard"`
|
|
// LastRelayed is the last time its hop prefix was seen relaying. The
|
|
// prefix can be shared with other repeaters, so it only marks a silent
|
|
// repeater as uncertain, never as heard.
|
|
LastRelayed *string `json:"lastRelayed"`
|
|
// Status is the saved repeater status (active, maintenance,
|
|
// decommissioned); null when none is saved.
|
|
Status *string `json:"status"`
|
|
// Comaintainers are the other active claimers, by display name.
|
|
Comaintainers []string `json:"comaintainers"`
|
|
}
|
|
|
|
type myRepeatersJSON struct {
|
|
Repeaters []myRepeaterJSON `json:"repeaters"`
|
|
}
|
|
|
|
// handleMyRepeaters lists the repeaters the caller claimed, by name (then
|
|
// pubkey). A node retention moved to inactive_nodes is read from there and
|
|
// marked inactive; a claim of a node in neither table is kept as known: false.
|
|
func (s *Server) handleMyRepeaters(w http.ResponseWriter, _ *http.Request, u *users.User, _ *users.Session) {
|
|
claims, err := s.auth.st.RepeaterClaimsByUser(u.ID)
|
|
if err != nil {
|
|
log.Printf("[users] my repeaters %d: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
out := myRepeatersJSON{Repeaters: make([]myRepeaterJSON, 0, len(claims))}
|
|
if len(claims) == 0 {
|
|
writeJSON(w, out)
|
|
return
|
|
}
|
|
pks := make([]string, len(claims))
|
|
for i, c := range claims {
|
|
pks[i] = c.Pubkey
|
|
}
|
|
nodes := map[string]notifyNode{}
|
|
inactive := map[string]notifyNode{}
|
|
if s.db != nil {
|
|
if nodes, err = s.db.NotifyNodes(pks, false); err != nil {
|
|
log.Printf("[users] my repeaters %d: nodes: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
var missing []string
|
|
for _, pk := range pks {
|
|
if _, ok := nodes[pk]; !ok {
|
|
missing = append(missing, pk)
|
|
}
|
|
}
|
|
if len(missing) > 0 {
|
|
if inactive, err = s.db.InactiveNodeBasics(missing); err != nil {
|
|
log.Printf("[users] my repeaters %d: inactive nodes: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
infos, err := s.auth.st.RepeaterInfoOfUserRepeaters(u.ID)
|
|
if err != nil {
|
|
log.Printf("[users] my repeaters %d: info: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
claimers, err := s.auth.st.RepeaterClaimsOfUserRepeaters(u.ID)
|
|
if err != nil {
|
|
log.Printf("[users] my repeaters %d: claims: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
heard := map[string]time.Time{}
|
|
var relay map[string]RepeaterRelayInfo
|
|
if s.store != nil {
|
|
heard, _ = s.store.LastHeardMap(pks)
|
|
relay = s.store.GetRepeaterRelayInfoMap(s.cfg.GetHealthThresholds().RelayActiveHours)
|
|
}
|
|
for _, c := range claims {
|
|
nd, known := nodes[c.Pubkey]
|
|
gone := false
|
|
if !known {
|
|
nd, gone = inactive[c.Pubkey]
|
|
known = gone
|
|
}
|
|
r := myRepeaterJSON{Pubkey: c.Pubkey, Name: nd.Name, Known: known, Inactive: gone, Role: strings.ToLower(nd.Role),
|
|
ClaimedAt: rfc3339(c.ClaimedAt), Comaintainers: []string{}}
|
|
last, _ := parseRelayTS(nd.LastSeen)
|
|
if t, ok := heard[c.Pubkey]; ok && t.After(last) {
|
|
last = t
|
|
}
|
|
if !last.IsZero() {
|
|
ts := last.UTC().Format(time.RFC3339)
|
|
r.LastHeard = &ts
|
|
}
|
|
if info, ok := lookupRelayInfo(relay, c.Pubkey); ok {
|
|
r.LastRelayed = stringPtrOrNil(info.LastRelayed)
|
|
}
|
|
if info, ok := infos[c.Pubkey]; ok {
|
|
r.Status = stringPtrOrNil(info.Status)
|
|
}
|
|
for _, o := range claimers[c.Pubkey] {
|
|
if o.UserID != u.ID {
|
|
r.Comaintainers = append(r.Comaintainers, o.DisplayName)
|
|
}
|
|
}
|
|
out.Repeaters = append(out.Repeaters, r)
|
|
}
|
|
sort.SliceStable(out.Repeaters, func(i, j int) bool {
|
|
a, b := out.Repeaters[i], out.Repeaters[j]
|
|
if an, bn := strings.ToLower(a.Name), strings.ToLower(b.Name); an != bn {
|
|
// Named repeaters first, then the unnamed by pubkey.
|
|
if an == "" || bn == "" {
|
|
return bn == ""
|
|
}
|
|
return an < bn
|
|
}
|
|
return a.Pubkey < b.Pubkey
|
|
})
|
|
writeJSON(w, out)
|
|
}
|