Fix the protocol handling on the relay pages

This commit is contained in:
Daniele Tonon
2023-07-13 15:39:08 +02:00
parent 582093503b
commit 4e2f485466
2 changed files with 13 additions and 9 deletions

View File

@@ -51,6 +51,17 @@ func render(w http.ResponseWriter, r *http.Request) {
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 := code
hostname = strings.Replace(hostname, "wss://", "", 1)
hostname = strings.Replace(hostname, "ws://", "", 1)
hostname = strings.Replace(hostname, "wss:/", "", 1) // Some browsers replace upfront '//' with '/'
hostname = strings.Replace(hostname, "ws:/", "", 1) // Some browsers replace upfront '//' with '/'
http.Redirect(w, r, "/"+hostname, http.StatusFound)
}
if urlMatcher.MatchString(code) {
renderRelayPage(w, r)
return

View File

@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/nbd-wtf/go-nostr"
@@ -16,12 +15,6 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
code := r.URL.Path[1:]
hostname := code
if strings.HasPrefix(code, "wss://") {
hostname = code[6:]
}
if strings.HasPrefix(code, "ws://") {
hostname = code[5:]
}
fmt.Println("hostname", hostname)
@@ -29,7 +22,7 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
defer cancel()
// relay metadata
info, _ := nip11.Fetch(r.Context(), code)
info, _ := nip11.Fetch(r.Context(), hostname)
if info == nil {
info = &nip11.RelayInformationDocument{
Name: hostname,
@@ -38,7 +31,7 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
// last notes
var lastNotes []*nostr.Event
if relay, err := pool.EnsureRelay(code); err == nil {
if relay, err := pool.EnsureRelay(hostname); err == nil {
lastNotes, _ = relay.QuerySync(ctx, nostr.Filter{
Kinds: []int{1},
Limit: 50,