From 2d3e8ebbb663a95e750206bc66f309427c6750b9 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Sun, 8 Dec 2019 17:35:27 +0200 Subject: [PATCH] Add /delete command --- telegram/commands.go | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/telegram/commands.go b/telegram/commands.go index 6b1ccda..8718955 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -48,7 +48,7 @@ var chatCommands = map[string]command{ "ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"}, "leave": command{"", "leave current chat"}, "close": command{"", "close current secret chat"}, - //"delete": command{"", "delete current chat from chat list"}, + "delete": command{"", "delete current chat from chat list"}, //"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"}, } @@ -113,6 +113,15 @@ func parseCommand(cmdline string) (string, []string) { return bodyFields[0][1:], bodyFields[1:] } +func (c *Client) unsubscribe(chatID int64) { + gateway.SendPresence( + c.xmpp, + c.jid, + gateway.SPFrom(strconv.FormatInt(chatID, 10)), + gateway.SPType("unsubscribed"), + ) +} + func (c *Client) usernameOrIdToId(username string) (int32, error) { userID, err := strconv.ParseInt(username, 10, 32) // couldn't parse the id, try to lookup as a username @@ -170,12 +179,7 @@ func (c *Client) ProcessTransportCommand(cmdline string) string { } for id := range c.cache.chats { - gateway.SendPresence( - c.xmpp, - c.jid, - gateway.SPFrom(strconv.FormatInt(id, 10)), - gateway.SPType("unsubscribed"), - ) + c.unsubscribe(id) } c.Session.Login = "" @@ -553,12 +557,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return err.Error(), true } - gateway.SendPresence( - c.xmpp, - c.jid, - gateway.SPFrom(strconv.FormatInt(chatID, 10)), - gateway.SPType("unsubscribed"), - ) + c.unsubscribe(chatID) } // close secret chat case "close": @@ -577,13 +576,19 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return err.Error(), true } - gateway.SendPresence( - c.xmpp, - c.jid, - gateway.SPFrom(strconv.FormatInt(chatID, 10)), - gateway.SPType("unsubscribed"), - ) + c.unsubscribe(chatID) } + // delete current chat + case "delete": + _, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{ + ChatId: chatID, + RemoveFromChatList: true, + }) + if err != nil { + return err.Error(), true + } + + c.unsubscribe(chatID) case "help": return helpString(helpTypeChat), true default: