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
templateMapping = map[string]string{
"profile_sitemap": "sitemap.xml",
"relay_sitemap": "sitemap.xml",
"archive_sitemap": "sitemap.xml",
}
funcMap := template.FuncMap{

View File

@@ -272,7 +272,6 @@ type RelayPage struct {
ClientsPartial `tmpl:"clients"`
FooterPartial `tmpl:"footer"`
Type string
Info *nip11.RelayInformationDocument
Hostname string
Proxy string
@@ -283,3 +282,31 @@ type RelayPage struct {
func (*RelayPage) TemplateText() string {
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 := ""
path_prefix := ""
pathPrefix := ""
title := ""
area := ""
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" {
prefix = "pa"
path_prefix = ""
pathPrefix = ""
title = "Nostr npubs archive"
} else {
prefix = "ra"
path_prefix = "r/"
pathPrefix = "r/"
title = "Nostr relays archive"
}
@@ -89,7 +89,7 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
HeadCommonPartial: HeadCommonPartial{IsProfile: false},
Title: title,
PathPrefix: path_prefix,
PathPrefix: pathPrefix,
Data: data,
ModifiedAt: modifiedAt,
PaginationUrl: area,
@@ -97,6 +97,13 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
PrevPage: fmt.Sprint(prevPage),
})
} 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,
Hostname: hostname,
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"),
})
} 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">
{{if .Npub}}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{if not (eq "" .Npub)}}
<url>
<loc>https://{{s.Domain}}/{{.Npub | escapeString}}</loc>
<lastmod>{{.ModifiedAt | escapeString}}</lastmod>
<loc>https://{{.Host}}/{{.Npub}}</loc>
<lastmod>{{.ModifiedAt}}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
{{end}}
{{if .Hostname}}
{{if not (eq "" .RelayHostname)}}
<url>
<loc>https://{{s.Domain}}/r/{{.Hostname | escapeString}}</loc>
<lastmod>{{.ModifiedAt | escapeString}}</lastmod>
<loc>https://{{.Host}}/r/{{.RelayHostname}}</loc>
<lastmod>{{.ModifiedAt}}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
{{end}}
{{if .LastNotes}}
{{range .LastNotes}}
{{range $i, $ee := .LastNotes}}
<url>
<loc>https://{{s.Domain}}/{{.Nevent | escapeString}}</loc>
<lastmod>{{.ModifiedAt | escapeString}}</lastmod>
<loc>https://{{$.Host}}/{{$ee.Nevent}}</loc>
<lastmod>{{$ee.ModifiedAtStr}}</lastmod>
<changefreq>never</changefreq>
<priority>0.5</priority>
</url>
{{end}}
{{end}}
{{if .Data}}
{{range $element := .Data }}
<url>
<loc>https://{{s.Domain}}/{{$.PathPrefix}}{{$element | trimProtocol | escapeString}}</loc>
<lastmod>{{$.ModifiedAt | escapeString}}</lastmod>
<loc>https://{{$.Host}}/{{$.PathPrefix}}{{$element}}</loc>
<lastmod>{{$.ModifiedAt}}</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
{{end}}
{{end}}
</urlset>

View File

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