From 65025d49671d3e3632000baf074aeb0590718792 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 21 Oct 2023 12:19:30 -0300 Subject: [PATCH] tmpl relay.html and replace LastNoteItem struct with a simpler EnhancedEvent with methods instead of hardcoded values. --- data.go | 57 +++++++++++++++--------- main.go | 1 - pages.go | 27 +++++++++++- render_relay.go | 70 +++++++++++++----------------- templates/profile.html | 10 ++--- templates/relay.html | 98 +++++++++++++++--------------------------- 6 files changed, 131 insertions(+), 132 deletions(-) diff --git a/data.go b/data.go index 84ec9a6..1c88ab8 100644 --- a/data.go +++ b/data.go @@ -14,18 +14,17 @@ import ( "github.com/nbd-wtf/go-nostr/nip19" ) -type LastNotesItem struct { - Npub string - NpubShort string - Nevent string - Content string - CreatedAt string - ModifiedAt string - IsReply bool +type EnhancedEvent struct { + event *nostr.Event + relays []string } -func (i LastNotesItem) Preview() template.HTML { - lines := strings.Split(html.EscapeString(i.Content), "\n") +func (ee EnhancedEvent) IsReply() bool { + return nip10.GetImmediateReply(ee.event.Tags) != nil +} + +func (ee EnhancedEvent) Preview() template.HTML { + lines := strings.Split(html.EscapeString(ee.event.Content), "\n") var processedLines []string for _, line := range lines { if strings.TrimSpace(line) == "" { @@ -38,6 +37,29 @@ func (i LastNotesItem) Preview() template.HTML { return template.HTML(strings.Join(processedLines, "
")) } +func (ee EnhancedEvent) Npub() string { + npub, _ := nip19.EncodePublicKey(ee.event.PubKey) + return npub +} + +func (ee EnhancedEvent) NpubShort() string { + npub := ee.Npub() + return npub[:8] + "…" + npub[len(npub)-4:] +} + +func (ee EnhancedEvent) Nevent() string { + nevent, _ := nip19.EncodeEvent(ee.event.ID, ee.relays, ee.event.PubKey) + return nevent +} + +func (ee EnhancedEvent) CreatedAtStr() string { + return time.Unix(int64(ee.event.CreatedAt), 0).Format("2006-01-02 15:04:05") +} + +func (ee EnhancedEvent) ModifiedAtStr() string { + return time.Unix(int64(ee.event.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00") +} + type Data struct { templateId TemplateID typ string @@ -54,7 +76,7 @@ type Data struct { authorRelays []string authorLong string authorShort string - renderableLastNotes []LastNotesItem + renderableLastNotes []EnhancedEvent kindDescription string kindNIP string video string @@ -86,7 +108,7 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e modifiedAt := time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00") author := event - var renderableLastNotes []LastNotesItem + var renderableLastNotes []EnhancedEvent parentNevent := "" authorRelays := []string{} var content string @@ -148,16 +170,9 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e } } - renderableLastNotes = make([]LastNotesItem, len(lastNotes)) + renderableLastNotes = make([]EnhancedEvent, len(lastNotes)) for i, levt := range lastNotes { - nevent, _ := nip19.EncodeEvent(levt.ID, []string{}, levt.PubKey) - renderableLastNotes[i] = LastNotesItem{ - Nevent: nevent, - Content: levt.Content, - CreatedAt: time.Unix(int64(levt.CreatedAt), 0).Format("2006-01-02 15:04:05"), - ModifiedAt: time.Unix(int64(levt.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00"), - IsReply: nip10.GetImmediateReply(levt.Tags) != nil, - } + renderableLastNotes[i] = EnhancedEvent{levt, []string{}} } if err != nil { return nil, err diff --git a/main.go b/main.go index 3075012..921d368 100644 --- a/main.go +++ b/main.go @@ -71,7 +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": "relay.html", "relay_sitemap": "sitemap.xml", "archive_sitemap": "sitemap.xml", } diff --git a/pages.go b/pages.go index fa9bf07..9edff25 100644 --- a/pages.go +++ b/pages.go @@ -7,6 +7,7 @@ import ( "html/template" "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip11" "github.com/tylermmorton/tmpl" ) @@ -244,7 +245,7 @@ type ProfilePage struct { Content string CreatedAt string Domain string - LastNotes []LastNotesItem + LastNotes []EnhancedEvent Metadata nostr.ProfileMetadata NormalizedAuthorWebsiteURL string RenderedAuthorAboutText template.HTML @@ -258,3 +259,27 @@ type ProfilePage struct { func (*ProfilePage) TemplateText() string { return tmplProfile } + +var ( + //go:embed templates/relay.html + tmplRelay string + RelayTemplate = tmpl.MustCompile(&RelayPage{}) +) + +type RelayPage struct { + HeadCommonPartial `tmpl:"head_common"` + TopPartial `tmpl:"top"` + ClientsPartial `tmpl:"clients"` + FooterPartial `tmpl:"footer"` + + Type string + Info *nip11.RelayInformationDocument + Hostname string + Proxy string + LastNotes []EnhancedEvent + ModifiedAt string +} + +func (*RelayPage) TemplateText() string { + return tmplRelay +} diff --git a/render_relay.go b/render_relay.go index c5126e0..f8cf6ca 100644 --- a/render_relay.go +++ b/render_relay.go @@ -7,18 +7,19 @@ import ( "time" "github.com/nbd-wtf/go-nostr" - "github.com/nbd-wtf/go-nostr/nip10" "github.com/nbd-wtf/go-nostr/nip11" - "github.com/nbd-wtf/go-nostr/nip19" ) func renderRelayPage(w http.ResponseWriter, r *http.Request) { code := r.URL.Path[1:] hostname := code[2:] - typ := "relay" + isSitemap := false + numResults := 1000 + if strings.HasSuffix(hostname, ".xml") { hostname = hostname[:len(hostname)-4] - typ = "relay_sitemap" + numResults = 5000 + isSitemap = true } ctx, cancel := context.WithTimeout(r.Context(), time.Second*5) @@ -33,48 +34,20 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) { } // last notes - events_num := 1000 - if typ == "relay_sitemap" { - events_num = 5000 - } var lastNotes []*nostr.Event if relay, err := pool.EnsureRelay(hostname); err == nil { lastNotes, _ = relay.QuerySync(ctx, nostr.Filter{ Kinds: []int{1}, - Limit: events_num, + Limit: numResults, }) } - renderableLastNotes := make([]LastNotesItem, len(lastNotes)) + renderableLastNotes := make([]EnhancedEvent, len(lastNotes)) lastEventAt := time.Now() - relay := []string{"wss://" + hostname} - for i, levt := range lastNotes { - nevent, _ := nip19.EncodeEvent(levt.ID, relay, levt.PubKey) - npub, _ := nip19.EncodePublicKey(levt.PubKey) - npubShort := npub[:8] + "…" + npub[len(npub)-4:] - renderableLastNotes[i] = LastNotesItem{ - Npub: npub, - NpubShort: npubShort, - Nevent: nevent, - Content: levt.Content, - CreatedAt: time.Unix(int64(levt.CreatedAt), 0).Format("2006-01-02 15:04:05"), - ModifiedAt: time.Unix(int64(levt.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00"), - IsReply: nip10.GetImmediateReply(levt.Tags) != nil, - } - if i == 0 { - lastEventAt = time.Unix(int64(levt.CreatedAt), 0) - } + if len(lastNotes) > 0 { + lastEventAt = time.Unix(int64(lastNotes[0].CreatedAt), 0) } - - params := map[string]any{ - "clients": []ClientReference{ - {Name: "Coracle", URL: "https://coracle.social/relays/" + hostname}, - }, - "type": "relay", - "info": info, - "hostname": hostname, - "proxy": "https://" + hostname + "/njump/proxy?src=", - "lastNotes": renderableLastNotes, - "modifiedAt": lastEventAt.Format("2006-01-02T15:04:05Z07:00"), + for i, levt := range lastNotes { + renderableLastNotes[i] = EnhancedEvent{levt, []string{"wss://" + hostname}} } if len(renderableLastNotes) != 0 { @@ -83,8 +56,23 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "max-age=60") } - if err := tmpls.ExecuteTemplate(w, templateMapping[typ], params); err != nil { - log.Error().Err(err).Msg("error rendering") - return + if !isSitemap { + RelayTemplate.Render(w, &RelayPage{ + HeadCommonPartial: HeadCommonPartial{IsProfile: false}, + ClientsPartial: ClientsPartial{ + Clients: []ClientReference{ + {Name: "Coracle", URL: "https://coracle.social/relays/" + hostname}, + }, + }, + + Type: "relay", + Info: info, + Hostname: hostname, + Proxy: "https://" + hostname + "/njump/proxy?src=", + LastNotes: renderableLastNotes, + ModifiedAt: lastEventAt.Format("2006-01-02T15:04:05Z07:00"), + }) + } else { + // ArchiveSitemapTemplate.Render TODO } } diff --git a/templates/profile.html b/templates/profile.html index 608acdd..20389dc 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -109,15 +109,15 @@
diff --git a/templates/relay.html b/templates/relay.html index 88888fd..a9427a8 100644 --- a/templates/relay.html +++ b/templates/relay.html @@ -1,123 +1,95 @@ - + - - Nostr Relay {{.hostname | escapeString}} - {{.info.Name | escapeString}} - - - - - {{ if .info.Icon }} - - - {{end}} {{ if .info.Description }} - - + Nostr Relay {{.Hostname}} - {{.Info.Name}} + + + + {{ if not (eq "" .Info.Icon) }} + + + {{end}} {{ if not (eq "" .Info.Description) }} + + {{end}} - {{template "head_common.html" .}} + {{template "head_common" .}} - {{template "top.html" .}} + {{template "top" .}}
- {{.info.Name | escapeString}} + {{.Info.Name}}  
- +
-

{{.info.Name | escapeString}}

+

{{.Info.Name}}

-
wss://{{.hostname | escapeString}}
-
- {{.info.Description | escapeString | basicFormatting}} -
+
wss://{{.Hostname}}
+
{{.Info.Description}}
- {{if .info.PubKey}} + {{ if not (eq "" .Info.PubKey) }}
Public Key
- {{.info.PubKey | escapeString}} + {{.Info.PubKey}}
- {{end}} {{if .info.Contact}} + {{ end }} + + {{ if not (eq "" .Info.Contact) }} - {{end}} + {{ end }}
- {{template "column_clients.html" .}} + {{template "clients" .ClientsPartial}}
- {{template "footer.html"}} - - + {{template "footer" .}}