Unread markers now also work for servers
MODIFY
ui/window.go
ui/window.go
@@ -66,7 +66,7 @@ type Window struct {
session *discordgo.Session
selectedGuildNode *tview.TreeNode
- selectedGuild *discordgo.UserGuild
+ selectedGuild *discordgo.Guild
selectedChannelNode *tview.TreeNode
selectedChannel *discordgo.Channel
@@ -150,31 +150,34 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
guildList.SetRoot(guildRootNode)
guildList.SetBorder(true)
guildList.SetTopLevel(1)
-
- window.registerGuildMemberHandlers()
-
- discordutil.SortGuilds(window.session.State.Settings, guilds)
-
- for _, tempGuild := range guilds {
- guild := tempGuild
- guildNode := tview.NewTreeNode(guild.Name)
- guildRootNode.AddChild(guildNode)
- guildNode.SetSelectable(true)
- guildNode.SetSelectedFunc(func() {
+ guildList.SetSelectedFunc(func(node *tview.TreeNode) {
if window.selectedGuildNode != nil {
- window.selectedGuildNode.SetColor(tcell.ColorWhite)
+ window.updateServerReadStatus(window.selectedGuild.ID, window.selectedGuildNode, false)
+ }
+
+ guildID, ok := node.GetReference().(string)
+ if !ok {
+ window.ShowErrorDialog("Guild couldn't be loaded.")
+ return
}
- window.selectedGuildNode = guildNode
- window.selectedGuildNode.SetColor(tview.Styles.ContrastBackgroundColor)
+ guild, cacheError := window.session.Guild(guildID)
+ if cacheError != nil {
+ window.ShowErrorDialog(cacheError.Error())
+ return
+ }
+ window.selectedGuildNode = node
window.selectedGuild = guild
- requestError := session.RequestGuildMembers(guild.ID, "", 0)
+
+ window.updateServerReadStatus(window.selectedGuild.ID, window.selectedGuildNode, true)
+
+ requestError := session.RequestGuildMembers(guildID, "", 0)
if requestError != nil {
fmt.Fprintln(window.commandView, "Error retrieving all guild members.")
}
- channelLoadError := window.channelTree.LoadGuild(guild.ID)
+ channelLoadError := window.channelTree.LoadGuild(guildID)
if channelLoadError != nil {
window.ShowErrorDialog(channelLoadError.Error())
} else {
@@ -183,13 +186,27 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
}
}
- userLoadError := window.userList.LoadGuild(guild.ID)
+ userLoadError := window.userList.LoadGuild(guildID)
if userLoadError != nil {
window.ShowErrorDialog(userLoadError.Error())
}
window.RefreshLayout()
})
+
+ window.registerGuildMemberHandlers()
+
+ discordutil.SortGuilds(window.session.State.Settings, guilds)
+
+ for _, tempGuild := range guilds {
+ guild := tempGuild
+ guildNode := tview.NewTreeNode(guild.Name)
+ guildNode.SetReference(guild.ID)
+ guildRootNode.AddChild(guildNode)
+
+ window.updateServerReadStatus(guild.ID, guildNode, false)
+
+ guildNode.SetSelectable(true)
}
if len(guildRootNode.GetChildren()) > 0 {
@@ -911,6 +928,24 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
return &window, nil
}
+func (window *Window) updateServerReadStatus(guildID string, guildNode *tview.TreeNode, isSelected bool) {
+ if isSelected {
+ guildNode.SetColor(tview.Styles.ContrastBackgroundColor)
+ } else {
+ realGuild, cacheError := window.session.State.Guild(guildID)
+ if cacheError == nil {
+ for _, channel := range realGuild.Channels {
+ if !readstate.HasBeenRead(channel) {
+ guildNode.SetColor(tcell.ColorRed)
+ return
+ }
+ }
+ }
+
+ guildNode.SetColor(tview.Styles.PrimaryTextColor)
+ }
+}
+
func (window *Window) prepareMessage(inputText string) string {
output := codeBlockRegex.ReplaceAllStringFunc(inputText, func(input string) string {
return strings.Replace(input, ":", "\\:", -1)
@@ -1149,6 +1184,15 @@ func (window *Window) startMessageHandlerRoutines(input, edit, delete chan *disc
continue
}
+ if channel.Type == discordgo.ChannelTypeGuildText {
+ for _, guildNode := range window.guildList.GetRoot().GetChildren() {
+ if guildNode.GetReference() != channel.GuildID {
+ window.updateServerReadStatus(channel.GuildID, guildNode, false)
+ break
+ }
+ }
+ }
+
if channel.Type == discordgo.ChannelTypeGuildText || channel.Type == discordgo.ChannelTypeDM ||
channel.Type == discordgo.ChannelTypeGroupDM {
// TODO,HACK.FIXME Since the cache is inconsistent, I have to
@@ -1736,6 +1780,13 @@ func (window *Window) LoadChannel(channel *discordgo.Channel) error {
readstate.UpdateRead(channel.ID, channel.LastMessageID)
+ // Here we make the assumption that the channel we are loading must be part
+ // of the currently loaded guild, since we don't allow loading a channel of
+ // a guilder otherwise.
+ if channel.GuildID != "" {
+ window.updateServerReadStatus(window.selectedGuild.ID, window.selectedGuildNode, true)
+ }
+
return nil
}
@@ -1778,8 +1829,8 @@ func (window *Window) GetRegisteredCommands() map[string]commands.Command {
return window.commands
}
-// GetSelectedGuild returns a reference to the currently selected UserGuild.
-func (window *Window) GetSelectedGuild() *discordgo.UserGuild {
+// GetSelectedGuild returns a reference to the currently selected Guild.
+func (window *Window) GetSelectedGuild() *discordgo.Guild {
return window.selectedGuild
}

Unread markers now also work for servers