Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Guild leaving / deleting didn't clean up properly
Browse files Browse the repository at this point in the history
Too many wrong IFs that prevented anything from happening at all and the
chatview wasn't properly cleaned up with the corresponding function.
  • Loading branch information
Bios-Marcel committed Oct 16, 2020
1 parent 6eab4ff commit 2a3d783
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
19 changes: 6 additions & 13 deletions ui/guildlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,12 @@ func (g *GuildList) SetOnGuildSelect(handler func(guildID string)) {
// RemoveGuild removes the node that refers to the given guildID.
func (g *GuildList) RemoveGuild(guildID string) {
children := g.GetRoot().GetChildren()
indexToRemove := -1
for index, node := range children {
if node.GetReference() == guildID {
indexToRemove = index
g.GetRoot().SetChildren(append(children[:index], children[index+1:]...))
break
}
}

if indexToRemove != -1 {
g.GetRoot().SetChildren(append(children[:indexToRemove], children[indexToRemove+1:]...))
}
}

// AddGuild adds a new node that references the given guildID and shows the
Expand All @@ -128,11 +123,9 @@ func (g *GuildList) AddGuild(guildID, name string) {

// UpdateName updates the name of the guild with the given ID.
func (g *GuildList) UpdateName(guildID, newName string) {
for _, node := range g.GetRoot().GetChildren() {
if node.GetReference() == guildID {
node.SetText(tviewutil.Escape(newName))
break
}
node := tviewutil.GetNodeByReference(guildID, g.TreeView)
if node != nil {
node.SetText(tviewutil.Escape(newName))
}
}

Expand All @@ -144,7 +137,7 @@ func (g *GuildList) setNotificationCount(count int) {
}
}

func (g *GuildList) amountOfUnreadGuilds() int {
func (g *GuildList) countUnreadGuilds() int {
var unreadCount int
for _, child := range g.GetRoot().GetChildren() {
if !readstate.HasGuildBeenRead((child.GetReference()).(string)) {
Expand All @@ -158,7 +151,7 @@ func (g *GuildList) amountOfUnreadGuilds() int {
// UpdateUnreadGuildCount finds the number of guilds containing unread
// channels and updates the title accordingly.
func (g *GuildList) UpdateUnreadGuildCount() {
g.setNotificationCount(g.amountOfUnreadGuilds())
g.setNotificationCount(g.countUnreadGuilds())
}

// MarkAsLoaded selects the guild and marks it as loaded.
Expand Down
18 changes: 9 additions & 9 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -1879,25 +1879,25 @@ func (window *Window) registerGuildHandlers() {

go func() {
for guildRemove := range guildRemoveChannel {
if window.selectedGuild == nil {
continue
}

if window.previousChannel != nil && window.previousChannel.GuildID == guildRemove.ID {
window.previousChannel = nil
}

if window.selectedGuild.ID == guildRemove.ID {
guildID := guildRemove.ID
guildID := guildRemove.ID
window.guildList.RemoveGuild(guildID)

selectedGuild := window.selectedGuild
if selectedGuild != nil && selectedGuild.ID == guildID {
window.app.QueueUpdateDraw(func() {
if window.selectedChannel != nil && window.selectedChannel.GuildID == guildID {
window.chatView.ClearViewAndCache()
window.selectedChannel = nil
window.UnloadChannel()
//Unload channel sets the selectedChannel as the previous
//one, which isn't correct.
window.previousChannel = nil
}

window.channelTree.Clear()
window.userList.Clear()
window.guildList.RemoveGuild(guildID)
window.selectedGuild = nil
})
}
Expand Down

0 comments on commit 2a3d783

Please sign in to comment.