forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_archive.go
77 lines (68 loc) · 1.52 KB
/
render_archive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/nbd-wtf/go-nostr/nip19"
)
func renderArchive(w http.ResponseWriter, r *http.Request) {
resultsPerPage := 50
lastIndex := strings.LastIndex(r.URL.Path, "/")
page := 1
if lastIndex != -1 {
pageString := r.URL.Path[lastIndex+1:]
pageInt, err := strconv.Atoi(pageString)
if err != nil {
page = 1
} else {
page = pageInt
}
}
prefix := ""
path_prefix := ""
title := ""
area := strings.Split(r.URL.Path[1:], "/")[0]
if area == "npubs-archive" {
prefix = "pa"
path_prefix = ""
title = "Nostr npubs archive"
} else {
prefix = "ra"
path_prefix = "r/"
title = "Nostr relays archive"
}
keys := cache.GetPaginatedkeys(prefix, page, resultsPerPage)
data := []string{}
for i := 0; i < len(keys); i++ {
if area == "npubs-archive" {
npub, _ := nip19.EncodePublicKey(keys[i])
data = append(data, npub)
} else {
data = append(data, keys[i])
}
}
prevPage := page - 1
nextPage := page + 1
if len(keys) == 0 {
prevPage = 0
nextPage = 0
}
params := map[string]any{
"title": title,
"pathPrefix": path_prefix,
"data": data,
"paginationUrl": area,
"nextPage": fmt.Sprint(nextPage),
"prevPage": fmt.Sprint(prevPage),
}
if len(data) != 0 {
w.Header().Set("Cache-Control", "max-age=3600")
} else {
w.Header().Set("Cache-Control", "max-age=60")
}
if err := tmpl.ExecuteTemplate(w, "archive.html", params); err != nil {
log.Error().Err(err).Msg("error rendering")
return
}
}