diff --git a/data.go b/data.go index 6d1a6ea..53df525 100644 --- a/data.go +++ b/data.go @@ -134,7 +134,8 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e } data := &Data{ - event: event, + event: event, + relays: relays, } data.npub, _ = nip19.EncodePublicKey(event.PubKey) @@ -144,7 +145,6 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e data.naddrNaked = "" data.createdAt = time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02 15:04:05") data.modifiedAt = time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00") - data.authorRelays = []string{} eventRelays := []string{} diff --git a/nostr.go b/nostr.go index d6cec31..274e27c 100644 --- a/nostr.go +++ b/nostr.go @@ -133,7 +133,9 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve // try to fetch in our internal eventstore first if res, _ := wdb.QuerySync(ctx, filter); len(res) != 0 { - return res[0], nil, err + evt := res[0] + scheduleEventExpiration(evt.ID, time.Hour*24*7) + return evt, getRelaysForEvent(evt.ID), err } // otherwise fetch from external relays @@ -191,11 +193,11 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve // save stuff in cache and in internal store wdb.Publish(ctx, *result) // save relays if we got them - attachRelaysToEvent(result, successRelays...) + allRelays := attachRelaysToEvent(result.ID, successRelays...) // keep track of what we have to delete later scheduleEventExpiration(result.ID, time.Hour*24*7) - return result, successRelays, nil + return result, allRelays, nil } func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSitemap bool) []*nostr.Event { @@ -222,7 +224,7 @@ func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSite defer func() { external <- notes }() - ctx, cancel := context.WithTimeout(ctx, time.Second*4) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() relays = unique(append(relays, getRelay(), getRelay())) ch := pool.SubManyEose(ctx, relays, nostr.Filters{filter}) @@ -232,10 +234,10 @@ func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSite if !more { return } - notes = append(lastNotes, ie.Event) + notes = append(notes, ie.Event) if store { db.SaveEvent(ctx, ie.Event) - attachRelaysToEvent(ie.Event, ie.Relay.URL) + attachRelaysToEvent(ie.Event.ID, ie.Relay.URL) scheduleEventExpiration(ie.Event.ID, time.Hour*24) } case <-ctx.Done(): @@ -248,7 +250,7 @@ func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSite if useLocalStore { lastNotes, _ = eventstore.RelayWrapper{Store: db}.QuerySync(ctx, filter) } - if len(lastNotes) < 2 { + if len(lastNotes) < 5 { // if we didn't get enough notes (or if we didn't even query the local store), wait for the external relays lastNotes = <-external } diff --git a/routines.go b/routines.go index 776d287..66fa8f5 100644 --- a/routines.go +++ b/routines.go @@ -47,7 +47,7 @@ func deleteOldCachedEvents(ctx context.Context) { if expires < now { // time to delete this - id := spl[2] + id := spl[1] res, _ := wdb.QuerySync(ctx, nostr.Filter{IDs: []string{id}}) if len(res) > 0 { log.Debug().Msgf("deleting %s", res[0].ID) diff --git a/utils.go b/utils.go index d3356b0..494081a 100644 --- a/utils.go +++ b/utils.go @@ -239,13 +239,21 @@ func getParentNevent(event *nostr.Event) string { return parentNevent } -func attachRelaysToEvent(event *nostr.Event, relays ...string) { - key := "rls:" + event.ID +func attachRelaysToEvent(eventId string, relays ...string) []string { + key := "rls:" + eventId existingRelays := make([]string, 0, 10) if exists := cache.GetJSON(key, &existingRelays); exists { relays = unique(append(existingRelays, relays...)) } cache.SetJSONWithTTL(key, relays, time.Hour*24*7) + return relays +} + +func getRelaysForEvent(eventId string) []string { + key := "rls:" + eventId + relays := make([]string, 0, 10) + cache.GetJSON(key, &relays) + return relays } func scheduleEventExpiration(eventId string, ts time.Duration) {