Migrate to TDlib 1.7.9

This commit is contained in:
Bohdan Horbeshko 2021-12-04 13:10:54 -05:00
parent bc37cf0c4f
commit 105f5017c3
8 changed files with 139 additions and 112 deletions

View file

@ -8,12 +8,7 @@ The configuration file is compatible with Zhabogram 2.0, so you can easily copy
Prerequisites: Go links the binary against the `glibc` version present in the system where the build process happens, so it should be less or equal to the version of `glibc` in the system where Telegabber will run. Prerequisites: Go links the binary against the `glibc` version present in the system where the build process happens, so it should be less or equal to the version of `glibc` in the system where Telegabber will run.
* Build TDLib according to [TDLib build instructions](https://tdlib.github.io/td/build.html). The 1.5.0 version is needed currently; for switching to it, run this after `cd td`: * Build TDLib according to [TDLib build instructions](https://tdlib.github.io/td/build.html). Roll back to commit 8d7bda00a535d1eda684c3c8802e85d69c89a14a if the compatibility got broken.
```
git fetch --all --tags --prune
git checkout tags/v1.5.0
```
* Install Go (tested with 1.13, but may work with earlier versions too). * Install Go (tested with 1.13, but may work with earlier versions too).
* Open the source dir in a new shell (to make sure that `$GOPATH` works) and run `make`. Dependencies will be installed automatically. * Open the source dir in a new shell (to make sure that `$GOPATH` works) and run `make`. Dependencies will be installed automatically.

1
go.mod
View file

@ -14,3 +14,4 @@ require (
) )
replace gosrc.io/xmpp => github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c replace gosrc.io/xmpp => github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c
replace github.com/zelenin/go-tdlib => github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac

2
go.sum
View file

@ -28,6 +28,8 @@ github.com/go-interpreter/wagon v0.6.0/go.mod h1:5+b/MBYkclRZngKF5s6qrgWxSLgE9F5
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac h1:5FQGW4yHSkbwm+4i/8ef7FvkIFt4NOM4HexSbvPduRo=
github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=

View file

