Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish directly to db #39

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"time"

"github.com/fiatjaf/eventstore"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/policies"
"github.com/joho/godotenv"
Expand All @@ -30,6 +31,7 @@ type Config struct {
}

var pool *nostr.SimplePool
var wdb nostr.RelayStore
var relays []string
var config Config
var trustNetwork []string
Expand Down Expand Up @@ -77,6 +79,7 @@ func main() {
if err := db.Init(); err != nil {
panic(err)
}
wdb = eventstore.RelayWrapper{Store: &db}

relay.RejectEvent = append(relay.RejectEvent,
policies.RejectEventsWithBase64Media,
Expand Down Expand Up @@ -121,7 +124,7 @@ func main() {
"wss://relay.siamstr.com",
}

go refreshTrustNetwork(relay, ctx)
go refreshTrustNetwork(ctx, relay)

mux := relay.Router()
static := http.FileServer(http.Dir(config.StaticPath))
Expand Down Expand Up @@ -208,7 +211,7 @@ func updateTrustNetworkFilter() {
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 {
timeout, cancel := context.WithTimeout(ctx, 4*time.Second)
defer cancel()
Expand All @@ -224,13 +227,13 @@ func refreshProfiles(ctx context.Context, relay *khatru.Relay) {
}}

for ev := range pool.SubManyEose(timeout, seedRelays, filters) {
relay.AddEvent(ctx, ev.Event)
wdb.Publish(ctx, *ev.Event)
}
}
log.Println("👤 profiles refreshed: ", len(trustNetwork))
}

func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
func refreshTrustNetwork(ctx context.Context, relay *khatru.Relay) {

runTrustNetworkRefresh := func() {
timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
Expand Down Expand Up @@ -276,7 +279,7 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
}

if ev.Event.Kind == nostr.KindProfileMetadata {
relay.AddEvent(ctx, ev.Event)
wdb.Publish(ctx, *ev.Event)
}
}
}
Expand All @@ -287,7 +290,7 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
for {
runTrustNetworkRefresh()
updateTrustNetworkFilter()
archiveTrustedNotes(relay, ctx)
archiveTrustedNotes(ctx, relay)
}
}

Expand Down Expand Up @@ -329,9 +332,9 @@ func appendOneHopNetwork(pubkey string) {
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)
go refreshProfiles(ctx, relay)
go refreshProfiles(ctx)

filters := []nostr.Filter{{
Kinds: []int{
Expand Down Expand Up @@ -389,7 +392,7 @@ func processEvent(ctx context.Context, ev *nostr.Event, relay *khatru.Relay) {
return
}

relay.AddEvent(ctx, ev)
wdb.Publish(ctx, *ev)
relay.BroadcastEvent(ev)
trustedNotes++
log.Println("📦 archived note: ", ev.ID)
Expand Down
Loading