relay and archive sitemaps xml (with a nice hack because htmlgo is not xml-friendly.)

This commit is contained in:
fiatjaf
2023-10-21 13:34:32 -03:00
parent 65025d4967
commit f70b13afe7
6 changed files with 62 additions and 27 deletions

View File

@@ -71,8 +71,6 @@ func main() {
// use a mapping to expressly link the templates and share them between more kinds/types // use a mapping to expressly link the templates and share them between more kinds/types
templateMapping = map[string]string{ templateMapping = map[string]string{
"profile_sitemap": "sitemap.xml", "profile_sitemap": "sitemap.xml",
"relay_sitemap": "sitemap.xml",
"archive_sitemap": "sitemap.xml",
} }
funcMap := template.FuncMap{ funcMap := template.FuncMap{

View File

@@ -272,7 +272,6 @@ type RelayPage struct {
ClientsPartial `tmpl:"clients"` ClientsPartial `tmpl:"clients"`
FooterPartial `tmpl:"footer"` FooterPartial `tmpl:"footer"`
Type string
Info *nip11.RelayInformationDocument Info *nip11.RelayInformationDocument
Hostname string Hostname string
Proxy string Proxy string
@@ -283,3 +282,31 @@ type RelayPage struct {
func (*RelayPage) TemplateText() string { func (*RelayPage) TemplateText() string {
return tmplRelay return tmplRelay
} }
var (
//go:embed templates/sitemap.xml
tmplSitemap string
SitemapTemplate = tmpl.MustCompile(&SitemapPage{})
)
type SitemapPage struct {
Host string
ModifiedAt string
// for the profile sitemap
Npub string
// for the relay sitemap
RelayHostname string
// for the profile and relay sitemaps
LastNotes []EnhancedEvent
// for the archive sitemap
PathPrefix string
Data []string
}
func (*SitemapPage) TemplateText() string {
return tmplSitemap
}

View File

@@ -35,7 +35,7 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
} }
prefix := "" prefix := ""
path_prefix := "" pathPrefix := ""
title := "" title := ""
area := "" area := ""
if strings.HasPrefix(r.URL.Path[1:], "npubs-archive") { if strings.HasPrefix(r.URL.Path[1:], "npubs-archive") {
@@ -44,11 +44,11 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
if area == "npubs-archive" { if area == "npubs-archive" {
prefix = "pa" prefix = "pa"
path_prefix = "" pathPrefix = ""
title = "Nostr npubs archive" title = "Nostr npubs archive"
} else { } else {
prefix = "ra" prefix = "ra"
path_prefix = "r/" pathPrefix = "r/"
title = "Nostr relays archive" title = "Nostr relays archive"
} }
@@ -89,7 +89,7 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
HeadCommonPartial: HeadCommonPartial{IsProfile: false}, HeadCommonPartial: HeadCommonPartial{IsProfile: false},
Title: title, Title: title,
PathPrefix: path_prefix, PathPrefix: pathPrefix,
Data: data, Data: data,
ModifiedAt: modifiedAt, ModifiedAt: modifiedAt,
PaginationUrl: area, PaginationUrl: area,
@@ -97,6 +97,13 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
PrevPage: fmt.Sprint(prevPage), PrevPage: fmt.Sprint(prevPage),
}) })
} else { } else {
// ArchiveSitemapTemplate.Render TODO w.Header().Add("content-type", "text/xml")
w.Write([]byte(XML_HEADER))
SitemapTemplate.Render(w, &SitemapPage{
Host: s.Domain,
ModifiedAt: modifiedAt,
PathPrefix: pathPrefix,
Data: data,
})
} }
} }

View File

@@ -65,7 +65,6 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
}, },
}, },
Type: "relay",
Info: info, Info: info,
Hostname: hostname, Hostname: hostname,
Proxy: "https://" + hostname + "/njump/proxy?src=", Proxy: "https://" + hostname + "/njump/proxy?src=",
@@ -73,6 +72,13 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
ModifiedAt: lastEventAt.Format("2006-01-02T15:04:05Z07:00"), ModifiedAt: lastEventAt.Format("2006-01-02T15:04:05Z07:00"),
}) })
} else { } else {
// ArchiveSitemapTemplate.Render TODO w.Header().Add("content-type", "text/xml")
w.Write([]byte(XML_HEADER))
SitemapTemplate.Render(w, &SitemapPage{
Host: s.Domain,
ModifiedAt: lastEventAt.Format("2006-01-02T15:04:05Z07:00"),
LastNotes: renderableLastNotes,
RelayHostname: hostname,
})
} }
} }

View File

@@ -1,39 +1,34 @@
<?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"> {{if not (eq "" .Npub)}}
{{if .Npub}}
<url> <url>
<loc>https://{{s.Domain}}/{{.Npub | escapeString}}</loc> <loc>https://{{.Host}}/{{.Npub}}</loc>
<lastmod>{{.ModifiedAt | escapeString}}</lastmod> <lastmod>{{.ModifiedAt}}</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
<priority>0.8</priority> <priority>0.8</priority>
</url> </url>
{{end}} {{end}}
{{if .Hostname}} {{if not (eq "" .RelayHostname)}}
<url> <url>
<loc>https://{{s.Domain}}/r/{{.Hostname | escapeString}}</loc> <loc>https://{{.Host}}/r/{{.RelayHostname}}</loc>
<lastmod>{{.ModifiedAt | escapeString}}</lastmod> <lastmod>{{.ModifiedAt}}</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
<priority>0.8</priority> <priority>0.8</priority>
</url> </url>
{{end}} {{end}}
{{if .LastNotes}} {{range $i, $ee := .LastNotes}}
{{range .LastNotes}}
<url> <url>
<loc>https://{{s.Domain}}/{{.Nevent | escapeString}}</loc> <loc>https://{{$.Host}}/{{$ee.Nevent}}</loc>
<lastmod>{{.ModifiedAt | escapeString}}</lastmod> <lastmod>{{$ee.ModifiedAtStr}}</lastmod>
<changefreq>never</changefreq> <changefreq>never</changefreq>
<priority>0.5</priority> <priority>0.5</priority>
</url> </url>
{{end}} {{end}}
{{end}}
{{if .Data}}
{{range $element := .Data }} {{range $element := .Data }}
<url> <url>
<loc>https://{{s.Domain}}/{{$.PathPrefix}}{{$element | trimProtocol | escapeString}}</loc> <loc>https://{{$.Host}}/{{$.PathPrefix}}{{$element}}</loc>
<lastmod>{{$.ModifiedAt | escapeString}}</lastmod> <lastmod>{{$.ModifiedAt}}</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
<priority>0.5</priority> <priority>0.5</priority>
</url> </url>
{{end}} {{end}}
{{end}}
</urlset> </urlset>

View File

@@ -22,6 +22,8 @@ import (
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
) )
const XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
var ( var (
urlSuffixMatcher = regexp.MustCompile(`[\w-_.]+\.[\w-_.]+(\/[\/\w]*)?$`) urlSuffixMatcher = regexp.MustCompile(`[\w-_.]+\.[\w-_.]+(\/[\/\w]*)?$`)
nostrEveryMatcher = regexp.MustCompile(`nostr:((npub|note|nevent|nprofile|naddr)1[a-z0-9]+)\b`) nostrEveryMatcher = regexp.MustCompile(`nostr:((npub|note|nevent|nprofile|naddr)1[a-z0-9]+)\b`)