-
Notifications
You must be signed in to change notification settings - Fork 24
/
redirect.go
63 lines (53 loc) · 1.5 KB
/
redirect.go
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
package main
import (
"fmt"
"math/rand"
"net/http"
"fiatjaf.com/leafdb"
"github.com/nbd-wtf/go-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) {
var target string
defer func() {
switch r.Method {
case "POST":
fmt.Fprintf(w, 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 {
params := leafdb.AnyQuery("pubkey-archive")
params.Skip = rand.Intn(50)
for val := range internal.View(params) {
pka := val.(*PubKeyArchive)
npub, _ := nip19.EncodePublicKey(pka.Pubkey)
target = "/" + npub
return
}
}
// otherwise try to pick an event
const RELAY = "wss://nostr.wine"
for evt := range relayLastNotes(r.Context(), RELAY, 1) {
nevent, _ := nip19.EncodeEvent(evt.ID, []string{RELAY}, evt.PubKey)
target = "/" + nevent
return
}
// go to a hardcoded place
target = "/npub1sg6plzptd64u62a878hep2kev88swjh3tw00gjsfl8f237lmu63q0uf63m"
return
}
func redirectFromPSlash(w http.ResponseWriter, r *http.Request) {
code, _ := nip19.EncodePublicKey(r.URL.Path[3:])
http.Redirect(w, r, "/"+code, http.StatusFound)
return
}
func redirectFromESlash(w http.ResponseWriter, r *http.Request) {
code, _ := nip19.EncodeEvent(r.URL.Path[3:], []string{}, "")
http.Redirect(w, r, "/"+code, http.StatusFound)
return
}