aboutsummaryrefslogtreecommitdiff
path: root/telegram
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-12-04 00:14:32 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-12-04 00:14:32 +0300
commit0c0c8e777ac52def4c867eaf4fddc626b08a1171 (patch)
tree69dd429710cf7532ec22c4dde29efaf006e5a664 /telegram
parent9d31a390a8bf5c554eb0e934a2bfe2fa8feca8eb (diff)
Handle edits and deletions
Diffstat (limited to 'telegram')
-rw-r--r--telegram/handlers.go45
1 files changed, 45 insertions, 0 deletions
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)
+ }
+}