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 Presence struct {
|
|
|
|
guilds moreatomic.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
type presences struct {
|
2021-11-03 22:16:02 +00:00
|
|
|
mut sync.RWMutex
|
2021-08-08 20:19:15 +00:00
|
|
|
presences map[discord.UserID]discord.Presence
|
2020-11-30 00:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ store.PresenceStore = (*Presence)(nil)
|
|
|
|
|
|
|
|
func NewPresence() *Presence {
|
|
|
|
return &Presence{
|
|
|
|
guilds: *moreatomic.NewMap(func() interface{} {
|
|
|
|
return &presences{
|
2021-08-08 20:19:15 +00:00
|
|
|
presences: make(map[discord.UserID]discord.Presence, 1),
|
2020-11-30 00:57:58 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Presence) Reset() error {
|
|
|
|
return s.guilds.Reset()
|
|
|
|
}
|
|
|
|
|
2021-08-08 20:19:15 +00:00
|
|
|
func (s *Presence) Presence(gID discord.GuildID, uID discord.UserID) (*discord.Presence, error) {
|
2020-11-30 00:57:58 +00:00
|
|
|
iv, ok := s.guilds.Load(gID)
|
|
|
|
if !ok {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := iv.(*presences)
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
ps.mut.RLock()
|
|
|
|
defer ps.mut.RUnlock()
|
2020-11-30 00:57:58 +00:00
|
|
|
|
|
|
|
p, ok := ps.presences[uID]
|
|
|
|
if ok {
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-08-08 20:19:15 +00:00
|
|
|
func (s *Presence) Presences(guildID discord.GuildID) ([]discord.Presence, error) {
|
2020-11-30 00:57:58 +00:00
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := iv.(*presences)
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
ps.mut.RLock()
|
|
|
|
defer ps.mut.RUnlock()
|
2020-11-30 00:57:58 +00:00
|
|
|
|
2021-08-08 20:19:15 +00:00
|
|
|
var presences = make([]discord.Presence, 0, len(ps.presences))
|
2020-11-30 00:57:58 +00:00
|
|
|
for _, p := range ps.presences {
|
|
|
|
presences = append(presences, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return presences, nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:16:02 +00:00
|
|
|
func (s *Presence) PresenceSet(guildID discord.GuildID, p *discord.Presence, update bool) error {
|
2020-11-30 00:57:58 +00:00
|
|
|
iv, _ := s.guilds.LoadOrStore(guildID)
|
|
|
|
|
|
|
|
ps := iv.(*presences)
|
|
|
|
|
|
|
|
ps.mut.Lock()
|
|
|
|
defer ps.mut.Unlock()
|
|
|
|
|
|
|
|
// Shitty if check is better than a realloc every time.
|
|
|
|
if ps.presences == nil {
|
2021-08-08 20:19:15 +00:00
|
|
|
ps.presences = make(map[discord.UserID]discord.Presence, 1)
|
2020-11-30 00:57:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 19:39:49 +00:00
|
|
|
if _, ok := ps.presences[p.User.ID]; !ok || update {
|
2021-11-03 22:16:02 +00:00
|
|
|
ps.presences[p.User.ID] = *p
|
2021-06-03 19:39:49 +00:00
|
|
|
}
|
2020-11-30 00:57:58 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Presence) PresenceRemove(guildID discord.GuildID, userID discord.UserID) error {
|
|
|
|
iv, ok := s.guilds.Load(guildID)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := iv.(*presences)
|
|
|
|
|
|
|
|
ps.mut.Lock()
|
|
|
|
delete(ps.presences, userID)
|
|
|
|
ps.mut.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|