From 4e587cf186dba6b1c400d75f62a4e70e66dc8441 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 27 Jul 2023 13:00:52 -0300 Subject: [PATCH] relay pages at /r/... and nip05 pages at /p/... --- render.go | 32 ++++++++++++++++--------------- render_relay.go | 2 +- templates/profile.html | 43 +++++++++++++++++++++++++----------------- utils.go | 16 ++++++++-------- 4 files changed, 52 insertions(+), 41 deletions(-) diff --git a/render.go b/render.go index e9a85f9..414ed3c 100644 --- a/render.go +++ b/render.go @@ -31,7 +31,22 @@ func render(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(code, "e/") { code, _ = nip19.EncodeEvent(code[2:], []string{}, "") } else if strings.HasPrefix(code, "p/") { - code, _ = nip19.EncodePublicKey(code[2:]) + if urlSuffixMatcher.MatchString(code) { + // it's a nip05 + code = code[2:] + } else { + // it's a hex pubkey + code, _ = nip19.EncodePublicKey(code[2:]) + } + } else if strings.HasPrefix(code, "r/") { + hostname := code[2:] + if strings.HasPrefix(hostname, "wss:/") || strings.HasPrefix(hostname, "ws:/") { + hostname = trimProtocol(hostname) + http.Redirect(w, r, "/r/"+hostname, http.StatusFound) + } else { + renderRelayPage(w, r) + } + return } else if strings.HasPrefix(code, "nostr:") { http.Redirect(w, r, "/"+code[6:], http.StatusFound) } else if strings.HasPrefix(code, "npub") && strings.HasSuffix(code, ".xml") { @@ -50,19 +65,6 @@ func render(w http.ResponseWriter, r *http.Request) { // code can be a nevent, nprofile, npub or nip05 identifier, in which case we try to fetch the associated event event, err := getEvent(r.Context(), code) if err != nil { - // this will fail if code is a relay URL, in which case we will handle it differently - - // If the protocol is present strip it and redirect - if strings.HasPrefix(code, "wss:/") || strings.HasPrefix(code, "ws:/") { - hostname := trimProtocol(code) - http.Redirect(w, r, "/"+hostname, http.StatusFound) - } - - if urlMatcher.MatchString(code) { - renderRelayPage(w, r) - return - } - http.Error(w, "error fetching event: "+err.Error(), 404) return } @@ -301,7 +303,7 @@ func render(w http.ResponseWriter, r *http.Request) { "kindNIP": kindNIP, "lastNotes": renderableLastNotes, "parentNevent": parentNevent, - "authorRelays": authorRelays, + "authorRelays": authorRelays, } // if a mapping is not found fallback to raw diff --git a/render_relay.go b/render_relay.go index dfd2602..db44add 100644 --- a/render_relay.go +++ b/render_relay.go @@ -13,7 +13,7 @@ import ( func renderRelayPage(w http.ResponseWriter, r *http.Request) { code := r.URL.Path[1:] - hostname := code + hostname := code[2:] typ := "relay" if strings.HasSuffix(hostname, ".xml") { hostname = code[:len(hostname)-4] diff --git a/templates/profile.html b/templates/profile.html index 322557d..a1dc630 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -7,16 +7,26 @@ {{ if .metadata.Picture }} - - {{end}} - {{ if .metadata.About }} - + + {{end}} {{ if .metadata.About }} + {{end}} - - + + {{template "head_common.html" }} @@ -28,10 +38,10 @@
- {{.metadata.Name | escapeString}} - {{.metadata.DisplayName | escapeString}} + {{.metadata.Name | escapeString}} + {{.metadata.DisplayName | escapeString}}
@@ -77,13 +87,12 @@
Posting on these relays
- {{if .authorRelays}} - {{range $index, $element := .authorRelays}} - {{$element}} - {{end}} - {{else}} - No relays found, sorry! Try to reload to search again. - {{end}} + {{if .authorRelays}} {{range $index, $element := .authorRelays}} + {{$element}} + {{end}} {{else}} No relays found, sorry! Try to reload to search + again. {{end}}
diff --git a/utils.go b/utils.go index 9d2eb31..47ea82d 100644 --- a/utils.go +++ b/utils.go @@ -92,7 +92,7 @@ var kindNIPS = map[int]string{ 30078: "78", } -var urlMatcher = regexp.MustCompile(`^(wss?:\/\/)?[\w-_.]+\.[\w-_.]+(\/[\/\w]*)?$`) +var urlSuffixMatcher = regexp.MustCompile(`[\w-_.]+\.[\w-_.]+(\/[\/\w]*)?$`) type ClientReference struct { Name string @@ -327,13 +327,13 @@ func mdToHTML(md string) string { func unique(strSlice []string) []string { keys := make(map[string]bool) - list := []string{} + list := []string{} for _, entry := range strSlice { - if _, ok := keys[entry]; !ok { - keys[entry] = true - list = append(list, entry) - } - } + if _, ok := keys[entry]; !ok { + keys[entry] = true + list = append(list, entry) + } + } return list } @@ -343,4 +343,4 @@ func trimProtocol(relay string) string { relay = strings.TrimPrefix(relay, "wss:/") // Some browsers replace upfront '//' with '/' relay = strings.TrimPrefix(relay, "ws:/") // Some browsers replace upfront '//' with '/' return relay -} \ No newline at end of file +}