fixing implementation of previous commit in multiple places.

This commit is contained in:
fiatjaf
2023-11-05 14:39:02 -03:00
parent 9967d10cd6
commit e477d38e66
4 changed files with 22 additions and 12 deletions

View File

@@ -135,6 +135,7 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
data := &Data{ data := &Data{
event: event, event: event,
relays: relays,
} }
data.npub, _ = nip19.EncodePublicKey(event.PubKey) data.npub, _ = nip19.EncodePublicKey(event.PubKey)
@@ -144,7 +145,6 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
data.naddrNaked = "" data.naddrNaked = ""
data.createdAt = time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02 15:04:05") 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.modifiedAt = time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00")
data.authorRelays = []string{} data.authorRelays = []string{}
eventRelays := []string{} eventRelays := []string{}

View File

@@ -133,7 +133,9 @@ func getEvent(ctx context.Context, code string, relayHints []string) (*nostr.Eve
// try to fetch in our internal eventstore first // try to fetch in our internal eventstore first
if res, _ := wdb.QuerySync(ctx, filter); len(res) != 0 { 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 // 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 // save stuff in cache and in internal store
wdb.Publish(ctx, *result) wdb.Publish(ctx, *result)
// save relays if we got them // save relays if we got them
attachRelaysToEvent(result, successRelays...) allRelays := attachRelaysToEvent(result.ID, successRelays...)
// keep track of what we have to delete later // keep track of what we have to delete later
scheduleEventExpiration(result.ID, time.Hour*24*7) 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 { 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() { defer func() {
external <- notes external <- notes
}() }()
ctx, cancel := context.WithTimeout(ctx, time.Second*4) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel() defer cancel()
relays = unique(append(relays, getRelay(), getRelay())) relays = unique(append(relays, getRelay(), getRelay()))
ch := pool.SubManyEose(ctx, relays, nostr.Filters{filter}) ch := pool.SubManyEose(ctx, relays, nostr.Filters{filter})
@@ -232,10 +234,10 @@ func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSite
if !more { if !more {
return return
} }
notes = append(lastNotes, ie.Event) notes = append(notes, ie.Event)
if store { if store {
db.SaveEvent(ctx, ie.Event) 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) scheduleEventExpiration(ie.Event.ID, time.Hour*24)
} }
case <-ctx.Done(): case <-ctx.Done():
@@ -248,7 +250,7 @@ func authorLastNotes(ctx context.Context, pubkey string, relays []string, isSite
if useLocalStore { if useLocalStore {
lastNotes, _ = eventstore.RelayWrapper{Store: db}.QuerySync(ctx, filter) 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 // if we didn't get enough notes (or if we didn't even query the local store), wait for the external relays
lastNotes = <-external lastNotes = <-external
} }

View File

@@ -47,7 +47,7 @@ func deleteOldCachedEvents(ctx context.Context) {
if expires < now { if expires < now {
// time to delete this // time to delete this
id := spl[2] id := spl[1]
res, _ := wdb.QuerySync(ctx, nostr.Filter{IDs: []string{id}}) res, _ := wdb.QuerySync(ctx, nostr.Filter{IDs: []string{id}})
if len(res) > 0 { if len(res) > 0 {
log.Debug().Msgf("deleting %s", res[0].ID) log.Debug().Msgf("deleting %s", res[0].ID)

View File

@@ -239,13 +239,21 @@ func getParentNevent(event *nostr.Event) string {
return parentNevent return parentNevent
} }
func attachRelaysToEvent(event *nostr.Event, relays ...string) { func attachRelaysToEvent(eventId string, relays ...string) []string {
key := "rls:" + event.ID key := "rls:" + eventId
existingRelays := make([]string, 0, 10) existingRelays := make([]string, 0, 10)
if exists := cache.GetJSON(key, &existingRelays); exists { if exists := cache.GetJSON(key, &existingRelays); exists {
relays = unique(append(existingRelays, relays...)) relays = unique(append(existingRelays, relays...))
} }
cache.SetJSONWithTTL(key, relays, time.Hour*24*7) 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) { func scheduleEventExpiration(eventId string, ts time.Duration) {