From c32362e220d9ceeea969f7f250a502f01b2f5207 Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Wed, 12 Jul 2023 23:18:46 +0200 Subject: [PATCH] Add initial support to profile sitamaps --- .air.toml | 2 +- main.go | 1 + nostr.go | 10 ++++++++-- render.go | 27 +++++++++++++++++++++------ templates/head.html | 2 ++ templates/sitemap.xml | 17 +++++++++++++++++ 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 templates/sitemap.xml diff --git a/.air.toml b/.air.toml index 08bb5c1..77e9cfa 100644 --- a/.air.toml +++ b/.air.toml @@ -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" diff --git a/main.go b/main.go index c6cedd5..786fb3e 100644 --- a/main.go +++ b/main.go @@ -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" diff --git a/nostr.go b/nostr.go index efa960b..db78d7d 100644 --- a/nostr.go +++ b/nostr.go @@ -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) diff --git a/render.go b/render.go index 836af3d..c964e75 100644 --- a/render.go +++ b/render.go @@ -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 /") 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, diff --git a/templates/head.html b/templates/head.html index 5f1cc15..3b056a4 100644 --- a/templates/head.html +++ b/templates/head.html @@ -4,6 +4,8 @@ + {{ if .metadata.Picture }} + + + /{{.npub | escapeString}} + {{.modifiedAt | escapeString}} + hourly + 1.0 + +{{range .lastNotes}} + + /{{.Nevent | escapeString}} + {{.ModifiedAt | escapeString}} + monthly + 0.8 + +{{end}} + \ No newline at end of file