messages.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package ui
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "github.com/atotto/clipboard"
  12. "github.com/ayntgl/discordgo"
  13. "github.com/ayntgl/discordo/discord"
  14. "github.com/gdamore/tcell/v2"
  15. "github.com/rivo/tview"
  16. )
  17. type MessagesTextView struct {
  18. *tview.TextView
  19. app *App
  20. }
  21. func NewMessagesTextView(app *App) *MessagesTextView {
  22. mtv := &MessagesTextView{
  23. TextView: tview.NewTextView(),
  24. app: app,
  25. }
  26. mtv.SetDynamicColors(true)
  27. mtv.SetRegions(true)
  28. mtv.SetWordWrap(true)
  29. mtv.SetChangedFunc(func() {
  30. mtv.app.Draw()
  31. })
  32. mtv.SetBorder(true)
  33. mtv.SetBorderPadding(0, 0, 1, 1)
  34. mtv.SetInputCapture(mtv.onInputCapture)
  35. return mtv
  36. }
  37. func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  38. if mtv.app.SelectedChannel == nil {
  39. return nil
  40. }
  41. ms := mtv.app.SelectedChannel.Messages
  42. if len(ms) == 0 {
  43. return nil
  44. }
  45. switch e.Name() {
  46. case mtv.app.Config.Keybindings.SelectPreviousMessage:
  47. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  48. mtv.app.SelectedMessage = len(ms) - 1
  49. } else {
  50. mtv.app.SelectedMessage--
  51. if mtv.app.SelectedMessage < 0 {
  52. mtv.app.SelectedMessage = 0
  53. }
  54. }
  55. mtv.app.MessagesTextView.
  56. Highlight(ms[mtv.app.SelectedMessage].ID).
  57. ScrollToHighlight()
  58. return nil
  59. case mtv.app.Config.Keybindings.SelectNextMessage:
  60. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  61. mtv.app.SelectedMessage = len(ms) - 1
  62. } else {
  63. mtv.app.SelectedMessage++
  64. if mtv.app.SelectedMessage >= len(ms) {
  65. mtv.app.SelectedMessage = len(ms) - 1
  66. }
  67. }
  68. mtv.app.MessagesTextView.
  69. Highlight(ms[mtv.app.SelectedMessage].ID).
  70. ScrollToHighlight()
  71. return nil
  72. case mtv.app.Config.Keybindings.SelectFirstMessage:
  73. mtv.app.SelectedMessage = 0
  74. mtv.app.MessagesTextView.
  75. Highlight(ms[mtv.app.SelectedMessage].ID).
  76. ScrollToHighlight()
  77. return nil
  78. case mtv.app.Config.Keybindings.SelectLastMessage:
  79. mtv.app.SelectedMessage = len(ms) - 1
  80. mtv.app.MessagesTextView.
  81. Highlight(ms[mtv.app.SelectedMessage].ID).
  82. ScrollToHighlight()
  83. return nil
  84. case mtv.app.Config.Keybindings.OpenAttachment:
  85. if ms[mtv.app.SelectedMessage].Attachments != nil {
  86. for _, a := range ms[mtv.app.SelectedMessage].Attachments {
  87. // We are caching the files, but files with the same name can still exist, so it ultimately does not matter
  88. t, _ := os.UserCacheDir()
  89. f, err := os.Create(filepath.Join(t, a.Filename))
  90. if err != nil {
  91. f.Close()
  92. return nil
  93. }
  94. response, err := http.Get(a.URL)
  95. if err != nil {
  96. f.Close()
  97. return nil
  98. }
  99. d, err := ioutil.ReadAll(response.Body)
  100. if err != nil {
  101. f.Close()
  102. return nil
  103. }
  104. f.Write(d)
  105. f.Close()
  106. if runtime.GOOS == "windows" {
  107. // On windows, `start` can do all the heavy lifting through `cmd`
  108. cmd := exec.Command("cmd", "/C start "+f.Name())
  109. err := cmd.Run()
  110. if err != nil {
  111. return nil
  112. }
  113. } else if runtime.GOOS == "linux" {
  114. // On linux, we'll assume xdg-open exists and try using that
  115. cmd := exec.Command("xdg-open", f.Name())
  116. err := cmd.Run()
  117. if err != nil {
  118. return nil
  119. }
  120. } else if runtime.GOOS == "darwin" {
  121. // On MacOS open should do the trick
  122. cmd := exec.Command("open", f.Name())
  123. err := cmd.Run()
  124. if err != nil {
  125. return nil
  126. }
  127. } else {
  128. // If we're on none of those platforms, I think I made some sort of fatal mistake
  129. return nil
  130. }
  131. }
  132. }
  133. return nil
  134. case mtv.app.Config.Keybindings.DownloadAttachment:
  135. if ms[mtv.app.SelectedMessage].Attachments != nil {
  136. // Download the files to the configured location
  137. for _, a := range ms[mtv.app.SelectedMessage].Attachments {
  138. f, err := os.Create(filepath.Join(mtv.app.Config.General.AttachmentDownloadsDir + a.Filename))
  139. if err != nil {
  140. f.Close()
  141. return nil
  142. }
  143. response, err := http.Get(a.URL)
  144. if err != nil {
  145. f.Close()
  146. return nil
  147. }
  148. d, err := ioutil.ReadAll(response.Body)
  149. if err != nil {
  150. f.Close()
  151. return nil
  152. }
  153. f.Write(d)
  154. f.Close()
  155. }
  156. }
  157. case mtv.app.Config.Keybindings.OpenMessageActionsList:
  158. messageActionsList := tview.NewList()
  159. hs := mtv.app.MessagesTextView.GetHighlights()
  160. if len(hs) == 0 {
  161. return nil
  162. }
  163. _, m := discord.FindMessageByID(mtv.app.SelectedChannel.Messages, hs[0])
  164. if m == nil {
  165. return nil
  166. }
  167. if discord.HasPermission(mtv.app.Session.State, mtv.app.SelectedChannel.ID, discordgo.PermissionSendMessages) {
  168. messageActionsList.
  169. AddItem("Reply", "", 'r', nil).
  170. AddItem("Mention Reply", "", 'R', nil)
  171. }
  172. if m.ReferencedMessage != nil {
  173. messageActionsList.AddItem("Select Reply", "", 'm', nil)
  174. }
  175. messageActionsList.
  176. ShowSecondaryText(false).
  177. AddItem("Copy Content", "", 'c', nil).
  178. AddItem("Copy ID", "", 'i', nil).
  179. SetDoneFunc(func() {
  180. mtv.app.
  181. SetRoot(mtv.app.MainFlex, true).
  182. SetFocus(mtv.app.MessagesTextView)
  183. }).
  184. SetSelectedFunc(func(_ int, mainText string, _ string, _ rune) {
  185. onMessageActionsListSelected(mtv.app, mainText, m)
  186. }).
  187. SetTitle("Press the Escape key to close").
  188. SetBorder(true)
  189. mtv.app.SetRoot(messageActionsList, true)
  190. return nil
  191. case "Esc":
  192. mtv.app.SelectedMessage = -1
  193. mtv.app.SetFocus(mtv.app.MainFlex)
  194. mtv.app.MessagesTextView.
  195. Clear().
  196. Highlight()
  197. return nil
  198. }
  199. return e
  200. }
  201. func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Message) {
  202. switch mainText {
  203. case "Copy Content":
  204. if err := clipboard.WriteAll(m.Content); err != nil {
  205. return
  206. }
  207. app.SetRoot(app.MainFlex, false)
  208. case "Copy ID":
  209. if err := clipboard.WriteAll(m.ID); err != nil {
  210. return
  211. }
  212. app.SetRoot(app.MainFlex, false)
  213. case "Reply":
  214. app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  215. app.
  216. SetRoot(app.MainFlex, false).
  217. SetFocus(app.MessageInputField)
  218. case "Mention Reply":
  219. app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  220. app.
  221. SetRoot(app.MainFlex, false).
  222. SetFocus(app.MessageInputField)
  223. case "Select Reply":
  224. app.SelectedMessage, _ = discord.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  225. app.MessagesTextView.
  226. Highlight(m.ReferencedMessage.ID).
  227. ScrollToHighlight()
  228. app.
  229. SetRoot(app.MainFlex, false).
  230. SetFocus(app.MessagesTextView)
  231. }
  232. }
  233. type MessageInputField struct {
  234. *tview.InputField
  235. app *App
  236. }
  237. func NewMessageInputField(app *App) *MessageInputField {
  238. mi := &MessageInputField{
  239. InputField: tview.NewInputField(),
  240. app: app,
  241. }
  242. mi.SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
  243. mi.SetPlaceholder("Message...")
  244. mi.SetPlaceholderStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor))
  245. mi.SetTitleAlign(tview.AlignLeft)
  246. mi.SetBorder(true)
  247. mi.SetBorderPadding(0, 0, 1, 1)
  248. mi.SetInputCapture(mi.onInputCapture)
  249. return mi
  250. }
  251. func (mi *MessageInputField) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  252. switch e.Name() {
  253. case "Enter":
  254. if mi.app.SelectedChannel == nil {
  255. return nil
  256. }
  257. t := strings.TrimSpace(mi.app.MessageInputField.GetText())
  258. if t == "" {
  259. return nil
  260. }
  261. if len(mi.app.MessagesTextView.GetHighlights()) != 0 {
  262. _, m := discord.FindMessageByID(mi.app.SelectedChannel.Messages, mi.app.MessagesTextView.GetHighlights()[0])
  263. d := &discordgo.MessageSend{
  264. Content: t,
  265. Reference: m.Reference(),
  266. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  267. }
  268. if strings.HasPrefix(mi.app.MessageInputField.GetTitle(), "[@]") {
  269. d.AllowedMentions.RepliedUser = true
  270. } else {
  271. d.AllowedMentions.RepliedUser = false
  272. }
  273. go mi.app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  274. mi.app.SelectedMessage = -1
  275. mi.app.MessagesTextView.Highlight()
  276. mi.app.MessageInputField.SetTitle("")
  277. } else {
  278. go mi.app.Session.ChannelMessageSend(mi.app.SelectedChannel.ID, t)
  279. }
  280. mi.app.MessageInputField.SetText("")
  281. return nil
  282. case "Ctrl+V":
  283. text, _ := clipboard.ReadAll()
  284. text = mi.app.MessageInputField.GetText() + text
  285. mi.app.MessageInputField.SetText(text)
  286. return nil
  287. case "Esc":
  288. mi.app.MessageInputField.
  289. SetText("").
  290. SetTitle("")
  291. mi.app.SetFocus(mi.app.MainFlex)
  292. mi.app.SelectedMessage = -1
  293. mi.app.MessagesTextView.Highlight()
  294. return nil
  295. case mi.app.Config.Keybindings.OpenExternalEditor:
  296. e := os.Getenv("EDITOR")
  297. if e == "" {
  298. return nil
  299. }
  300. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  301. if err != nil {
  302. return nil
  303. }
  304. defer os.Remove(f.Name())
  305. cmd := exec.Command(e, f.Name())
  306. cmd.Stdin = os.Stdin
  307. cmd.Stdout = os.Stdout
  308. mi.app.Suspend(func() {
  309. err = cmd.Run()
  310. if err != nil {
  311. return
  312. }
  313. })
  314. b, err := io.ReadAll(f)
  315. if err != nil {
  316. return nil
  317. }
  318. mi.app.MessageInputField.SetText(string(b))
  319. return nil
  320. }
  321. return e
  322. }