|
|
@@ -2,7 +2,6 @@ package ui
|
|
|
|
|
|
import (
|
|
|
"io"
|
|
|
- "io/ioutil"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
@@ -11,8 +10,9 @@ import (
|
|
|
"strings"
|
|
|
|
|
|
"github.com/atotto/clipboard"
|
|
|
- "github.com/ayntgl/astatine"
|
|
|
- "github.com/ayntgl/discordo/discord"
|
|
|
+ "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"
|
|
|
@@ -34,13 +34,15 @@ func NewMessagesTextView(app *App) *MessagesTextView {
|
|
|
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)
|
|
|
- mtv.SetInputCapture(mtv.onInputCapture)
|
|
|
+
|
|
|
return mtv
|
|
|
}
|
|
|
|
|
|
@@ -49,50 +51,57 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- ms := mtv.app.SelectedChannel.Messages
|
|
|
- if len(ms) == 0 {
|
|
|
+ // Messages should return messages ordered from latest to earliest.
|
|
|
+ ms, err := mtv.app.State.Cabinet.Messages(mtv.app.SelectedChannel.ID)
|
|
|
+ if err != nil || len(ms) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
switch e.Name() {
|
|
|
case mtv.app.Config.Keys.SelectPreviousMessage:
|
|
|
+ // If there are no highlighted regions, select the latest (last) message in the messages TextView.
|
|
|
if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
|
|
|
- mtv.app.SelectedMessage = len(ms) - 1
|
|
|
+ mtv.app.SelectedMessage = 0
|
|
|
} else {
|
|
|
- mtv.app.SelectedMessage--
|
|
|
- if mtv.app.SelectedMessage < 0 {
|
|
|
+ // If the selected message is the oldest (first) message, select the latest (last) message in the messages TextView.
|
|
|
+ if mtv.app.SelectedMessage == len(ms)-1 {
|
|
|
mtv.app.SelectedMessage = 0
|
|
|
+ } else {
|
|
|
+ mtv.app.SelectedMessage++
|
|
|
}
|
|
|
}
|
|
|
|
|
|
mtv.app.MessagesTextView.
|
|
|
- Highlight(ms[mtv.app.SelectedMessage].ID).
|
|
|
+ Highlight(ms[mtv.app.SelectedMessage].ID.String()).
|
|
|
ScrollToHighlight()
|
|
|
return nil
|
|
|
case mtv.app.Config.Keys.SelectNextMessage:
|
|
|
+ // If there are no highlighted regions, select the latest (last) message in the messages TextView.
|
|
|
if len(mtv.app.MessagesTextView.GetHighlights()) == 0 {
|
|
|
- mtv.app.SelectedMessage = len(ms) - 1
|
|
|
+ mtv.app.SelectedMessage = 0
|
|
|
} else {
|
|
|
- mtv.app.SelectedMessage++
|
|
|
- if mtv.app.SelectedMessage >= len(ms) {
|
|
|
+ // If the selected message is the latest (last) message, select the oldest (first) message in the messages TextView.
|
|
|
+ if mtv.app.SelectedMessage == 0 {
|
|
|
mtv.app.SelectedMessage = len(ms) - 1
|
|
|
+ } else {
|
|
|
+ mtv.app.SelectedMessage--
|
|
|
}
|
|
|
}
|
|
|
|
|
|
mtv.app.MessagesTextView.
|
|
|
- Highlight(ms[mtv.app.SelectedMessage].ID).
|
|
|
+ Highlight(ms[mtv.app.SelectedMessage].ID.String()).
|
|
|
ScrollToHighlight()
|
|
|
return nil
|
|
|
case mtv.app.Config.Keys.SelectFirstMessage:
|
|
|
- mtv.app.SelectedMessage = 0
|
|
|
+ mtv.app.SelectedMessage = len(ms) - 1
|
|
|
mtv.app.MessagesTextView.
|
|
|
- Highlight(ms[mtv.app.SelectedMessage].ID).
|
|
|
+ Highlight(ms[mtv.app.SelectedMessage].ID.String()).
|
|
|
ScrollToHighlight()
|
|
|
return nil
|
|
|
case mtv.app.Config.Keys.SelectLastMessage:
|
|
|
- mtv.app.SelectedMessage = len(ms) - 1
|
|
|
+ mtv.app.SelectedMessage = 0
|
|
|
mtv.app.MessagesTextView.
|
|
|
- Highlight(ms[mtv.app.SelectedMessage].ID).
|
|
|
+ Highlight(ms[mtv.app.SelectedMessage].ID.String()).
|
|
|
ScrollToHighlight()
|
|
|
return nil
|
|
|
case mtv.app.Config.Keys.OpenMessageActionsList:
|
|
|
@@ -101,7 +110,12 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- _, m := discord.FindMessageByID(mtv.app.SelectedChannel.Messages, hs[0])
|
|
|
+ mID, err := discord.ParseSnowflake(hs[0])
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ _, m := findMessageByID(ms, discord.MessageID(mID))
|
|
|
if m == nil {
|
|
|
return nil
|
|
|
}
|
|
|
@@ -119,16 +133,16 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
actionsList.SetBorderPadding(0, 0, 1, 1)
|
|
|
|
|
|
// If the client user has `SEND_MESSAGES` permission, add a new action to reply to the message.
|
|
|
- if discord.HasPermission(mtv.app.Session.State, mtv.app.SelectedChannel.ID, astatine.PermissionSendMessages) {
|
|
|
+ if hasPermission(mtv.app.State, mtv.app.SelectedChannel.ID, discord.PermissionSendMessages) {
|
|
|
actionsList.AddItem("Reply", "", 'r', func() {
|
|
|
- mtv.app.MessageInputField.SetTitle("Replying to " + m.Author.String())
|
|
|
+ mtv.app.MessageInputField.SetTitle("Replying to " + m.Author.Tag())
|
|
|
mtv.app.
|
|
|
SetRoot(mtv.app.MainFlex, true).
|
|
|
SetFocus(mtv.app.MessageInputField)
|
|
|
})
|
|
|
|
|
|
actionsList.AddItem("Mention Reply", "", 'R', func() {
|
|
|
- mtv.app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
|
|
|
+ mtv.app.MessageInputField.SetTitle("[@] Replying to " + m.Author.Tag())
|
|
|
mtv.app.
|
|
|
SetRoot(mtv.app.MainFlex, true).
|
|
|
SetFocus(mtv.app.MessageInputField)
|
|
|
@@ -136,9 +150,9 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
}
|
|
|
|
|
|
// If the client user has the `MANAGE_MESSAGES` permission, add a new action to delete the message.
|
|
|
- if discord.HasPermission(mtv.app.Session.State, mtv.app.SelectedChannel.ID, astatine.PermissionManageMessages) {
|
|
|
+ if hasPermission(mtv.app.State, mtv.app.SelectedChannel.ID, discord.PermissionManageMessages) {
|
|
|
actionsList.AddItem("Delete", "", 'd', func() {
|
|
|
- go mtv.deleteMessage(m)
|
|
|
+ go mtv.deleteMessage(*m)
|
|
|
mtv.app.
|
|
|
SetRoot(mtv.app.MainFlex, true).
|
|
|
SetFocus(mtv.app.MessagesTextView)
|
|
|
@@ -148,9 +162,9 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
// If the referenced message exists, add a new action to select the reply.
|
|
|
if m.ReferencedMessage != nil {
|
|
|
actionsList.AddItem("Select Reply", "", 'm', func() {
|
|
|
- mtv.app.SelectedMessage, _ = discord.FindMessageByID(mtv.app.SelectedChannel.Messages, m.ReferencedMessage.ID)
|
|
|
+ mtv.app.SelectedMessage, _ = findMessageByID(ms, m.ReferencedMessage.ID)
|
|
|
mtv.app.MessagesTextView.
|
|
|
- Highlight(m.ReferencedMessage.ID).
|
|
|
+ Highlight(m.ReferencedMessage.ID.String()).
|
|
|
ScrollToHighlight()
|
|
|
mtv.app.
|
|
|
SetRoot(mtv.app.MainFlex, true).
|
|
|
@@ -189,7 +203,7 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
mtv.app.SetFocus(mtv.app.MessagesTextView)
|
|
|
})
|
|
|
actionsList.AddItem("Copy ID", "", 'i', func() {
|
|
|
- if err := clipboard.WriteAll(m.ID); err != nil {
|
|
|
+ if err := clipboard.WriteAll(m.ID.String()); err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -211,7 +225,7 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
return e
|
|
|
}
|
|
|
|
|
|
-func (mtv *MessagesTextView) downloadAttachment(as []*astatine.MessageAttachment) error {
|
|
|
+func (mtv *MessagesTextView) downloadAttachment(as []discord.Attachment) error {
|
|
|
for _, a := range as {
|
|
|
f, err := os.Create(filepath.Join(mtv.app.Config.AttachmentDownloadsDir, a.Filename))
|
|
|
if err != nil {
|
|
|
@@ -224,7 +238,7 @@ func (mtv *MessagesTextView) downloadAttachment(as []*astatine.MessageAttachment
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- d, err := ioutil.ReadAll(resp.Body)
|
|
|
+ d, err := io.ReadAll(resp.Body)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -235,7 +249,7 @@ func (mtv *MessagesTextView) downloadAttachment(as []*astatine.MessageAttachment
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (mtv *MessagesTextView) openAttachment(as []*astatine.MessageAttachment) error {
|
|
|
+func (mtv *MessagesTextView) openAttachment(as []discord.Attachment) error {
|
|
|
for _, a := range as {
|
|
|
cacheDirPath, _ := os.UserCacheDir()
|
|
|
f, err := os.Create(filepath.Join(cacheDirPath, a.Filename))
|
|
|
@@ -249,7 +263,7 @@ func (mtv *MessagesTextView) openAttachment(as []*astatine.MessageAttachment) er
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- d, err := ioutil.ReadAll(resp.Body)
|
|
|
+ d, err := io.ReadAll(resp.Body)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -261,28 +275,40 @@ func (mtv *MessagesTextView) openAttachment(as []*astatine.MessageAttachment) er
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (mtv *MessagesTextView) deleteMessage(m *astatine.Message) {
|
|
|
+func (mtv *MessagesTextView) deleteMessage(m discord.Message) {
|
|
|
mtv.Clear()
|
|
|
|
|
|
- mtv.app.SelectedChannel.Messages = append(mtv.app.SelectedChannel.Messages[:mtv.app.SelectedMessage], mtv.app.SelectedChannel.Messages[mtv.app.SelectedMessage+1:]...)
|
|
|
+ err := mtv.app.State.MessageRemove(m.ChannelID, m.ID)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ err = mtv.app.State.DeleteMessage(m.ChannelID, m.ID, "Unknown")
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- err := mtv.app.Session.ChannelMessageDelete(m.ChannelID, m.ID)
|
|
|
+ // The returned slice will be sorted from latest to oldest.
|
|
|
+ ms, err := mtv.app.State.Messages(m.ChannelID, mtv.app.Config.MessagesLimit)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- for _, m := range mtv.app.SelectedChannel.Messages {
|
|
|
- mtv.app.ChannelsTreeView.drawMessage(m)
|
|
|
+ for i := len(ms) - 1; i >= 0; i-- {
|
|
|
+ _, err = mtv.app.MessagesTextView.Write(buildMessage(mtv.app, ms[i]))
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type MessageInputField struct {
|
|
|
+type MessageInput struct {
|
|
|
*tview.InputField
|
|
|
app *App
|
|
|
}
|
|
|
|
|
|
-func NewMessageInputField(app *App) *MessageInputField {
|
|
|
- mi := &MessageInputField{
|
|
|
+func NewMessageInput(app *App) *MessageInput {
|
|
|
+ mi := &MessageInput{
|
|
|
InputField: tview.NewInputField(),
|
|
|
app: app,
|
|
|
}
|
|
|
@@ -297,7 +323,7 @@ func NewMessageInputField(app *App) *MessageInputField {
|
|
|
return mi
|
|
|
}
|
|
|
|
|
|
-func (mi *MessageInputField) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
+func (mi *MessageInput) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
switch e.Name() {
|
|
|
case "Enter":
|
|
|
if mi.app.SelectedChannel == nil {
|
|
|
@@ -309,27 +335,37 @@ func (mi *MessageInputField) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|
|
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.MessagesTextView.GetHighlights()) != 0 {
|
|
|
- _, m := discord.FindMessageByID(mi.app.SelectedChannel.Messages, mi.app.MessagesTextView.GetHighlights()[0])
|
|
|
- d := &astatine.MessageSend{
|
|
|
+ mID, err := discord.ParseSnowflake(mi.app.MessagesTextView.GetHighlights()[0])
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ _, m := findMessageByID(ms, discord.MessageID(mID))
|
|
|
+ d := api.SendMessageData{
|
|
|
Content: t,
|
|
|
- Reference: m.Reference(),
|
|
|
- AllowedMentions: &astatine.MessageAllowedMentions{RepliedUser: false},
|
|
|
+ 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 = true
|
|
|
- } else {
|
|
|
- d.AllowedMentions.RepliedUser = false
|
|
|
+ d.AllowedMentions.RepliedUser = option.True
|
|
|
}
|
|
|
|
|
|
- go mi.app.Session.ChannelMessageSendComplex(m.ChannelID, d)
|
|
|
+ go mi.app.State.SendMessageComplex(m.ChannelID, d)
|
|
|
|
|
|
mi.app.SelectedMessage = -1
|
|
|
mi.app.MessagesTextView.Highlight()
|
|
|
|
|
|
mi.app.MessageInputField.SetTitle("")
|
|
|
} else {
|
|
|
- go mi.app.Session.ChannelMessageSend(mi.app.SelectedChannel.ID, t)
|
|
|
+ go mi.app.State.SendMessage(mi.app.SelectedChannel.ID, t)
|
|
|
}
|
|
|
|
|
|
mi.app.MessageInputField.SetText("")
|