mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 04:07:07 +00:00
Get rid of edited message hash comparison in favour of EditDate check
This commit is contained in:
parent
5f4165ac13
commit
06964d832e
5 changed files with 20 additions and 82 deletions
2
Makefile
2
Makefile
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
COMMIT := $(shell git rev-parse --short HEAD)
|
COMMIT := $(shell git rev-parse --short HEAD)
|
||||||
TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551"
|
TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551"
|
||||||
VERSION := "v1.12.2"
|
VERSION := "v1.12.3"
|
||||||
MAKEOPTS := "-j4"
|
MAKEOPTS := "-j4"
|
||||||
|
|
||||||
all:
|
all:
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import (
|
||||||
goxmpp "gosrc.io/xmpp"
|
goxmpp "gosrc.io/xmpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version string = "1.12.2"
|
var version string = "1.12.3"
|
||||||
var commit string
|
var commit string
|
||||||
|
|
||||||
var sm *goxmpp.StreamManager
|
var sm *goxmpp.StreamManager
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"hash/maphash"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -56,7 +55,6 @@ type Client struct {
|
||||||
|
|
||||||
lastMsgHashes map[int64]uint64
|
lastMsgHashes map[int64]uint64
|
||||||
lastMsgIds map[int64]string
|
lastMsgIds map[int64]string
|
||||||
msgHashSeed maphash.Seed
|
|
||||||
|
|
||||||
LastBotCmdString string
|
LastBotCmdString string
|
||||||
|
|
||||||
|
|
@ -149,7 +147,6 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
|
||||||
DelayedStatuses: make(map[int64]*DelayedStatus),
|
DelayedStatuses: make(map[int64]*DelayedStatus),
|
||||||
lastMsgHashes: make(map[int64]uint64),
|
lastMsgHashes: make(map[int64]uint64),
|
||||||
lastMsgIds: make(map[int64]string),
|
lastMsgIds: make(map[int64]string),
|
||||||
msgHashSeed: maphash.MakeSeed(),
|
|
||||||
XmppClientFeatures: make(map[string]*[]string),
|
XmppClientFeatures: make(map[string]*[]string),
|
||||||
AvatarHashes: make(map[int64]*HashedAvatar),
|
AvatarHashes: make(map[int64]*HashedAvatar),
|
||||||
locks: clientLocks{
|
locks: clientLocks{
|
||||||
|
|
|
||||||
|
|
@ -245,8 +245,6 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content)
|
|
||||||
|
|
||||||
var forceCmd bool
|
var forceCmd bool
|
||||||
if c.LastBotCmdString != "" && update.Message.IsOutgoing {
|
if c.LastBotCmdString != "" && update.Message.IsOutgoing {
|
||||||
if update.Message.Content.MessageContentType() == client.TypeMessageText {
|
if update.Message.Content.MessageContentType() == client.TypeMessageText {
|
||||||
|
|
@ -283,8 +281,6 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
|
||||||
|
|
||||||
markupFunction := c.getFormatter()
|
markupFunction := c.getFormatter()
|
||||||
|
|
||||||
defer c.updateLastMessageHash(update.ChatId, update.MessageId, update.NewContent)
|
|
||||||
|
|
||||||
log.Debugf("newContent: %#v", update.NewContent)
|
log.Debugf("newContent: %#v", update.NewContent)
|
||||||
|
|
||||||
lock := c.getChatMessageLock(update.ChatId)
|
lock := c.getChatMessageLock(update.ChatId)
|
||||||
|
|
@ -308,7 +304,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
|
||||||
return
|
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)
|
textContent := update.NewContent.(*client.MessageText)
|
||||||
log.Debugf("textContent: %#v", textContent.Text)
|
log.Debugf("textContent: %#v", textContent.Text)
|
||||||
|
|
||||||
|
|
@ -316,6 +312,23 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
|
||||||
sId := strconv.FormatInt(update.MessageId, 10)
|
sId := strconv.FormatInt(update.MessageId, 10)
|
||||||
var isCarbon bool
|
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
|
// use XEP-0308 edits only if the last message is edited for sure, fallback otherwise
|
||||||
if c.Session.NativeEdits {
|
if c.Session.NativeEdits {
|
||||||
lastXmppId, ok := c.getLastChatMessageId(update.ChatId)
|
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
|
var text strings.Builder
|
||||||
|
|
||||||
if replaceId == "" {
|
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())
|
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)
|
c.sendMarker(update.Message.ChatId, update.Message.Id, gateway.MarkerTypeReceived)
|
||||||
|
|
||||||
// clean uploaded files
|
// clean uploaded files
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,8 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"hash/maphash"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -1758,61 +1756,6 @@ func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []st
|
||||||
return jids
|
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) {
|
func (c *Client) UpdateLastChatMessageId(chatId int64, messageId string) {
|
||||||
c.locks.lastMsgIdsLock.Lock()
|
c.locks.lastMsgIdsLock.Lock()
|
||||||
defer c.locks.lastMsgIdsLock.Unlock()
|
defer c.locks.lastMsgIdsLock.Unlock()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue