2024-09-10 22:37:48 -03:00
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2025-02-11 14:14:48 -03:00
|
|
|
cache_memory "github.com/nbd-wtf/go-nostr/sdk/cache/memory"
|
2024-09-10 22:37:48 -03:00
|
|
|
)
|
|
|
|
|
2025-01-01 18:16:36 -03:00
|
|
|
type ProfileRef struct {
|
2024-09-10 22:37:48 -03:00
|
|
|
Pubkey string
|
|
|
|
Relay string
|
|
|
|
Petname string
|
|
|
|
}
|
|
|
|
|
2025-01-01 18:16:36 -03:00
|
|
|
func (f ProfileRef) Value() string { return f.Pubkey }
|
2024-09-10 22:37:48 -03:00
|
|
|
|
2025-01-01 18:16:36 -03:00
|
|
|
func (sys *System) FetchFollowList(ctx context.Context, pubkey string) GenericList[ProfileRef] {
|
2025-02-11 14:14:48 -03:00
|
|
|
if sys.FollowListCache == nil {
|
|
|
|
sys.FollowListCache = cache_memory.New32[GenericList[ProfileRef]](1000)
|
|
|
|
}
|
|
|
|
|
2025-01-14 20:55:37 -03:00
|
|
|
fl, _ := fetchGenericList(sys, ctx, pubkey, 3, kind_3, parseProfileRef, sys.FollowListCache)
|
2024-09-10 22:37:48 -03:00
|
|
|
return fl
|
|
|
|
}
|
|
|
|
|
2025-01-01 18:16:36 -03:00
|
|
|
func (sys *System) FetchMuteList(ctx context.Context, pubkey string) GenericList[ProfileRef] {
|
2025-02-11 14:14:48 -03:00
|
|
|
if sys.MuteListCache == nil {
|
|
|
|
sys.MuteListCache = cache_memory.New32[GenericList[ProfileRef]](1000)
|
|
|
|
}
|
|
|
|
|
2025-01-14 20:55:37 -03:00
|
|
|
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] {
|
2025-02-11 14:14:48 -03:00
|
|
|
if sys.FollowSetsCache == nil {
|
|
|
|
sys.FollowSetsCache = cache_memory.New32[GenericSets[ProfileRef]](1000)
|
|
|
|
}
|
|
|
|
|
2025-01-14 20:55:37 -03:00
|
|
|
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) {
|
2024-09-10 22:37:48 -03:00
|
|
|
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])
|
|
|
|
}
|
2025-01-17 13:44:50 -03:00
|
|
|
|
2024-09-10 22:37:48 -03:00
|
|
|
if len(tag) > 3 {
|
|
|
|
fw.Petname = strings.TrimSpace(tag[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-17 13:44:50 -03:00
|
|
|
return fw, true
|
2024-09-10 22:37:48 -03:00
|
|
|
}
|