Support chosen quotes in replies and replies from other chats

This commit is contained in:
Bohdan Horbeshko 2024-01-10 14:30:00 -05:00
parent f2807779aa
commit 4532748c84
3 changed files with 195 additions and 115 deletions

View file

@ -196,7 +196,7 @@ func (c *Client) unsubscribe(chatID int64) error {
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) { func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
for i := len(messages) - 1; i >= 0; i-- { for i := len(messages) - 1; i >= 0; i-- {
message := messages[i] message := messages[i]
reply, _ := c.getMessageReply(message) reply, _ := c.getMessageReply(message, false, true)
gateway.SendMessage( gateway.SendMessage(
c.jid, c.jid,

View file

@ -37,6 +37,14 @@ type VCardInfo struct {
Info string Info string
} }
type messageStub struct {
MessageId int64
ChatId int64
Sender string
Date int32
Text string
}
var errOffline = errors.New("TDlib instance is offline") var errOffline = errors.New("TDlib instance is offline")
var spaceRegex = regexp.MustCompile(`\s+`) var spaceRegex = regexp.MustCompile(`\s+`)
@ -342,18 +350,35 @@ func (c *Client) formatSender(message *client.Message) string {
return c.formatContact(c.getSenderId(message)) return c.formatContact(c.getSenderId(message))
} }
func (c *Client) getMessageReply(message *client.Message) (reply *gateway.Reply, replyMsg *client.Message) { func (c *Client) messageToStub(message *client.Message, preview bool, text string) *messageStub {
if message.ReplyTo != nil && message.ReplyTo.MessageReplyToType() == client.TypeMessageReplyToMessage { if text == "" {
replyTo, _ := message.ReplyTo.(*client.MessageReplyToMessage) text = c.messageContentToText(message.Content, message.ChatId, preview)
// TODO: support replies from other chats }
if message.ChatId != replyTo.ChatId { return &messageStub{
log.Warn("Reply from other/unknown chat") MessageId: message.Id,
log.Debugf("replyTo: %#v", replyTo) ChatId: message.ChatId,
return Sender: c.formatSender(message),
Date: message.Date,
Text: text,
}
} }
var err error func (c *Client) getMessageReply(message *client.Message, preview bool, noContent bool) (gatewayReply *gateway.Reply, tgReply *messageStub) {
replyMsg, err = c.client.GetMessage(&client.GetMessageRequest{ if message.ReplyTo != nil && message.ReplyTo.MessageReplyToType() == client.TypeMessageReplyToMessage {
replyTo, _ := message.ReplyTo.(*client.MessageReplyToMessage)
var text string
if replyTo.Quote != nil && !noContent {
text = formatter.Format(
replyTo.Quote.Text,
replyTo.Quote.Entities,
c.getFormatter(),
)
// make the whole quote fit one line
text = strings.ReplaceAll(text, "\n", " ")
}
if message.ChatId == replyTo.ChatId {
// obtain message from this chat
replyMsg, err := c.client.GetMessage(&client.GetMessageRequest{
ChatId: message.ChatId, ChatId: message.ChatId,
MessageId: replyTo.MessageId, MessageId: replyTo.MessageId,
}) })
@ -362,14 +387,38 @@ func (c *Client) getMessageReply(message *client.Message) (reply *gateway.Reply,
return return
} }
if !noContent {
tgReply = c.messageToStub(replyMsg, preview, text)
}
replyId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, message.ChatId, replyTo.MessageId) replyId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, message.ChatId, replyTo.MessageId)
if err != nil { if err != nil {
replyId = strconv.FormatInt(replyTo.MessageId, 10) replyId = strconv.FormatInt(replyTo.MessageId, 10)
} }
reply = &gateway.Reply{
gatewayReply = &gateway.Reply{
Author: fmt.Sprintf("%v@%s", c.getSenderId(replyMsg), gateway.Jid.Full()), Author: fmt.Sprintf("%v@%s", c.getSenderId(replyMsg), gateway.Jid.Full()),
Id: replyId, Id: replyId,
} }
} else if !noContent {
// it's safe to assume there's no need to pass ChatId here
// as it's needed only for pin messages which are not allowed in replies
if text == "" && replyTo.Content != nil {
text = c.messageContentToText(replyTo.Content, 0, preview)
}
if text == "" {
log.Error("Empty reply from other/unknown chat")
log.Debugf("replyTo: %#v", replyTo)
return
}
tgReply = &messageStub{
Sender: c.formatOrigin(replyTo.Origin) + " @ " + c.formatContact(replyTo.ChatId),
Date: replyTo.OriginSendDate,
Text: text,
}
}
} }
return return
@ -391,9 +440,16 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
return "" return ""
} }
return c.formatMessageContent(preview, c.messageToStub(message, preview, ""))
}
func (c *Client) formatMessageContent(preview bool, message *messageStub) string {
var str strings.Builder var str strings.Builder
// add messageid and sender // add messageid and sender
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatSender(message))) if message.MessageId != 0 {
str.WriteString(fmt.Sprintf("%v | ", message.MessageId))
}
str.WriteString(fmt.Sprintf("%s | ", message.Sender))
// add date // add date
if !preview { if !preview {
str.WriteString( str.WriteString(
@ -404,10 +460,7 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
} }
// text message // text message
var text string text := message.Text
if message.Content != nil {
text = c.messageToText(message, preview)
}
if text != "" { if text != "" {
if !preview { if !preview {
str.WriteString(text) str.WriteString(text)
@ -424,30 +477,33 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
return str.String() return str.String()
} }
func (c *Client) formatForward(fwd *client.MessageForwardInfo) string { func (c *Client) formatOrigin(origin client.MessageOrigin) string {
switch fwd.Origin.MessageOriginType() { if origin == nil {
return ""
}
switch origin.MessageOriginType() {
case client.TypeMessageOriginUser: case client.TypeMessageOriginUser:
originUser := fwd.Origin.(*client.MessageOriginUser) originUser := origin.(*client.MessageOriginUser)
return c.formatContact(originUser.SenderUserId) return c.formatContact(originUser.SenderUserId)
case client.TypeMessageOriginChat: case client.TypeMessageOriginChat:
originChat := fwd.Origin.(*client.MessageOriginChat) originChat := origin.(*client.MessageOriginChat)
var signature string var signature string
if originChat.AuthorSignature != "" { if originChat.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature) signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature)
} }
return c.formatContact(originChat.SenderChatId) + signature return c.formatContact(originChat.SenderChatId) + signature
case client.TypeMessageOriginHiddenUser: case client.TypeMessageOriginHiddenUser:
originUser := fwd.Origin.(*client.MessageOriginHiddenUser) originUser := origin.(*client.MessageOriginHiddenUser)
return originUser.SenderName return originUser.SenderName
case client.TypeMessageOriginChannel: case client.TypeMessageOriginChannel:
channel := fwd.Origin.(*client.MessageOriginChannel) channel := origin.(*client.MessageOriginChannel)
var signature string var signature string
if channel.AuthorSignature != "" { if channel.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", channel.AuthorSignature) signature = fmt.Sprintf(" (%s)", channel.AuthorSignature)
} }
return c.formatContact(channel.ChatId) + signature return c.formatContact(channel.ChatId) + signature
} }
return "Unknown forward type" return "Unknown origin type"
} }
func (c *Client) formatFile(file *client.File, compact bool) (string, string) { func (c *Client) formatFile(file *client.File, compact bool) (string, string) {
@ -593,20 +649,24 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
return "<empty message>" return "<empty message>"
} }
return c.messageContentToText(message.Content, message.ChatId, preview)
}
func (c *Client) messageContentToText(content client.MessageContent, chatId int64, preview bool) string {
markupMode := c.getFormatter() markupMode := c.getFormatter()
switch message.Content.MessageContentType() { switch content.MessageContentType() {
case client.TypeMessageSticker: case client.TypeMessageSticker:
sticker, _ := message.Content.(*client.MessageSticker) sticker, _ := content.(*client.MessageSticker)
return sticker.Sticker.Emoji return sticker.Sticker.Emoji
case client.TypeMessageAnimatedEmoji: case client.TypeMessageAnimatedEmoji:
animatedEmoji, _ := message.Content.(*client.MessageAnimatedEmoji) animatedEmoji, _ := content.(*client.MessageAnimatedEmoji)
return animatedEmoji.Emoji return animatedEmoji.Emoji
case client.TypeMessageBasicGroupChatCreate, client.TypeMessageSupergroupChatCreate: case client.TypeMessageBasicGroupChatCreate, client.TypeMessageSupergroupChatCreate:
return "has created chat" return "has created chat"
case client.TypeMessageChatJoinByLink: case client.TypeMessageChatJoinByLink:
return "joined chat via invite link" return "joined chat via invite link"
case client.TypeMessageChatAddMembers: case client.TypeMessageChatAddMembers:
addMembers, _ := message.Content.(*client.MessageChatAddMembers) addMembers, _ := content.(*client.MessageChatAddMembers)
text := "invited " text := "invited "
if len(addMembers.MemberUserIds) > 0 { if len(addMembers.MemberUserIds) > 0 {
@ -615,19 +675,19 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
return text return text
case client.TypeMessageChatDeleteMember: case client.TypeMessageChatDeleteMember:
deleteMember, _ := message.Content.(*client.MessageChatDeleteMember) deleteMember, _ := content.(*client.MessageChatDeleteMember)
return "kicked " + c.formatContact(deleteMember.UserId) return "kicked " + c.formatContact(deleteMember.UserId)
case client.TypeMessagePinMessage: case client.TypeMessagePinMessage:
pinMessage, _ := message.Content.(*client.MessagePinMessage) pinMessage, _ := content.(*client.MessagePinMessage)
return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, preview, nil) return "pinned message: " + c.formatMessage(chatId, pinMessage.MessageId, preview, nil)
case client.TypeMessageChatChangeTitle: case client.TypeMessageChatChangeTitle:
changeTitle, _ := message.Content.(*client.MessageChatChangeTitle) changeTitle, _ := content.(*client.MessageChatChangeTitle)
return "chat title set to: " + changeTitle.Title return "chat title set to: " + changeTitle.Title
case client.TypeMessageLocation: case client.TypeMessageLocation:
location, _ := message.Content.(*client.MessageLocation) location, _ := content.(*client.MessageLocation)
return c.formatLocation(location.Location) return c.formatLocation(location.Location)
case client.TypeMessageVenue: case client.TypeMessageVenue:
venue, _ := message.Content.(*client.MessageVenue) venue, _ := content.(*client.MessageVenue)
if preview { if preview {
return venue.Venue.Title return venue.Venue.Title
} else { } else {
@ -639,7 +699,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessagePhoto: case client.TypeMessagePhoto:
photo, _ := message.Content.(*client.MessagePhoto) photo, _ := content.(*client.MessagePhoto)
if preview { if preview {
return photo.Caption.Text return photo.Caption.Text
} else { } else {
@ -650,7 +710,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessageAudio: case client.TypeMessageAudio:
audio, _ := message.Content.(*client.MessageAudio) audio, _ := content.(*client.MessageAudio)
if preview { if preview {
return audio.Caption.Text return audio.Caption.Text
} else { } else {
@ -661,7 +721,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessageVideo: case client.TypeMessageVideo:
video, _ := message.Content.(*client.MessageVideo) video, _ := content.(*client.MessageVideo)
if preview { if preview {
return video.Caption.Text return video.Caption.Text
} else { } else {
@ -672,7 +732,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessageDocument: case client.TypeMessageDocument:
document, _ := message.Content.(*client.MessageDocument) document, _ := content.(*client.MessageDocument)
if preview { if preview {
return document.Caption.Text return document.Caption.Text
} else { } else {
@ -683,7 +743,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessageText: case client.TypeMessageText:
text, _ := message.Content.(*client.MessageText) text, _ := content.(*client.MessageText)
if preview { if preview {
return text.Text.Text return text.Text.Text
} else { } else {
@ -694,7 +754,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessageVoiceNote: case client.TypeMessageVoiceNote:
voice, _ := message.Content.(*client.MessageVoiceNote) voice, _ := content.(*client.MessageVoiceNote)
if preview { if preview {
return voice.Caption.Text return voice.Caption.Text
} else { } else {
@ -707,7 +767,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
case client.TypeMessageVideoNote: case client.TypeMessageVideoNote:
return "" return ""
case client.TypeMessageAnimation: case client.TypeMessageAnimation:
animation, _ := message.Content.(*client.MessageAnimation) animation, _ := content.(*client.MessageAnimation)
if preview { if preview {
return animation.Caption.Text return animation.Caption.Text
} else { } else {
@ -718,7 +778,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessageContact: case client.TypeMessageContact:
contact, _ := message.Content.(*client.MessageContact) contact, _ := content.(*client.MessageContact)
if preview { if preview {
return contact.Contact.FirstName + " " + contact.Contact.LastName return contact.Contact.FirstName + " " + contact.Contact.LastName
} else { } else {
@ -736,10 +796,10 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
) )
} }
case client.TypeMessageDice: case client.TypeMessageDice:
dice, _ := message.Content.(*client.MessageDice) dice, _ := content.(*client.MessageDice)
return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value) return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value)
case client.TypeMessagePoll: case client.TypeMessagePoll:
poll, _ := message.Content.(*client.MessagePoll) poll, _ := content.(*client.MessagePoll)
if preview { if preview {
return poll.Poll.Question return poll.Poll.Question
@ -765,7 +825,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
return strings.Join(rows, "\n") return strings.Join(rows, "\n")
} }
case client.TypeMessageChatSetMessageAutoDeleteTime: case client.TypeMessageChatSetMessageAutoDeleteTime:
ttl, _ := message.Content.(*client.MessageChatSetMessageAutoDeleteTime) ttl, _ := content.(*client.MessageChatSetMessageAutoDeleteTime)
name := c.formatContact(ttl.FromUserId) name := c.formatContact(ttl.FromUserId)
if name == "" { if name == "" {
if ttl.MessageAutoDeleteTime == 0 { if ttl.MessageAutoDeleteTime == 0 {
@ -782,7 +842,7 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
} }
} }
return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType()) return fmt.Sprintf("unknown message (%s)", content.MessageContentType())
} }
func (c *Client) contentToFile(content client.MessageContent) (*client.File, *client.File) { func (c *Client) contentToFile(content client.MessageContent) (*client.File, *client.File) {
@ -856,7 +916,7 @@ func (c *Client) countCharsInLines(lines *[]string) (count int) {
return return
} }
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) (string, int, int) { func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string) (string, *gateway.Reply) {
isPM, err := c.IsPM(message.ChatId) isPM, err := c.IsPM(message.ChatId)
if err != nil { if err != nil {
log.Errorf("Could not determine if chat is PM: %v", err) log.Errorf("Could not determine if chat is PM: %v", err)
@ -865,7 +925,6 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
// with carbons, hide for all messages in PM and only for outgoing in group chats // with carbons, hide for all messages in PM and only for outgoing in group chats
hideSender := isCarbonsEnabled && (message.IsOutgoing || isPM) hideSender := isCarbonsEnabled && (message.IsOutgoing || isPM)
var replyStart, replyEnd int
prefix := []string{} prefix := []string{}
// message direction // message direction
var directionChar string var directionChar string
@ -894,21 +953,34 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
prefix = append(prefix, sender) prefix = append(prefix, sender)
} }
} }
// reply to // reply to
if message.ReplyTo != nil && message.ReplyTo.MessageReplyToType() == client.TypeMessageReplyToMessage { preview := true
replyTo, _ := message.ReplyTo.(*client.MessageReplyToMessage) reply, tgReply := c.getMessageReply(message, preview, false)
if tgReply != nil {
var replyStart, replyEnd int
if len(prefix) > 0 { if len(prefix) > 0 {
replyStart = c.countCharsInLines(&prefix) + (len(prefix)-1)*len(messageHeaderSeparator) replyStart = c.countCharsInLines(&prefix) + (len(prefix)-1)*len(messageHeaderSeparator)
} }
replyLine := "reply: " + c.formatMessage(message.ChatId, replyTo.MessageId, true, replyMsg)
replyLine := "reply: " + c.formatMessageContent(preview, tgReply)
prefix = append(prefix, replyLine) prefix = append(prefix, replyLine)
replyEnd = replyStart + utf8.RuneCountInString(replyLine) replyEnd = replyStart + utf8.RuneCountInString(replyLine)
if len(prefix) > 0 { if len(prefix) > 0 {
replyEnd += len(messageHeaderSeparator) replyEnd += len(messageHeaderSeparator)
} }
if reply != nil {
reply.Start = uint64(replyStart)
reply.End = uint64(replyEnd)
} }
}
if message.ForwardInfo != nil { if message.ForwardInfo != nil {
prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo)) prefix = append(prefix, "fwd: "+c.formatOrigin(message.ForwardInfo.Origin))
} }
// preview // preview
if previewString != "" { if previewString != "" {
@ -919,7 +991,7 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
prefix = append(prefix, "file: "+fileString) prefix = append(prefix, "file: "+fileString)
} }
return strings.Join(prefix, messageHeaderSeparator), replyStart, replyEnd return strings.Join(prefix, messageHeaderSeparator), reply
} }
func (c *Client) ensureDownloadFile(file *client.File) *client.File { func (c *Client) ensureDownloadFile(file *client.File) *client.File {
@ -944,8 +1016,8 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
jids := c.getCarbonFullJids(isCarbon, "") jids := c.getCarbonFullJids(isCarbon, "")
var text, oob, auxText string var text, oob, auxText string
var reply *gateway.Reply
reply, replyMsg := c.getMessageReply(message) var replyObtained bool
content := message.Content content := message.Content
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto { if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
@ -981,12 +1053,10 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
} else if !c.Session.RawMessages { } else if !c.Session.RawMessages {
var newText strings.Builder var newText strings.Builder
prefix, replyStart, replyEnd := c.messageToPrefix(message, previewName, fileName, replyMsg) prefix, prefixReply := c.messageToPrefix(message, previewName, fileName)
reply = prefixReply
replyObtained = true
newText.WriteString(prefix) newText.WriteString(prefix)
if reply != nil {
reply.Start = uint64(replyStart)
reply.End = uint64(replyEnd)
}
if text != "" { if text != "" {
// \n if it is groupchat and message is not empty // \n if it is groupchat and message is not empty
@ -1004,6 +1074,9 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
} }
} }
} }
if !replyObtained {
reply, _ = c.getMessageReply(message, false, true)
}
// mark message as read // mark message as read
c.client.ViewMessages(&client.ViewMessagesRequest{ c.client.ViewMessages(&client.ViewMessagesRequest{

View file

@ -436,15 +436,12 @@ func TestMessageToPrefix1(t *testing.T) {
}, },
}, },
} }
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "", nil) prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "")
if prefix != "➡ 42 | fwd: ziz" { if prefix != "➡ 42 | fwd: ziz" {
t.Errorf("Wrong prefix: %v", prefix) t.Errorf("Wrong prefix: %v", prefix)
} }
if replyStart != 0 { if gatewayReply != nil {
t.Errorf("Wrong replyStart: %v", replyStart) t.Errorf("Reply is not nil: %v", gatewayReply)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
} }
} }
@ -457,15 +454,12 @@ func TestMessageToPrefix2(t *testing.T) {
}, },
}, },
} }
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "", nil) prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "")
if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" { if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" {
t.Errorf("Wrong prefix: %v", prefix) t.Errorf("Wrong prefix: %v", prefix)
} }
if replyStart != 0 { if gatewayReply != nil {
t.Errorf("Wrong replyStart: %v", replyStart) t.Errorf("Reply is not nil: %v", gatewayReply)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
} }
} }
@ -478,15 +472,12 @@ func TestMessageToPrefix3(t *testing.T) {
}, },
}, },
} }
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg", nil) prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg")
if prefix != "< 56 | fwd: (zuz) | file: a.jpg" { if prefix != "< 56 | fwd: (zuz) | file: a.jpg" {
t.Errorf("Wrong prefix: %v", prefix) t.Errorf("Wrong prefix: %v", prefix)
} }
if replyStart != 0 { if gatewayReply != nil {
t.Errorf("Wrong replyStart: %v", replyStart) t.Errorf("Reply is not nil: %v", gatewayReply)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
} }
} }
@ -495,15 +486,12 @@ func TestMessageToPrefix4(t *testing.T) {
Id: 23, Id: 23,
IsOutgoing: true, IsOutgoing: true,
} }
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", nil) prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "")
if prefix != "> 23" { if prefix != "> 23" {
t.Errorf("Wrong prefix: %v", prefix) t.Errorf("Wrong prefix: %v", prefix)
} }
if replyStart != 0 { if gatewayReply != nil {
t.Errorf("Wrong replyStart: %v", replyStart) t.Errorf("Reply is not nil: %v", gatewayReply)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
} }
} }
@ -516,43 +504,62 @@ func TestMessageToPrefix5(t *testing.T) {
}, },
}, },
} }
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg", nil) prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg")
if prefix != "< 560 | fwd: (zyz) | preview: h.jpg | file: a.jpg" { if prefix != "< 560 | fwd: (zyz) | preview: h.jpg | file: a.jpg" {
t.Errorf("Wrong prefix: %v", prefix) t.Errorf("Wrong prefix: %v", prefix)
} }
if replyStart != 0 { if gatewayReply != nil {
t.Errorf("Wrong replyStart: %v", replyStart) t.Errorf("Reply is not nil: %v", gatewayReply)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
} }
} }
func TestMessageToPrefix6(t *testing.T) { func TestMessageToPrefix6(t *testing.T) {
message := client.Message{ message := client.Message{
Id: 23, Id: 23,
ChatId: 25,
IsOutgoing: true, IsOutgoing: true,
ReplyTo: &client.MessageReplyToMessage{ ReplyTo: &client.MessageReplyToMessage{
MessageId: 42, ChatId: 41,
Quote: &client.FormattedText{
Text: "tist\nuz\niz",
},
Origin: &client.MessageOriginHiddenUser{
SenderName: "ziz",
},
}, },
} }
reply := client.Message{ prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "")
Id: 42, if prefix != "> 23 | reply: ziz @ unknown contact: TDlib instance is offline | tist uz iz" {
t.Errorf("Wrong prefix: %v", prefix)
}
if gatewayReply != nil {
t.Errorf("Reply is not nil: %v", gatewayReply)
}
}
func TestMessageToPrefix7(t *testing.T) {
message := client.Message{
Id: 23,
ChatId: 42,
IsOutgoing: true,
ReplyTo: &client.MessageReplyToMessage{
ChatId: 41,
Content: &client.MessageText{ Content: &client.MessageText{
Text: &client.FormattedText{ Text: &client.FormattedText{
Text: "tist", Text: "tist",
}, },
}, },
Origin: &client.MessageOriginChannel{
AuthorSignature: "zaz",
},
},
} }
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", &reply) prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "")
if prefix != "> 23 | reply: 42 | | tist" { if prefix != "> 23 | reply: (zaz) @ unknown contact: TDlib instance is offline | tist" {
t.Errorf("Wrong prefix: %v", prefix) t.Errorf("Wrong prefix: %v", prefix)
} }
if replyStart != 4 { if gatewayReply != nil {
t.Errorf("Wrong replyStart: %v", replyStart) t.Errorf("Reply is not nil: %v", gatewayReply)
}
if replyEnd != 26 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
} }
} }