nip60: prevent duplicate token entries after doing some operation.

This commit is contained in:
fiatjaf
2025-01-29 21:23:15 -03:00
parent 37db1c924a
commit 78a9411d8a

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"iter" "iter"
"slices"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -152,12 +153,20 @@ func loadStash(
// get all pending stuff and assign them to this, then delete the pending stuff // get all pending stuff and assign them to this, then delete the pending stuff
for _, he := range wl.pendingHistory[wallet.Identifier] { for _, he := range wl.pendingHistory[wallet.Identifier] {
wallet.History = append(wallet.History, he) if !slices.ContainsFunc(wallet.History, func(c HistoryEntry) bool {
return c.event.ID == he.event.ID
}) {
wallet.History = append(wallet.History, he)
}
} }
delete(wl.pendingHistory, wallet.Identifier) delete(wl.pendingHistory, wallet.Identifier)
wallet.tokensMu.Lock() wallet.tokensMu.Lock()
for _, token := range wl.pendingTokens[wallet.Identifier] { for _, token := range wl.pendingTokens[wallet.Identifier] {
wallet.Tokens = append(wallet.Tokens, token) if !slices.ContainsFunc(wallet.Tokens, func(c Token) bool {
return c.event.ID == token.event.ID
}) {
wallet.Tokens = append(wallet.Tokens, token)
}
} }
delete(wl.pendingTokens, wallet.Identifier) delete(wl.pendingTokens, wallet.Identifier)
wallet.tokensMu.Unlock() wallet.tokensMu.Unlock()
@ -188,7 +197,11 @@ func loadStash(
if wallet, ok := wl.wallets[spl[2]]; ok { if wallet, ok := wl.wallets[spl[2]]; ok {
wallet.tokensMu.Lock() wallet.tokensMu.Lock()
wallet.Tokens = append(wallet.Tokens, token) if !slices.ContainsFunc(wallet.Tokens, func(c Token) bool {
return c.event.ID == token.event.ID
}) {
wallet.Tokens = append(wallet.Tokens, token)
}
wallet.tokensMu.Unlock() wallet.tokensMu.Unlock()
} else { } else {
wl.pendingTokens[spl[2]] = append(wl.pendingTokens[spl[2]], token) wl.pendingTokens[spl[2]] = append(wl.pendingTokens[spl[2]], token)
@ -227,7 +240,11 @@ func loadStash(
} }
if wallet, ok := wl.wallets[spl[2]]; ok { if wallet, ok := wl.wallets[spl[2]]; ok {
wallet.History = append(wallet.History, he) if !slices.ContainsFunc(wallet.History, func(c HistoryEntry) bool {
return c.event.ID == he.event.ID
}) {
wallet.History = append(wallet.History, he)
}
} else { } else {
wl.pendingHistory[spl[2]] = append(wl.pendingHistory[spl[2]], he) wl.pendingHistory[spl[2]] = append(wl.pendingHistory[spl[2]], he)
} }