aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2021-12-04 21:10:54 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2021-12-04 21:10:54 +0300
commit105f5017c35f92a9e2f5398a06cfdd3f1da31bad (patch)
treec602c7eb3ede37718894e7dea7875490cbc8d728 /telegram/utils.go
parentbc37cf0c4f0d58752f92238a013513be9aa3caff (diff)
Migrate to TDlib 1.7.9
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go95
1 files changed, 57 insertions, 38 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index f7e7a28..3082b5f 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -6,7 +6,6 @@ import (
"fmt"
"github.com/pkg/errors"
"io"
- "math"
"os"
"path/filepath"
"regexp"
@@ -44,7 +43,7 @@ func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.Us
return nil, nil, err
}
- return c.GetContactByID(chat.Id, chat)
+ return c.GetContactByID(chat.ID, chat)
}
// GetContactByID gets user and chat information from cache (or tries to retrieve it, if missing)
@@ -58,18 +57,13 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
var ok bool
var err error
- if id <= math.MaxInt32 && id >= math.MinInt32 {
- userID := int32(id)
- user, ok = c.cache.GetUser(userID)
- if !ok && userID > 0 {
- user, err = c.client.GetUser(&client.GetUserRequest{
- UserId: userID,
- })
- if err != nil {
- return nil, nil, err
- }
-
- c.cache.SetUser(userID, user)
+ user, ok = c.cache.GetUser(id)
+ if !ok && id > 0 {
+ user, err = c.client.GetUser(&client.GetUserRequest{
+ UserID: id,
+ })
+ if err == nil {
+ c.cache.SetUser(id, user)
}
}
@@ -77,7 +71,7 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
if !ok {
if chat == nil {
cacheChat, err = c.client.GetChat(&client.GetChatRequest{
- ChatId: id,
+ ChatID: id,
})
if err != nil {
// error is irrelevant if the user was found successfully
@@ -200,11 +194,11 @@ func (c *Client) formatContact(chatID int64) string {
var str string
if chat != nil {
- str = fmt.Sprintf("%s (%v)", chat.Title, chat.Id)
+ str = fmt.Sprintf("%s (%v)", chat.Title, chat.ID)
} else if user != nil {
username := user.Username
if username == "" {
- username = strconv.FormatInt(int64(user.Id), 10)
+ username = strconv.FormatInt(user.ID, 10)
}
str = fmt.Sprintf("%s %s (%v)", user.FirstName, user.LastName, username)
@@ -221,8 +215,8 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
var err error
if message == nil {
message, err = c.client.GetMessage(&client.GetMessageRequest{
- ChatId: chatID,
- MessageId: messageID,
+ ChatID: chatID,
+ MessageID: messageID,
})
if err != nil {
return fmt.Sprintf("<error fetching message: %s>", err.Error())
@@ -234,7 +228,16 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
}
var str strings.Builder
- str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(int64(message.SenderUserId))))
+ var senderId int64
+ switch message.Sender.MessageSenderType() {
+ case client.TypeMessageSenderUser:
+ senderUser, _ := message.Sender.(*client.MessageSenderUser)
+ senderId = senderUser.UserID
+ case client.TypeMessageSenderChat:
+ senderChat, _ := message.Sender.(*client.MessageSenderChat)
+ senderId = senderChat.ChatID
+ }
+ str.WriteString(fmt.Sprintf("%v | %s | ", message.ID, c.formatContact(senderId)))
if !preview {
str.WriteString(
time.Unix(int64(message.Date), 0).
@@ -276,7 +279,7 @@ func (c *Client) formatContent(file *client.File, filename string) string {
filename,
file.Size/1024,
c.content.Link,
- fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.Id))),
+ fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.ID))),
filepath.Ext(filename),
)
}
@@ -295,17 +298,17 @@ func (c *Client) messageToText(message *client.Message) string {
addMembers, _ := message.Content.(*client.MessageChatAddMembers)
text := "invited "
- if len(addMembers.MemberUserIds) > 0 {
- text += c.formatContact(int64(addMembers.MemberUserIds[0]))
+ if len(addMembers.MemberUserIDs) > 0 {
+ text += c.formatContact(addMembers.MemberUserIDs[0])
}
return text
case client.TypeMessageChatDeleteMember:
deleteMember, _ := message.Content.(*client.MessageChatDeleteMember)
- return "kicked " + c.formatContact(int64(deleteMember.UserId))
+ return "kicked " + c.formatContact(deleteMember.UserID)
case client.TypeMessagePinMessage:
pinMessage, _ := message.Content.(*client.MessagePinMessage)
- return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, false, nil)
+ return "pinned message: " + c.formatMessage(message.ChatID, pinMessage.MessageID, false, nil)
case client.TypeMessageChatChangeTitle:
changeTitle, _ := message.Content.(*client.MessageChatChangeTitle)
return "chat title set to: " + changeTitle.Title
@@ -393,7 +396,7 @@ func (c *Client) contentToFilename(content client.MessageContent) (*client.File,
sizes := photo.Photo.Sizes
if len(sizes) > 1 {
file := sizes[len(sizes)-1].Photo
- return file, strconv.FormatInt(int64(file.Id), 10) + ".jpg"
+ return file, strconv.FormatInt(file.ID, 10) + ".jpg"
}
return nil, ""
case client.TypeMessageAudio:
@@ -419,16 +422,32 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
} else {
directionChar = "⬅ "
}
- prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10))
+ prefix = append(prefix, directionChar+strconv.FormatInt(message.ID, 10))
// show sender in group chats
- if message.ChatId < 0 && message.SenderUserId != 0 {
- prefix = append(prefix, c.formatContact(int64(message.SenderUserId)))
+ if message.ChatID < 0 && message.Sender != nil {
+ var senderId int64
+ switch message.Sender.MessageSenderType() {
+ case client.TypeMessageSenderUser:
+ senderUser, _ := message.Sender.(*client.MessageSenderUser)
+ senderId = senderUser.UserID
+ case client.TypeMessageSenderChat:
+ senderChat, _ := message.Sender.(*client.MessageSenderChat)
+ senderId = senderChat.ChatID
+ }
+ 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(int64(originUser.SenderUserId)))
+ 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))
@@ -438,12 +457,12 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
if channel.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", channel.AuthorSignature)
}
- prefix = append(prefix, "fwd: "+c.formatContact(channel.ChatId)+signature)
+ prefix = append(prefix, "fwd: "+c.formatContact(channel.ChatID)+signature)
}
}
// reply to
- if message.ReplyToMessageId != 0 {
- prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, nil))
+ if message.ReplyToMessageID != 0 {
+ prefix = append(prefix, "reply: "+c.formatMessage(message.ChatID, message.ReplyToMessageID, true, nil))
}
if fileString != "" {
prefix = append(prefix, "file: "+fileString)
@@ -484,8 +503,8 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
}
c.client.EditMessageText(&client.EditMessageTextRequest{
- ChatId: chatID,
- MessageId: messageID,
+ ChatID: chatID,
+ MessageID: messageID,
InputMessageContent: message,
})
} else {
@@ -500,7 +519,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
var file *client.InputFileRemote
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
file = &client.InputFileRemote{
- Id: text,
+ ID: text,
}
}
@@ -531,8 +550,8 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
}
_, err := c.client.SendMessage(&client.SendMessageRequest{
- ChatId: chatID,
- ReplyToMessageId: reply,
+ ChatID: chatID,
+ ReplyToMessageID: reply,
InputMessageContent: message,
})
if err != nil {