aboutsummaryrefslogtreecommitdiff
path: root/telegram
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
parentc141c4ad2bebe51562be0a7cfe0671f34b0a49fb (diff)
Reflect Telegram edits natively by nativeedits option
Diffstat (limited to 'telegram')
-rw-r--r--telegram/client.go3
-rw-r--r--telegram/commands.go14
-rw-r--r--telegram/handlers.go51
-rw-r--r--telegram/utils.go29
4 files changed, 81 insertions, 16 deletions
diff --git a/telegram/client.go b/telegram/client.go
index 38dff4c..79f27d5 100644
--- a/telegram/client.go
+++ b/telegram/client.go
@@ -45,6 +45,7 @@ type Client struct {
DelayedStatusesLock sync.Mutex
lastMsgHashes map[int64]uint64
+ lastMsgIds map[int64]string
msgHashSeed maphash.Seed
locks clientLocks
@@ -58,6 +59,7 @@ type clientLocks struct {
outboxLock sync.Mutex
editOutboxLock sync.Mutex
lastMsgHashesLock sync.Mutex
+ lastMsgIdsLock sync.RWMutex
authorizerReadLock sync.Mutex
authorizerWriteLock sync.Mutex
@@ -119,6 +121,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
options: options,
DelayedStatuses: make(map[int64]*DelayedStatus),
lastMsgHashes: make(map[int64]uint64),
+ lastMsgIds: make(map[int64]string),
msgHashSeed: maphash.MakeSeed(),
locks: clientLocks{
chatMessageLocks: make(map[int64]*sync.Mutex),
diff --git a/telegram/commands.go b/telegram/commands.go
index b5c856e..9251ebb 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -201,6 +201,7 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
strconv.FormatInt(message.Id, 10),
c.xmpp,
reply,
+ "",
false,
false,
)
@@ -380,9 +381,20 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
}
case "config":
if len(args) > 1 {
+ var msg string
if gateway.MessageOutgoingPermissionVersion == 0 && args[0] == "carbons" && args[1] == "true" {
return "The server did not allow to enable carbons"
}
+ if !c.Session.RawMessages && args[0] == "nativeedits" && args[1] == "true" {
+ return "nativeedits only works with rawmessages as of yet, enable it first"
+ }
+ if c.Session.NativeEdits && args[0] == "rawmessages" && args[1] == "false" {
+ _, err := c.Session.Set("nativeedits", "false")
+ if err != nil {
+ return err.Error()
+ }
+ msg = "Automatically disabling nativeedits too...\n"
+ }
value, err := c.Session.Set(args[0], args[1])
if err != nil {
@@ -390,7 +402,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
}
gateway.DirtySessions = true
- return fmt.Sprintf("%s set to %s", args[0], value)
+ return fmt.Sprintf("%s%s set to %s", msg, args[0], value)
} else if len(args) > 0 {
value, err := c.Session.Get(args[0])
if err != nil {
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)
}
}
}
diff --git a/telegram/utils.go b/telegram/utils.go
index 966c5c2..2082645 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -911,14 +911,17 @@ func (c *Client) countCharsInLines(lines *[]string) (count int) {
return
}
+func (c *Client) isCarbonsEnabled() bool {
+ return gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons
+}
+
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string) (string, *gateway.Reply) {
isPM, err := c.IsPM(message.ChatId)
if err != nil {
log.Errorf("Could not determine if chat is PM: %v", err)
}
- isCarbonsEnabled := gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons
// with carbons, hide for all messages in PM and only for outgoing in group chats
- hideSender := isCarbonsEnabled && (message.IsOutgoing || isPM)
+ hideSender := c.isCarbonsEnabled() && (message.IsOutgoing || isPM)
prefix := []string{}
// message direction
@@ -1007,7 +1010,7 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File {
// ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
- isCarbon := gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons && message.IsOutgoing
+ isCarbon := c.isCarbonsEnabled() && message.IsOutgoing
jids := c.getCarbonFullJids(isCarbon, "")
var text, oob, auxText string
@@ -1083,11 +1086,12 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
sChatId := strconv.FormatInt(chatId, 10)
for _, jid := range jids {
- gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, isCarbon, c.Session.Receipts)
+ gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, "", isCarbon, c.Session.Receipts)
if auxText != "" {
- gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, isCarbon, c.Session.Receipts)
+ gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, "", isCarbon, c.Session.Receipts)
}
}
+ c.UpdateLastChatMessageId(chatId, sId)
}
// MarkAsRead marks a message as read
@@ -1588,6 +1592,21 @@ func (c *Client) hasLastMessageHashChanged(chatId, messageId int64, content clie
return !ok || oldHash != newHash
}
+func (c *Client) UpdateLastChatMessageId(chatId int64, messageId string) {
+ c.locks.lastMsgIdsLock.Lock()
+ defer c.locks.lastMsgIdsLock.Unlock()
+
+ c.lastMsgIds[chatId] = messageId
+}
+
+func (c *Client) getLastChatMessageId(chatId int64) (string, bool) {
+ c.locks.lastMsgIdsLock.RLock()
+ defer c.locks.lastMsgIdsLock.RUnlock()
+
+ xmppId, ok := c.lastMsgIds[chatId]
+ return xmppId, ok
+}
+
func (c *Client) getFormatter() formatter.MarkupModeType {
return formatter.MarkupModeXEP0393
}