diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-03-08 15:51:19 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-03-08 15:51:19 +0300 |
commit | b1135b070b751998cb98eded6d29bdd7b800ecb9 (patch) | |
tree | 7063960016850ca04587ac5a94e60f59f676de88 /telegram/utils.go | |
parent | 4a5b83dff5c568871d5624202e2ee27e5d0de242 (diff) |
Cut fallback quotes out
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index f06abd7..fe6ca14 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -30,6 +30,7 @@ var spaceRegex = regexp.MustCompile(`\s+`) var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n") const newlineChar string = "\n" +const messageHeaderSeparator string = " | " // GetContactByUsername resolves username to user id retrieves user and chat information func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) { @@ -710,7 +711,15 @@ func (c *Client) contentToFile(content client.MessageContent) (*client.File, *cl return nil, nil } -func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) string { +func (c *Client) countCharsInLines(lines *[]string) (count int) { + for _, line := range *lines { + count += len(line) + } + return +} + +func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) (string, int, int) { + var replyStart, replyEnd int prefix := []string{} // message direction var directionChar string @@ -734,7 +743,10 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string, } // reply to if message.ReplyToMessageId != 0 { - prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, replyMsg)) + replyStart = c.countCharsInLines(&prefix) + (len(prefix) - 1) * len(messageHeaderSeparator) + replyLine := "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, replyMsg) + prefix = append(prefix, replyLine) + replyEnd = replyStart + len(replyLine) + len(messageHeaderSeparator) } if message.ForwardInfo != nil { prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo)) @@ -748,7 +760,7 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string, prefix = append(prefix, "file: "+fileString) } - return strings.Join(prefix, " | ") + return strings.Join(prefix, messageHeaderSeparator), replyStart, replyEnd } func (c *Client) ensureDownloadFile(file *client.File) *client.File { @@ -805,19 +817,26 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { } text = oob } else if !c.Session.RawMessages { - var prefix strings.Builder - prefix.WriteString(c.messageToPrefix(message, previewName, fileName, replyMsg)) + var newText strings.Builder + + prefix, replyStart, replyEnd := c.messageToPrefix(message, previewName, fileName, replyMsg) + newText.WriteString(prefix) + if reply != nil { + reply.Start = uint64(replyStart) + reply.End = uint64(replyEnd) + } + if text != "" { // \n if it is groupchat and message is not empty if chatId < 0 { - prefix.WriteString("\n") + newText.WriteString("\n") } else if chatId > 0 { - prefix.WriteString(" | ") + newText.WriteString(" | ") } - prefix.WriteString(text) + newText.WriteString(text) } - text = prefix.String() + text = newText.String() } } } |