Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
update guild on channel_* events [close #411]
Browse files Browse the repository at this point in the history
  • Loading branch information
andersfylling committed Jul 9, 2021
1 parent 18065ab commit 8fc225a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 3 deletions.
77 changes: 74 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ type guildCacheContainer struct {
Members map[Snowflake]*Member
}

func (g *guildCacheContainer) addChannelID(id Snowflake) {
alreadyStored := func() bool {
for i := range g.ChannelIDs {
if g.ChannelIDs[i] == id {
return true
}
}
return false
}

if !alreadyStored() {
g.ChannelIDs = append(g.ChannelIDs, id)
}
}

func (g *guildCacheContainer) removeChannelID(id Snowflake) {
index := func() int {
for i := range g.ChannelIDs {
if g.ChannelIDs[i] == id {
return i
}
}
return -1
}

i := index()
if i == -1 {
return
}

g.ChannelIDs[i] = g.ChannelIDs[len(g.ChannelIDs)-1]
g.ChannelIDs = g.ChannelIDs[:len(g.ChannelIDs)-1]
}

func retrieveChannels(ids []Snowflake, repo *channelsCache) []*Channel {
channels := make([]*Channel, 0, len(ids))

Expand Down Expand Up @@ -209,10 +243,27 @@ func (c *BasicCache) ChannelCreate(data []byte) (*ChannelCreate, error) {
}
c.Patch(channel)

channel2 := DeepCopy(channel).(*Channel)

wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if channel2.GuildID.IsZero() {
return
}

c.Guilds.Lock()
c.Guilds.Store[channel2.GuildID].addChannelID(channel2.ID)
c.Guilds.Unlock()
}()

c.Channels.Lock()
defer c.Channels.Unlock()
_ = c.saveChannel(channel)
return &ChannelCreate{Channel: DeepCopy(channel).(*Channel)}, nil
c.Channels.Unlock()

wg.Wait()
return &ChannelCreate{Channel: channel2}, nil
}

func (c *BasicCache) saveChannel(channel *Channel) error {
Expand Down Expand Up @@ -245,6 +296,12 @@ func (c *BasicCache) ChannelUpdate(data []byte) (*ChannelUpdate, error) {
}
channelID := metadata.ID

if !metadata.GuildID.IsZero() {
c.Guilds.Lock()
c.Guilds.Store[metadata.GuildID].addChannelID(channelID)
c.Guilds.Unlock()
}

c.Channels.Lock()
defer c.Channels.Unlock()

Expand Down Expand Up @@ -280,10 +337,24 @@ func (c *BasicCache) ChannelDelete(data []byte) (*ChannelDelete, error) {
}
c.Patch(cd)

wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if cd.Channel.GuildID.IsZero() {
return
}

c.Guilds.Lock()
c.Guilds.Store[cd.Channel.GuildID].removeChannelID(cd.Channel.ID)
c.Guilds.Unlock()
}()

c.Channels.Lock()
defer c.Channels.Unlock()
delete(c.Channels.Store, cd.Channel.ID)
c.Channels.Unlock()

wg.Wait()
return cd, nil
}

Expand Down
65 changes: 65 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,71 @@ func TestBasicCache_Guilds(t *testing.T) {
}
})

t.Run("channel-create", func(t *testing.T) {
const guildID = 1
const channelID = 20

cache := NewBasicCache()
cache.Guilds.Store[guildID] = &guildCacheContainer{Guild: &Guild{ID: guildID}}

data := []byte(fmt.Sprintf(`{"id":%d,"guild_id":%d}`, channelID, guildID))
_, err := cacheDispatcher(cache, EvtChannelCreate, data)
if err != nil {
t.Fatal("failed to create event", err)
}

channelIDs := cache.Guilds.Store[guildID].ChannelIDs
if len(channelIDs) != 1 {
t.Fatal("channel was not linked to guild")
}
if channelIDs[0] != Snowflake(channelID) {
t.Error("wrong channel id")
}
})

t.Run("channel-update", func(t *testing.T) {
const guildID = 1
const channelID = 20

cache := NewBasicCache()
cache.Guilds.Store[guildID] = &guildCacheContainer{Guild: &Guild{ID: guildID}}

data := []byte(fmt.Sprintf(`{"id":%d,"guild_id":%d}`, channelID, guildID))
_, err := cacheDispatcher(cache, EvtChannelUpdate, data)
if err != nil {
t.Fatal("failed to create event", err)
}

channelIDs := cache.Guilds.Store[guildID].ChannelIDs
if len(channelIDs) != 1 {
t.Fatal("channel was not linked to guild")
}
if channelIDs[0] != Snowflake(channelID) {
t.Error("wrong channel id")
}
})

t.Run("channel-delete", func(t *testing.T) {
const guildID = 1
const channelID = 20

cache := NewBasicCache()
cache.Guilds.Store[guildID] = &guildCacheContainer{
Guild: &Guild{ID: guildID},
ChannelIDs: []Snowflake{channelID},
}

data := []byte(fmt.Sprintf(`{"id":%d,"guild_id":%d}`, channelID, guildID))
_, err := cacheDispatcher(cache, EvtChannelDelete, data)
if err != nil {
t.Fatal("failed to create event", err)
}

channelIDs := cache.Guilds.Store[guildID].ChannelIDs
if len(channelIDs) != 0 {
t.Error("there should be no channels")
}
})
}

func TestBasicCache_GuildMembers(t *testing.T) {
Expand Down

0 comments on commit 8fc225a

Please sign in to comment.