ForwardInfo refactoring

This commit is contained in:
Bohdan Horbeshko 2022-01-10 04:29:50 -05:00
parent 9f04ed51bd
commit 77f9c9f74e

View file

@ -271,6 +271,32 @@ 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 {
switch fwd.Origin.MessageForwardOriginType() {
case client.TypeMessageForwardOriginUser:
originUser := fwd.Origin.(*client.MessageForwardOriginUser)
return c.formatContact(originUser.SenderUserID)
case client.TypeMessageForwardOriginChat:
originChat := fwd.Origin.(*client.MessageForwardOriginChat)
var signature string
if originChat.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature)
}
return c.formatContact(originChat.SenderChatID)+signature
case client.TypeMessageForwardOriginHiddenUser:
originUser := fwd.Origin.(*client.MessageForwardOriginHiddenUser)
return originUser.SenderName
case client.TypeMessageForwardOriginChannel:
channel := fwd.Origin.(*client.MessageForwardOriginChannel)
var signature string
if channel.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", channel.AuthorSignature)
}
return c.formatContact(channel.ChatID)+signature
}
return "Unknown forward type"
}
func (c *Client) formatContent(file *client.File, filename string) string { func (c *Client) formatContent(file *client.File, filename string) string {
if file == nil { if file == nil {
return "" return ""
@ -447,34 +473,14 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
} }
prefix = append(prefix, c.formatContact(senderId)) prefix = append(prefix, c.formatContact(senderId))
} }
if message.ForwardInfo != nil {
switch message.ForwardInfo.Origin.MessageForwardOriginType() {
case client.TypeMessageForwardOriginUser:
originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginUser)
prefix = append(prefix, "fwd: "+c.formatContact(originUser.SenderUserID))
case client.TypeMessageForwardOriginChat:
originChat := message.ForwardInfo.Origin.(*client.MessageForwardOriginChat)
var signature string
if originChat.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature)
}
prefix = append(prefix, "fwd: "+c.formatContact(originChat.SenderChatID)+signature)
case client.TypeMessageForwardOriginHiddenUser:
originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginHiddenUser)
prefix = append(prefix, fmt.Sprintf("fwd: anonymous (%s)", originUser.SenderName))
case client.TypeMessageForwardOriginChannel:
channel := message.ForwardInfo.Origin.(*client.MessageForwardOriginChannel)
var signature string
if channel.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", channel.AuthorSignature)
}
prefix = append(prefix, "fwd: "+c.formatContact(channel.ChatID)+signature)
}
}
// reply to // reply to
if message.ReplyToMessageID != 0 { if message.ReplyToMessageID != 0 {
prefix = append(prefix, "reply: "+c.formatMessage(message.ChatID, message.ReplyToMessageID, true, nil)) prefix = append(prefix, "reply: "+c.formatMessage(message.ChatID, message.ReplyToMessageID, true, nil))
} }
if message.ForwardInfo != nil {
prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo))
}
// file
if fileString != "" { if fileString != "" {
prefix = append(prefix, "file: "+fileString) prefix = append(prefix, "file: "+fileString)
} }