Sending a message in a private chat will now cause a reordering of the chat list.
MODIFY
discordutil/channel.go
discordutil/channel.go
@@ -62,22 +62,25 @@ func GetPrivateChannelName(channel *discordgo.Channel) string {
return channelName
}
-// SortPrivateChannels sorts private channels depending on their last message.
-func SortPrivateChannels(channels []*discordgo.Channel) {
- sort.Slice(channels, func(a, b int) bool {
- channelA := channels[a]
- channelB := channels[b]
-
- messageA, parseError := strconv.ParseInt(channelA.LastMessageID, 10, 64)
+// CompareChannels checks which channel is smaller. Smaller meaning it is the
+// one with the more recent message.
+func CompareChannels(a, b *discordgo.Channel) bool {
+ messageA, parseError := strconv.ParseInt(a.LastMessageID, 10, 64)
if parseError != nil {
return false
}
- messageB, parseError := strconv.ParseInt(channelB.LastMessageID, 10, 64)
+ messageB, parseError := strconv.ParseInt(b.LastMessageID, 10, 64)
if parseError != nil {
return true
}
return messageA > messageB
+}
+
+// SortPrivateChannels sorts private channels depending on their last message.
+func SortPrivateChannels(channels []*discordgo.Channel) {
+ sort.Slice(channels, func(a, b int) bool {
+ return CompareChannels(channels[a], channels[b])
})
}

MODIFY
ui/privatechats.go
ui/privatechats.go
package ui
import (
+ "sort"
+
"github.com/Bios-Marcel/cordless/discordutil"
"github.com/gdamore/tcell"
@@ -205,6 +207,34 @@ func (privateList *PrivateChatList) MarkChannelAsUnread(channel *discordgo.Chann
}
}
+func (privateList *PrivateChatList) ReorderChannelList() {
+ children := privateList.chatsNode.GetChildren()
+ sort.Slice(children, func(a, b int) bool {
+ nodeA := children[a]
+ nodeB := children[b]
+
+ referenceA, ok := nodeA.GetReference().(string)
+ if !ok {
+ return false
+ }
+ referenceB, ok := nodeB.GetReference().(string)
+ if !ok {
+ return true
+ }
+
+ channelA, stateError := privateList.state.Channel(referenceA)
+ if stateError != nil {
+ return false
+ }
+ channelB, stateError := privateList.state.Channel(referenceB)
+ if stateError != nil {
+ return true
+ }
+
+ return discordutil.CompareChannels(channelA, channelB)
+ })
+}
+
// MarkChannelAsLoaded marks a channel as loaded, coloring it blue. If
// a different channel had loaded before, it's set to read.
func (privateList *PrivateChatList) MarkChannelAsLoaded(channel *discordgo.Channel) {

MODIFY
ui/window.go
ui/window.go
@@ -1047,8 +1047,8 @@ func (window *Window) registerMessageEventHandler(input, edit, delete chan *disc
})
}
-// startMessageHandlerRoutines registers the handlers for certain message events.
-// It updates the cache and the UI if necessary.
+// startMessageHandlerRoutines registers the handlers for certain message
+// events. It updates the cache and the UI if necessary.
func (window *Window) startMessageHandlerRoutines(input, edit, delete chan *discordgo.Message, bulkDelete chan *discordgo.MessageDeleteBulk) {
go func() {
for message := range input {
@@ -1061,14 +1061,25 @@ func (window *Window) startMessageHandlerRoutines(input, edit, delete chan *disc
})
}
- if tempMessage.Author.ID == window.session.State.User.ID {
+ channel, stateError := window.session.State.Channel(tempMessage.ChannelID)
+ if stateError != nil {
continue
}
- channel, stateError := window.session.State.Channel(tempMessage.ChannelID)
- if stateError != nil {
+ if channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM {
+ // TODO,HACK.FIXME Since the cache is inconsistent, I have to
+ // update it myself. This should be moved over into the
+ // discordgo code ASAP.
+ channel.LastMessageID = message.ID
+ window.app.QueueUpdateDraw(func() {
+ window.privateList.ReorderChannelList()
+ })
+ }
+
+ if tempMessage.Author.ID == window.session.State.User.ID {
continue
}
+
if (window.selectedChannel == nil || tempMessage.ChannelID != window.selectedChannel.ID) ||
!window.userActive {
mentionsYou := false
@@ -1576,9 +1587,7 @@ func (window *Window) RefreshLayout() {
func (window *Window) LoadChannel(channel *discordgo.Channel) error {
var messages []*discordgo.Message
- // Data not present
if channel.LastMessageID != "" && len(channel.Messages) == 0 {
- //Check Cache first
cache, cacheError := window.session.State.Channel(channel.ID)
if cacheError == nil || cache != nil && len(cache.Messages) == 0 {
var discordError error
@@ -1617,9 +1626,7 @@ func (window *Window) LoadChannel(channel *discordgo.Channel) error {
window.selectedGuildNode.SetColor(tview.Styles.PrimaryTextColor)
window.selectedGuildNode = nil
}
- }
- if channel.GuildID == "" {
window.selectedGuild = nil
window.selectedGuildNode = nil
}

Sending a message in a private chat will now cause a reordering of the chat list.