aboutsummaryrefslogtreecommitdiff
path: root/telegram/handlers.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2024-01-29 12:28:15 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2024-01-29 12:28:15 +0300
commitea004b7f7c11fa0ddf560317fd9d6f9b2869144a (patch)
tree96157b08b5db8fde4088fde0cf1515be0c73bf6a /telegram/handlers.go
parentc141c4ad2bebe51562be0a7cfe0671f34b0a49fb (diff)
Reflect Telegram edits natively by nativeedits option
Diffstat (limited to 'telegram/handlers.go')
-rw-r--r--telegram/handlers.go51
1 files changed, 41 insertions, 10 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go
index c715932..dfdd3d5 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -269,9 +269,9 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
c.SendMessageLock.Lock()
c.SendMessageLock.Unlock()
- xmppId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, update.ChatId, update.MessageId)
+ xmppId, xmppIdErr := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, update.ChatId, update.MessageId)
var ignoredResource string
- if err == nil {
+ if xmppIdErr == nil {
ignoredResource = c.popFromEditOutbox(xmppId)
} else {
log.Infof("Couldn't retrieve XMPP message ids for %v, an echo may happen", update.MessageId)
@@ -286,19 +286,50 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
if update.NewContent.MessageContentType() == client.TypeMessageText && c.hasLastMessageHashChanged(update.ChatId, update.MessageId, update.NewContent) {
textContent := update.NewContent.(*client.MessageText)
- var editChar string
- if c.Session.AsciiArrows {
- editChar = "e "
- } else {
- editChar = "✎ "
+ var replaceId string
+ sId := strconv.FormatInt(update.MessageId, 10)
+ var isCarbon bool
+
+ // use XEP-0308 edits only if the last message is edited for sure, fallback otherwise
+ if c.Session.NativeEdits {
+ lastXmppId, ok := c.getLastChatMessageId(update.ChatId)
+ if xmppIdErr != nil {
+ xmppId = sId
+ }
+ if ok && lastXmppId == xmppId {
+ replaceId = xmppId
+ message, err := c.client.GetMessage(&client.GetMessageRequest{
+ ChatId: update.ChatId,
+ MessageId: update.MessageId,
+ })
+ if err == nil {
+ isCarbon = c.isCarbonsEnabled() && message.IsOutgoing
+ } else {
+ log.Errorf("No message %v/%v found, cannot reliably determine if it's a carbon", update.ChatId, update.MessageId)
+ }
+ } else {
+ log.Infof("Mismatching message ids: %v %v, falling back to separate edit message", lastXmppId, xmppId)
+ }
}
- text := editChar + fmt.Sprintf("%v | %s", update.MessageId, formatter.Format(
+
+ text := formatter.Format(
textContent.Text.Text,
textContent.Text.Entities,
markupFunction,
- ))
+ )
+
+ if replaceId == "" {
+ var editChar string
+ if c.Session.AsciiArrows {
+ editChar = "e "
+ } else {
+ editChar = "✎ "
+ }
+ text = editChar + fmt.Sprintf("%v | %s", update.MessageId, text)
+ }
+
for _, jid := range jids {
- gateway.SendMessage(jid, strconv.FormatInt(update.ChatId, 10), text, "e"+strconv.FormatInt(update.MessageId, 10), c.xmpp, nil, false, false)
+ gateway.SendMessage(jid, strconv.FormatInt(update.ChatId, 10), text, "e"+sId, c.xmpp, nil, replaceId, isCarbon, false)
}
}
}