| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- package chat
- import (
- "fmt"
- "log/slog"
- "github.com/ayn2op/discordo/internal/clipboard"
- "github.com/ayn2op/discordo/internal/config"
- "github.com/ayn2op/discordo/internal/ui"
- "github.com/ayn2op/tview"
- "github.com/ayn2op/tview/help"
- "github.com/ayn2op/tview/keybind"
- "github.com/diamondburned/arikawa/v3/discord"
- "github.com/diamondburned/arikawa/v3/gateway"
- "github.com/diamondburned/ningen/v3"
- "github.com/gdamore/tcell/v3"
- )
- type dmNode struct{}
- type guildsTree struct {
- *tview.TreeView
- cfg *config.Config
- chatView *View
- // Fast-path indexes for frequent event handlers (read updates, picker
- // navigation). They mirror the current rendered tree and are rebuilt on
- // READY before nodes are added.
- guildNodeByID map[discord.GuildID]*tview.TreeNode
- channelNodeByID map[discord.ChannelID]*tview.TreeNode
- dmRootNode *tview.TreeNode
- }
- var _ help.KeyMap = (*guildsTree)(nil)
- func newGuildsTree(cfg *config.Config, chatView *View) *guildsTree {
- gt := &guildsTree{
- TreeView: tview.NewTreeView(),
- cfg: cfg,
- chatView: chatView,
- guildNodeByID: make(map[discord.GuildID]*tview.TreeNode),
- channelNodeByID: make(map[discord.ChannelID]*tview.TreeNode),
- }
- gt.Box = ui.ConfigureBox(gt.Box, &cfg.Theme)
- gt.
- SetRoot(tview.NewTreeNode("")).
- SetTopLevel(1).
- SetMarkers(tview.TreeMarkers{
- Expanded: cfg.Sidebar.Markers.Expanded,
- Collapsed: cfg.Sidebar.Markers.Collapsed,
- Leaf: cfg.Sidebar.Markers.Leaf,
- }).
- SetGraphics(cfg.Theme.GuildsTree.Graphics).
- SetGraphicsColor(tcell.GetColor(cfg.Theme.GuildsTree.GraphicsColor)).
- SetSelectedFunc(gt.onSelected).
- SetTitle("Guilds")
- return gt
- }
- func (gt *guildsTree) ShortHelp() []keybind.Keybind {
- cfg := gt.cfg.Keybinds.GuildsTree
- selectCurrent := cfg.SelectCurrent.Keybind
- collapseParent := cfg.CollapseParentNode.Keybind
- selectHelp := selectCurrent.Help()
- selectDesc := selectHelp.Desc
- if node := gt.GetCurrentNode(); node != nil {
- if len(node.GetChildren()) > 0 {
- if node.IsExpanded() {
- selectDesc = "collapse"
- } else {
- selectDesc = "expand"
- }
- } else {
- switch node.GetReference().(type) {
- case discord.GuildID, dmNode:
- selectDesc = "expand"
- }
- }
- }
- selectCurrent.SetHelp(selectHelp.Key, selectDesc)
- collapseHelp := collapseParent.Help()
- collapseParent.SetHelp(collapseHelp.Key, "collapse parent")
- shortHelp := []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, selectCurrent}
- if gt.canCollapseParent(gt.GetCurrentNode()) {
- shortHelp = append(shortHelp, collapseParent)
- }
- return shortHelp
- }
- func (gt *guildsTree) FullHelp() [][]keybind.Keybind {
- cfg := gt.cfg.Keybinds.GuildsTree
- selectCurrent := cfg.SelectCurrent.Keybind
- collapseParent := cfg.CollapseParentNode.Keybind
- selectHelp := selectCurrent.Help()
- selectDesc := selectHelp.Desc
- if node := gt.GetCurrentNode(); node != nil {
- if len(node.GetChildren()) > 0 {
- if node.IsExpanded() {
- selectDesc = "collapse"
- } else {
- selectDesc = "expand"
- }
- } else {
- switch node.GetReference().(type) {
- case discord.GuildID, dmNode:
- selectDesc = "expand"
- }
- }
- }
- selectCurrent.SetHelp(selectHelp.Key, selectDesc)
- collapseHelp := collapseParent.Help()
- collapseParent.SetHelp(collapseHelp.Key, "collapse parent")
- actions := []keybind.Keybind{selectCurrent, cfg.MoveToParentNode.Keybind}
- if gt.canCollapseParent(gt.GetCurrentNode()) {
- actions = append(actions, collapseParent)
- }
- return [][]keybind.Keybind{
- {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
- actions,
- {cfg.YankID.Keybind},
- }
- }
- func (gt *guildsTree) canCollapseParent(node *tview.TreeNode) bool {
- if node == nil {
- return false
- }
- path := gt.GetPath(node)
- // Path layout is [root, ..., node]. A non-root parent means at least 3 nodes.
- if len(path) < 3 {
- return false
- }
- parent := path[len(path)-2]
- return parent != nil && parent.GetLevel() != 0
- }
- func (gt *guildsTree) resetNodeIndex() {
- // Keep allocated map capacity; READY can rebuild often during reconnects.
- clear(gt.guildNodeByID)
- clear(gt.channelNodeByID)
- gt.dmRootNode = nil
- }
- func (gt *guildsTree) createFolderNode(folder gateway.GuildFolder, guildsByID map[discord.GuildID]*gateway.GuildCreateEvent) {
- name := "Folder"
- if folder.Name != "" {
- name = folder.Name
- }
- folderNode := tview.NewTreeNode(name).SetExpanded(gt.cfg.Theme.GuildsTree.AutoExpandFolders)
- if folder.Color != 0 {
- folderStyle := tcell.StyleDefault.Foreground(tcell.NewHexColor(int32(folder.Color)))
- gt.setNodeLineStyle(folderNode, folderStyle)
- }
- gt.GetRoot().AddChild(folderNode)
- for _, guildID := range folder.GuildIDs {
- if guildEvent, ok := guildsByID[guildID]; ok {
- gt.createGuildNode(folderNode, guildEvent.Guild)
- }
- }
- }
- func (gt *guildsTree) unreadStyle(indication ningen.UnreadIndication) tcell.Style {
- var style tcell.Style
- switch indication {
- case ningen.ChannelRead:
- style = style.Dim(true)
- case ningen.ChannelMentioned:
- style = style.Underline(true)
- fallthrough
- case ningen.ChannelUnread:
- style = style.Bold(true)
- }
- return style
- }
- func (gt *guildsTree) getGuildNodeStyle(guildID discord.GuildID) tcell.Style {
- indication := gt.chatView.state.GuildIsUnread(guildID, ningen.GuildUnreadOpts{UnreadOpts: ningen.UnreadOpts{IncludeMutedCategories: true}})
- return gt.unreadStyle(indication)
- }
- func (gt *guildsTree) getChannelNodeStyle(channelID discord.ChannelID) tcell.Style {
- indication := gt.chatView.state.ChannelIsUnread(channelID, ningen.UnreadOpts{IncludeMutedCategories: true})
- return gt.unreadStyle(indication)
- }
- func (gt *guildsTree) createGuildNode(n *tview.TreeNode, guild discord.Guild) {
- guildNode := tview.NewTreeNode(guild.Name).SetReference(guild.ID).SetExpandable(true).SetExpanded(false)
- gt.setNodeLineStyle(guildNode, gt.getGuildNodeStyle(guild.ID))
- n.AddChild(guildNode)
- gt.guildNodeByID[guild.ID] = guildNode
- }
- func (gt *guildsTree) createChannelNode(node *tview.TreeNode, channel discord.Channel) {
- if channel.Type != discord.DirectMessage && channel.Type != discord.GroupDM && channel.Type != discord.GuildCategory && !gt.chatView.state.HasPermissions(channel.ID, discord.PermissionViewChannel) {
- return
- }
- channelNode := tview.NewTreeNode(ui.ChannelToString(channel, gt.cfg.Icons, gt.chatView.state)).SetReference(channel.ID)
- if channel.Type == discord.GuildForum {
- channelNode.SetExpandable(true).SetExpanded(false)
- }
- gt.setNodeLineStyle(channelNode, gt.getChannelNodeStyle(channel.ID))
- node.AddChild(channelNode)
- gt.channelNodeByID[channel.ID] = channelNode
- }
- func (gt *guildsTree) setNodeLineStyle(node *tview.TreeNode, style tcell.Style) {
- line := node.GetLine()
- for i := range line {
- line[i].Style = style
- }
- node.SetLine(line)
- }
- func (gt *guildsTree) createChannelNodes(node *tview.TreeNode, channels []discord.Channel) {
- // Preserve exact ordering semantics:
- // 1) top-level non-categories (in input order),
- // 2) categories that have at least one child in the source slice (in input order),
- // 3) parented channels under already-created categories (in input order).
- //
- // We precompute parent presence once to avoid the O(n^2) category-child scan.
- hasChildByParentID := make(map[discord.ChannelID]struct{}, len(channels))
- for _, channel := range channels {
- if channel.ParentID.IsValid() {
- hasChildByParentID[channel.ParentID] = struct{}{}
- }
- }
- for _, channel := range channels {
- if channel.Type != discord.GuildCategory && !channel.ParentID.IsValid() {
- gt.createChannelNode(node, channel)
- }
- }
- for _, channel := range channels {
- if channel.Type == discord.GuildCategory {
- if _, ok := hasChildByParentID[channel.ID]; ok {
- gt.createChannelNode(node, channel)
- }
- }
- }
- for _, channel := range channels {
- if channel.ParentID.IsValid() {
- // Parent categories are inserted earlier in this function, so this
- // lookup is O(1) and avoids per-channel subtree walks.
- parent := gt.channelNodeByID[channel.ParentID]
- if parent != nil {
- gt.createChannelNode(parent, channel)
- }
- }
- }
- }
- func (gt *guildsTree) onSelected(node *tview.TreeNode) {
- if len(node.GetChildren()) != 0 {
- node.SetExpanded(!node.IsExpanded())
- return
- }
- switch ref := node.GetReference().(type) {
- case discord.GuildID:
- go gt.chatView.state.MemberState.Subscribe(ref)
- channels, err := gt.chatView.state.Cabinet.Channels(ref)
- if err != nil {
- slog.Error("failed to get channels", "err", err, "guild_id", ref)
- return
- }
- ui.SortGuildChannels(channels)
- gt.createChannelNodes(node, channels)
- node.Expand()
- case discord.ChannelID:
- channel, err := gt.chatView.state.Cabinet.Channel(ref)
- if err != nil {
- slog.Error("failed to get channel from state", "channel_id", ref)
- return
- }
- // Handle forum channels differently - they contain threads, not direct messages
- if channel.Type == discord.GuildForum {
- // Get all channels from the guild - this includes active threads from GuildCreateEvent
- allChannels, err := gt.chatView.state.Cabinet.Channels(channel.GuildID)
- if err != nil {
- slog.Error("failed to get channels for forum threads", "err", err, "guild_id", channel.GuildID)
- return
- }
- // Filter for threads that belong to this forum channel
- var forumThreads []discord.Channel
- for _, ch := range allChannels {
- if ch.ParentID == channel.ID && (ch.Type == discord.GuildPublicThread ||
- ch.Type == discord.GuildPrivateThread ||
- ch.Type == discord.GuildAnnouncementThread) {
- forumThreads = append(forumThreads, ch)
- }
- }
- // Add threads as child nodes
- for _, thread := range forumThreads {
- gt.createChannelNode(node, thread)
- }
- node.Expand()
- return
- }
- limit := gt.cfg.MessagesLimit
- messages, err := gt.chatView.state.Messages(channel.ID, uint(limit))
- if err != nil {
- slog.Error("failed to get messages", "err", err, "channel_id", channel.ID, "limit", limit)
- return
- }
- go gt.chatView.state.ReadState.MarkRead(channel.ID, channel.LastMessageID)
- if guildID := channel.GuildID; guildID.IsValid() {
- gt.chatView.messagesList.requestGuildMembers(guildID, messages)
- }
- gt.chatView.SetSelectedChannel(channel)
- gt.chatView.clearTypers()
- gt.chatView.messageInput.stopTypingTimer()
- gt.chatView.messagesList.reset()
- gt.chatView.messagesList.setTitle(*channel)
- gt.chatView.messagesList.setMessages(messages)
- gt.chatView.messagesList.ScrollToEnd()
- hasNoPerm := channel.Type != discord.DirectMessage && channel.Type != discord.GroupDM && !gt.chatView.state.HasPermissions(channel.ID, discord.PermissionSendMessages)
- gt.chatView.messageInput.SetDisabled(hasNoPerm)
- var text string
- if hasNoPerm {
- text = "You do not have permission to send messages in this channel."
- } else {
- text = "Message..."
- if gt.cfg.AutoFocus {
- gt.chatView.app.SetFocus(gt.chatView.messageInput)
- }
- }
- gt.chatView.messageInput.SetPlaceholder(tview.NewLine(tview.NewSegment(text, tcell.StyleDefault.Dim(true))))
- case dmNode: // Direct messages folder
- channels, err := gt.chatView.state.PrivateChannels()
- if err != nil {
- slog.Error("failed to get private channels", "err", err)
- return
- }
- ui.SortPrivateChannels(channels)
- for _, c := range channels {
- gt.createChannelNode(node, c)
- }
- node.Expand()
- }
- }
- func (gt *guildsTree) collapseParentNode(node *tview.TreeNode) {
- gt.
- GetRoot().
- Walk(func(n, parent *tview.TreeNode) bool {
- if n == node && parent.GetLevel() != 0 {
- parent.Collapse()
- gt.SetCurrentNode(parent)
- return false
- }
- return true
- })
- }
- func (gt *guildsTree) handleInput(event *tcell.EventKey) *tcell.EventKey {
- switch {
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.CollapseParentNode.Keybind):
- gt.collapseParentNode(gt.GetCurrentNode())
- return nil
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.MoveToParentNode.Keybind):
- return tcell.NewEventKey(tcell.KeyRune, "K", tcell.ModNone)
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.Up.Keybind):
- return tcell.NewEventKey(tcell.KeyUp, "", tcell.ModNone)
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.Down.Keybind):
- return tcell.NewEventKey(tcell.KeyDown, "", tcell.ModNone)
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.Top.Keybind):
- gt.Move(gt.GetRowCount() * -1)
- // return tcell.NewEventKey(tcell.KeyHome, 0, tcell.ModNone)
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.Bottom.Keybind):
- gt.Move(gt.GetRowCount())
- // return tcell.NewEventKey(tcell.KeyEnd, 0, tcell.ModNone)
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.SelectCurrent.Keybind):
- return tcell.NewEventKey(tcell.KeyEnter, "", tcell.ModNone)
- case keybind.Matches(event, gt.cfg.Keybinds.GuildsTree.YankID.Keybind):
- gt.yankID()
- }
- return nil
- }
- func (gt *guildsTree) InputHandler(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
- event = gt.handleInput(event)
- if event == nil {
- return
- }
- gt.TreeView.InputHandler(event, setFocus)
- }
- func (gt *guildsTree) yankID() {
- node := gt.GetCurrentNode()
- if node == nil {
- return
- }
- // Reference of a tree node in the guilds tree is its ID.
- // discord.Snowflake (discord.GuildID and discord.ChannelID) have the String method.
- if id, ok := node.GetReference().(fmt.Stringer); ok {
- go clipboard.Write(clipboard.FmtText, []byte(id.String()))
- }
- }
- func (gt *guildsTree) findNodeByReference(reference any) *tview.TreeNode {
- switch ref := reference.(type) {
- case discord.GuildID:
- return gt.guildNodeByID[ref]
- case discord.ChannelID:
- return gt.channelNodeByID[ref]
- case dmNode:
- return gt.dmRootNode
- default:
- // Fallback keeps this helper safe for non-indexed custom references.
- var found *tview.TreeNode
- gt.GetRoot().Walk(func(node, _ *tview.TreeNode) bool {
- if node.GetReference() == reference {
- found = node
- return false
- }
- return true
- })
- return found
- }
- }
- func (gt *guildsTree) findNodeByChannelID(channelID discord.ChannelID) *tview.TreeNode {
- channel, err := gt.chatView.state.Cabinet.Channel(channelID)
- if err != nil {
- slog.Error("failed to get channel", "channel_id", channelID, "err", err)
- return nil
- }
- var reference any
- if guildID := channel.GuildID; guildID.IsValid() {
- reference = guildID
- } else {
- reference = dmNode{}
- }
- if parentNode := gt.findNodeByReference(reference); parentNode != nil {
- if len(parentNode.GetChildren()) == 0 {
- gt.onSelected(parentNode)
- }
- }
- node := gt.findNodeByReference(channelID)
- return node
- }
- func (gt *guildsTree) expandPathToNode(node *tview.TreeNode) {
- if node == nil {
- return
- }
- for _, n := range gt.GetPath(node) {
- n.Expand()
- }
- }
|