abstract querying of author and relay last notes, and caching.

This commit is contained in:
fiatjaf
2023-10-26 22:30:20 -03:00
parent c4c3f0d524
commit 8111e16960
3 changed files with 48 additions and 45 deletions

View File

@@ -1,12 +1,10 @@
package main
import (
"context"
"net/http"
"strings"
"time"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip11"
)
@@ -20,17 +18,12 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
}
isSitemap := false
numResults := 1000
if strings.HasSuffix(hostname, ".xml") {
hostname = hostname[:len(hostname)-4]
numResults = 5000
isSitemap = true
}
ctx, cancel := context.WithTimeout(r.Context(), time.Second*5)
defer cancel()
// relay metadata
info, _ := nip11.Fetch(r.Context(), hostname)
if info == nil {
@@ -40,13 +33,7 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
}
// last notes
var lastNotes []*nostr.Event
if relay, err := pool.EnsureRelay(hostname); err == nil {
lastNotes, _ = relay.QuerySync(ctx, nostr.Filter{
Kinds: []int{1},
Limit: numResults,
})
}
lastNotes := relayLastNotes(r.Context(), hostname, isSitemap)
renderableLastNotes := make([]EnhancedEvent, len(lastNotes))
lastEventAt := time.Now()
if len(lastNotes) > 0 {