-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathredirect.go
More file actions
71 lines (61 loc) · 1.6 KB
/
Copy pathredirect.go
File metadata and controls
71 lines (61 loc) · 1.6 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
package main
import (
"fmt"
"math/rand"
"net/http"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip19"
)
func redirectToFavicon(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/njump/static/favicon/android-chrome-192x192.png", http.StatusFound)
}
func redirectToRandom(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var target string
defer func() {
switch r.Method {
case "POST":
fmt.Fprintf(w, "%s", target[1:])
case "GET":
http.Redirect(w, r, target, http.StatusFound)
}
}()
// 50% of chance of picking a pubkey
if ra := rand.Intn(2); ra == 0 {
if len(npubsArchive) > 0 {
for pk := range npubsArchive {
if rand.Intn(12) < 2 {
target = "/" + nip19.EncodeNpub(pk)
return
}
}
}
}
// otherwise try to pick an event
const RELAY = "wss://nostr.wine"
for evt := range relayLastNotes(ctx, RELAY, 1) {
target = "/" + nip19.EncodeNevent(evt.ID, []string{RELAY}, evt.PubKey)
return
}
// go to a hardcoded place
target = "/npub1sg6plzptd64u62a878hep2kev88swjh3tw00gjsfl8f237lmu63q0uf63m"
return
}
func redirectFromPSlash(w http.ResponseWriter, r *http.Request) {
pk, err := nostr.PubKeyFromHexCheap(r.URL.Path[3:])
if err != nil {
http.Error(w, "invalid public key hex", 400)
return
}
http.Redirect(w, r, "/"+nip19.EncodeNpub(pk), http.StatusFound)
return
}
func redirectFromESlash(w http.ResponseWriter, r *http.Request) {
id, err := nostr.IDFromHex(r.URL.Path[3:])
if err != nil {
http.Error(w, "invalid public key hex", 400)
return
}
http.Redirect(w, r, "/"+nip19.EncodeNevent(id, nil, nostr.ZeroPK), http.StatusFound)
return
}