From 133d787d38aedf9a55d2f619fbf81f45f2397a3e Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Wed, 4 Dec 2019 20:37:46 +0200 Subject: [PATCH] Fix lossy int64->int32 conversion --- telegram/handlers.go | 6 ++--- telegram/utils.go | 63 +++++++++++++++++++++++--------------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/telegram/handlers.go b/telegram/handlers.go index 0b76b48..b72ff2c 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -104,13 +104,13 @@ func (c *Client) updateHandler() { func (c *Client) updateUser(update *client.UpdateUser) { cache.users[update.User.Id] = update.User show, status := userStatusToText(update.User.Status) - c.processStatusUpdate(update.User.Id, status, show) + c.processStatusUpdate(int64(update.User.Id), status, show) } // user status changed func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { show, status := userStatusToText(update.Status) - c.processStatusUpdate(update.UserId, status, show, gateway.SPImmed(false)) + c.processStatusUpdate(int64(update.UserId), status, show, gateway.SPImmed(false)) } // new chat discovered @@ -149,7 +149,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { } if update.Chat.Id < 0 { - c.processStatusUpdate(int32(update.Chat.Id), update.Chat.Title, "chat") + c.processStatusUpdate(update.Chat.Id, update.Chat.Title, "chat") } } diff --git a/telegram/utils.go b/telegram/utils.go index de70152..d2d2509 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/pkg/errors" "io" + "math" "os" "path/filepath" "regexp" @@ -41,11 +42,11 @@ func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.Us return nil, nil, err } - return c.GetContactByID(int32(chat.Id), chat) + return c.GetContactByID(chat.Id, chat) } // GetContactByID gets user and chat information from cache (or tries to retrieve it, if missing) -func (c *Client) GetContactByID(id int32, chat *client.Chat) (*client.Chat, *client.User, error) { +func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *client.User, error) { if !c.online { return nil, nil, errOffline } @@ -55,32 +56,34 @@ func (c *Client) GetContactByID(id int32, chat *client.Chat) (*client.Chat, *cli var ok bool var err error - user, ok = cache.users[id] - if !ok && id > 0 { - user, err = c.client.GetUser(&client.GetUserRequest{ - UserId: id, - }) - if err != nil { - return nil, nil, err - } - - cache.users[id] = user - } - - chatID := int64(id) - cacheChat, ok = cache.chats[chatID] - if !ok { - if chat == nil { - cacheChat, err = c.client.GetChat(&client.GetChatRequest{ - ChatId: chatID, + if id <= math.MaxInt32 && id >= math.MinInt32 { + userID := int32(id) + user, ok = cache.users[userID] + if !ok && userID > 0 { + user, err = c.client.GetUser(&client.GetUserRequest{ + UserId: userID, }) if err != nil { return nil, nil, err } - cache.chats[chatID] = cacheChat + cache.users[userID] = user + } + } + + cacheChat, ok = cache.chats[id] + if !ok { + if chat == nil { + cacheChat, err = c.client.GetChat(&client.GetChatRequest{ + ChatId: id, + }) + if err != nil { + return nil, nil, err + } + + cache.chats[id] = cacheChat } else { - cache.chats[chatID] = chat + cache.chats[id] = chat } } if chat == nil { @@ -120,7 +123,7 @@ func userStatusToText(status client.UserStatus) (string, string) { return show, textStatus } -func (c *Client) processStatusUpdate(chatID int32, status string, show string, args ...args.V) error { +func (c *Client) processStatusUpdate(chatID int64, status string, show string, args ...args.V) error { if !c.online { return nil } @@ -174,7 +177,7 @@ func (c *Client) processStatusUpdate(chatID int32, status string, show string, a return nil } -func (c *Client) formatContact(chatID int32) string { +func (c *Client) formatContact(chatID int64) string { if chatID == 0 { return "" } @@ -220,7 +223,7 @@ 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(message.SenderUserId))) + str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(int64(message.SenderUserId)))) // TODO: timezone if !preview { str.WriteString(time.Unix(int64(message.Date), 0).UTC().Format("02 Jan 2006 15:04:05 | ")) @@ -278,13 +281,13 @@ func (c *Client) messageToText(message *client.Message) string { text := "invited " if len(addMembers.MemberUserIds) > 0 { - text += c.formatContact(addMembers.MemberUserIds[0]) + text += c.formatContact(int64(addMembers.MemberUserIds[0])) } return text case client.TypeMessageChatDeleteMember: deleteMember, _ := message.Content.(*client.MessageChatDeleteMember) - return "kicked " + c.formatContact(deleteMember.UserId) + return "kicked " + c.formatContact(int64(deleteMember.UserId)) case client.TypeMessagePinMessage: pinMessage, _ := message.Content.(*client.MessagePinMessage) return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, false, nil) @@ -376,13 +379,13 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str prefix = append(prefix, directionChar+strconv.Itoa(int(message.Id))) // show sender in group chats if message.ChatId < 0 && message.SenderUserId != 0 { - prefix = append(prefix, c.formatContact(message.SenderUserId)) + prefix = append(prefix, c.formatContact(int64(message.SenderUserId))) } 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)) + prefix = append(prefix, "fwd: "+c.formatContact(int64(originUser.SenderUserId))) case client.TypeMessageForwardOriginHiddenUser: originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginHiddenUser) prefix = append(prefix, fmt.Sprintf("fwd: anonymous (%s)", originUser.SenderName)) @@ -392,7 +395,7 @@ 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(int32(channel.ChatId))+signature) + prefix = append(prefix, "fwd: "+c.formatContact(channel.ChatId)+signature) } } // reply to