guilds.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package ui
  2. import (
  3. "sort"
  4. "github.com/ayntgl/astatine"
  5. "github.com/ayntgl/discordo/discord"
  6. "github.com/rivo/tview"
  7. )
  8. type GuildsList struct {
  9. *tview.List
  10. app *App
  11. }
  12. func NewGuildsList(app *App) *GuildsList {
  13. gl := &GuildsList{
  14. List: tview.NewList(),
  15. app: app,
  16. }
  17. gl.AddItem("Direct Messages", "", 0, nil)
  18. gl.ShowSecondaryText(false)
  19. gl.SetTitle("Guilds")
  20. gl.SetTitleAlign(tview.AlignLeft)
  21. gl.SetBorder(true)
  22. gl.SetBorderPadding(0, 0, 1, 1)
  23. gl.SetSelectedFunc(gl.onSelected)
  24. return gl
  25. }
  26. func (gl *GuildsList) onSelected(idx int, mainText string, secondaryText string, shortcut rune) {
  27. rootTreeNode := gl.app.ChannelsTreeView.GetRoot()
  28. rootTreeNode.ClearChildren()
  29. gl.app.SelectedMessage = -1
  30. gl.app.MessagesTextView.
  31. Highlight().
  32. Clear()
  33. gl.app.MessageInputField.SetText("")
  34. if mainText == "Direct Messages" {
  35. cs := gl.app.Session.State.PrivateChannels
  36. sort.Slice(cs, func(i, j int) bool {
  37. return cs[i].LastMessageID > cs[j].LastMessageID
  38. })
  39. for _, c := range cs {
  40. channelTreeNode := tview.NewTreeNode(discord.ChannelToString(c)).
  41. SetReference(c.ID)
  42. rootTreeNode.AddChild(channelTreeNode)
  43. }
  44. } else { // Guild
  45. cs := gl.app.Session.State.Guilds[idx-1].Channels
  46. sort.Slice(cs, func(i, j int) bool {
  47. return cs[i].Position < cs[j].Position
  48. })
  49. for _, c := range cs {
  50. if (c.Type == astatine.ChannelTypeGuildText || c.Type == astatine.ChannelTypeGuildNews) && (c.ParentID == "") {
  51. channelTreeNode := tview.NewTreeNode(discord.ChannelToString(c)).
  52. SetReference(c.ID)
  53. rootTreeNode.AddChild(channelTreeNode)
  54. }
  55. }
  56. CATEGORY:
  57. for _, c := range cs {
  58. if c.Type == astatine.ChannelTypeGuildCategory {
  59. for _, nestedChannel := range cs {
  60. if nestedChannel.ParentID == c.ID {
  61. channelTreeNode := tview.NewTreeNode(c.Name).
  62. SetReference(c.ID)
  63. rootTreeNode.AddChild(channelTreeNode)
  64. continue CATEGORY
  65. }
  66. }
  67. channelTreeNode := tview.NewTreeNode(c.Name).
  68. SetReference(c.ID)
  69. rootTreeNode.AddChild(channelTreeNode)
  70. }
  71. }
  72. for _, c := range cs {
  73. if (c.Type == astatine.ChannelTypeGuildText || c.Type == astatine.ChannelTypeGuildNews) && (c.ParentID != "") {
  74. var parentTreeNode *tview.TreeNode
  75. rootTreeNode.Walk(func(node, _ *tview.TreeNode) bool {
  76. if node.GetReference() == c.ParentID {
  77. parentTreeNode = node
  78. return false
  79. }
  80. return true
  81. })
  82. if parentTreeNode != nil {
  83. channelTreeNode := tview.NewTreeNode(discord.ChannelToString(c)).
  84. SetReference(c.ID)
  85. parentTreeNode.AddChild(channelTreeNode)
  86. }
  87. }
  88. }
  89. }
  90. gl.app.ChannelsTreeView.SetCurrentNode(rootTreeNode)
  91. gl.app.SetFocus(gl.app.ChannelsTreeView)
  92. }