diff --git a/data.go b/data.go index 21d3a63..eac9f56 100644 --- a/data.go +++ b/data.go @@ -130,15 +130,6 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e switch event.Kind { case 0: - key := "" - eventsToFetch := 100 - if isProfileSitemap { - key = "lns:" + event.PubKey - eventsToFetch = 50000 - } else { - key = "ln:" + event.PubKey - } - { rawAuthorRelays := []string{} ctx, cancel := context.WithTimeout(ctx, time.Second*4) @@ -157,17 +148,7 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e } } - var lastNotes []*nostr.Event - - if ok := cache.GetJSON(key, &lastNotes); !ok { - ctx, cancel := context.WithTimeout(ctx, time.Second*4) - lastNotes = getLastNotes(ctx, code, eventsToFetch) - cancel() - if len(lastNotes) > 0 { - cache.SetJSONWithTTL(key, lastNotes, time.Hour*24) - } - } - + lastNotes := authorLastNotes(ctx, event.PubKey, authorRelays, isProfileSitemap) renderableLastNotes = make([]EnhancedEvent, len(lastNotes)) for i, levt := range lastNotes { renderableLastNotes[i] = EnhancedEvent{levt, []string{}} diff --git a/nostr.go b/nostr.go index a766750..23e0f61 100644 --- a/nostr.go +++ b/nostr.go @@ -191,19 +191,21 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve return result, successRelays, nil } -func getLastNotes(ctx context.Context, code string, limit int) []*nostr.Event { - if limit <= 0 { - limit = 10 +func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSitemap bool) []*nostr.Event { + key := "" + limit := 100 + if isSitemap { + key = "lns:" + pubkey + limit = 50000 + } else { + key = "ln:" + pubkey } - pp := sdk.InputToProfile(ctx, code) - if pp == nil { - return nil + lastNotes := make([]*nostr.Event, 0, limit) + if ok := cache.GetJSON(key, &lastNotes); ok { + return lastNotes } - pubkeyRelays := relaysForPubkey(ctx, pp.PublicKey, pp.Relays...) - relays := append(pp.Relays, pubkeyRelays...) - ctx, cancel := context.WithTimeout(ctx, time.Second*4) defer cancel() @@ -214,12 +216,11 @@ func getLastNotes(ctx context.Context, code string, limit int) []*nostr.Event { ch := pool.SubManyEose(ctx, relays, nostr.Filters{ { Kinds: []int{nostr.KindTextNote}, - Authors: []string{pp.PublicKey}, + Authors: []string{pubkey}, Limit: limit, }, }) - lastNotes := make([]*nostr.Event, 0, 20) for { select { case ie, more := <-ch: @@ -234,7 +235,41 @@ func getLastNotes(ctx context.Context, code string, limit int) []*nostr.Event { end: slices.SortFunc(lastNotes, func(a, b *nostr.Event) bool { return a.CreatedAt > b.CreatedAt }) + if len(lastNotes) > 0 { + cache.SetJSONWithTTL(key, lastNotes, time.Hour*24) + } + return lastNotes +} +func relayLastNotes(ctx context.Context, relayUrl string, isSitemap bool) []*nostr.Event { + key := "" + limit := 1000 + if isSitemap { + key = "rlns:" + nostr.NormalizeURL(relayUrl) + limit = 5000 + } else { + key = "rln:" + nostr.NormalizeURL(relayUrl) + } + + lastNotes := make([]*nostr.Event, 0, limit) + if ok := cache.GetJSON(key, &lastNotes); ok { + return lastNotes + } + + ctx, cancel := context.WithTimeout(ctx, time.Second*4) + defer cancel() + + if relay, err := pool.EnsureRelay(relayUrl); err == nil { + lastNotes, _ = relay.QuerySync(ctx, nostr.Filter{ + Kinds: []int{1}, + Limit: limit, + }) + } + + slices.SortFunc(lastNotes, func(a, b *nostr.Event) bool { return a.CreatedAt > b.CreatedAt }) + if len(lastNotes) > 0 { + cache.SetJSONWithTTL(key, lastNotes, time.Hour*24) + } return lastNotes } diff --git a/render_relay.go b/render_relay.go index 5e25a6e..cf574ba 100644 --- a/render_relay.go +++ b/render_relay.go @@ -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 {