Add relays archive

This commit is contained in:
Daniele Tonon
2023-07-19 01:06:43 +02:00
parent eabec9b590
commit 37ff53f22c
4 changed files with 52 additions and 14 deletions

View File

@@ -35,6 +35,7 @@ var (
func updateArchives(ctx context.Context) {
for {
loadNpubsArchive(ctx)
loadRelaysArchive(ctx)
// Wait for 24 hours before executing the function again
time.Sleep(24 * time.Hour)
}
@@ -79,7 +80,8 @@ func main() {
http.HandleFunc("/njump/image/", generate)
http.HandleFunc("/njump/proxy/", proxy)
http.Handle("/njump/static/", http.StripPrefix("/njump/", http.FileServer(http.FS(static))))
http.HandleFunc("/npubs-archive/", renderProfilesArchive)
http.HandleFunc("/npubs-archive/", renderArchive)
http.HandleFunc("/relays-archive/", renderArchive)
http.HandleFunc("/", render)
log.Print("listening at http://0.0.0.0:" + s.Port)

View File

@@ -9,8 +9,8 @@ import (
"github.com/nbd-wtf/go-nostr/nip19"
)
func renderProfilesArchive(w http.ResponseWriter, r *http.Request) {
resultsPerPage := 100
func renderArchive(w http.ResponseWriter, r *http.Request) {
resultsPerPage := 50
lastIndex := strings.LastIndex(r.URL.Path, "/")
page := 1
if lastIndex != -1 {
@@ -23,11 +23,26 @@ func renderProfilesArchive(w http.ResponseWriter, r *http.Request) {
}
}
keys := cache.GetPaginatedkeys("pa", page, resultsPerPage)
npubs := []string{}
prefix := ""
title := ""
area := strings.Split(r.URL.Path[1:], "/")[0]
if area == "npubs-archive" {
prefix = "pa"
title = "Nostr npubs archive"
} else {
prefix = "ra"
title = "Nostr relays archive"
}
keys := cache.GetPaginatedkeys(prefix, page, resultsPerPage)
data := []string{}
for i := 0; i < len(keys); i++ {
npub, _ := nip19.EncodePublicKey(keys[i])
npubs = append(npubs, npub)
if area == "npubs-archive" {
npub, _ := nip19.EncodePublicKey(keys[i])
data = append(data, npub)
} else {
data = append(data, keys[i])
}
}
prevPage := page - 1
@@ -38,12 +53,14 @@ func renderProfilesArchive(w http.ResponseWriter, r *http.Request) {
}
params := map[string]any{
"title": title,
"data": data,
"pagination_url": area ,
"nextPage": fmt.Sprint(nextPage),
"prevPage": fmt.Sprint(prevPage),
"data": npubs,
}
w.Header().Set("Cache-Control", "max-age=604800")
w.Header().Set("Cache-Control", "max-age=86400")
if err := tmpl.ExecuteTemplate(w, "archive.html", params); err != nil {
log.Error().Err(err).Msg("error rendering")

View File

@@ -2,7 +2,7 @@
<html class="theme--default">
<meta charset="UTF-8" />
<head>
<title>Nostr npubs archive</title>
<title>{{.title}}</title>
{{template "head_common.html" }}
</head>
@@ -14,7 +14,7 @@
<div class="container">
<div class="column columnA">
<div class="info-wrapper">
Nostr npubs archive
{{.title}}
<span class="display">&nbsp;</span>
</div>
<div class="pic-wrapper">
@@ -25,7 +25,7 @@
<div class="column column_content">
<div class="field info-wrapper">
<h1 class="name">
Nostr npubs archive
{{.title}}
</h1>
</div>
<div class="field separator long"></div>
@@ -39,11 +39,11 @@
</div>
{{if not (eq .prevPage "0")}}
<a href="/npubs-archive/{{.prevPage | escapeString}}"><< Prev page</a>
<a href="/{{.pagination_url}}/{{.prevPage | escapeString}}"><< Prev page</a>
{{end}}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{{if not (eq .nextPage "0")}}
<a href="/npubs-archive/{{.nextPage | escapeString}}">Next page >></a>
<a href="/{{.pagination_url}}/{{.nextPage | escapeString}}">Next page >></a>
{{end}}
</div>

View File

@@ -365,3 +365,22 @@ func loadNpubsArchive(ctx context.Context) {
cache.SetWithTTL("pa:"+contact, nil, time.Hour*24*90)
}
}
func loadRelaysArchive(ctx context.Context) {
fmt.Println("Refreshing the relays archive")
relaysArchive := make([]string, 0, 500)
for _, pubkey := range trustedPubKeys {
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
pubkeyContacts := relaysForPubkey(ctx, pubkey)
relaysArchive = append(relaysArchive, pubkeyContacts...)
cancel()
}
relaysArchive = unique(relaysArchive)
for _, relay := range relaysArchive {
fmt.Printf("Adding relay %s\n", relay)
cache.SetWithTTL("ra:"+relay, nil, time.Hour*24*7)
}
}