diff --git a/telegram/client.go b/telegram/client.go index e1b84fe..df983eb 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -2,7 +2,6 @@ package telegram import ( "github.com/pkg/errors" - "hash/maphash" "path/filepath" "strconv" "sync" @@ -82,7 +81,6 @@ type Client struct { lastMsgHashes map[int64]uint64 lastMsgIds map[int64]string - msgHashSeed maphash.Seed mucCache map[int64]*MUCState @@ -181,7 +179,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{ diff --git a/telegram/handlers.go b/telegram/handlers.go index d18d356..96b4dfe 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -253,8 +253,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 { @@ -291,8 +289,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) @@ -324,7 +320,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) @@ -332,6 +328,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 && !isMUC + // 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) @@ -345,19 +358,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 && !isMUC - // 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 == "" { @@ -474,8 +474,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 diff --git a/telegram/utils.go b/telegram/utils.go index ec84da7..f29963d 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -4,10 +4,8 @@ import ( "bytes" "crypto/sha1" "encoding/base64" - "encoding/binary" "fmt" "github.com/pkg/errors" - "hash/maphash" "io" "io/ioutil" "net/http" @@ -2407,61 +2405,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()