mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-09-07 10:49:38 +00:00
This commit refactors the Store interface in State into smaller interfaces in package store. These interfaces are combined into one structure called a "Cabinet". The default implementation of those interfaces have been rewritten in package defaultstore, while the old no-op implementation stays with the store package. This commit also omitted several state handlers for user events, as it is unclear what they are actually structured like.
107 lines
2 KiB
Go
107 lines
2 KiB
Go
package defaultstore
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
|
"github.com/diamondburned/arikawa/v2/gateway"
|
|
"github.com/diamondburned/arikawa/v2/internal/moreatomic"
|
|
"github.com/diamondburned/arikawa/v2/state/store"
|
|
)
|
|
|
|
type Presence struct {
|
|
guilds moreatomic.Map
|
|
}
|
|
|
|
type presences struct {
|
|
mut sync.Mutex
|
|
presences map[discord.UserID]gateway.Presence
|
|
}
|
|
|
|
var _ store.PresenceStore = (*Presence)(nil)
|
|
|
|
func NewPresence() *Presence {
|
|
return &Presence{
|
|
guilds: *moreatomic.NewMap(func() interface{} {
|
|
return &presences{
|
|
presences: make(map[discord.UserID]gateway.Presence, 1),
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
|
|
func (s *Presence) Reset() error {
|
|
return s.guilds.Reset()
|
|
}
|
|
|
|
func (s *Presence) Presence(gID discord.GuildID, uID discord.UserID) (*gateway.Presence, error) {
|
|
iv, ok := s.guilds.Load(gID)
|
|
if !ok {
|
|
return nil, store.ErrNotFound
|
|
}
|
|
|
|
ps := iv.(*presences)
|
|
|
|
ps.mut.Lock()
|
|
defer ps.mut.Unlock()
|
|
|
|
p, ok := ps.presences[uID]
|
|
if ok {
|
|
return &p, nil
|
|
}
|
|
|
|
return nil, store.ErrNotFound
|
|
}
|
|
|
|
func (s *Presence) Presences(guildID discord.GuildID) ([]gateway.Presence, error) {
|
|
iv, ok := s.guilds.Load(guildID)
|
|
if !ok {
|
|
return nil, store.ErrNotFound
|
|
}
|
|
|
|
ps := iv.(*presences)
|
|
|
|
ps.mut.Lock()
|
|
defer ps.mut.Unlock()
|
|
|
|
var presences = make([]gateway.Presence, 0, len(ps.presences))
|
|
for _, p := range ps.presences {
|
|
presences = append(presences, p)
|
|
}
|
|
|
|
return presences, nil
|
|
}
|
|
|
|
func (s *Presence) PresenceSet(guildID discord.GuildID, presence gateway.Presence) error {
|
|
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 {
|
|
ps.presences = make(map[discord.UserID]gateway.Presence, 1)
|
|
}
|
|
|
|
ps.presences[presence.User.ID] = presence
|
|
|
|
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
|
|
}
|