stop sending npubs to relays and use hints when fetching notes.

This commit is contained in:
fiatjaf
2023-07-08 20:08:51 -03:00
parent 9f2272072e
commit 3531ecc4ca
3 changed files with 16 additions and 11 deletions

View File

@@ -121,12 +121,17 @@ func getEvent(ctx context.Context, code string) (*nostr.Event, error) {
return nil, fmt.Errorf("couldn't find this %s", prefix)
}
func getLastNotes(ctx context.Context, pubkey string) ([]nostr.Event, error) {
func getLastNotes(ctx context.Context, code string) []*nostr.Event {
pp := sdk.InputToProfile(ctx, code)
if pp == nil {
return nil
}
ctx, cancel := context.WithTimeout(ctx, time.Millisecond*1500)
defer cancel()
relays := make([]string, 0, 20)
for _, relay := range sdk.FetchRelaysForPubkey(ctx, pool, pubkey, relays...) {
relays := pp.Relays
for _, relay := range sdk.FetchRelaysForPubkey(ctx, pool, pp.PublicKey, pp.Relays...) {
if relay.Outbox {
relays = append(relays, relay.URL)
}
@@ -140,13 +145,13 @@ func getLastNotes(ctx context.Context, pubkey string) ([]nostr.Event, error) {
events := pool.SubManyEose(ctx, relays, nostr.Filters{
{
Kinds: []int{nostr.KindTextNote},
Authors: []string{pubkey},
Authors: []string{pp.PublicKey},
Limit: 20,
},
})
lastNotes := make([]nostr.Event, 0, 20)
lastNotes := make([]*nostr.Event, 0, 20)
for event := range events {
lastNotes = append(lastNotes, *event)
lastNotes = append(lastNotes, event)
}
return lastNotes, nil
return lastNotes
}

View File

@@ -71,7 +71,7 @@ func render(w http.ResponseWriter, r *http.Request) {
if event.Kind == 0 {
typ = "profile"
thisLastNotes, err := getLastNotes(r.Context(), code)
thisLastNotes := getLastNotes(r.Context(), code)
lastNotes = make([]Event, len(thisLastNotes))
for i, n := range thisLastNotes {
this_nevent, _ := nip19.EncodeEvent(n.ID, []string{}, n.PubKey)
@@ -80,7 +80,7 @@ func render(w http.ResponseWriter, r *http.Request) {
Nevent: this_nevent,
Content: n.Content,
CreatedAt: this_date,
ParentNevent: findParentNevent(&n),
ParentNevent: getParentNevent(n),
}
}
if err != nil {
@@ -93,7 +93,7 @@ func render(w http.ResponseWriter, r *http.Request) {
typ = "note"
note, _ = nip19.EncodeNote(event.ID)
content = event.Content
parentNevent = findParentNevent(event)
parentNevent = getParentNevent(event)
} else if event.Kind == 6 {
typ = "note"
if reposted := event.Tags.GetFirst([]string{"e", ""}); reposted != nil {

View File

@@ -181,7 +181,7 @@ func getPreviewStyle(r *http.Request) string {
}
}
func findParentNevent(event *nostr.Event) string {
func getParentNevent(event *nostr.Event) string {
parentNevent := ""
replyTag := nip10.GetImmediateReply(event.Tags)
if replyTag != nil {