Преглед на файлове

feat: add action to delete selected message (#164)

ayntgl преди 3 години
родител
ревизия
205c9dacd6
променени са 2 файла, в които са добавени 35 реда и са изтрити 6 реда
  1. 8 4
      ui/channels.go
  2. 27 2
      ui/messages.go

+ 8 - 4
ui/channels.go

@@ -62,12 +62,16 @@ func (ctv *ChannelsTreeView) onSelected(n *tview.TreeNode) {
 
 		for i := len(ms) - 1; i >= 0; i-- {
 			ctv.app.SelectedChannel.Messages = append(ctv.app.SelectedChannel.Messages, ms[i])
-			_, err = ctv.app.MessagesTextView.Write(buildMessage(ctv.app, ms[i]))
-			if err != nil {
-				return
-			}
+			ctv.drawMessage(ms[i])
 		}
 
 		ctv.app.MessagesTextView.ScrollToEnd()
 	}()
 }
+
+func (ctv *ChannelsTreeView) drawMessage(m *astatine.Message) {
+	_, err := ctv.app.MessagesTextView.Write(buildMessage(ctv.app, m))
+	if err != nil {
+		return
+	}
+}

+ 27 - 2
ui/messages.go

@@ -118,7 +118,7 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
 		actionsList.SetBorder(true)
 		actionsList.SetBorderPadding(0, 0, 1, 1)
 
-		// If the client user has `SEND_MESSAGES` permission, add the appropriate actions to the list.
+		// 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) {
 			actionsList.AddItem("Reply", "", 'r', func() {
 				mtv.app.MessageInputField.SetTitle("Replying to " + m.Author.String())
@@ -135,7 +135,17 @@ func (mtv *MessagesTextView) onInputCapture(e *tcell.EventKey) *tcell.EventKey {
 			})
 		}
 
-		// If the referenced message exists, add the appropriate actions to the list.
+		// 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) {
+			actionsList.AddItem("Delete", "", 'd', func() {
+				go mtv.deleteMessage(m)
+				mtv.app.
+					SetRoot(mtv.app.MainFlex, true).
+					SetFocus(mtv.app.MessagesTextView)
+			})
+		}
+
+		// 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)
@@ -251,6 +261,21 @@ func (mtv *MessagesTextView) openAttachment(as []*astatine.MessageAttachment) er
 	return nil
 }
 
+func (mtv *MessagesTextView) deleteMessage(m *astatine.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.Session.ChannelMessageDelete(m.ChannelID, m.ID)
+	if err != nil {
+		return
+	}
+
+	for _, m := range mtv.app.SelectedChannel.Messages {
+		mtv.app.ChannelsTreeView.drawMessage(m)
+	}
+}
+
 type MessageInputField struct {
 	*tview.InputField
 	app *App