From 0c0c8e777ac52def4c867eaf4fddc626b08a1171 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 3 Dec 2019 23:14:32 +0200 Subject: [PATCH] Handle edits and deletions --- telegram/handlers.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/telegram/handlers.go b/telegram/handlers.go index d2d5b61..a2e45f7 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -1,8 +1,10 @@ package telegram import ( + "fmt" "strconv" "strings" + "sync" "dev.narayana.im/narayana/telegabber/xmpp/gateway" @@ -14,6 +16,22 @@ func uhOh() { log.Fatal("Update type mismatch") } +func int64SliceToStringSlice(ints []int64) []string { + strings := make([]string, len(ints)) + wg := sync.WaitGroup{} + + for i, xi := range ints { + wg.Add(1) + go func(i int, xi int64) { + strings[i] = strconv.FormatInt(xi, 10) + wg.Done() + }(i, xi) + } + wg.Wait() + + return strings +} + func (c *Client) updateHandler() { listener := c.client.GetListener() defer listener.Close() @@ -45,6 +63,18 @@ func (c *Client) updateHandler() { uhOh() } c.updateNewMessage(typedUpdate) + case client.TypeUpdateMessageContent: + typedUpdate, ok := update.(*client.UpdateMessageContent) + if !ok { + uhOh() + } + c.updateMessageContent(typedUpdate) + case client.TypeUpdateDeleteMessages: + typedUpdate, ok := update.(*client.UpdateDeleteMessages) + if !ok { + uhOh() + } + c.updateDeleteMessages(typedUpdate) default: // log only handled types continue @@ -155,3 +185,18 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { // forward message to XMPP gateway.SendMessage(c.jid, strconv.Itoa(int(update.Message.ChatId)), text, c.xmpp) } + +func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { + if update.NewContent.MessageContentType() == client.TypeMessageText { + textContent := update.NewContent.(*client.MessageText) + text := fmt.Sprintf("✎ %v | %s", update.MessageId, textContent.Text.Text) + gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) + } +} + +func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { + if update.IsPermanent { + text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",") + gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) + } +}