messages.go 9.4 KB

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