Fix lossy int64->int32 conversion

This commit is contained in:
bodqhrohro 2019-12-04 20:37:46 +02:00
parent 5b82d5d718
commit 133d787d38
2 changed files with 36 additions and 33 deletions

View file

@ -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")
}
}

View file

@ -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 {
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: id,
UserId: userID,
})
if err != nil {
return nil, nil, err
}
cache.users[id] = user
cache.users[userID] = user
}
}
chatID := int64(id)
cacheChat, ok = cache.chats[chatID]
cacheChat, ok = cache.chats[id]
if !ok {
if chat == nil {
cacheChat, err = c.client.GetChat(&client.GetChatRequest{
ChatId: chatID,
ChatId: id,
})
if err != nil {
return nil, nil, err
}
cache.chats[chatID] = cacheChat
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