Files
mailgoserver/internal/webui/navcounts.go
T

70 lines
1.8 KiB
Go

package webui
import (
"net/http"
"time"
)
// navCounts are the small per-resource counts shown as sidebar badges on every page
// (and reused by the dashboard's own stat tiles, which use the same numbers) — scoped
// to the current admin exactly like every list page already is.
type navCounts struct {
DomainCount, SenderCount, MailboxCount, IPCount, DKIMCount, BlacklistCount int
}
func (a *App) computeNavCounts(r *http.Request) navCounts {
scope := scopeFromContext(r)
var c navCounts
if scope.Global {
c.DomainCount, _ = a.DB.CountActiveDomains()
c.SenderCount, _ = a.DB.CountActiveSenders()
c.DKIMCount, _ = a.DB.CountActiveDKIMKeys()
if entries, err := a.DB.ListBlacklist(); err == nil {
now := time.Now()
for _, e := range entries {
if e.ExpiresAt.After(now) {
c.BlacklistCount++
}
}
}
} else {
domains, _ := a.DB.ListDomains()
for _, d := range domains {
if d.IsActive && scope.Allowed(d.ID) {
c.DomainCount++
}
}
senders, _ := a.DB.ListSenders()
for _, s := range senders {
if s.IsActive && scope.Allowed(s.DomainID) {
c.SenderCount++
}
}
keys, _ := a.DB.ListActiveDKIMKeysWithDomain()
for _, k := range keys {
if scope.Allowed(k.DomainID) {
c.DKIMCount++
}
}
}
// Mailboxes and IPs always need a per-row pass regardless of scope.Global (no
// dedicated CountActive* helpers exist for them), same as dashboard.go already did
// for mailboxes before this was centralized.
mailboxes, _ := a.DB.ListMailboxes()
for _, m := range mailboxes {
if m.IsActive && (scope.Global || scope.Allowed(m.DomainID)) {
c.MailboxCount++
}
}
ips, _ := a.DB.ListWhitelistedIPs()
for _, ip := range ips {
if ip.IsActive && (scope.Global || scope.Allowed(ip.DomainID)) {
c.IPCount++
}
}
return c
}