model.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package chat
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "sync"
  6. "time"
  7. "github.com/ayn2op/discordo/internal/config"
  8. "github.com/ayn2op/discordo/internal/ui"
  9. "github.com/ayn2op/tview"
  10. "github.com/ayn2op/tview/keybind"
  11. "github.com/ayn2op/tview/layers"
  12. "github.com/diamondburned/arikawa/v3/discord"
  13. "github.com/diamondburned/ningen/v3"
  14. "github.com/diamondburned/ningen/v3/states/read"
  15. "github.com/gdamore/tcell/v3"
  16. )
  17. const typingDuration = 10 * time.Second
  18. const (
  19. flexLayerName = "flex"
  20. mentionsListLayerName = "mentionsList"
  21. attachmentsListLayerName = "attachmentsList"
  22. confirmModalLayerName = "confirmModal"
  23. channelsPickerLayerName = "channelsPicker"
  24. )
  25. type Model struct {
  26. *layers.Layers
  27. rootFlex *tview.Flex
  28. mainFlex *tview.Flex
  29. rightFlex *tview.Flex
  30. guildsTree *guildsTree
  31. messagesList *messagesList
  32. messageInput *messageInput
  33. channelsPicker *channelsPicker
  34. selectedChannel *discord.Channel
  35. selectedChannelMu sync.RWMutex
  36. typersMu sync.RWMutex
  37. typers map[discord.UserID]*time.Timer
  38. confirmModalDone func(label string)
  39. confirmModalPreviousFocus tview.Primitive
  40. app *tview.Application
  41. cfg *config.Config
  42. state *ningen.State
  43. token string
  44. }
  45. func NewView(app *tview.Application, cfg *config.Config, token string) *Model {
  46. v := &Model{
  47. Layers: layers.New(),
  48. rootFlex: tview.NewFlex(),
  49. mainFlex: tview.NewFlex(),
  50. rightFlex: tview.NewFlex(),
  51. typers: make(map[discord.UserID]*time.Timer),
  52. app: app,
  53. cfg: cfg,
  54. token: token,
  55. }
  56. v.guildsTree = newGuildsTree(cfg, v)
  57. v.messagesList = newMessagesList(cfg, v)
  58. v.messageInput = newMessageInput(cfg, v)
  59. v.channelsPicker = newChannelsPicker(cfg, v)
  60. v.channelsPicker.SetCancelFunc(v.closePicker)
  61. v.SetBackgroundLayerStyle(v.cfg.Theme.Dialog.BackgroundStyle.Style)
  62. v.buildLayout()
  63. return v
  64. }
  65. func (v *Model) SelectedChannel() *discord.Channel {
  66. v.selectedChannelMu.RLock()
  67. defer v.selectedChannelMu.RUnlock()
  68. return v.selectedChannel
  69. }
  70. func (v *Model) SetSelectedChannel(channel *discord.Channel) {
  71. v.selectedChannelMu.Lock()
  72. v.selectedChannel = channel
  73. v.selectedChannelMu.Unlock()
  74. }
  75. func (v *Model) buildLayout() {
  76. v.Clear()
  77. v.rootFlex.Clear()
  78. v.rightFlex.Clear()
  79. v.mainFlex.Clear()
  80. v.rightFlex.
  81. SetDirection(tview.FlexRow).
  82. AddItem(v.messagesList, 0, 1, false).
  83. AddItem(v.messageInput, 3, 1, false)
  84. // The guilds tree is always focused first at start-up.
  85. v.mainFlex.
  86. AddItem(v.guildsTree, 0, 1, true).
  87. AddItem(v.rightFlex, 0, 4, false)
  88. v.rootFlex.
  89. SetDirection(tview.FlexRow).
  90. AddItem(v.mainFlex, 0, 1, true)
  91. v.AddLayer(v.rootFlex, layers.WithName(flexLayerName), layers.WithResize(true), layers.WithVisible(true))
  92. v.AddLayer(v.messageInput.mentionsList, layers.WithName(mentionsListLayerName), layers.WithResize(false), layers.WithVisible(false))
  93. }
  94. func (v *Model) togglePicker() {
  95. if v.HasLayer(channelsPickerLayerName) {
  96. v.closePicker()
  97. } else {
  98. v.openPicker()
  99. }
  100. }
  101. func (v *Model) openPicker() {
  102. v.AddLayer(
  103. ui.Centered(v.channelsPicker, v.cfg.Picker.Width, v.cfg.Picker.Height),
  104. layers.WithName(channelsPickerLayerName),
  105. layers.WithResize(true),
  106. layers.WithVisible(true),
  107. layers.WithOverlay(),
  108. ).SendToFront(channelsPickerLayerName)
  109. v.channelsPicker.update()
  110. }
  111. func (v *Model) closePicker() {
  112. v.RemoveLayer(channelsPickerLayerName)
  113. v.channelsPicker.Update()
  114. }
  115. func (v *Model) toggleGuildsTree() {
  116. // The guilds tree is visible if the number of items is two.
  117. if v.mainFlex.GetItemCount() == 2 {
  118. v.mainFlex.RemoveItem(v.guildsTree)
  119. if v.guildsTree.HasFocus() {
  120. v.app.SetFocus(v.mainFlex)
  121. }
  122. } else {
  123. v.buildLayout()
  124. v.app.SetFocus(v.guildsTree)
  125. }
  126. }
  127. func (v *Model) focusGuildsTree() bool {
  128. // The guilds tree is not hidden if the number of items is two.
  129. if v.mainFlex.GetItemCount() == 2 {
  130. v.app.SetFocus(v.guildsTree)
  131. return true
  132. }
  133. return false
  134. }
  135. func (v *Model) focusMessageInput() bool {
  136. if !v.messageInput.GetDisabled() {
  137. v.app.SetFocus(v.messageInput)
  138. return true
  139. }
  140. return false
  141. }
  142. func (v *Model) focusPrevious() {
  143. switch v.app.GetFocus() {
  144. case v.messagesList: // Handle both a.messagesList and a.flex as well as other edge cases (if there is).
  145. if v.focusGuildsTree() {
  146. return
  147. }
  148. fallthrough
  149. case v.guildsTree:
  150. if v.focusMessageInput() {
  151. return
  152. }
  153. fallthrough
  154. case v.messageInput:
  155. v.app.SetFocus(v.messagesList)
  156. }
  157. }
  158. func (v *Model) focusNext() {
  159. switch v.app.GetFocus() {
  160. case v.messagesList:
  161. if v.focusMessageInput() {
  162. return
  163. }
  164. fallthrough
  165. case v.messageInput: // Handle both a.messageInput and a.flex as well as other edge cases (if there is).
  166. if v.focusGuildsTree() {
  167. return
  168. }
  169. fallthrough
  170. case v.guildsTree:
  171. v.app.SetFocus(v.messagesList)
  172. }
  173. }
  174. func (v *Model) HandleEvent(event tcell.Event) tview.Command {
  175. switch event := event.(type) {
  176. case *tview.InitEvent:
  177. return tview.EventCommand(func() tcell.Event {
  178. if err := v.OpenState(v.token); err != nil {
  179. slog.Error("failed to open chat state", "err", err)
  180. return tcell.NewEventError(err)
  181. }
  182. return nil
  183. })
  184. case *QuitEvent:
  185. return tview.BatchCommand{
  186. v.closeState(),
  187. tview.Quit(),
  188. }
  189. case *tview.ModalDoneEvent:
  190. if v.HasLayer(confirmModalLayerName) {
  191. v.RemoveLayer(confirmModalLayerName)
  192. if v.confirmModalPreviousFocus != nil {
  193. v.app.SetFocus(v.confirmModalPreviousFocus)
  194. }
  195. onDone := v.confirmModalDone
  196. v.confirmModalDone = nil
  197. v.confirmModalPreviousFocus = nil
  198. if onDone != nil {
  199. onDone(event.ButtonLabel)
  200. }
  201. return tview.RedrawCommand{}
  202. }
  203. case *tview.KeyEvent:
  204. redraw := tview.RedrawCommand{}
  205. switch {
  206. case keybind.Matches(event, v.cfg.Keybinds.FocusGuildsTree.Keybind):
  207. v.messageInput.removeMentionsList()
  208. v.focusGuildsTree()
  209. return redraw
  210. case keybind.Matches(event, v.cfg.Keybinds.FocusMessagesList.Keybind):
  211. v.messageInput.removeMentionsList()
  212. v.app.SetFocus(v.messagesList)
  213. return redraw
  214. case keybind.Matches(event, v.cfg.Keybinds.FocusMessageInput.Keybind):
  215. v.focusMessageInput()
  216. return redraw
  217. case keybind.Matches(event, v.cfg.Keybinds.FocusPrevious.Keybind):
  218. v.focusPrevious()
  219. return redraw
  220. case keybind.Matches(event, v.cfg.Keybinds.FocusNext.Keybind):
  221. v.focusNext()
  222. return redraw
  223. case keybind.Matches(event, v.cfg.Keybinds.Logout.Keybind):
  224. return tview.BatchCommand{v.closeState(), v.logout()}
  225. case keybind.Matches(event, v.cfg.Keybinds.ToggleGuildsTree.Keybind):
  226. v.toggleGuildsTree()
  227. return redraw
  228. case keybind.Matches(event, v.cfg.Keybinds.ToggleChannelsPicker.Keybind):
  229. v.togglePicker()
  230. return redraw
  231. }
  232. }
  233. cmd := v.Layers.HandleEvent(event)
  234. return v.consumeLayerCommands(cmd)
  235. }
  236. func (v *Model) consumeLayerCommands(command tview.Command) tview.Command {
  237. if command == nil {
  238. return nil
  239. }
  240. var commands []tview.Command
  241. switch c := command.(type) {
  242. case tview.BatchCommand:
  243. commands = c
  244. default:
  245. commands = []tview.Command{c}
  246. }
  247. remaining := make([]tview.Command, 0, len(commands))
  248. for _, cmd := range commands {
  249. switch c := cmd.(type) {
  250. case layers.OpenLayerCommand:
  251. if v.HasLayer(c.Name) {
  252. v.ShowLayer(c.Name).SendToFront(c.Name)
  253. }
  254. continue
  255. case layers.CloseLayerCommand:
  256. if v.HasLayer(c.Name) {
  257. v.HideLayer(c.Name)
  258. }
  259. continue
  260. case layers.ToggleLayerCommand:
  261. if v.HasLayer(c.Name) {
  262. if v.GetVisible(c.Name) {
  263. v.HideLayer(c.Name)
  264. } else {
  265. v.ShowLayer(c.Name).SendToFront(c.Name)
  266. }
  267. }
  268. continue
  269. }
  270. remaining = append(remaining, cmd)
  271. }
  272. if len(remaining) == 0 {
  273. return nil
  274. }
  275. if len(remaining) == 1 {
  276. return remaining[0]
  277. }
  278. return tview.BatchCommand(remaining)
  279. }
  280. func (v *Model) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
  281. v.confirmModalPreviousFocus = v.app.GetFocus()
  282. v.confirmModalDone = onDone
  283. modal := tview.NewModal().
  284. SetText(prompt).
  285. AddButtons(buttons)
  286. v.
  287. AddLayer(
  288. ui.Centered(modal, 0, 0),
  289. layers.WithName(confirmModalLayerName),
  290. layers.WithResize(true),
  291. layers.WithVisible(true),
  292. layers.WithOverlay(),
  293. ).
  294. SendToFront(confirmModalLayerName)
  295. }
  296. func (v *Model) onReadUpdate(event *read.UpdateEvent) {
  297. v.app.QueueUpdateDraw(func() {
  298. // Use indexed node lookup to avoid walking the whole tree on every read
  299. // event. This runs frequently while reading/typing across channels.
  300. if event.GuildID.IsValid() {
  301. if guildNode := v.guildsTree.findNodeByReference(event.GuildID); guildNode != nil {
  302. v.guildsTree.setNodeLineStyle(guildNode, v.guildsTree.getGuildNodeStyle(event.GuildID))
  303. }
  304. }
  305. // Channel style is always updated for the target channel regardless of
  306. // whether it's in a guild or DM.
  307. if channelNode := v.guildsTree.findNodeByReference(event.ChannelID); channelNode != nil {
  308. v.guildsTree.setNodeLineStyle(channelNode, v.guildsTree.getChannelNodeStyle(event.ChannelID))
  309. }
  310. })
  311. }
  312. func (v *Model) clearTypers() {
  313. v.typersMu.Lock()
  314. for _, timer := range v.typers {
  315. timer.Stop()
  316. }
  317. clear(v.typers)
  318. v.typersMu.Unlock()
  319. v.updateFooter()
  320. }
  321. func (v *Model) addTyper(userID discord.UserID) {
  322. v.typersMu.Lock()
  323. typer, ok := v.typers[userID]
  324. if ok {
  325. typer.Reset(typingDuration)
  326. } else {
  327. v.typers[userID] = time.AfterFunc(typingDuration, func() {
  328. v.removeTyper(userID)
  329. })
  330. }
  331. v.typersMu.Unlock()
  332. v.updateFooter()
  333. }
  334. func (v *Model) removeTyper(userID discord.UserID) {
  335. v.typersMu.Lock()
  336. if typer, ok := v.typers[userID]; ok {
  337. typer.Stop()
  338. delete(v.typers, userID)
  339. }
  340. v.typersMu.Unlock()
  341. v.updateFooter()
  342. }
  343. func (v *Model) updateFooter() {
  344. selectedChannel := v.SelectedChannel()
  345. if selectedChannel == nil {
  346. return
  347. }
  348. guildID := selectedChannel.GuildID
  349. v.typersMu.RLock()
  350. defer v.typersMu.RUnlock()
  351. var footer string
  352. if len(v.typers) > 0 {
  353. var names []string
  354. for userID := range v.typers {
  355. var name string
  356. if guildID.IsValid() {
  357. member, err := v.state.Cabinet.Member(guildID, userID)
  358. if err != nil {
  359. slog.Error("failed to get member from state", "err", err, "guild_id", guildID, "user_id", userID)
  360. continue
  361. }
  362. if member.Nick != "" {
  363. name = member.Nick
  364. } else {
  365. name = member.User.DisplayOrUsername()
  366. }
  367. } else {
  368. for _, recipient := range selectedChannel.DMRecipients {
  369. if recipient.ID == userID {
  370. name = recipient.DisplayOrUsername()
  371. break
  372. }
  373. }
  374. }
  375. if name != "" {
  376. names = append(names, name)
  377. }
  378. }
  379. switch len(names) {
  380. case 1:
  381. footer = names[0] + " is typing..."
  382. case 2:
  383. footer = fmt.Sprintf("%s and %s are typing...", names[0], names[1])
  384. case 3:
  385. footer = fmt.Sprintf("%s, %s, and %s are typing...", names[0], names[1], names[2])
  386. default:
  387. footer = "Several people are typing..."
  388. }
  389. }
  390. go v.app.QueueUpdateDraw(func() { v.messagesList.SetFooter(footer) })
  391. }