| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- package ui
- import (
- "io"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "regexp"
- "strings"
- "github.com/atotto/clipboard"
- "github.com/diamondburned/arikawa/v3/api"
- "github.com/diamondburned/arikawa/v3/discord"
- "github.com/diamondburned/arikawa/v3/utils/json/option"
- "github.com/gdamore/tcell/v2"
- "github.com/rivo/tview"
- "github.com/skratchdot/open-golang/open"
- )
- var linkRegex = regexp.MustCompile("https?://.+")
- type MessagesPanel struct {
- *tview.TextView
- app *App
- }
- func NewMessagesPanel(app *App) *MessagesPanel {
- mtv := &MessagesPanel{
- TextView: tview.NewTextView(),
- app: app,
- }
- mtv.SetDynamicColors(true)
- mtv.SetRegions(true)
- mtv.SetWordWrap(true)
- mtv.SetInputCapture(mtv.onInputCapture)
- mtv.SetChangedFunc(func() {
- mtv.app.Draw()
- })
- mtv.SetTitleAlign(tview.AlignLeft)
- mtv.SetBorder(true)
- mtv.SetBorderPadding(0, 0, 1, 1)
- return mtv
- }
- func (mp *MessagesPanel) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
- if mp.app.SelectedChannel == nil {
- return nil
- }
- // Messages should return messages ordered from latest to earliest.
- ms, err := mp.app.State.Cabinet.Messages(mp.app.SelectedChannel.ID)
- if err != nil || len(ms) == 0 {
- return nil
- }
- switch e.Name() {
- case mp.app.Config.Keys.SelectPreviousMessage:
- // If there are no highlighted regions, select the latest (last) message in the messages panel.
- if len(mp.app.MessagesPanel.GetHighlights()) == 0 {
- mp.app.SelectedMessage = 0
- } else {
- // If the selected message is the oldest (first) message, select the latest (last) message in the messages panel.
- if mp.app.SelectedMessage == len(ms)-1 {
- mp.app.SelectedMessage = 0
- } else {
- mp.app.SelectedMessage++
- }
- }
- mp.app.MessagesPanel.
- Highlight(ms[mp.app.SelectedMessage].ID.String()).
- ScrollToHighlight()
- return nil
- case mp.app.Config.Keys.SelectNextMessage:
- // If there are no highlighted regions, select the latest (last) message in the messages panel.
- if len(mp.app.MessagesPanel.GetHighlights()) == 0 {
- mp.app.SelectedMessage = 0
- } else {
- // If the selected message is the latest (last) message, select the oldest (first) message in the messages panel.
- if mp.app.SelectedMessage == 0 {
- mp.app.SelectedMessage = len(ms) - 1
- } else {
- mp.app.SelectedMessage--
- }
- }
- mp.app.MessagesPanel.
- Highlight(ms[mp.app.SelectedMessage].ID.String()).
- ScrollToHighlight()
- return nil
- case mp.app.Config.Keys.SelectFirstMessage:
- mp.app.SelectedMessage = len(ms) - 1
- mp.app.MessagesPanel.
- Highlight(ms[mp.app.SelectedMessage].ID.String()).
- ScrollToHighlight()
- return nil
- case mp.app.Config.Keys.SelectLastMessage:
- mp.app.SelectedMessage = 0
- mp.app.MessagesPanel.
- Highlight(ms[mp.app.SelectedMessage].ID.String()).
- ScrollToHighlight()
- return nil
- case mp.app.Config.Keys.OpenMessageActionsList:
- hs := mp.app.MessagesPanel.GetHighlights()
- if len(hs) == 0 {
- return nil
- }
- mID, err := discord.ParseSnowflake(hs[0])
- if err != nil {
- return nil
- }
- _, m := findMessageByID(ms, discord.MessageID(mID))
- if m == nil {
- return nil
- }
- actionsList := NewMessageActionsList(mp.app, m)
- mp.app.SetRoot(actionsList, true)
- return nil
- case "Esc":
- mp.app.SelectedMessage = -1
- mp.app.SetFocus(mp.app.MainFlex)
- mp.app.MessagesPanel.
- Clear().
- Highlight().
- SetTitle("")
- return nil
- }
- return e
- }
- type MessageActionsList struct {
- *tview.List
- app *App
- message *discord.Message
- }
- func NewMessageActionsList(app *App, m *discord.Message) *MessageActionsList {
- mal := &MessageActionsList{
- List: tview.NewList(),
- app: app,
- message: m,
- }
- mal.ShowSecondaryText(false)
- mal.SetDoneFunc(func() {
- app.
- SetRoot(app.MainFlex, true).
- SetFocus(app.MessagesPanel)
- })
- // If the client user has the `SEND_MESSAGES` permission, add "Reply" and "Mention Reply" actions.
- if hasPermission(app.State, app.SelectedChannel.ID, discord.PermissionSendMessages) {
- mal.AddItem("Reply", "", 'r', mal.replyAction)
- mal.AddItem("Mention Reply", "", 'R', mal.mentionReplyAction)
- }
- // If the referenced message exists, add a new action to select the reply.
- if m.ReferencedMessage != nil {
- mal.AddItem("Select Reply", "", 'm', mal.selectReplyAction)
- }
- // If the content of the message contains link(s), add the appropriate actions to the list.
- links := linkRegex.FindAllString(m.Content, -1)
- if len(links) != 0 {
- mal.AddItem("Open Link", "", 'l', func() {
- for _, l := range links {
- go open.Run(l)
- }
- app.SetRoot(app.MainFlex, true)
- app.SetFocus(app.MessagesPanel)
- })
- }
- // If the message contains attachments, add the appropriate actions to the actions list.
- if len(m.Attachments) != 0 {
- mal.AddItem("Open Attachment", "", 'o', mal.openAttachmentAction)
- mal.AddItem("Download Attachment", "", 'd', mal.downloadAttachmentAction)
- }
- // If the client user has the `MANAGE_MESSAGES` permission, add a new action to delete the message.
- if hasPermission(app.State, app.SelectedChannel.ID, discord.PermissionManageMessages) {
- mal.AddItem("Delete", "", 'd', mal.deleteAction)
- }
- mal.AddItem("Copy Content", "", 'c', mal.copyContentAction)
- mal.AddItem("Copy ID", "", 'i', mal.copyIDAction)
- mal.SetTitle("Press the Escape key to close")
- mal.SetTitleAlign(tview.AlignLeft)
- mal.SetBorder(true)
- mal.SetBorderPadding(0, 0, 1, 1)
- return mal
- }
- func (mal *MessageActionsList) replyAction() {
- mal.app.MessageInputField.SetTitle("Replying to " + mal.message.Author.Tag())
- mal.app.
- SetRoot(mal.app.MainFlex, true).
- SetFocus(mal.app.MessageInputField)
- }
- func (mal *MessageActionsList) mentionReplyAction() {
- mal.app.MessageInputField.SetTitle("[@] Replying to " + mal.message.Author.Tag())
- mal.app.
- SetRoot(mal.app.MainFlex, true).
- SetFocus(mal.app.MessageInputField)
- }
- func (mal *MessageActionsList) selectReplyAction() {
- ms, err := mal.app.State.Cabinet.Messages(mal.message.ChannelID)
- if err != nil {
- return
- }
- mal.app.SelectedMessage, _ = findMessageByID(ms, mal.message.ReferencedMessage.ID)
- mal.app.MessagesPanel.
- Highlight(mal.message.ReferencedMessage.ID.String()).
- ScrollToHighlight()
- mal.app.
- SetRoot(mal.app.MainFlex, true).
- SetFocus(mal.app.MessagesPanel)
- }
- func (mal *MessageActionsList) openAttachmentAction() {
- for _, a := range mal.message.Attachments {
- cacheDirPath, _ := os.UserCacheDir()
- f, err := os.Create(filepath.Join(cacheDirPath, a.Filename))
- if err != nil {
- return
- }
- defer f.Close()
- resp, err := http.Get(a.URL)
- if err != nil {
- return
- }
- d, err := io.ReadAll(resp.Body)
- if err != nil {
- return
- }
- f.Write(d)
- go open.Run(f.Name())
- }
- mal.app.
- SetRoot(mal.app.MainFlex, true).
- SetFocus(mal.app.MessagesPanel)
- }
- func (mal *MessageActionsList) downloadAttachmentAction() {
- for _, a := range mal.message.Attachments {
- f, err := os.Create(filepath.Join(mal.app.Config.AttachmentDownloadsDir, a.Filename))
- if err != nil {
- return
- }
- defer f.Close()
- resp, err := http.Get(a.URL)
- if err != nil {
- return
- }
- d, err := io.ReadAll(resp.Body)
- if err != nil {
- return
- }
- f.Write(d)
- }
- mal.app.
- SetRoot(mal.app.MainFlex, true).
- SetFocus(mal.app.MessagesPanel)
- }
- func (mal *MessageActionsList) deleteAction() {
- mal.app.MessagesPanel.Clear()
- err := mal.app.State.MessageRemove(mal.message.ChannelID, mal.message.ID)
- if err != nil {
- return
- }
- err = mal.app.State.DeleteMessage(mal.message.ChannelID, mal.message.ID, "Unknown")
- if err != nil {
- return
- }
- // The returned slice will be sorted from latest to oldest.
- ms, err := mal.app.State.Cabinet.Messages(mal.message.ChannelID)
- if err != nil {
- return
- }
- for i := len(ms) - 1; i >= 0; i-- {
- _, err = mal.app.MessagesPanel.Write(buildMessage(mal.app, ms[i]))
- if err != nil {
- return
- }
- }
- mal.app.
- SetRoot(mal.app.MainFlex, true).
- SetFocus(mal.app.MessagesPanel)
- }
- func (mal *MessageActionsList) copyContentAction() {
- err := clipboard.WriteAll(mal.message.Content)
- if err != nil {
- return
- }
- mal.app.SetRoot(mal.app.MainFlex, true)
- mal.app.SetFocus(mal.app.MessagesPanel)
- }
- func (mal *MessageActionsList) copyIDAction() {
- err := clipboard.WriteAll(mal.message.ID.String())
- if err != nil {
- return
- }
- mal.app.SetRoot(mal.app.MainFlex, true)
- mal.app.SetFocus(mal.app.MessagesPanel)
- }
- type MessageInput struct {
- *tview.InputField
- app *App
- }
- func NewMessageInput(app *App) *MessageInput {
- mi := &MessageInput{
- InputField: tview.NewInputField(),
- app: app,
- }
- mi.SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
- mi.SetPlaceholder("Message...")
- mi.SetPlaceholderStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor))
- mi.SetTitleAlign(tview.AlignLeft)
- mi.SetBorder(true)
- mi.SetBorderPadding(0, 0, 1, 1)
- mi.SetInputCapture(mi.onInputCapture)
- return mi
- }
- func (mi *MessageInput) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
- switch e.Name() {
- case "Enter":
- if mi.app.SelectedChannel == nil {
- return nil
- }
- t := strings.TrimSpace(mi.app.MessageInputField.GetText())
- if t == "" {
- return nil
- }
- ms, err := mi.app.State.Messages(mi.app.SelectedChannel.ID, mi.app.Config.MessagesLimit)
- if err != nil {
- return nil
- }
- if len(mi.app.MessagesPanel.GetHighlights()) != 0 {
- mID, err := discord.ParseSnowflake(mi.app.MessagesPanel.GetHighlights()[0])
- if err != nil {
- return nil
- }
- _, m := findMessageByID(ms, discord.MessageID(mID))
- d := api.SendMessageData{
- Content: t,
- Reference: m.Reference,
- AllowedMentions: &api.AllowedMentions{RepliedUser: option.False},
- }
- // If the title of the message InputField widget has "[@]" as a prefix, send the message as a reply and mention the replied user.
- if strings.HasPrefix(mi.app.MessageInputField.GetTitle(), "[@]") {
- d.AllowedMentions.RepliedUser = option.True
- }
- go mi.app.State.SendMessageComplex(m.ChannelID, d)
- mi.app.SelectedMessage = -1
- mi.app.MessagesPanel.Highlight()
- mi.app.MessageInputField.SetTitle("")
- } else {
- go mi.app.State.SendMessage(mi.app.SelectedChannel.ID, t)
- }
- mi.app.MessageInputField.SetText("")
- return nil
- case "Ctrl+V":
- text, _ := clipboard.ReadAll()
- text = mi.app.MessageInputField.GetText() + text
- mi.app.MessageInputField.SetText(text)
- return nil
- case "Esc":
- mi.app.MessageInputField.
- SetText("").
- SetTitle("")
- mi.app.SetFocus(mi.app.MainFlex)
- mi.app.SelectedMessage = -1
- mi.app.MessagesPanel.Highlight()
- return nil
- case mi.app.Config.Keys.OpenExternalEditor:
- e := os.Getenv("EDITOR")
- if e == "" {
- return nil
- }
- f, err := os.CreateTemp(os.TempDir(), "discordo-*.md")
- if err != nil {
- return nil
- }
- defer os.Remove(f.Name())
- cmd := exec.Command(e, f.Name())
- cmd.Stdin = os.Stdin
- cmd.Stdout = os.Stdout
- mi.app.Suspend(func() {
- err = cmd.Run()
- if err != nil {
- return
- }
- })
- b, err := io.ReadAll(f)
- if err != nil {
- return nil
- }
- mi.app.MessageInputField.SetText(string(b))
- return nil
- }
- return e
- }
|