-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp_ui.go
More file actions
396 lines (339 loc) · 11 KB
/
Copy pathhttp_ui.go
File metadata and controls
396 lines (339 loc) · 11 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package nara
import (
"encoding/json"
"fmt"
"net/http"
"github.com/sirupsen/logrus"
"github.com/eljojo/nara/types"
)
// SSE endpoint for real-time social events (shooting stars!)
func (network *Network) httpEventsSSEHandler(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
logrus.Errorf("SSE handler panic: %v", r)
}
}()
// Set SSE headers
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
// Get flusher for streaming
flusher, ok := w.(http.Flusher)
if !ok {
logrus.Error("SSE not supported - ResponseWriter doesn't implement http.Flusher")
http.Error(w, "SSE not supported", http.StatusInternalServerError)
return
}
// Subscribe to events
eventChan := network.subscribeSSE()
defer network.unsubscribeSSE(eventChan)
// Send initial connection event
fmt.Fprintf(w, "event: connected\ndata: {\"server\":\"%s\"}\n\n", network.meName())
flusher.Flush()
// Stream events until client disconnects
for {
select {
case event := <-eventChan:
// Get UI format from the payload
var uiFormat map[string]string
if event.Social != nil {
uiFormat = event.Social.UIFormat()
} else if event.Ping != nil {
uiFormat = event.Ping.UIFormat()
} else if event.Observation != nil {
uiFormat = event.Observation.UIFormat()
} else if event.HeyThere != nil {
uiFormat = event.HeyThere.UIFormat()
} else if event.Chau != nil {
uiFormat = event.Chau.UIFormat()
} else if event.Seen != nil {
uiFormat = event.Seen.UIFormat()
}
// Skip if no UI format available
if uiFormat == nil {
continue
}
// Send simple event with just UI data
data := map[string]interface{}{
"id": event.ID,
"service": event.Service,
"timestamp": event.Timestamp,
"emitter": event.Emitter,
"icon": uiFormat["icon"],
"text": uiFormat["text"],
"detail": uiFormat["detail"],
}
jsonData, err := json.Marshal(data)
if err != nil {
logrus.Errorf("SSE: failed to marshal event: %v", err)
continue
}
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", event.Service, jsonData)
flusher.Flush()
case <-r.Context().Done():
return
}
}
}
// Clout scores from this nara's perspective
func (network *Network) httpCloutHandler(w http.ResponseWriter, r *http.Request) {
var clout map[types.NaraName]float64
if network.local.Projections != nil {
clout = network.local.Projections.Clout().DeriveClout(network.local.Soul, network.local.Me.Status.Personality)
} else {
clout = make(map[types.NaraName]float64)
}
response := map[string]interface{}{
"server": network.meName(),
"clout": clout,
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(response); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}
// Recent social events
func (network *Network) httpRecentEventsHandler(w http.ResponseWriter, r *http.Request) {
var events []SocialEvent
if network.local.SyncLedger != nil {
// Convert SyncEvents to legacy SocialEvents
syncEvents := network.local.SyncLedger.GetRecentSocialEvents(5)
for _, se := range syncEvents {
if legacy := se.ToSocialEvent(); legacy != nil {
events = append(events, *legacy)
}
}
}
// Convert to JSON-friendly format (timestamps in seconds for UI)
var eventList []map[string]interface{}
for _, e := range events {
eventList = append(eventList, map[string]interface{}{
"actor": e.Actor,
"target": e.Target,
"reason": e.Reason,
"message": TeaseMessage(e.Reason, e.Actor, e.Target),
"timestamp": e.Timestamp / 1e9, // Convert nanoseconds to seconds for UI
})
}
response := map[string]interface{}{
"server": network.meName(),
"events": eventList,
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(response); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}
// Tease counts - objective count of teases per actor (no personality influence)
func (network *Network) httpTeaseCountsHandler(w http.ResponseWriter, r *http.Request) {
var counts map[types.NaraName]int
if network.local.SyncLedger != nil {
counts = network.local.SyncLedger.GetTeaseCounts()
} else {
counts = make(map[types.NaraName]int)
}
// Convert to sorted list for the response
type teaseCount struct {
Actor types.NaraName `json:"actor"`
Count int `json:"count"`
}
var teases []teaseCount
for actor, count := range counts {
teases = append(teases, teaseCount{Actor: actor, Count: count})
}
// Sort by count descending
for i := 0; i < len(teases); i++ {
for j := i + 1; j < len(teases); j++ {
if teases[j].Count > teases[i].Count {
teases[i], teases[j] = teases[j], teases[i]
}
}
}
response := map[string]interface{}{
"server": network.meName(),
"teases": teases,
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(response); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}
// World Journey HTTP handlers
// POST /world/relay - Receive and forward a world journey message
// This is the main transport for world messages between naras
func (network *Network) httpWorldRelayHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var wm WorldMessage
if err := json.NewDecoder(r.Body).Decode(&wm); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
if network.worldHandler == nil {
http.Error(w, "World journey not initialized", http.StatusServiceUnavailable)
return
}
// HandleIncoming verifies signatures, adds our hop, and forwards
if err := network.worldHandler.HandleIncoming(&wm); err != nil {
logrus.Warnf("🌍 World relay failed: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"from": network.meName(),
}); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}
// POST /world/start - Start a new world journey
func (network *Network) httpWorldStartHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req struct {
Message string `json:"message"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
if req.Message == "" {
http.Error(w, "Message is required", http.StatusBadRequest)
return
}
if network.worldHandler == nil {
http.Error(w, "World journey not initialized", http.StatusServiceUnavailable)
return
}
wm, err := network.worldHandler.StartJourney(req.Message)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"id": wm.ID,
"message": wm.OriginalMessage,
}); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}
// GET /world/journeys - Get completed world journeys
func (network *Network) httpWorldJourneysHandler(w http.ResponseWriter, r *http.Request) {
network.worldJourneysMu.RLock()
journeys := make([]*WorldMessage, len(network.worldJourneys))
copy(journeys, network.worldJourneys)
network.worldJourneysMu.RUnlock()
// Return most recent first, limit to 20
if len(journeys) > 20 {
journeys = journeys[len(journeys)-20:]
}
// Reverse order (most recent first)
for i, j := 0, len(journeys)-1; i < j; i, j = i+1, j-1 {
journeys[i], journeys[j] = journeys[j], journeys[i]
}
// Convert to response format
var response []map[string]interface{}
for _, wm := range journeys {
hops := make([]map[string]interface{}, len(wm.Hops))
for i, hop := range wm.Hops {
hops[i] = map[string]interface{}{
"nara": hop.Nara,
"timestamp": hop.Timestamp,
"stamp": hop.Stamp,
"signature": hop.Signature,
}
}
rewards := CalculateWorldRewards(wm)
response = append(response, map[string]interface{}{
"id": wm.ID,
"message": wm.OriginalMessage,
"originator": wm.Originator,
"hops": hops,
"rewards": rewards,
"complete": wm.IsComplete(),
})
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"journeys": response,
"server": network.meName(),
}); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}
// GET /network/map - All known nodes with coordinates for visualization
func (network *Network) httpNetworkMapHandler(w http.ResponseWriter, r *http.Request) {
network.local.mu.Lock()
defer network.local.mu.Unlock()
var nodes []map[string]interface{}
// Add ourselves
network.local.Me.mu.Lock()
meCoords := network.local.Me.Status.Coordinates
network.local.Me.mu.Unlock()
nodes = append(nodes, map[string]interface{}{
"name": network.meName(),
"coordinates": meCoords,
"online": true,
"is_self": true,
})
// Add all neighbours
for name, nara := range network.Neighbourhood {
nara.mu.Lock()
coords := nara.Status.Coordinates
nara.mu.Unlock()
// Get our observation of this peer
myObs := network.local.getObservationLocked(types.NaraName(name))
node := map[string]interface{}{
"name": name,
"coordinates": coords,
"online": myObs.Online == "ONLINE",
"is_self": false,
}
if myObs.LastPingRTT > 0 {
node["rtt_to_us"] = myObs.LastPingRTT
}
if myObs.AvgPingRTT > 0 {
node["avg_rtt"] = myObs.AvgPingRTT
}
nodes = append(nodes, node)
}
response := map[string]interface{}{
"nodes": nodes,
"server": network.meName(),
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(response); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}
// httpProximityHandler returns this nara's barrio information
// Naras in the same barrio (grid cell) share the same emoji and are considered "nearby"
func (network *Network) httpProximityHandler(w http.ResponseWriter, r *http.Request) {
// Get my barrio info
myEmoji := network.GetMyBarrioEmoji()
// Find all naras in my barrio
var barrioMembers []types.NaraName
for name := range network.Neighbourhood {
if network.IsInMyBarrio(name) {
barrioMembers = append(barrioMembers, name)
}
}
response := map[string]interface{}{
"server": network.meName(),
"barrio_emoji": myEmoji,
"barrio_members": barrioMembers,
"grid_size": network.calculateGridSize(),
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(response); err != nil {
logrus.WithError(err).Warn("Failed to encode response")
}
}