Add relay sitemaps

This commit is contained in:
Daniele Tonon
2023-07-13 21:47:25 +02:00
parent 85c31ee3df
commit 6d7d9416f4
4 changed files with 31 additions and 13 deletions

View File

@@ -46,6 +46,7 @@ func main() {
templateMapping["note"] = "note.html" templateMapping["note"] = "note.html"
templateMapping["address"] = "other.html" templateMapping["address"] = "other.html"
templateMapping["relay"] = "relay.html" templateMapping["relay"] = "relay.html"
templateMapping["relay_sitemap"] = "sitemap.xml"
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"basicFormatting": basicFormatting, "basicFormatting": basicFormatting,

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
@@ -13,8 +14,12 @@ import (
func renderRelayPage(w http.ResponseWriter, r *http.Request) { func renderRelayPage(w http.ResponseWriter, r *http.Request) {
code := r.URL.Path[1:] code := r.URL.Path[1:]
hostname := code hostname := code
typ := "relay"
if strings.HasSuffix(hostname, ".xml") {
hostname = code[:len(hostname)-4]
typ = "relay_sitemap"
}
fmt.Println("hostname", hostname) fmt.Println("hostname", hostname)
@@ -30,40 +35,49 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
} }
// last notes // last notes
events_num := 50
if typ == "relay_sitemap" {
events_num = 50000
}
var lastNotes []*nostr.Event var lastNotes []*nostr.Event
if relay, err := pool.EnsureRelay(hostname); err == nil { if relay, err := pool.EnsureRelay(hostname); err == nil {
lastNotes, _ = relay.QuerySync(ctx, nostr.Filter{ lastNotes, _ = relay.QuerySync(ctx, nostr.Filter{
Kinds: []int{1}, Kinds: []int{1},
Limit: 50, Limit: events_num,
}) })
} }
renderableLastNotes := make([]*Event, len(lastNotes)) renderableLastNotes := make([]*Event, len(lastNotes))
lastEventAt := time.Now()
for i, n := range lastNotes { for i, n := range lastNotes {
nevent, _ := nip19.EncodeEvent(n.ID, []string{}, n.PubKey) nevent, _ := nip19.EncodeEvent(n.ID, []string{}, n.PubKey)
date := time.Unix(int64(n.CreatedAt), 0).Format("2006-01-02 15:04:05")
renderableLastNotes[i] = &Event{ renderableLastNotes[i] = &Event{
Nevent: nevent, Nevent: nevent,
Content: n.Content, Content: n.Content,
CreatedAt: date, CreatedAt: time.Unix(int64(n.CreatedAt), 0).Format("2006-01-02 15:04:05"),
ModifiedAt: time.Unix(int64(n.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00"),
ParentNevent: getParentNevent(n), ParentNevent: getParentNevent(n),
} }
if i == 0 {
lastEventAt = time.Unix(int64(n.CreatedAt), 0)
}
} }
params := map[string]any{ params := map[string]any{
"clients": []ClientReference{ "clients": []ClientReference{
{Name: "Coracle", URL: "https://coracle.social/relays/" + hostname}, {Name: "Coracle", URL: "https://coracle.social/relays/" + hostname},
}, },
"type": "relay", "type": "relay",
"info": info, "info": info,
"hostname": hostname, "hostname": hostname,
"proxy": "https://" + hostname + "/njump/proxy?src=", "proxy": "https://" + hostname + "/njump/proxy?src=",
"lastNotes": renderableLastNotes, "lastNotes": renderableLastNotes,
"modifiedAt": lastEventAt.Format("2006-01-02T15:04:05Z07:00"),
} }
// +build !nocache // +build !nocache
w.Header().Set("Cache-Control", "max-age=604800") w.Header().Set("Cache-Control", "max-age=604800")
if err := tmpl.ExecuteTemplate(w, templateMapping["relay"], params); err != nil { if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {
log.Error().Err(err).Msg("error rendering") log.Error().Err(err).Msg("error rendering")
return return
} }

View File

@@ -59,6 +59,8 @@
<!-----------> <!----------->
{{ if eq .type "relay" }} {{ if eq .type "relay" }}
<title>Nostr Relay {{.hostname}}</title> <title>Nostr Relay {{.hostname}}</title>
<link rel="sitemap" type="application/xml" title="Sitemap for {{.hostname | escapeString}}"
href="/{{.hostname | escapeString}}.xml">
{{end}} {{end}}
<!-----------> <!----------->
{{ if eq .type "other" }} {{ if eq .type "other" }}

View File

@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url> <url>
<loc>https://nostr.com/{{.npub | escapeString}}</loc> {{if .npub}}<loc>https://nostr.com/{{.npub | escapeString}}</loc>{{end}}
{{if .hostname}}<loc>https://nostr.com/{{.hostname | escapeString}}</loc>{{end}}
<lastmod>{{.modifiedAt | escapeString}}</lastmod> <lastmod>{{.modifiedAt | escapeString}}</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
<priority>0.8</priority> <priority>0.8</priority>