mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 06:14:22 +01:00
- adjusting many small things related to nostr-sdk usage - fetching profiles in a smarter way - decoupling the logic for rendering profile pages from the `grabData`/`getEvent` flow of other event pages. - incorporating nostr-sdk more holistically, including more hints stuff - improving nostr-sdk itself after some bugs and weird behaviors observed here - set up opentelemetry (should probably remove this later)
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
)
|
|
|
|
var npubsArchive = make([]string, 0, 5000)
|
|
|
|
func updateArchives(ctx context.Context) {
|
|
// do this so we don't run this every time we restart it locally
|
|
|
|
time.Sleep(10 * time.Minute)
|
|
|
|
for {
|
|
loadNpubsArchive(ctx)
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-time.After(24 * time.Hour):
|
|
}
|
|
}
|
|
}
|
|
|
|
func deleteOldCachedEvents(ctx context.Context) {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-time.After(time.Hour):
|
|
log.Debug().Msg("deleting old cached events")
|
|
now := time.Now().Unix()
|
|
for _, key := range cache.GetPaginatedKeys("ttl:", 1, 500) {
|
|
spl := strings.Split(key, ":")
|
|
if len(spl) != 2 {
|
|
log.Error().Str("key", key).Msg("broken 'ttl:' key")
|
|
continue
|
|
}
|
|
|
|
var expires int64
|
|
if ok := cache.GetJSON(key, &expires); !ok {
|
|
log.Error().Str("key", key).Msg("failed to get 'ttl:' key")
|
|
continue
|
|
}
|
|
|
|
if expires < now {
|
|
// time to delete this
|
|
id := spl[1]
|
|
res, _ := sys.StoreRelay.QuerySync(ctx, nostr.Filter{IDs: []string{id}})
|
|
if len(res) > 0 {
|
|
log.Debug().Msgf("deleting %s", res[0].ID)
|
|
if err := sys.Store.DeleteEvent(ctx, res[0]); err != nil {
|
|
log.Warn().Err(err).Stringer("event", res[0]).Msg("failed to delete")
|
|
}
|
|
}
|
|
cache.Delete(key)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadNpubsArchive(ctx context.Context) {
|
|
log.Debug().Msg("refreshing the npubs archive")
|
|
|
|
contactsArchive := make([]string, 0, 500)
|
|
for _, pubkey := range s.TrustedPubKeys {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
|
|
pubkeyContacts := contactsForPubkey(ctx, pubkey)
|
|
contactsArchive = append(contactsArchive, pubkeyContacts...)
|
|
cancel()
|
|
}
|
|
|
|
for _, contact := range unique(contactsArchive) {
|
|
log.Debug().Msgf("adding contact %s", contact)
|
|
cache.SetWithTTL("pa:"+contact, nil, time.Hour*24*90)
|
|
}
|
|
}
|