2020-11-30 00:57:58 +00:00
|
|
|
package defaultstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/internal/moreatomic"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state/store"
|
2020-11-30 00:57:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Member struct {
|
|
|
|
guilds moreatomic.Map // discord.GuildID -> *guildMembers
|
|
|
|
}
|
|
|
|
|
|
|
|
type guildMembers struct {
|
2021-11-03 22:16:02 +00:00
|
|
|
mut sync.RWMutex
|
2020-11-30 00:57:58 +00:00
|
|
|
members map[discord.UserID]discord.Member
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ store.MemberStore = (*Member)(nil)
|
|
|
|
|
|
|
|
func NewMember() *Member {
|
|
|
|
return &Member{
|
|
|
|
guilds: *moreatomic.NewMap(func() interface{} {
|
|
|
|
return &guildMembers{
|
|
|
|
members: make(map[discord.UserID]discord.Member, 1),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Member) Reset() error {
|
|
|
|
return s.guilds.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Member) Member(guildID discord.GuildID, userID discord.UserID) (*discord.Member, error) {
|
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
gm := iv.(*guildMembers)
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
gm.mut.RLock()
|
|
|
|
defer gm.mut.RUnlock()
|
2020-11-30 00:57:58 +00:00
|
|
|
|
|
|
|
m, ok := gm.members[userID]
|
|
|
|
if ok {
|
|
|
|
return &m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Member) Members(guildID discord.GuildID) ([]discord.Member, error) {
|
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
gm := iv.(*guildMembers)
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
gm.mut.RLock()
|
|
|
|
defer gm.mut.RUnlock()
|
2020-11-30 00:57:58 +00:00
|
|
|
|
|
|
|
var members = make([]discord.Member, 0, len(gm.members))
|
|
|
|
for _, m := range gm.members {
|
|
|
|
members = append(members, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
func (s *Member) MemberSet(guildID discord.GuildID, m *discord.Member, update bool) error {
|
2020-11-30 00:57:58 +00:00
|
|
|
iv, _ := s.guilds.LoadOrStore(guildID)
|
|
|
|
gm := iv.(*guildMembers)
|
|
|
|
|
|
|
|
gm.mut.Lock()
|
2021-06-03 19:39:49 +00:00
|
|
|
if _, ok := gm.members[m.User.ID]; !ok || update {
|
2021-11-03 22:16:02 +00:00
|
|
|
gm.members[m.User.ID] = *m
|
2021-06-03 19:39:49 +00:00
|
|
|
}
|
2020-11-30 00:57:58 +00:00
|
|
|
gm.mut.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Member) MemberRemove(guildID discord.GuildID, userID discord.UserID) error {
|
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gm := iv.(*guildMembers)
|
|
|
|
|
|
|
|
gm.mut.Lock()
|
|
|
|
delete(gm.members, userID)
|
|
|
|
gm.mut.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|