Skip to content

Commit

Permalink
Merge pull request #32 from bitvora/dev-profileRefresh
Browse files Browse the repository at this point in the history
customize the trust network by follower count
  • Loading branch information
barrydeen authored Sep 12, 2024
2 parents 9124cc3 + 49bd213 commit 37a9e1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
7 changes: 4 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ RELAY_URL="wss://wot.utxo.one"
DB_PATH="db"

# where we should store the index.html and static files
INDEX_PATH="templates/index.html"
STATIC_PATH="templates/static"
INDEX_PATH="/mnt/dev/bitvora/wot-relay/templates/index.html"
STATIC_PATH="/mnt/dev/bitvora/wot-relay/templates/static/"

# relay behavior

# how often to refresh the relay's view of the WoT in HOURS
REFRESH_INTERVAL_HOURS=1
REFRESH_INTERVAL_HOURS=1
MINIMUM_FOLLOWERS=5
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ DB_PATH="/home/ubuntu/wot-relay/db" # any path you would like the database to be
INDEX_PATH="/home/ubuntu/wot-relay/templates/index.html" # path to the index.html file
STATIC_PATH="/home/ubuntu/wot-relay/templates/static" # path to the static folder
REFRESH_INTERVAL_HOURS=24 # interval in hours to refresh the web of trust
MINIMUM_FOLLOWERS=3 #how many followers before they're allowed in the WoT
```

### 4. Build the project
Expand Down
33 changes: 22 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
IndexPath string
StaticPath string
RefreshInterval int
MinimumFollowers int
}

var pool *nostr.SimplePool
Expand All @@ -36,6 +37,7 @@ var seedRelays []string
var booted bool
var oneHopNetwork []string
var trustNetworkMap map[string]bool
var pubkeyFollowerCount = make(map[string]int)

func main() {
nostr.InfoLogger = log.New(io.Discard, "", 0)
Expand Down Expand Up @@ -162,6 +164,12 @@ func LoadConfig() Config {
refreshInterval, _ := strconv.Atoi(os.Getenv("REFRESH_INTERVAL_HOURS"))
log.Println("πŸ”„ refresh interval set to", refreshInterval, "hours")

if os.Getenv("MINIMUM_FOLLOWERS") == "" {
os.Setenv("MINIMUM_FOLLOWERS", "1")
}

minimumFollowers, _ := strconv.Atoi(os.Getenv("MINIMUM_FOLLOWERS"))

config := Config{
RelayName: getEnv("RELAY_NAME"),
RelayPubkey: getEnv("RELAY_PUBKEY"),
Expand All @@ -171,6 +179,7 @@ func LoadConfig() Config {
IndexPath: getEnv("INDEX_PATH"),
StaticPath: getEnv("STATIC_PATH"),
RefreshInterval: refreshInterval,
MinimumFollowers: minimumFollowers,
}

return config
Expand All @@ -187,11 +196,15 @@ func getEnv(key string) string {
func updateTrustNetworkFilter() {
trustNetworkMap = make(map[string]bool)

nKeys := uint64(len(trustNetwork))
log.Println("🌐 updating trust network map with", nKeys, "keys")
for _, trustedPubkey := range trustNetwork {
trustNetworkMap[trustedPubkey] = true
log.Println("🌐 updating trust network map")
for pubkey, count := range pubkeyFollowerCount {
if count >= config.MinimumFollowers {
trustNetworkMap[pubkey] = true
appendPubkey(pubkey)
}
}

log.Println("🌐 trust network map updated with", len(trustNetwork), "keys")
}

func refreshProfiles(ctx context.Context, relay *khatru.Relay) {
Expand Down Expand Up @@ -230,6 +243,7 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
log.Println("πŸ” fetching owner's follows")
for ev := range pool.SubManyEose(timeoutCtx, seedRelays, filters) {
for _, contact := range ev.Event.Tags.GetAll([]string{"p"}) {
pubkeyFollowerCount[contact[1]]++ // Increment follower count for the pubkey
appendOneHopNetwork(contact[1])
}
}
Expand All @@ -251,9 +265,7 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {

for ev := range pool.SubManyEose(timeout, seedRelays, filters) {
for _, contact := range ev.Event.Tags.GetAll([]string{"p"}) {
if len(contact) > 1 {
appendPubkey(contact[1])
}
pubkeyFollowerCount[contact[1]]++ // Increment follower count for the pubkey
}

for _, relay := range ev.Event.Tags.GetAll([]string{"r"}) {
Expand All @@ -264,9 +276,8 @@ func refreshTrustNetwork(relay *khatru.Relay, ctx context.Context) {
relay.AddEvent(ctx, ev.Event)
}
}

}
log.Println("πŸ«‚ network size:", len(trustNetwork))
log.Println("πŸ«‚ total network size:", len(pubkeyFollowerCount))
log.Println("πŸ”— relays discovered:", len(relays))
}

Expand Down Expand Up @@ -355,7 +366,7 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {

case ev, ok := <-eventChan:
if !ok {
log.Println("πŸ“¦ SubMany channel closed")
log.Println("πŸ“¦ subscription channel closed")
log.Println("πŸ“¦ archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
return
}
Expand All @@ -367,7 +378,7 @@ func archiveTrustedNotes(relay *khatru.Relay, ctx context.Context) {

relay.AddEvent(ctx, ev.Event)
trustedNotes++
log.Println("πŸ“¦ archived note: ", ev.Event.ID)
//log.Println("πŸ“¦ archived note: ", ev.Event.ID)
} else {
untrustedNotes++
}
Expand Down

0 comments on commit 37a9e1c

Please sign in to comment.