messages.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package ui
  2. import (
  3. "io"
  4. "net/http"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. "github.com/atotto/clipboard"
  11. "github.com/diamondburned/arikawa/v3/api"
  12. "github.com/diamondburned/arikawa/v3/discord"
  13. "github.com/diamondburned/arikawa/v3/utils/json/option"
  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.SetInputCapture(mtv.onInputCapture)
  32. mtv.SetChangedFunc(func() {
  33. mtv.app.Draw()
  34. })
  35. mtv.SetTitleAlign(tview.AlignLeft)
  36. mtv.SetBorder(true)
  37. mtv.SetBorderPadding(0, 0, 1, 1)
  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. // Messages should return messages ordered from latest to earliest.
  45. ms, err := mtv.app.State.Cabinet.Messages(mtv.app.SelectedChannel.ID)
  46. if err != nil || len(ms) == 0 {
  47. return nil
  48. }
  49. switch e.Name() {
  50. case mtv.app.Config.Keys.SelectPreviousMessage:
  51. // If there are no highlighted regions, select the latest (last) message in the messages TextView.
  52. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  53. mtv.app.SelectedMessage = 0
  54. } else {
  55. // If the selected message is the oldest (first) message, select the latest (last) message in the messages TextView.
  56. if mtv.app.SelectedMessage == len(ms)-1 {
  57. mtv.app.SelectedMessage = 0
  58. } else {
  59. mtv.app.SelectedMessage++
  60. }
  61. }
  62. mtv.app.MessagesTextView.
  63. Highlight(ms[mtv.app.SelectedMessage].ID.String()).
  64. ScrollToHighlight()
  65. return nil
  66. case mtv.app.Config.Keys.SelectNextMessage:
  67. // If there are no highlighted regions, select the latest (last) message in the messages TextView.
  68. if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
  69. mtv.app.SelectedMessage = 0
  70. } else {
  71. // If the selected message is the latest (last) message, select the oldest (first) message in the messages TextView.
  72. if mtv.app.SelectedMessage == 0 {
  73. mtv.app.SelectedMessage = len(ms) - 1
  74. } else {
  75. mtv.app.SelectedMessage--
  76. }
  77. }
  78. mtv.app.MessagesTextView.
  79. Highlight(ms[mtv.app.SelectedMessage].ID.String()).
  80. ScrollToHighlight()
  81. return nil
  82. case mtv.app.Config.Keys.SelectFirstMessage:
  83. mtv.app.SelectedMessage = len(ms) - 1
  84. mtv.app.MessagesTextView.
  85. Highlight(ms[mtv.app.SelectedMessage].ID.String()).
  86. ScrollToHighlight()
  87. return nil
  88. case mtv.app.Config.Keys.SelectLastMessage:
  89. mtv.app.SelectedMessage = 0
  90. mtv.app.MessagesTextView.
  91. Highlight(ms[mtv.app.SelectedMessage].ID.String()).
  92. ScrollToHighlight()
  93. return nil
  94. case mtv.app.Config.Keys.OpenMessageActionsList:
  95. hs := mtv.app.MessagesTextView.GetHighlights()
  96. if len(hs) == 0 {
  97. return nil
  98. }
  99. mID, err := discord.ParseSnowflake(hs[0])
  100. if err != nil {
  101. return nil
  102. }
  103. _, m := findMessageByID(ms, discord.MessageID(mID))
  104. if m == nil {
  105. return nil
  106. }
  107. actionsList := tview.NewList()
  108. actionsList.ShowSecondaryText(false)
  109. actionsList.SetDoneFunc(func() {
  110. mtv.app.
  111. SetRoot(mtv.app.MainFlex, true).
  112. SetFocus(mtv.app.MessagesTextView)
  113. })
  114. actionsList.SetTitle("Press the Escape key to close")
  115. actionsList.SetTitleAlign(tview.AlignLeft)
  116. actionsList.SetBorder(true)
  117. actionsList.SetBorderPadding(0, 0, 1, 1)
  118. // If the client user has `SEND_MESSAGES` permission, add a new action to reply to the message.
  119. if hasPermission(mtv.app.State, mtv.app.SelectedChannel.ID, discord.PermissionSendMessages) {
  120. actionsList.AddItem("Reply", "", 'r', func() {
  121. mtv.app.MessageInputField.SetTitle("Replying to " + m.Author.Tag())
  122. mtv.app.
  123. SetRoot(mtv.app.MainFlex, true).
  124. SetFocus(mtv.app.MessageInputField)
  125. })
  126. actionsList.AddItem("Mention Reply", "", 'R', func() {
  127. mtv.app.MessageInputField.SetTitle("[@] Replying to " + m.Author.Tag())
  128. mtv.app.
  129. SetRoot(mtv.app.MainFlex, true).
  130. SetFocus(mtv.app.MessageInputField)
  131. })
  132. }
  133. // If the client user has the `MANAGE_MESSAGES` permission, add a new action to delete the message.
  134. if hasPermission(mtv.app.State, mtv.app.SelectedChannel.ID, discord.PermissionManageMessages) {
  135. actionsList.AddItem("Delete", "", 'd', func() {
  136. go mtv.deleteMessage(*m)
  137. mtv.app.
  138. SetRoot(mtv.app.MainFlex, true).
  139. SetFocus(mtv.app.MessagesTextView)
  140. })
  141. }
  142. // If the referenced message exists, add a new action to select the reply.
  143. if m.ReferencedMessage != nil {
  144. actionsList.AddItem("Select Reply", "", 'm', func() {
  145. mtv.app.SelectedMessage, _ = findMessageByID(ms, m.ReferencedMessage.ID)
  146. mtv.app.MessagesTextView.
  147. Highlight(m.ReferencedMessage.ID.String()).
  148. ScrollToHighlight()
  149. mtv.app.
  150. SetRoot(mtv.app.MainFlex, true).
  151. SetFocus(mtv.app.MessagesTextView)
  152. })
  153. }
  154. // If the content of the message contains link(s), add the appropriate actions to the list.
  155. links := linkRegex.FindAllString(m.Content, -1)
  156. if len(links) != 0 {
  157. actionsList.AddItem("Open Link", "", 'l', func() {
  158. for _, l := range links {
  159. go open.Run(l)
  160. }
  161. })
  162. }
  163. // If the message contains attachments, add the appropriate actions to the actions list.
  164. if len(m.Attachments) != 0 {
  165. actionsList.AddItem("Download Attachment", "", 'd', func() {
  166. go mtv.downloadAttachment(m.Attachments)
  167. mtv.app.SetRoot(mtv.app.MainFlex, true)
  168. })
  169. actionsList.AddItem("Open Attachment", "", 'o', func() {
  170. go mtv.openAttachment(m.Attachments)
  171. mtv.app.SetRoot(mtv.app.MainFlex, true)
  172. })
  173. }
  174. actionsList.AddItem("Copy Content", "", 'c', func() {
  175. if err := clipboard.WriteAll(m.Content); err != nil {
  176. return
  177. }
  178. mtv.app.SetRoot(mtv.app.MainFlex, true)
  179. mtv.app.SetFocus(mtv.app.MessagesTextView)
  180. })
  181. actionsList.AddItem("Copy ID", "", 'i', func() {
  182. if err := clipboard.WriteAll(m.ID.String()); err != nil {
  183. return
  184. }
  185. mtv.app.SetRoot(mtv.app.MainFlex, true)
  186. mtv.app.SetFocus(mtv.app.MessagesTextView)
  187. })
  188. mtv.app.SetRoot(actionsList, true)
  189. return nil
  190. case "Esc":
  191. mtv.app.SelectedMessage = -1
  192. mtv.app.SetFocus(mtv.app.MainFlex)
  193. mtv.app.MessagesTextView.
  194. Clear().
  195. Highlight()
  196. return nil
  197. }
  198. return e
  199. }
  200. func (mtv *MessagesTextView) downloadAttachment(as []discord.Attachment) error {
  201. for _, a := range as {
  202. f, err := os.Create(filepath.Join(mtv.app.Config.AttachmentDownloadsDir, a.Filename))
  203. if err != nil {
  204. return err
  205. }
  206. defer f.Close()
  207. resp, err := http.Get(a.URL)
  208. if err != nil {
  209. return err
  210. }
  211. d, err := io.ReadAll(resp.Body)
  212. if err != nil {
  213. return err
  214. }
  215. f.Write(d)
  216. }
  217. return nil
  218. }
  219. func (mtv *MessagesTextView) openAttachment(as []discord.Attachment) error {
  220. for _, a := range as {
  221. cacheDirPath, _ := os.UserCacheDir()
  222. f, err := os.Create(filepath.Join(cacheDirPath, a.Filename))
  223. if err != nil {
  224. return err
  225. }
  226. defer f.Close()
  227. resp, err := http.Get(a.URL)
  228. if err != nil {
  229. return err
  230. }
  231. d, err := io.ReadAll(resp.Body)
  232. if err != nil {
  233. return err
  234. }
  235. f.Write(d)
  236. go open.Run(f.Name())
  237. }
  238. return nil
  239. }
  240. func (mtv *MessagesTextView) deleteMessage(m discord.Message) {
  241. mtv.Clear()
  242. err := mtv.app.State.MessageRemove(m.ChannelID, m.ID)
  243. if err != nil {
  244. return
  245. }
  246. err = mtv.app.State.DeleteMessage(m.ChannelID, m.ID, "Unknown")
  247. if err != nil {
  248. return
  249. }
  250. // The returned slice will be sorted from latest to oldest.
  251. ms, err := mtv.app.State.Messages(m.ChannelID, mtv.app.Config.MessagesLimit)
  252. if err != nil {
  253. return
  254. }
  255. for i := len(ms) - 1; i >= 0; i-- {
  256. _, err = mtv.app.MessagesTextView.Write(buildMessage(mtv.app, ms[i]))
  257. if err != nil {
  258. return
  259. }
  260. }
  261. }
  262. type MessageInput struct {
  263. *tview.InputField
  264. app *App
  265. }
  266. func NewMessageInput(app *App) *MessageInput {
  267. mi := &MessageInput{
  268. InputField: tview.NewInputField(),
  269. app: app,
  270. }
  271. mi.SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
  272. mi.SetPlaceholder("Message...")
  273. mi.SetPlaceholderStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor))
  274. mi.SetTitleAlign(tview.AlignLeft)
  275. mi.SetBorder(true)
  276. mi.SetBorderPadding(0, 0, 1, 1)
  277. mi.SetInputCapture(mi.onInputCapture)
  278. return mi
  279. }
  280. func (mi *MessageInput) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
  281. switch e.Name() {
  282. case "Enter":
  283. if mi.app.SelectedChannel == nil {
  284. return nil
  285. }
  286. t := strings.TrimSpace(mi.app.MessageInputField.GetText())
  287. if t == "" {
  288. return nil
  289. }
  290. ms, err := mi.app.State.Messages(mi.app.SelectedChannel.ID, mi.app.Config.MessagesLimit)
  291. if err != nil {
  292. return nil
  293. }
  294. if len(mi.app.MessagesTextView.GetHighlights()) != 0 {
  295. mID, err := discord.ParseSnowflake(mi.app.MessagesTextView.GetHighlights()[0])
  296. if err != nil {
  297. return nil
  298. }
  299. _, m := findMessageByID(ms, discord.MessageID(mID))
  300. d := api.SendMessageData{
  301. Content: t,
  302. Reference: m.Reference,
  303. AllowedMentions: &api.AllowedMentions{RepliedUser: option.False},
  304. }
  305. // If the title of the message InputField widget has "[@]" as a prefix, send the message as a reply and mention the replied user.
  306. if strings.HasPrefix(mi.app.MessageInputField.GetTitle(), "[@]") {
  307. d.AllowedMentions.RepliedUser = option.True
  308. }
  309. go mi.app.State.SendMessageComplex(m.ChannelID, d)
  310. mi.app.SelectedMessage = -1
  311. mi.app.MessagesTextView.Highlight()
  312. mi.app.MessageInputField.SetTitle("")
  313. } else {
  314. go mi.app.State.SendMessage(mi.app.SelectedChannel.ID, t)
  315. }
  316. mi.app.MessageInputField.SetText("")
  317. return nil
  318. case "Ctrl+V":
  319. text, _ := clipboard.ReadAll()
  320. text = mi.app.MessageInputField.GetText() + text
  321. mi.app.MessageInputField.SetText(text)
  322. return nil
  323. case "Esc":
  324. mi.app.MessageInputField.
  325. SetText("").
  326. SetTitle("")
  327. mi.app.SetFocus(mi.app.MainFlex)
  328. mi.app.SelectedMessage = -1
  329. mi.app.MessagesTextView.Highlight()
  330. return nil
  331. case mi.app.Config.Keys.OpenExternalEditor:
  332. e := os.Getenv("EDITOR")
  333. if e == "" {
  334. return nil
  335. }
  336. f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
  337. if err != nil {
  338. return nil
  339. }
  340. defer os.Remove(f.Name())
  341. cmd := exec.Command(e, f.Name())
  342. cmd.Stdin = os.Stdin
  343. cmd.Stdout = os.Stdout
  344. mi.app.Suspend(func() {
  345. err = cmd.Run()
  346. if err != nil {
  347. return
  348. }
  349. })
  350. b, err := io.ReadAll(f)
  351. if err != nil {
  352. return nil
  353. }
  354. mi.app.MessageInputField.SetText(string(b))
  355. return nil
  356. }
  357. return e
  358. }