@ -17,7 +17,7 @@ type Status struct {
// a thread-safe manner // a thread-safe manner
type Cache struct { type Cache struct {
chats map[int64]*client.Chat chats map[int64]*client.Chat
users map[int32]*client.User users map[int64]*client.User
statuses map[int64]*Status statuses map[int64]*Status
chatsLock sync.Mutex chatsLock sync.Mutex
usersLock sync.Mutex usersLock sync.Mutex
@ -28,7 +28,7 @@ type Cache struct {
func NewCache() *Cache { func NewCache() *Cache {
return &Cache{ return &Cache{
chats: map[int64]*client.Chat{}, chats: map[int64]*client.Chat{},
users: map[int32]*client.User{}, users: map[int64]*client.User{},
statuses: map[int64]*Status{}, statuses: map[int64]*Status{},
} }
} }
@ -48,11 +48,11 @@ func (cache *Cache) ChatsKeys() []int64 {
// UsersKeys grabs user ids synchronously to avoid lockups // UsersKeys grabs user ids synchronously to avoid lockups
// while they are used // while they are used
func (cache *Cache) UsersKeys() []int32 { func (cache *Cache) UsersKeys() []int64 {
cache.usersLock.Lock() cache.usersLock.Lock()
defer cache.usersLock.Unlock() defer cache.usersLock.Unlock()
var keys []int32 var keys []int64
for id := range cache.users { for id := range cache.users {
keys = append(keys, id) keys = append(keys, id)
} }
@ -89,7 +89,7 @@ func (cache *Cache) GetChat(id int64) (*client.Chat, bool) {
} }
// GetUser retrieves user by id if it's present in the cache // GetUser retrieves user by id if it's present in the cache
func (cache *Cache) GetUser(id int32) (*client.User, bool) { func (cache *Cache) GetUser(id int64) (*client.User, bool) {
cache.usersLock.Lock() cache.usersLock.Lock()
defer cache.usersLock.Unlock() defer cache.usersLock.Unlock()
@ -115,7 +115,7 @@ func (cache *Cache) SetChat(id int64, chat *client.Chat) {
} }
// SetUser stores a user in the cache // SetUser stores a user in the cache
func (cache *Cache) SetUser(id int32, user *client.User) { func (cache *Cache) SetUser(id int64, user *client.User) {
cache.usersLock.Lock() cache.usersLock.Lock()
defer cache.usersLock.Unlock() defer cache.usersLock.Unlock()

View file

@ -87,7 +87,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
UseMessageDatabase: true, UseMessageDatabase: true,
UseSecretChats: conf.Tdlib.Client.UseSecretChats, UseSecretChats: conf.Tdlib.Client.UseSecretChats,
ApiId: int32(apiID), ApiID: int64(apiID),
ApiHash: conf.Tdlib.Client.APIHash, ApiHash: conf.Tdlib.Client.APIHash,
SystemLanguageCode: "en", SystemLanguageCode: "en",

View file

@ -3,7 +3,6 @@ package telegram
import ( import (
"fmt" "fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"math"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -134,8 +133,8 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
} }
} }
func (c *Client) usernameOrIDToID(username string) (int32, error) { func (c *Client) usernameOrIDToID(username string) (int64, error) {
userID, err := strconv.ParseInt(username, 10, 32) userID, err := strconv.ParseInt(username, 10, 64)
// couldn't parse the id, try to lookup as a username // couldn't parse the id, try to lookup as a username
if err != nil { if err != nil {
chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
@ -145,13 +144,13 @@ func (c *Client) usernameOrIDToID(username string) (int32, error) {
return 0, err return 0, err
} }
userID = chat.Id userID = chat.ID
if userID <= 0 || userID > math.MaxInt32 { if userID <= 0 {
return 0, errors.New("Not a user") return 0, errors.New("Not a user")
} }
} }
return int32(userID), nil return userID, nil
} }
// ProcessTransportCommand executes a command sent directly to the component // ProcessTransportCommand executes a command sent directly to the component
@ -345,10 +344,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
ChatId: chatID, ChatID: chatID,
Limit: limit, Limit: limit,
SenderUserId: c.me.Id, Sender: &client.MessageSenderUser{UserID: c.me.ID},
Filter: &client.SearchMessagesFilterEmpty{}, Filter: &client.SearchMessagesFilterEmpty{},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -358,13 +357,13 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
var messageIds []int64 var messageIds []int64
for _, message := range messages.Messages { for _, message := range messages.Messages {
if message != nil { if message != nil {
messageIds = append(messageIds, message.Id) messageIds = append(messageIds, message.ID)
} }
} }
_, err = c.client.DeleteMessages(&client.DeleteMessagesRequest{ _, err = c.client.DeleteMessages(&client.DeleteMessagesRequest{
ChatId: chatID, ChatID: chatID,
MessageIds: messageIds, MessageIDs: messageIds,
Revoke: true, Revoke: true,
}) })
if err != nil { if err != nil {
@ -384,10 +383,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
ChatId: chatID, ChatID: chatID,
Limit: 1, Limit: 1,
SenderUserId: c.me.Id, Sender: &client.MessageSenderUser{UserID: c.me.ID},
Filter: &client.SearchMessagesFilterEmpty{}, Filter: &client.SearchMessagesFilterEmpty{},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -407,7 +406,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
text := regex.ReplaceAllString(messageText.Text.Text, strings.Join(args[1:], " ")) text := regex.ReplaceAllString(messageText.Text.Text, strings.Join(args[1:], " "))
c.ProcessOutgoingMessage(chatID, text, message.Id, "") c.ProcessOutgoingMessage(chatID, text, message.ID, "")
// add @contact // add @contact
case "add": case "add":
if len(args) < 1 { if len(args) < 1 {
@ -427,7 +426,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
gateway.SendPresence( gateway.SendPresence(
c.xmpp, c.xmpp,
c.jid, c.jid,
gateway.SPFrom(strconv.FormatInt(chat.Id, 10)), gateway.SPFrom(strconv.FormatInt(chat.ID, 10)),
gateway.SPType("subscribe"), gateway.SPType("subscribe"),
) )
// join https://t.me/publichat // join https://t.me/publichat
@ -477,7 +476,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
_, err = c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{ _, err = c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
UserId: int32(chatID), UserID: chatID,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -490,7 +489,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
if chatID > 0 { if chatID > 0 {
_, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{ _, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
UserIds: []int32{int32(chatID)}, UserIDs: []int64{chatID},
Title: args[0], Title: args[0],
}) })
if err != nil { if err != nil {
@ -500,8 +499,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
// blacklists current user // blacklists current user
case "block": case "block":
if chatID > 0 { if chatID > 0 {
_, err := c.client.BlockUser(&client.BlockUserRequest{ _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
UserId: int32(chatID), Sender: &client.MessageSenderUser{UserID: chatID},
IsBlocked: true,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -510,8 +510,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
// unblacklists current user // unblacklists current user
case "unblock": case "unblock":
if chatID > 0 { if chatID > 0 {
_, err := c.client.UnblockUser(&client.UnblockUserRequest{ _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
UserId: int32(chatID), Sender: &client.MessageSenderUser{UserID: chatID},
IsBlocked: false,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -530,8 +531,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{ _, err = c.client.AddChatMember(&client.AddChatMemberRequest{
ChatId: chatID, ChatID: chatID,
UserId: userID, UserID: userID,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -550,8 +551,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID, ChatID: chatID,
UserId: userID, UserID: userID,
Status: &client.ChatMemberStatusLeft{}, Status: &client.ChatMemberStatusLeft{},
}) })
if err != nil { if err != nil {
@ -579,8 +580,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID, ChatID: chatID,
UserId: userID, UserID: userID,
Status: &client.ChatMemberStatusBanned{ Status: &client.ChatMemberStatusBanned{
BannedUntilDate: until, BannedUntilDate: until,
}, },
@ -599,7 +600,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
chatType := chat.Type.ChatTypeType() chatType := chat.Type.ChatTypeType()
if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup { if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup {
_, err = c.client.LeaveChat(&client.LeaveChatRequest{ _, err = c.client.LeaveChat(&client.LeaveChatRequest{
ChatId: chatID, ChatID: chatID,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -618,7 +619,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
if chatType == client.TypeChatTypeSecret { if chatType == client.TypeChatTypeSecret {
chatTypeSecret, _ := chat.Type.(*client.ChatTypeSecret) chatTypeSecret, _ := chat.Type.(*client.ChatTypeSecret)
_, err = c.client.CloseSecretChat(&client.CloseSecretChatRequest{ _, err = c.client.CloseSecretChat(&client.CloseSecretChatRequest{
SecretChatId: chatTypeSecret.SecretChatId, SecretChatID: chatTypeSecret.SecretChatID,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -629,7 +630,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
// delete current chat // delete current chat
case "delete": case "delete":
_, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{ _, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{
ChatId: chatID, ChatID: chatID,
RemoveFromChatList: true, RemoveFromChatList: true,
}) })
if err != nil { if err != nil {
@ -653,7 +654,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
ChatId: chatID, ChatID: chatID,
Query: query, Query: query,
Limit: limit, Limit: limit,
Filter: &client.SearchMessagesFilterEmpty{}, Filter: &client.SearchMessagesFilterEmpty{},
@ -674,7 +675,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
messages, err := c.client.GetChatHistory(&client.GetChatHistoryRequest{ messages, err := c.client.GetChatHistory(&client.GetChatHistoryRequest{
ChatId: chatID, ChatID: chatID,
Limit: limit, Limit: limit,
}) })
if err != nil { if err != nil {
@ -690,7 +691,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
members, err := c.client.SearchChatMembers(&client.SearchChatMembersRequest{ members, err := c.client.SearchChatMembers(&client.SearchChatMembersRequest{
ChatId: chatID, ChatID: chatID,
Limit: 9999, Limit: 9999,
Query: query, Query: query,
Filter: &client.ChatMembersFilterMembers{}, Filter: &client.ChatMembersFilterMembers{},
@ -701,9 +702,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
var entries []string var entries []string
for _, member := range members.Members { for _, member := range members.Members {
var senderId int64
switch member.MemberID.MessageSenderType() {
case client.TypeMessageSenderUser:
memberUser, _ := member.MemberID.(*client.MessageSenderUser)
senderId = memberUser.UserID
case client.TypeMessageSenderChat:
memberChat, _ := member.MemberID.(*client.MessageSenderChat)
senderId = memberChat.ChatID
}
entries = append(entries, fmt.Sprintf( entries = append(entries, fmt.Sprintf(
"%v | role: %v", "%v | role: %v",
c.formatContact(int64(member.UserId)), c.formatContact(senderId),
member.Status.ChatMemberStatusType(), member.Status.ChatMemberStatusType(),
)) ))
} }

View file

@ -119,15 +119,15 @@ func (c *Client) updateHandler() {
// new user discovered // new user discovered
func (c *Client) updateUser(update *client.UpdateUser) { func (c *Client) updateUser(update *client.UpdateUser) {
c.cache.SetUser(update.User.Id, update.User) c.cache.SetUser(update.User.ID, update.User)
show, status := c.userStatusToText(update.User.Status) show, status := c.userStatusToText(update.User.Status)
go c.ProcessStatusUpdate(int64(update.User.Id), status, show) go c.ProcessStatusUpdate(update.User.ID, status, show)
} }
// user status changed // user status changed
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
show, status := c.userStatusToText(update.Status) show, status := c.userStatusToText(update.Status)
go c.ProcessStatusUpdate(int64(update.UserId), status, show, gateway.SPImmed(false)) go c.ProcessStatusUpdate(update.UserID, status, show, gateway.SPImmed(false))
} }
// new chat discovered // new chat discovered
@ -135,7 +135,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
go func() { go func() {
if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil { if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil {
_, err := c.client.DownloadFile(&client.DownloadFileRequest{ _, err := c.client.DownloadFile(&client.DownloadFileRequest{
FileId: update.Chat.Photo.Small.Id, FileID: update.Chat.Photo.Small.ID,
Priority: 32, Priority: 32,
Synchronous: true, Synchronous: true,
}) })
@ -145,7 +145,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
} }
} }
c.cache.SetChat(update.Chat.Id, update.Chat) c.cache.SetChat(update.Chat.ID, update.Chat)
var isChannel = false var isChannel = false
if update.Chat.Type.ChatTypeType() == client.TypeChatTypeSupergroup { if update.Chat.Type.ChatTypeType() == client.TypeChatTypeSupergroup {
@ -157,21 +157,21 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
} }
// don't subscribe to channel posters // don't subscribe to channel posters
if !((isChannel && update.Chat.LastReadInboxMessageId == 0) || if !((isChannel && update.Chat.LastReadInboxMessageID == 0) ||
// don't subscribe to chats with no conversation // don't subscribe to chats with no conversation
// (manual adding will trigger a subscribe anyway) // (manual adding will trigger a subscribe anyway)
(update.Chat.LastReadInboxMessageId == 0 && update.Chat.LastReadOutboxMessageId == 0)) { (update.Chat.LastReadInboxMessageID == 0 && update.Chat.LastReadOutboxMessageID == 0)) {
gateway.SendPresence( gateway.SendPresence(
c.xmpp, c.xmpp,
c.jid, c.jid,
gateway.SPFrom(strconv.FormatInt(update.Chat.Id, 10)), gateway.SPFrom(strconv.FormatInt(update.Chat.ID, 10)),
gateway.SPType("subscribe"), gateway.SPType("subscribe"),
gateway.SPNickname(update.Chat.Title), gateway.SPNickname(update.Chat.Title),
) )
} }
if update.Chat.Id < 0 { if update.Chat.ID < 0 {
c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat") c.ProcessStatusUpdate(update.Chat.ID, update.Chat.Title, "chat")
} }
}() }()
} }
@ -180,7 +180,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
go func() { go func() {
// guarantee sequential message delivering per chat // guarantee sequential message delivering per chat
lock := c.getChatMessageLock(update.Message.ChatId) lock := c.getChatMessageLock(update.Message.ChatID)
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
@ -192,7 +192,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"chat_id": update.Message.ChatId, "chat_id": update.Message.ChatID,
}).Warn("New message from chat") }).Warn("New message from chat")
text := c.messageToText(update.Message) text := c.messageToText(update.Message)
@ -201,7 +201,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
// download file(s) // download file(s)
if file != nil && !file.Local.IsDownloadingCompleted { if file != nil && !file.Local.IsDownloadingCompleted {
c.client.DownloadFile(&client.DownloadFileRequest{ c.client.DownloadFile(&client.DownloadFileRequest{
FileId: file.Id, FileID: file.ID,
Priority: 32, Priority: 32,
Synchronous: true, Synchronous: true,
}) })
@ -212,9 +212,9 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename))) prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename)))
if text != "" { if text != "" {
// \n if it is groupchat and message is not empty // \n if it is groupchat and message is not empty
if update.Message.ChatId < 0 { if update.Message.ChatID < 0 {
prefix.WriteString("\n") prefix.WriteString("\n")
} else if update.Message.ChatId > 0 { } else if update.Message.ChatID > 0 {
prefix.WriteString(" | ") prefix.WriteString(" | ")
} }
@ -226,12 +226,12 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
// mark message as read // mark message as read
c.client.ViewMessages(&client.ViewMessagesRequest{ c.client.ViewMessages(&client.ViewMessagesRequest{
ChatId: update.Message.ChatId, ChatID: update.Message.ChatID,
MessageIds: []int64{update.Message.Id}, MessageIDs: []int64{update.Message.ID},
ForceRead: true, ForceRead: true,
}) })
// forward message to XMPP // forward message to XMPP
gateway.SendMessage(c.jid, strconv.FormatInt(update.Message.ChatId, 10), text, c.xmpp) gateway.SendMessage(c.jid, strconv.FormatInt(update.Message.ChatID, 10), text, c.xmpp)
}() }()
} }
@ -240,20 +240,20 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
markupFunction := formatter.EntityToMarkdown markupFunction := formatter.EntityToMarkdown
if update.NewContent.MessageContentType() == client.TypeMessageText { if update.NewContent.MessageContentType() == client.TypeMessageText {
textContent := update.NewContent.(*client.MessageText) textContent := update.NewContent.(*client.MessageText)
text := fmt.Sprintf("✎ %v | %s", update.MessageId, formatter.Format( text := fmt.Sprintf("✎ %v | %s", update.MessageID, formatter.Format(
textContent.Text.Text, textContent.Text.Text,
formatter.SortEntities(textContent.Text.Entities), formatter.SortEntities(textContent.Text.Entities),
markupFunction, markupFunction,
)) ))
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatID, 10), text, c.xmpp)
} }
} }
// message(s) deleted // message(s) deleted
func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
if update.IsPermanent { if update.IsPermanent {
text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",") text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIDs), ",")
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatID, 10), text, c.xmpp)
} }
} }
@ -269,7 +269,7 @@ func (c *Client) updateFile(update *client.UpdateFile) {
fmt.Sprintf( fmt.Sprintf(
"%s/%s%s", "%s/%s%s",
c.content.Path, c.content.Path,
fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.Id))), fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.ID))),
filepath.Ext(update.File.Local.Path), filepath.Ext(update.File.Local.Path),
), ),
) )

View file

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"io" "io"
"math"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -44,7 +43,7 @@ func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.Us
return nil, nil, err 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) // 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 ok bool
var err error var err error
if id <= math.MaxInt32 && id >= math.MinInt32 { user, ok = c.cache.GetUser(id)
userID := int32(id) if !ok && id > 0 {
user, ok = c.cache.GetUser(userID) user, err = c.client.GetUser(&client.GetUserRequest{
if !ok && userID > 0 { UserID: id,
user, err = c.client.GetUser(&client.GetUserRequest{ })
UserId: userID, if err == nil {
}) c.cache.SetUser(id, user)
if err != nil {
return nil, nil, err
}
c.cache.SetUser(userID, user)
} }
} }
@ -77,7 +71,7 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
if !ok { if !ok {
if chat == nil { if chat == nil {
cacheChat, err = c.client.GetChat(&client.GetChatRequest{ cacheChat, err = c.client.GetChat(&client.GetChatRequest{
ChatId: id, ChatID: id,
}) })
if err != nil { if err != nil {
// error is irrelevant if the user was found successfully // error is irrelevant if the user was found successfully
@ -200,11 +194,11 @@ func (c *Client) formatContact(chatID int64) string {
var str string var str string
if chat != nil { 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 { } else if user != nil {
username := user.Username username := user.Username
if 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) 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 var err error
if message == nil { if message == nil {
message, err = c.client.GetMessage(&client.GetMessageRequest{ message, err = c.client.GetMessage(&client.GetMessageRequest{
ChatId: chatID, ChatID: chatID,
MessageId: messageID, MessageID: messageID,
}) })
if err != nil { if err != nil {
return fmt.Sprintf("<error fetching message: %s>", err.Error()) 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 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 { if !preview {
str.WriteString( str.WriteString(
time.Unix(int64(message.Date), 0). time.Unix(int64(message.Date), 0).
@ -276,7 +279,7 @@ func (c *Client) formatContent(file *client.File, filename string) string {
filename, filename,
file.Size/1024, file.Size/1024,
c.content.Link, c.content.Link,
fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.Id))), fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.ID))),
filepath.Ext(filename), filepath.Ext(filename),
) )
} }
@ -295,17 +298,17 @@ func (c *Client) messageToText(message *client.Message) string {
addMembers, _ := message.Content.(*client.MessageChatAddMembers) addMembers, _ := message.Content.(*client.MessageChatAddMembers)
text := "invited " text := "invited "
if len(addMembers.MemberUserIds) > 0 { if len(addMembers.MemberUserIDs) > 0 {
text += c.formatContact(int64(addMembers.MemberUserIds[0])) text += c.formatContact(addMembers.MemberUserIDs[0])
} }
return text return text
case client.TypeMessageChatDeleteMember: case client.TypeMessageChatDeleteMember:
deleteMember, _ := message.Content.(*client.MessageChatDeleteMember) deleteMember, _ := message.Content.(*client.MessageChatDeleteMember)
return "kicked " + c.formatContact(int64(deleteMember.UserId)) return "kicked " + c.formatContact(deleteMember.UserID)
case client.TypeMessagePinMessage: case client.TypeMessagePinMessage:
pinMessage, _ := message.Content.(*client.MessagePinMessage) 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: case client.TypeMessageChatChangeTitle:
changeTitle, _ := message.Content.(*client.MessageChatChangeTitle) changeTitle, _ := message.Content.(*client.MessageChatChangeTitle)
return "chat title set to: " + changeTitle.Title return "chat title set to: " + changeTitle.Title
@ -393,7 +396,7 @@ func (c *Client) contentToFilename(content client.MessageContent) (*client.File,
sizes := photo.Photo.Sizes sizes := photo.Photo.Sizes
if len(sizes) > 1 { if len(sizes) > 1 {
file := sizes[len(sizes)-1].Photo 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, "" return nil, ""
case client.TypeMessageAudio: case client.TypeMessageAudio:
@ -419,16 +422,32 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
} else { } else {
directionChar = "⬅ " directionChar = "⬅ "
} }
prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10)) prefix = append(prefix, directionChar+strconv.FormatInt(message.ID, 10))
// show sender in group chats // show sender in group chats
if message.ChatId < 0 && message.SenderUserId != 0 { if message.ChatID < 0 && message.Sender != nil {
prefix = append(prefix, 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
}
prefix = append(prefix, c.formatContact(senderId))
} }
if message.ForwardInfo != nil { if message.ForwardInfo != nil {
switch message.ForwardInfo.Origin.MessageForwardOriginType() { switch message.ForwardInfo.Origin.MessageForwardOriginType() {
case client.TypeMessageForwardOriginUser: case client.TypeMessageForwardOriginUser:
originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginUser) 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: case client.TypeMessageForwardOriginHiddenUser:
originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginHiddenUser) originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginHiddenUser)
prefix = append(prefix, fmt.Sprintf("fwd: anonymous (%s)", originUser.SenderName)) 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 != "" { if channel.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", 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 // 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 fileString != "" { if fileString != "" {
prefix = append(prefix, "file: "+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{ c.client.EditMessageText(&client.EditMessageTextRequest{
ChatId: chatID, ChatID: chatID,
MessageId: messageID, MessageID: messageID,
InputMessageContent: message, InputMessageContent: message,
}) })
} else { } else {
@ -500,7 +519,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
var file *client.InputFileRemote var file *client.InputFileRemote
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
file = &client.InputFileRemote{ 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{ _, err := c.client.SendMessage(&client.SendMessageRequest{
ChatId: chatID, ChatID: chatID,
ReplyToMessageId: reply, ReplyToMessageID: reply,
InputMessageContent: message, InputMessageContent: message,
}) })
if err != nil { if err != nil {