From 5a22b33744304f0ab06c0f32d767ca4a6661844e Mon Sep 17 00:00:00 2001 From: Marcel Schramm Date: Sun, 18 Oct 2020 15:29:24 +0200 Subject: [PATCH] Improve docs in discordutil.GetPrivateChannelName and split method into tview specific and unspecific one --- discordutil/channel.go | 21 ++++++++++++++++++--- ui/window.go | 14 ++------------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/discordutil/channel.go b/discordutil/channel.go index c30f422..d0e3167 100644 --- a/discordutil/channel.go +++ b/discordutil/channel.go @@ -29,15 +29,23 @@ func SortMessagesByTimestamp(messages []*discordgo.Message) { }) } -// GetPrivateChannelName generates a name for a private channel. -func GetPrivateChannelName(channel *discordgo.Channel) string { +// GetPrivateChannelNameUnescaped generates a name for a private channel. +// The name won't be escaped view tviewutil and therefore shouldn't be used +// for displaying it in tview components. +func GetPrivateChannelNameUnescaped(channel *discordgo.Channel) string { var channelName string if channel.Type == discordgo.ChannelTypeDM { + //The first recipient should always be us! channelName = channel.Recipients[0].Username + //Since the official client doesn't seem to allow creating nicks for + //simple DMs, we assume it isn't possible. } else if channel.Type == discordgo.ChannelTypeGroupDM { + //Groups can have custom names. if channel.Name != "" { channelName = channel.Name } else { + //Channels can have nicknames, but if they don't the default discord + //client just displays the recipients names sticked together. for index, recipient := range channel.Recipients { if index == 0 { channelName = recipient.Username @@ -48,11 +56,18 @@ func GetPrivateChannelName(channel *discordgo.Channel) string { } } + //This is a fallback, so we don't have an empty string. + //This happens sometimes, I am unsure when though. if channelName == "" { channelName = "Unnamed" } - return tviewutil.Escape(channelName) + return channelName +} + +// GetPrivateChannelName generates a name for a private channel. +func GetPrivateChannelName(channel *discordgo.Channel) string { + return tviewutil.Escape(GetPrivateChannelNameUnescaped(channel)) } // FindDMChannelWithUser tries to find a DM channel with the specified user as diff --git a/ui/window.go b/ui/window.go index 7294ffc..1cabbed 100644 --- a/ui/window.go +++ b/ui/window.go @@ -2098,18 +2098,8 @@ func (window *Window) handleNotification(message *discordgo.Message, channel *di if channel.Type == discordgo.ChannelTypeDM { notificationLocation = message.Author.Username } else if channel.Type == discordgo.ChannelTypeGroupDM { - notificationLocation = channel.Name - if notificationLocation == "" { - for index, recipient := range channel.Recipients { - if index == 0 { - notificationLocation = recipient.Username - } else { - notificationLocation = fmt.Sprintf("%s, %s", notificationLocation, recipient.Username) - } - } - } - - notificationLocation = message.Author.Username + " - " + notificationLocation + notificationLocation = message.Author.Username + " - " + + discordutil.GetPrivateChannelNameUnescaped(channel) } else if channel.Type == discordgo.ChannelTypeGuildText { guild, cacheError := window.session.State.Guild(message.GuildID) if guild != nil && cacheError == nil {