forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutines.go
81 lines (69 loc) · 1.86 KB
/
routines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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)
}
}