Handle edits and deletions

This commit is contained in:
bodqhrohro 2019-12-03 23:14:32 +02:00
parent 9d31a390a8
commit 0c0c8e777a

View file

@ -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)
}
}