@@ -5,12 +5,10 @@ import (
5
5
"fmt"
6
6
"iter"
7
7
"slices"
8
- "sync"
9
8
"time"
10
9
11
10
"github.com/fiatjaf/eventstore/lmdb"
12
11
"github.com/nbd-wtf/go-nostr"
13
- "github.com/nbd-wtf/go-nostr/nip19"
14
12
"github.com/nbd-wtf/go-nostr/sdk"
15
13
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
16
14
)
@@ -58,128 +56,24 @@ func initSystem() func() {
58
56
return db .Close
59
57
}
60
58
61
- func getEvent (ctx context.Context , code string ) (* nostr.Event , []string , error ) {
62
- // this is for deciding what relays will go on nevent and nprofile later
63
- priorityRelays := make (map [string ]int )
64
-
65
- prefix , data , err := nip19 .Decode (code )
59
+ func getEvent (ctx context.Context , code string , withRelays bool ) (* nostr.Event , []string , error ) {
60
+ evt , relays , err := sys .FetchSpecificEvent (ctx , code , true )
66
61
if err != nil {
67
- return nil , nil , fmt .Errorf ("failed to decode %w" , err )
62
+ return nil , nil , fmt .Errorf ("couldn't find this event, did you include accurate relay or author hints in it?" )
68
63
}
69
64
70
- author := ""
71
- authorRelaysPosition := 0
72
-
73
- var filter nostr.Filter
74
- relays := make ([]string , 0 , 10 )
75
-
76
- switch v := data .(type ) {
77
- case nostr.EventPointer :
78
- author = v .Author
79
- filter .IDs = []string {v .ID }
80
- relays = append (relays , v .Relays ... )
81
- relays = append (relays , relayConfig .JustIds ... )
82
- authorRelaysPosition = len (v .Relays ) // ensure author relays are checked after hinted relays
83
- for _ , r := range v .Relays {
84
- priorityRelays [r ] = 2
85
- }
86
- case nostr.EntityPointer :
87
- author = v .PublicKey
88
- filter .Authors = []string {v .PublicKey }
89
- filter .Tags = nostr.TagMap {
90
- "d" : []string {v .Identifier },
91
- }
92
- if v .Kind != 0 {
93
- filter .Kinds = append (filter .Kinds , v .Kind )
94
- }
95
- relays = append (relays , v .Relays ... )
96
- authorRelaysPosition = len (v .Relays ) // ensure author relays are checked after hinted relays
97
- case string :
98
- if prefix == "note" {
99
- filter .IDs = []string {v }
100
- relays = append (relays , relayConfig .JustIds ... )
101
- }
65
+ if ! withRelays {
66
+ return evt , nil , nil
102
67
}
103
68
104
- // try to fetch in our internal eventstore first
105
- if res , _ := sys .StoreRelay .QuerySync (ctx , filter ); len (res ) != 0 {
106
- evt := res [0 ]
69
+ if relays == nil {
107
70
return evt , internal .getRelaysForEvent (evt .ID ), nil
108
71
}
109
72
110
- if author != "" {
111
- // fetch relays for author
112
- authorRelays := sys .FetchOutboxRelays (ctx , author , 3 )
113
- relays = slices .Insert (relays , authorRelaysPosition , authorRelays ... )
114
- for _ , r := range authorRelays {
115
- priorityRelays [r ] = 1
116
- }
117
- }
118
-
119
- for len (relays ) < 5 {
120
- relays = append (relays , getRandomRelay ())
121
- }
122
-
123
- relays = unique (relays )
124
-
125
- var result * nostr.Event
126
- var successRelays []string = nil
127
-
128
- {
129
- // actually fetch the event here
130
- subManyCtx , cancel := context .WithTimeout (ctx , time .Second * 8 )
131
- defer cancel ()
132
-
133
- // keep track of where we have actually found the event so we can show that
134
- successRelays = make ([]string , 0 , len (relays ))
135
- countdown := 7.5
136
- go func () {
137
- for {
138
- time .Sleep (500 * time .Millisecond )
139
- if countdown <= 0 {
140
- cancel ()
141
- break
142
- }
143
- countdown -= 0.5
144
- }
145
- }()
146
-
147
- fetchProfileOnce := sync.Once {}
148
-
149
- for ie := range sys .Pool .SubManyEoseNonUnique (
150
- subManyCtx ,
151
- relays ,
152
- nostr.Filters {filter },
153
- nostr .WithLabel ("fetching-" + prefix ),
154
- ) {
155
- fetchProfileOnce .Do (func () {
156
- go sys .FetchProfileMetadata (ctx , ie .PubKey )
157
- })
158
-
159
- successRelays = append (successRelays , ie .Relay .URL )
160
- if result == nil || ie .CreatedAt > result .CreatedAt {
161
- result = ie .Event
162
- }
163
- countdown = min (countdown , 1 )
164
- }
165
- }
166
-
167
- if result == nil {
168
- return nil , nil , fmt .Errorf ("couldn't find this %s, did you include relay or author hints in it?" , prefix )
169
- }
170
-
171
- // save stuff in cache and in internal store
172
- sys .StoreRelay .Publish (ctx , * result )
173
73
// save relays if we got them
174
- allRelays := internal .attachRelaysToEvent (result .ID , successRelays ... )
175
- // put priority relays first so they get used in nevent and nprofile
176
- slices .SortFunc (allRelays , func (a , b string ) int {
177
- vpa , _ := priorityRelays [a ]
178
- vpb , _ := priorityRelays [b ]
179
- return vpb - vpa
180
- })
181
-
182
- return result , allRelays , nil
74
+ allRelays := internal .attachRelaysToEvent (evt .ID , relays ... )
75
+
76
+ return evt , allRelays , nil
183
77
}
184
78
185
79
func authorLastNotes (ctx context.Context , pubkey string ) []EnhancedEvent {
@@ -210,7 +104,7 @@ func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent {
210
104
relays := sys .FetchOutboxRelays (ctx , pubkey , 3 )
211
105
212
106
for len (relays ) < 3 {
213
- relays = unique (append (relays , getRandomRelay ()))
107
+ relays = unique (append (relays , sys . FallbackRelays . Next ()))
214
108
}
215
109
216
110
ch := sys .Pool .SubManyEose (ctx , relays , nostr.Filters {filter }, nostr .WithLabel ("authorlast" ))
0 commit comments