Add initial support to profile sitamaps

This commit is contained in:
Daniele Tonon
2023-07-12 23:18:46 +02:00
parent 0cad5a662a
commit c32362e220
6 changed files with 50 additions and 9 deletions

View File

@@ -14,7 +14,7 @@ tmp_dir = "tmp"
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "scss", "js"]
include_ext = ["go", "tpl", "tmpl", "html", "scss", "js", "xml"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"

View File

@@ -42,6 +42,7 @@ func main() {
// initialize templates
// use a mapping to expressly link the templates and share them between more kinds/types
templateMapping["profile"] = "profile.html"
templateMapping["profile_sitemap"] = "sitemap.xml"
templateMapping["note"] = "note.html"
templateMapping["address"] = "other.html"
templateMapping["relay"] = "relay.html"

View File

@@ -127,7 +127,13 @@ func getEvent(ctx context.Context, code string) (*nostr.Event, error) {
return nil, fmt.Errorf("couldn't find this %s", prefix)
}
func getLastNotes(ctx context.Context, code string) []*nostr.Event {
func getLastNotes(ctx context.Context, code string, options ...int) []*nostr.Event {
events_num := 10
if len(options) > 0 {
events_num = options[0]
}
pp := sdk.InputToProfile(ctx, code)
if pp == nil {
return nil
@@ -146,7 +152,7 @@ func getLastNotes(ctx context.Context, code string) []*nostr.Event {
{
Kinds: []int{nostr.KindTextNote},
Authors: []string{pp.PublicKey},
Limit: 10,
Limit: events_num,
},
})
lastNotes := make([]*nostr.Event, 0, 20)

View File

@@ -18,6 +18,7 @@ type Event struct {
Nevent string
Content string
CreatedAt string
ModifiedAt string
ParentNevent string
}
@@ -25,6 +26,7 @@ func render(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
w.Header().Set("Content-Type", "text/html")
typ := ""
code := r.URL.Path[1:]
if strings.HasPrefix(code, "e/") {
code, _ = nip19.EncodeNote(code[2:])
@@ -32,7 +34,11 @@ func render(w http.ResponseWriter, r *http.Request) {
code, _ = nip19.EncodePublicKey(code[2:])
} else if strings.HasPrefix(code, "nostr:") {
http.Redirect(w, r, "/"+code[6:], http.StatusFound)
} else if strings.HasPrefix(code, "npub") && strings.HasSuffix(code, ".xml") {
code = code[:len(code)-4]
typ = "profile_sitemap"
}
if code == "" {
fmt.Fprintf(w, "call /<nip19 code>")
return
@@ -59,21 +65,29 @@ func render(w http.ResponseWriter, r *http.Request) {
note := ""
naddr := ""
createdAt := time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02 15:04:05")
modifiedAt := time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00")
content := ""
typ := ""
author := event
var renderableLastNotes []*Event
parentNevent := ""
if event.Kind == 0 {
typ = "profile"
key := "ln:" + event.PubKey
key := ""
events_num := 10
if typ == "profile_sitemap" {
key = "lns:" + event.PubKey
events_num = 50000
} else {
typ = "profile"
key = "ln:" + event.PubKey
}
var lastNotes []*nostr.Event
if ok := cache.GetJSON(key, &lastNotes); !ok {
ctx, cancel := context.WithTimeout(r.Context(), time.Second*4)
lastNotes = getLastNotes(ctx, code)
lastNotes = getLastNotes(ctx, code, events_num)
cancel()
cache.SetJSONWithTTL(key, lastNotes, time.Hour*24)
}
@@ -81,11 +95,11 @@ func render(w http.ResponseWriter, r *http.Request) {
renderableLastNotes = make([]*Event, len(lastNotes))
for i, n := range lastNotes {
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{
Nevent: nevent,
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),
}
}
@@ -243,6 +257,7 @@ func render(w http.ResponseWriter, r *http.Request) {
params := map[string]any{
"createdAt": createdAt,
"modifiedAt": modifiedAt,
"clients": generateClientList(code, event),
"type": typ,
"title": title,

View File

@@ -4,6 +4,8 @@
<meta property="og:site_name" content="{{.npub | escapeString}}" />
<meta property="og:title" content="{{.title | escapeString}}" />
<link rel="canonical" href="https://nostr.com/{{.npub | escapeString }}" />
<link rel="sitemap" type="application/xml" title="Sitemap for {{.npub | escapeString}}"
href="/{{.npub | escapeString}}.xml">
{{ if .metadata.Picture }}
<meta property="og:image" content="{{.metadata.Picture | escapeString}}" />
<meta

17
templates/sitemap.xml Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>/{{.npub | escapeString}}</loc>
<lastmod>{{.modifiedAt | escapeString}}</lastmod>
<changefreq>hourly</changefreq>
<priority>1.0</priority>
</url>
{{range .lastNotes}}
<url>
<loc>/{{.Nevent | escapeString}}</loc>
<lastmod>{{.ModifiedAt | escapeString}}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
{{end}}
</urlset>