1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-08 04:57:31 +00:00

State: Fixed default store messages not working properly

This commit is contained in:
diamondburned (Forefront) 2020-02-21 22:03:44 -08:00
parent 587d36fd6c
commit 1201652aa1
2 changed files with 11 additions and 18 deletions

View file

@ -40,7 +40,7 @@ type State struct {
// List of channels with few messages, so it doesn't bother hitting the API // List of channels with few messages, so it doesn't bother hitting the API
// again. // again.
fewMessages []discord.Snowflake fewMessages map[discord.Snowflake]struct{}
fewMutex sync.Mutex fewMutex sync.Mutex
} }
@ -49,6 +49,7 @@ func NewFromSession(s *session.Session, store Store) (*State, error) {
Session: s, Session: s,
Store: store, Store: store,
StateLog: func(err error) {}, StateLog: func(err error) {},
fewMessages: map[discord.Snowflake]struct{}{},
} }
return state, state.hookSession() return state, state.hookSession()
@ -378,19 +379,16 @@ func (s *State) Messages(channelID discord.Snowflake) ([]discord.Message, error)
// Is the channel tiny? // Is the channel tiny?
s.fewMutex.Lock() s.fewMutex.Lock()
for _, ch := range s.fewMessages { if _, ok := s.fewMessages[channelID]; ok {
if ch == channelID {
// Yes, skip the state.
s.fewMutex.Unlock() s.fewMutex.Unlock()
return ms, nil return ms, nil
} }
}
// No, fetch from the state. // No, fetch from the state.
s.fewMutex.Unlock() s.fewMutex.Unlock()
} }
ms, err = s.Session.Messages(channelID, 100) ms, err = s.Session.Messages(channelID, uint(maxMsgs))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -418,7 +416,7 @@ func (s *State) Messages(channelID discord.Snowflake) ([]discord.Message, error)
if len(ms) < maxMsgs { if len(ms) < maxMsgs {
// Tiny channel, store this. // Tiny channel, store this.
s.fewMutex.Lock() s.fewMutex.Lock()
s.fewMessages = append(s.fewMessages, channelID) s.fewMessages[channelID] = struct{}{}
s.fewMutex.Unlock() s.fewMutex.Unlock()
return ms, nil return ms, nil

View file

@ -485,12 +485,7 @@ func (s *DefaultStore) MessageSet(message *discord.Message) error {
// Prepend the latest message at the end // Prepend the latest message at the end
if len(ms) > 0 { if end := s.MaxMessages(); len(ms) >= end {
var end = s.MaxMessages()
if len(ms) < end {
end = len(ms)
}
// Copy hack to prepend. This copies the 0th-(end-1)th entries to // Copy hack to prepend. This copies the 0th-(end-1)th entries to
// 1st-endth. // 1st-endth.
copy(ms[1:end], ms[0:end-1]) copy(ms[1:end], ms[0:end-1])