mirror of
https://github.com/bitvora/wot-relay.git
synced 2025-06-06 18:31:05 +00:00
Merge pull request #39 from bitvora/dev-profileRefresh
Publish directly to db
This commit is contained in:
commit
6cef35e0f1
21
main.go
21
main.go
@ -11,6 +11,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fiatjaf/eventstore"
|
||||||
"github.com/fiatjaf/khatru"
|
"github.com/fiatjaf/khatru"
|
||||||
"github.com/fiatjaf/khatru/policies"
|
"github.com/fiatjaf/khatru/policies"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
@ -30,6 +31,7 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var pool *nostr.SimplePool
|
var pool *nostr.SimplePool
|
||||||
|
var wdb nostr.RelayStore
|
||||||
var relays []string
|
var relays []string
|
||||||
var config Config
|
var config Config
|
||||||
var trustNetwork []string
|
var trustNetwork []string
|
||||||
@ -77,6 +79,7 @@ func main() {
|
|||||||
if err := db.Init(); err != nil {
|
if err := db.Init(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
wdb = eventstore.RelayWrapper{Store: &db}
|
||||||
|
|
||||||
relay.RejectEvent = append(relay.RejectEvent,
|
relay.RejectEvent = append(relay.RejectEvent,
|
||||||
policies.RejectEventsWithBase64Media,
|
policies.RejectEventsWithBase64Media,
|
||||||
@ -121,7 +124,7 @@ func main() {
|
|||||||
"wss://relay.siamstr.com",
|
"wss://relay.siamstr.com",
|
||||||
}
|
}
|
||||||
|
|
||||||
go refreshTrustNetwork(relay, ctx)
|
go refreshTrustNetwork(ctx, relay)
|
||||||
|
|
||||||
mux := relay.Router()
|
mux := relay.Router()
|
||||||
static := http.FileServer(http.Dir(config.StaticPath))
|
static := http.FileServer(http.Dir(config.StaticPath))
|
||||||
@ -208,7 +211,7 @@ func updateTrustNetworkFilter() {
|
|||||||
log.Println("🌐 trust network map updated with", len(trustNetwork), "keys")
|
log.Println("🌐 trust network map updated with", len(trustNetwork), "keys")
|
||||||
}
|
}
|
||||||
|
|
||||||
func refreshProfiles(ctx context.Context, relay *khatru.Relay) {
|
func refreshProfiles(ctx context.Context) {
|
||||||
for i := 0; i < len(trustNetwork); i += 200 {
|
for i := 0; i < len(trustNetwork); i += 200 {
|
||||||
timeout, cancel := context.WithTimeout(ctx, 4*time.Second)
|
timeout, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@ -224,13 +227,13 @@ func refreshProfiles(ctx context.Context, relay *khatru.Relay) {
|
|||||||
}}
|
}}
|
||||||
|
|
||||||
for ev := range pool.SubManyEose(timeout, seedRelays, filters) {
|
for ev := range pool.SubManyEose(timeout, seedRelays, filters) {
|
||||||
relay.AddEvent(ctx, ev.Event)
|
wdb.Publish(ctx, *ev.Event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("👤 profiles refreshed: ", len(trustNetwork))
|
log.Println("👤 profiles refreshed: ", len(trustNetwork))
|
||||||
}
|
}
|
||||||
|
|
||||||
func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
|
func refreshTrustNetwork(ctx context.Context, relay *khatru.Relay) {
|
||||||
|
|
||||||
runTrustNetworkRefresh := func() {
|
runTrustNetworkRefresh := func() {
|
||||||
timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||||
@ -276,7 +279,7 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ev.Event.Kind == nostr.KindProfileMetadata {
|
if ev.Event.Kind == nostr.KindProfileMetadata {
|
||||||
relay.AddEvent(ctx, ev.Event)
|
wdb.Publish(ctx, *ev.Event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -287,7 +290,7 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
|
|||||||
for {
|
for {
|
||||||
runTrustNetworkRefresh()
|
runTrustNetworkRefresh()
|
||||||
updateTrustNetworkFilter()
|
updateTrustNetworkFilter()
|
||||||
archiveTrustedNotes(relay, ctx)
|
archiveTrustedNotes(ctx, relay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,9 +332,9 @@ func appendOneHopNetwork(pubkey string) {
|
|||||||
oneHopNetwork = append(oneHopNetwork, pubkey)
|
oneHopNetwork = append(oneHopNetwork, pubkey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {
|
func archiveTrustedNotes(ctx context.Context, relay *khatru.Relay) {
|
||||||
timeout := time.After(time.Duration(config.RefreshInterval) * time.Hour)
|
timeout := time.After(time.Duration(config.RefreshInterval) * time.Hour)
|
||||||
go refreshProfiles(ctx, relay)
|
go refreshProfiles(ctx)
|
||||||
|
|
||||||
filters := []nostr.Filter{{
|
filters := []nostr.Filter{{
|
||||||
Kinds: []int{
|
Kinds: []int{
|
||||||
@ -389,7 +392,7 @@ func processEvent(ctx context.Context, ev *nostr.Event, relay *khatru.Relay) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
relay.AddEvent(ctx, ev)
|
wdb.Publish(ctx, *ev)
|
||||||
relay.BroadcastEvent(ev)
|
relay.BroadcastEvent(ev)
|
||||||
trustedNotes++
|
trustedNotes++
|
||||||
log.Println("📦 archived note: ", ev.ID)
|
log.Println("📦 archived note: ", ev.ID)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user