This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
outbox.go
89 lines (75 loc) · 1.95 KB
/
outbox.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
82
83
84
85
86
87
88
89
package sdk
import (
"context"
"fmt"
"sync"
"time"
"github.com/nbd-wtf/go-nostr"
)
func (sys *System) FetchOutboxRelays(ctx context.Context, pubkey string, n int) []string {
if relays, ok := sys.outboxShortTermCache.Get(pubkey); ok {
if len(relays) > n {
relays = relays[0:n]
}
return relays
}
if rl, ok := sys.RelayListCache.Get(pubkey); !ok || (rl.Event != nil && rl.Event.CreatedAt < nostr.Now()-60*60*24*7) {
// try to fetch relays list again if we don't have one or if ours is a week old
fetchGenericList(sys, ctx, pubkey, 10002, parseRelayFromKind10002, sys.RelayListCache, false)
}
relays := sys.Hints.TopN(pubkey, 6)
if len(relays) == 0 {
return []string{"wss://relay.damus.io", "wss://nos.lol"}
}
sys.outboxShortTermCache.SetWithTTL(pubkey, relays, time.Minute*2)
if len(relays) > n {
relays = relays[0:n]
}
return relays
}
func (sys *System) ExpandQueriesByAuthorAndRelays(
ctx context.Context,
filter nostr.Filter,
) (map[string]nostr.Filter, error) {
n := len(filter.Authors)
if n == 0 {
return nil, fmt.Errorf("no authors in filter")
}
relaysForPubkey := make(map[string][]string, n)
mu := sync.Mutex{}
wg := sync.WaitGroup{}
wg.Add(n)
for _, pubkey := range filter.Authors {
go func(pubkey string) {
defer wg.Done()
relayURLs := sys.FetchOutboxRelays(ctx, pubkey, 3)
c := 0
for _, r := range relayURLs {
relay, err := sys.Pool.EnsureRelay(r)
if err != nil {
continue
}
mu.Lock()
relaysForPubkey[pubkey] = append(relaysForPubkey[pubkey], relay.URL)
mu.Unlock()
c++
if c == 3 {
return
}
}
}(pubkey)
}
wg.Wait()
filterForRelay := make(map[string]nostr.Filter, n) // { [relay]: filter }
for pubkey, relays := range relaysForPubkey {
for _, relay := range relays {
flt, ok := filterForRelay[relay]
if !ok {
flt = filter.Clone()
filterForRelay[relay] = flt
}
flt.Authors = append(flt.Authors, pubkey)
}
}
return filterForRelay, nil
}