From a3f6d5f77402bf4a4d3fa01297f9fd78cc69a3b3 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sat, 27 Apr 2024 00:31:21 -0400 Subject: [PATCH] Support nativeedits for rawmessages=false --- Makefile | 2 +- telegabber.go | 2 +- telegram/commands.go | 10 ------- telegram/handlers.go | 31 ++++++++++++++-------- telegram/utils.go | 60 ++++++++++++++++++++++++------------------ telegram/utils_test.go | 40 +++++++++++++++++++++++----- 6 files changed, 90 insertions(+), 55 deletions(-) diff --git a/Makefile b/Makefile index 3b7cd19..a90eb8f 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ COMMIT := $(shell git rev-parse --short HEAD) TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551" -VERSION := "v1.9.2" +VERSION := "v1.9.3" MAKEOPTS := "-j4" all: diff --git a/telegabber.go b/telegabber.go index 6c10d0d..c39d91d 100644 --- a/telegabber.go +++ b/telegabber.go @@ -16,7 +16,7 @@ import ( goxmpp "gosrc.io/xmpp" ) -var version string = "1.9.2" +var version string = "1.9.3" var commit string var sm *goxmpp.StreamManager diff --git a/telegram/commands.go b/telegram/commands.go index d9dc1f2..1ce316b 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -384,16 +384,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) 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 { diff --git a/telegram/handlers.go b/telegram/handlers.go index 05c12ca..6266292 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -309,34 +309,43 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { } } - message, err := c.client.GetMessage(&client.GetMessageRequest{ + message, messageErr := c.client.GetMessage(&client.GetMessageRequest{ ChatId: update.ChatId, MessageId: update.MessageId, }) - if err == nil { + 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) } - text := formatter.Format( - textContent.Text.Text, - textContent.Text.Entities, - markupFunction, - ) + var text strings.Builder if replaceId == "" { var editChar string if c.Session.AsciiArrows { - editChar = "e " + editChar = "e" } else { - editChar = "✎ " + editChar = "✎" } - text = editChar + fmt.Sprintf("%v | %s", update.MessageId, text) + text.WriteString(fmt.Sprintf("%s %v | ", editChar, update.MessageId)) + } else if prefix != "" { + text.WriteString(prefix) + text.WriteString(c.getPrefixSeparator(update.ChatId)) } + text.WriteString(formatter.Format( + textContent.Text.Text, + textContent.Text.Entities, + markupFunction, + )) + + sChatId := strconv.FormatInt(update.ChatId, 10) for _, jid := range jids { - gateway.SendMessage(jid, strconv.FormatInt(update.ChatId, 10), text, "e"+sId, c.xmpp, nil, replaceId, isCarbon, false) + gateway.SendMessage(jid, sChatId, text.String(), "e"+sId, c.xmpp, nil, replaceId, isCarbon, false) } } } diff --git a/telegram/utils.go b/telegram/utils.go index 7ab5765..4509d1a 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -915,7 +915,7 @@ 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) { +func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, suppressReply bool) (string, *gateway.Reply) { isPM, err := c.IsPM(message.ChatId) if err != nil { log.Errorf("Could not determine if chat is PM: %v", err) @@ -953,27 +953,32 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string, } // reply to - preview := true - reply, tgReply := c.getMessageReply(message, preview, false) + var reply *gateway.Reply + if !suppressReply { + preview := true + gwReply, tgReply := c.getMessageReply(message, preview, false) - if tgReply != nil { - var replyStart, replyEnd int + if tgReply != nil { + reply = gwReply - if len(prefix) > 0 { - replyStart = c.countCharsInLines(&prefix) + (len(prefix)-1)*len(messageHeaderSeparator) - } + var replyStart, replyEnd int - replyLine := "reply: " + c.formatMessageContent(preview, tgReply) - prefix = append(prefix, replyLine) + if len(prefix) > 0 { + replyStart = c.countCharsInLines(&prefix) + (len(prefix)-1)*len(messageHeaderSeparator) + } - replyEnd = replyStart + utf8.RuneCountInString(replyLine) - if len(prefix) > 0 { - replyEnd += len(messageHeaderSeparator) - } + replyLine := "reply: " + c.formatMessageContent(preview, tgReply) + prefix = append(prefix, replyLine) - if reply != nil { - reply.Start = uint64(replyStart) - reply.End = uint64(replyEnd) + replyEnd = replyStart + utf8.RuneCountInString(replyLine) + if len(prefix) > 0 { + replyEnd += len(messageHeaderSeparator) + } + + if reply != nil { + reply.Start = uint64(replyStart) + reply.End = uint64(replyEnd) + } } } @@ -1008,6 +1013,17 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File { return file } +// \n if it is groupchat and message is not empty +func (c *Client) getPrefixSeparator(chatId int64) string { + var separator string + if chatId < 0 { + separator = "\n" + } else if chatId > 0 { + separator = " | " + } + return separator +} + // 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 := c.isCarbonsEnabled() && message.IsOutgoing @@ -1051,21 +1067,15 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { } else if !c.Session.RawMessages { var newText strings.Builder - prefix, prefixReply := c.messageToPrefix(message, previewName, fileName) + prefix, prefixReply := c.messageToPrefix(message, previewName, fileName, false) reply = prefixReply replyObtained = true newText.WriteString(prefix) if text != "" { - // \n if it is groupchat and message is not empty if prefix != "" { - if chatId < 0 { - newText.WriteString("\n") - } else if chatId > 0 { - newText.WriteString(" | ") - } + newText.WriteString(c.getPrefixSeparator(chatId)) } - newText.WriteString(text) } text = newText.String() diff --git a/telegram/utils_test.go b/telegram/utils_test.go index fa9c107..005d17b 100644 --- a/telegram/utils_test.go +++ b/telegram/utils_test.go @@ -436,7 +436,7 @@ func TestMessageToPrefix1(t *testing.T) { }, }, } - prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "") + prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "", false) if prefix != "➡ 42 | fwd: ziz" { t.Errorf("Wrong prefix: %v", prefix) } @@ -454,7 +454,7 @@ func TestMessageToPrefix2(t *testing.T) { }, }, } - prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "") + prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "", false) if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" { t.Errorf("Wrong prefix: %v", prefix) } @@ -472,7 +472,7 @@ func TestMessageToPrefix3(t *testing.T) { }, }, } - prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg") + prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg", false) if prefix != "< 56 | fwd: (zuz) | file: a.jpg" { t.Errorf("Wrong prefix: %v", prefix) } @@ -486,7 +486,7 @@ func TestMessageToPrefix4(t *testing.T) { Id: 23, IsOutgoing: true, } - prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "") + prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false) if prefix != "> 23" { t.Errorf("Wrong prefix: %v", prefix) } @@ -504,7 +504,7 @@ func TestMessageToPrefix5(t *testing.T) { }, }, } - prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg") + prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg", false) if prefix != "< 560 | fwd: (zyz) | preview: h.jpg | file: a.jpg" { t.Errorf("Wrong prefix: %v", prefix) } @@ -530,7 +530,7 @@ func TestMessageToPrefix6(t *testing.T) { }, }, } - prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "") + prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false) if prefix != "> 23 | reply: ziz @ unknown contact: TDlib instance is offline | tist uz iz" { t.Errorf("Wrong prefix: %v", prefix) } @@ -556,7 +556,7 @@ func TestMessageToPrefix7(t *testing.T) { }, }, } - prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "") + prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false) if prefix != "> 23 | reply: (zaz) @ unknown contact: TDlib instance is offline | tist" { t.Errorf("Wrong prefix: %v", prefix) } @@ -565,6 +565,32 @@ func TestMessageToPrefix7(t *testing.T) { } } +func TestMessageToPrefix8(t *testing.T) { + message := client.Message{ + Id: 23, + ChatId: 42, + IsOutgoing: true, + ReplyTo: &client.MessageReplyToMessage{ + ChatId: 41, + Content: &client.MessageText{ + Text: &client.FormattedText{ + Text: "tist", + }, + }, + Origin: &client.MessageOriginChannel{ + AuthorSignature: "zuz", + }, + }, + } + prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", true) + if prefix != "> 23" { + t.Errorf("Wrong prefix: %v", prefix) + } + if gatewayReply != nil { + t.Errorf("Reply is not nil: %v", gatewayReply) + } +} + func GetSenderIdEmpty(t *testing.T) { message := client.Message{} senderId := (&Client{}).getSenderId(&message)