| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package ui
- import (
- "io"
- "net/http"
- "os"
- "path/filepath"
- "regexp"
- "github.com/atotto/clipboard"
- "github.com/diamondburned/arikawa/v3/discord"
- "github.com/rivo/tview"
- "github.com/skratchdot/open-golang/open"
- )
- var linkRegex = regexp.MustCompile("https?://.+")
- type ActionsView struct {
- *tview.List
- core *Core
- message *discord.Message
- }
- func newActionsView(c *Core, m *discord.Message) *ActionsView {
- v := &ActionsView{
- List: tview.NewList(),
- core: c,
- message: m,
- }
- v.ShowSecondaryText(false)
- v.SetDoneFunc(func() {
- c.App.SetRoot(c.View, true)
- c.App.SetFocus(c.MessagesView)
- })
- isDM := channelIsInDMCategory(c.ChannelsView.selectedChannel)
- // If the client user has the `SEND_MESSAGES` permission, add "Reply" and "Mention Reply" actions.
- if isDM || !isDM && hasPermission(c.State, c.ChannelsView.selectedChannel.ID, discord.PermissionSendMessages) {
- v.AddItem("Reply", "", 'r', v.replyAction)
- v.AddItem("Mention Reply", "", 'R', v.mentionReplyAction)
- }
- // If the referenced message exists, add a new action to select the reply.
- if m.ReferencedMessage != nil {
- v.AddItem("Select Reply", "", 'm', v.selectReplyAction)
- }
- // If the content of the message contains link(s), add the appropriate actions.
- links := linkRegex.FindAllString(m.Content, -1)
- if len(links) != 0 {
- v.AddItem("Open Link", "", 'l', func() {
- for _, l := range links {
- go open.Run(l)
- }
- c.App.SetRoot(c.View, true)
- c.App.SetFocus(c.MessagesView)
- })
- }
- // If the message contains attachments, add the appropriate actions to the actions view.
- if len(m.Attachments) != 0 {
- v.AddItem("Open Attachment", "", 'o', v.openAttachmentAction)
- v.AddItem("Download Attachment", "", 'd', v.downloadAttachmentAction)
- }
- me, _ := c.State.MeStore.Me()
- // If the client user has the `MANAGE_MESSAGES` permission, add a new action to delete the message.
- if (isDM && m.Author.ID == me.ID) || (!isDM && hasPermission(c.State, c.ChannelsView.selectedChannel.ID, discord.PermissionManageMessages)) {
- v.AddItem("Delete", "", 'd', v.deleteAction)
- }
- v.AddItem("Copy Content", "", 'c', v.copyContentAction)
- v.AddItem("Copy ID", "", 'i', v.copyIDAction)
- v.AddItem("Copy Link", "", 'k', v.copyLinkAction)
- v.SetTitle("Press the Escape key to close")
- v.SetTitleAlign(tview.AlignLeft)
- v.SetBorder(true)
- v.SetBorderPadding(0, 0, 1, 1)
- return v
- }
- func (v *ActionsView) replyAction() {
- v.core.InputView.SetTitle("Replying to " + v.message.Author.Tag())
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.InputView)
- }
- func (v *ActionsView) mentionReplyAction() {
- v.core.InputView.SetTitle("[@] Replying to " + v.message.Author.Tag())
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.InputView)
- }
- func (v *ActionsView) selectReplyAction() {
- ms, err := v.core.State.Cabinet.Messages(v.message.ChannelID)
- if err != nil {
- return
- }
- v.core.MessagesView.selectedMessage, _ = findMessageByID(ms, v.message.ReferencedMessage.ID)
- v.core.MessagesView.
- Highlight(v.message.ReferencedMessage.ID.String()).
- ScrollToHighlight()
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.MessagesView)
- }
- func (v *ActionsView) openAttachmentAction() {
- for _, a := range v.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())
- }
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.MessagesView)
- }
- func (v *ActionsView) downloadAttachmentAction() {
- for _, a := range v.message.Attachments {
- path, err := os.UserHomeDir()
- if err != nil {
- path = os.TempDir()
- }
- path = filepath.Join(path, "Downloads", a.Filename)
- f, err := os.Create(path)
- 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)
- }
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.MessagesView)
- }
- func (v *ActionsView) deleteAction() {
- v.core.MessagesView.Clear()
- err := v.core.State.MessageRemove(v.message.ChannelID, v.message.ID)
- if err != nil {
- return
- }
- err = v.core.State.DeleteMessage(v.message.ChannelID, v.message.ID, "Unknown")
- if err != nil {
- return
- }
- // The returned slice will be sorted from latest to oldest.
- ms, err := v.core.State.Cabinet.Messages(v.message.ChannelID)
- if err != nil {
- return
- }
- for i := len(ms) - 1; i >= 0; i-- {
- _, err = v.core.MessagesView.Write(buildMessage(v.core, ms[i]))
- if err != nil {
- return
- }
- }
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.MessagesView)
- }
- func (v *ActionsView) copyContentAction() {
- err := clipboard.WriteAll(v.message.Content)
- if err != nil {
- return
- }
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.MessagesView)
- }
- func (v *ActionsView) copyIDAction() {
- err := clipboard.WriteAll(v.message.ID.String())
- if err != nil {
- return
- }
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.MessagesView)
- }
- func (v *ActionsView) copyLinkAction() {
- err := clipboard.WriteAll(v.message.URL())
- if err != nil {
- return
- }
- v.core.App.SetRoot(v.core.View, true)
- v.core.App.SetFocus(v.core.MessagesView)
- }
|