aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-12-04 21:37:46 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-12-04 21:37:46 +0300
commit133d787d38aedf9a55d2f619fbf81f45f2397a3e (patch)
tree122e0ca9d0d68bed0b177baa76785ca338a7b2b7 /telegram/utils.go
parent5b82d5d718eea29b24eb58b66d1488ed21e13500 (diff)
Fix lossy int64->int32 conversion
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go51
1 files changed, 27 insertions, 24 deletions
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
- }
+ 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.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