go-nostr/sdk/lists_profile.go

72 lines
1.6 KiB
Go
Raw Normal View History

package sdk
import (
"context"
"net/url"
"strings"
"github.com/nbd-wtf/go-nostr"
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
)
2025-01-01 18:16:36 -03:00
type ProfileRef struct {
Pubkey string
Relay string
Petname string
}
2025-01-01 18:16:36 -03:00
func (f ProfileRef) Value() string { return f.Pubkey }
2025-01-01 18:16:36 -03:00
func (sys *System) FetchFollowList(ctx context.Context, pubkey string) GenericList[ProfileRef] {
if sys.FollowListCache == nil {
sys.FollowListCache = cache_memory.New32[GenericList[ProfileRef]](1000)
}
fl, _ := fetchGenericList(sys, ctx, pubkey, 3, kind_3, parseProfileRef, sys.FollowListCache)
return fl
}
2025-01-01 18:16:36 -03:00
func (sys *System) FetchMuteList(ctx context.Context, pubkey string) GenericList[ProfileRef] {
if sys.MuteListCache == nil {
sys.MuteListCache = cache_memory.New32[GenericList[ProfileRef]](1000)
}
ml, _ := fetchGenericList(sys, ctx, pubkey, 10000, kind_10000, parseProfileRef, sys.MuteListCache)
2025-01-01 18:16:36 -03:00
return ml
}
2025-01-02 12:12:49 -03:00
func (sys *System) FetchFollowSets(ctx context.Context, pubkey string) GenericSets[ProfileRef] {
if sys.FollowSetsCache == nil {
sys.FollowSetsCache = cache_memory.New32[GenericSets[ProfileRef]](1000)
}
ml, _ := fetchGenericSets(sys, ctx, pubkey, 30000, kind_30000, parseProfileRef, sys.FollowSetsCache)
2025-01-02 12:12:49 -03:00
return ml
}
2025-01-01 18:16:36 -03:00
func parseProfileRef(tag nostr.Tag) (fw ProfileRef, ok bool) {
if len(tag) < 2 {
return fw, false
}
if tag[0] != "p" {
return fw, false
}
fw.Pubkey = tag[1]
if !nostr.IsValidPublicKey(fw.Pubkey) {
return fw, false
}
if len(tag) > 2 {
if _, err := url.Parse(tag[2]); err == nil {
fw.Relay = nostr.NormalizeURL(tag[2])
}
if len(tag) > 3 {
fw.Petname = strings.TrimSpace(tag[3])
}
}
return fw, true
}