mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 14:24:27 +01:00
abstract querying of author and relay last notes, and caching.
This commit is contained in:
21
data.go
21
data.go
@@ -130,15 +130,6 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
|
|||||||
|
|
||||||
switch event.Kind {
|
switch event.Kind {
|
||||||
case 0:
|
case 0:
|
||||||
key := ""
|
|
||||||
eventsToFetch := 100
|
|
||||||
if isProfileSitemap {
|
|
||||||
key = "lns:" + event.PubKey
|
|
||||||
eventsToFetch = 50000
|
|
||||||
} else {
|
|
||||||
key = "ln:" + event.PubKey
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
rawAuthorRelays := []string{}
|
rawAuthorRelays := []string{}
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
|
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
|
lastNotes := authorLastNotes(ctx, event.PubKey, authorRelays, isProfileSitemap)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
renderableLastNotes = make([]EnhancedEvent, len(lastNotes))
|
renderableLastNotes = make([]EnhancedEvent, len(lastNotes))
|
||||||
for i, levt := range lastNotes {
|
for i, levt := range lastNotes {
|
||||||
renderableLastNotes[i] = EnhancedEvent{levt, []string{}}
|
renderableLastNotes[i] = EnhancedEvent{levt, []string{}}
|
||||||
|
|||||||
57
nostr.go
57
nostr.go
@@ -191,19 +191,21 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
|
|||||||
return result, successRelays, nil
|
return result, successRelays, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLastNotes(ctx context.Context, code string, limit int) []*nostr.Event {
|
func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSitemap bool) []*nostr.Event {
|
||||||
if limit <= 0 {
|
key := ""
|
||||||
limit = 10
|
limit := 100
|
||||||
|
if isSitemap {
|
||||||
|
key = "lns:" + pubkey
|
||||||
|
limit = 50000
|
||||||
|
} else {
|
||||||
|
key = "ln:" + pubkey
|
||||||
}
|
}
|
||||||
|
|
||||||
pp := sdk.InputToProfile(ctx, code)
|
lastNotes := make([]*nostr.Event, 0, limit)
|
||||||
if pp == nil {
|
if ok := cache.GetJSON(key, &lastNotes); ok {
|
||||||
return nil
|
return lastNotes
|
||||||
}
|
}
|
||||||
|
|
||||||
pubkeyRelays := relaysForPubkey(ctx, pp.PublicKey, pp.Relays...)
|
|
||||||
relays := append(pp.Relays, pubkeyRelays...)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
|
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@@ -214,12 +216,11 @@ func getLastNotes(ctx context.Context, code string, limit int) []*nostr.Event {
|
|||||||
ch := pool.SubManyEose(ctx, relays, nostr.Filters{
|
ch := pool.SubManyEose(ctx, relays, nostr.Filters{
|
||||||
{
|
{
|
||||||
Kinds: []int{nostr.KindTextNote},
|
Kinds: []int{nostr.KindTextNote},
|
||||||
Authors: []string{pp.PublicKey},
|
Authors: []string{pubkey},
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
lastNotes := make([]*nostr.Event, 0, 20)
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case ie, more := <-ch:
|
case ie, more := <-ch:
|
||||||
@@ -234,7 +235,41 @@ func getLastNotes(ctx context.Context, code string, limit int) []*nostr.Event {
|
|||||||
|
|
||||||
end:
|
end:
|
||||||
slices.SortFunc(lastNotes, func(a, b *nostr.Event) bool { return a.CreatedAt > b.CreatedAt })
|
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
|
return lastNotes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
|
||||||
"github.com/nbd-wtf/go-nostr/nip11"
|
"github.com/nbd-wtf/go-nostr/nip11"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,17 +18,12 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isSitemap := false
|
isSitemap := false
|
||||||
numResults := 1000
|
|
||||||
|
|
||||||
if strings.HasSuffix(hostname, ".xml") {
|
if strings.HasSuffix(hostname, ".xml") {
|
||||||
hostname = hostname[:len(hostname)-4]
|
hostname = hostname[:len(hostname)-4]
|
||||||
numResults = 5000
|
|
||||||
isSitemap = true
|
isSitemap = true
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), time.Second*5)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// relay metadata
|
// relay metadata
|
||||||
info, _ := nip11.Fetch(r.Context(), hostname)
|
info, _ := nip11.Fetch(r.Context(), hostname)
|
||||||
if info == nil {
|
if info == nil {
|
||||||
@@ -40,13 +33,7 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// last notes
|
// last notes
|
||||||
var lastNotes []*nostr.Event
|
lastNotes := relayLastNotes(r.Context(), hostname, isSitemap)
|
||||||
if relay, err := pool.EnsureRelay(hostname); err == nil {
|
|
||||||
lastNotes, _ = relay.QuerySync(ctx, nostr.Filter{
|
|
||||||
Kinds: []int{1},
|
|
||||||
Limit: numResults,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
renderableLastNotes := make([]EnhancedEvent, len(lastNotes))
|
renderableLastNotes := make([]EnhancedEvent, len(lastNotes))
|
||||||
lastEventAt := time.Now()
|
lastEventAt := time.Now()
|
||||||
if len(lastNotes) > 0 {
|
if len(lastNotes) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user