From 0013baa247b54286fb86119b91a5180f4334f463 Mon Sep 17 00:00:00 2001
From: bodqhrohro <bodqhrohro@gmail.com>
Date: Fri, 6 Dec 2019 01:21:39 +0200
Subject: Add delete command

---
 telegram/commands.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++----
 telegram/utils.go    |  2 +-
 2 files changed, 48 insertions(+), 5 deletions(-)

(limited to 'telegram')

diff --git a/telegram/commands.go b/telegram/commands.go
index d3a8ea7..86477cc 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -8,6 +8,7 @@ import (
 
 	"dev.narayana.im/narayana/telegabber/xmpp/gateway"
 
+	log "github.com/sirupsen/logrus"
 	"github.com/zelenin/go-tdlib/client"
 )
 
@@ -27,7 +28,7 @@ var transportCommands = map[string]command{
 }
 
 var chatCommands = map[string]command{
-	//"d":          command{"[n]", "delete your last message(s)"},
+	"d": command{"[n]", "delete your last message(s)"},
 	//"s":          command{"regex replace", "edit your last message"},
 	//"add":        command{"@username", "add @username to your chat list"},
 	//"join":       command{"https://t.me/invite_link", "join to chat via invite link"},
@@ -242,12 +243,54 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
 
 // ProcessChatCommand executes a command sent in a mapped chat
 // and returns a response and the status of command support
-func (c *Client) ProcessChatCommand(cmdline string) (string, bool) {
-	cmd, _ := parseCommand(cmdline)
+func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) {
+	cmd, args := parseCommand(cmdline)
 	switch cmd {
+	// delete last message(s)
+	case "d":
+		if c.me == nil {
+			return "@me is not initialized", true
+		}
+		var limit int32
+		if len(args) > 0 {
+			limit64, err := strconv.ParseInt(args[0], 10, 32)
+			if err != nil {
+				return err.Error(), true
+			}
+			limit = int32(limit64)
+		} else {
+			limit = 1
+		}
+
+		messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
+			ChatId:       chatID,
+			Limit:        limit,
+			SenderUserId: c.me.Id,
+			Filter:       &client.SearchMessagesFilterEmpty{},
+		})
+		if err != nil {
+			return err.Error(), true
+		}
+		log.Debugf("pre-deletion query: %#v %#v", messages, messages.Messages)
+
+		var messageIds []int64
+		for _, message := range messages.Messages {
+			messageIds = append(messageIds, message.Id)
+		}
+
+		_, err = c.client.DeleteMessages(&client.DeleteMessagesRequest{
+			ChatId:     chatID,
+			MessageIds: messageIds,
+			Revoke:     true,
+		})
+		if err != nil {
+			return err.Error(), true
+		}
 	case "help":
 		return helpString(helpTypeChat), true
+	default:
+		return "", false
 	}
 
-	return "", false
+	return "", true
 }
diff --git a/telegram/utils.go b/telegram/utils.go
index a708c28..b088069 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -413,7 +413,7 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
 func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) {
 	if strings.HasPrefix(text, "/") {
 		// try to execute a command
-		response, isCommand := c.ProcessChatCommand(text)
+		response, isCommand := c.ProcessChatCommand(chatID, text)
 		if response != "" {
 			gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
 		}
-- 
cgit v1.2.3