Get rid of edited message hash comparison in favour of EditDate check

This commit is contained in:
Bohdan Horbeshko 2025-06-07 03:24:48 -04:00
parent 5f4165ac13
commit 06964d832e
5 changed files with 20 additions and 82 deletions

View file

@ -2,7 +2,7 @@
COMMIT := $(shell git rev-parse --short HEAD)
TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551"
VERSION := "v1.12.2"
VERSION := "v1.12.3"
MAKEOPTS := "-j4"
all:

View file

@ -16,7 +16,7 @@ import (
goxmpp "gosrc.io/xmpp"
)
var version string = "1.12.2"
var version string = "1.12.3"
var commit string
var sm *goxmpp.StreamManager

View file

@ -2,7 +2,6 @@ package telegram
import (
"github.com/pkg/errors"
"hash/maphash"
"path/filepath"
"strconv"
"sync"
@ -56,7 +55,6 @@ type Client struct {
lastMsgHashes map[int64]uint64
lastMsgIds map[int64]string
msgHashSeed maphash.Seed
LastBotCmdString string
@ -149,7 +147,6 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
DelayedStatuses: make(map[int64]*DelayedStatus),
lastMsgHashes: make(map[int64]uint64),
lastMsgIds: make(map[int64]string),
msgHashSeed: maphash.MakeSeed(),
XmppClientFeatures: make(map[string]*[]string),
AvatarHashes: make(map[int64]*HashedAvatar),
locks: clientLocks{

View file

@ -245,8 +245,6 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
lock.Lock()
defer lock.Unlock()
c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content)
var forceCmd bool
if c.LastBotCmdString != "" && update.Message.IsOutgoing {
if update.Message.Content.MessageContentType() == client.TypeMessageText {
@ -283,8 +281,6 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
markupFunction := c.getFormatter()
defer c.updateLastMessageHash(update.ChatId, update.MessageId, update.NewContent)
log.Debugf("newContent: %#v", update.NewContent)
lock := c.getChatMessageLock(update.ChatId)
@ -308,7 +304,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
return
}
if update.NewContent.MessageContentType() == client.TypeMessageText && c.hasLastMessageHashChanged(update.ChatId, update.MessageId, update.NewContent) {
if update.NewContent.MessageContentType() == client.TypeMessageText {
textContent := update.NewContent.(*client.MessageText)
log.Debugf("textContent: %#v", textContent.Text)
@ -316,6 +312,23 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
sId := strconv.FormatInt(update.MessageId, 10)
var isCarbon bool
message, messageErr := c.client.GetMessage(&client.GetMessageRequest{
ChatId: update.ChatId,
MessageId: update.MessageId,
})
var prefix string
if messageErr == nil {
if message.EditDate == 0 {
return
}
isCarbon = c.isCarbonsEnabled() && message.IsOutgoing
// reply correction support in clients is suboptimal yet, so cut them out for now
prefix, _ = c.messageToPrefix(message, "", "", true)
} else {
log.Errorf("No message %v/%v found, cannot reliably determine if it is a carbon and if it is edited", update.ChatId, update.MessageId)
}
// 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)
@ -329,19 +342,6 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
}
}
message, messageErr := c.client.GetMessage(&client.GetMessageRequest{
ChatId: update.ChatId,
MessageId: update.MessageId,
})
var prefix string
if messageErr == nil {
isCarbon = c.isCarbonsEnabled() && message.IsOutgoing
// reply correction support in clients is suboptimal yet, so cut them out for now
prefix, _ = c.messageToPrefix(message, "", "", true)
} else {
log.Errorf("No message %v/%v found, cannot reliably determine if it's a carbon", update.ChatId, update.MessageId)
}
var text strings.Builder
if replaceId == "" {
@ -411,8 +411,6 @@ func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucc
log.Errorf("failed to replace %v with %v: %v", update.OldMessageId, update.Message.Id, err.Error())
}
c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content)
c.sendMarker(update.Message.ChatId, update.Message.Id, gateway.MarkerTypeReceived)
// clean uploaded files

View file

@ -4,10 +4,8 @@ import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"fmt"
"github.com/pkg/errors"
"hash/maphash"
"io"
"io/ioutil"
"net/http"
@ -1758,61 +1756,6 @@ func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []st
return jids
}
func (c *Client) calculateMessageHash(messageId int64, content client.MessageContent) uint64 {
var h maphash.Hash
h.SetSeed(c.msgHashSeed)
buf8 := make([]byte, 8)
binary.BigEndian.PutUint64(buf8, uint64(messageId))
h.Write(buf8)
if content != nil && content.MessageContentType() == client.TypeMessageText {
textContent, ok := content.(*client.MessageText)
if !ok {
uhOh()
}
if textContent.Text != nil {
h.WriteString(textContent.Text.Text)
for _, entity := range textContent.Text.Entities {
buf4 := make([]byte, 4)
binary.BigEndian.PutUint32(buf4, uint32(entity.Offset))
h.Write(buf4)
binary.BigEndian.PutUint32(buf4, uint32(entity.Length))
h.Write(buf4)
h.WriteString(entity.Type.TextEntityTypeType())
}
}
}
return h.Sum64()
}
func (c *Client) updateLastMessageHash(chatId, messageId int64, content client.MessageContent) {
c.locks.lastMsgHashesLock.Lock()
defer c.locks.lastMsgHashesLock.Unlock()
c.lastMsgHashes[chatId] = c.calculateMessageHash(messageId, content)
}
func (c *Client) hasLastMessageHashChanged(chatId, messageId int64, content client.MessageContent) bool {
c.locks.lastMsgHashesLock.Lock()
defer c.locks.lastMsgHashesLock.Unlock()
oldHash, ok := c.lastMsgHashes[chatId]
newHash := c.calculateMessageHash(messageId, content)
if !ok {
log.Warnf("Last message hash for chat %v does not exist", chatId)
}
log.WithFields(log.Fields{
"old hash": oldHash,
"new hash": newHash,
}).Info("Message hashes")
return !ok || oldHash != newHash
}
func (c *Client) UpdateLastChatMessageId(chatId int64, messageId string) {
c.locks.lastMsgIdsLock.Lock()
defer c.locks.lastMsgIdsLock.Unlock()