-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathclients.go
More file actions
96 lines (81 loc) · 2.08 KB
/
clients.go
File metadata and controls
96 lines (81 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"cmp"
"net/http"
"slices"
"fiatjaf.com/nostr/khatru"
"github.com/fiatjaf/pyramid/global"
"github.com/fiatjaf/pyramid/global/relays"
"github.com/fiatjaf/pyramid/pyramid"
)
type relayClientInfo struct {
khatru.ClientInfo
RelayID global.RelayID
}
type relayClientSnapshot struct {
khatru.ClientSnapshot
RelayID global.RelayID
}
func detailsHandler(w http.ResponseWriter, r *http.Request) {
loggedUser, ok := global.GetLoggedUser(r)
if !ok || !pyramid.IsRoot(loggedUser) {
http.Error(w, "unauthorized", 403)
return
}
clients := make([]relayClientInfo, 0, 16)
for _, relay := range relays.GetAll() {
if relay.Relay == nil {
continue
}
for _, client := range relay.Relay.ListClients() {
clients = append(clients, relayClientInfo{ClientInfo: client, RelayID: relay.ID})
}
}
slices.SortFunc(clients, func(a, b relayClientInfo) int {
if diff := cmp.Compare(b.SubscriptionCount, a.SubscriptionCount); diff != 0 {
return diff
}
if diff := cmp.Compare(a.RelayID, b.RelayID); diff != 0 {
return diff
}
return cmp.Compare(a.ID, b.ID)
})
clientDetailsPage(loggedUser, clients).Render(r.Context(), w)
}
func clientDetailsHandler(w http.ResponseWriter, r *http.Request) {
loggedUser, ok := global.GetLoggedUser(r)
if !ok || !pyramid.IsRoot(loggedUser) {
http.Error(w, "unauthorized", 403)
return
}
var client khatru.ClientSnapshot
var found bool
relayID := global.RelayID(r.URL.Query().Get("r"))
relay := relays.GetRelay(relayID)
if relay != nil {
client, found = relay.GetClientSnapshot(r.PathValue("clientId"))
if !found {
http.NotFound(w, r)
return
}
} else {
for _, relay := range relays.GetAll() {
client, found = relay.Relay.GetClientSnapshot(r.PathValue("clientId"))
if found {
relayID = relay.ID
break
}
}
}
if !found {
http.NotFound(w, r)
return
}
slices.SortFunc(client.Subscriptions, func(a, b khatru.SubscriptionInfo) int {
return cmp.Compare(a.ID, b.ID)
})
singleClientDetailsPage(
loggedUser,
relayClientSnapshot{ClientSnapshot: client, RelayID: relayID},
).Render(r.Context(), w)
}