From 9c28af848d3adc9e998b6456b3ebc71172ca68fd Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Tue, 16 May 2023 18:17:44 -0400 Subject: Calls [WIP] --- telegram/cache/cache.go | 19 +++++++++++++++++++ telegram/utils.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) (limited to 'telegram') diff --git a/telegram/cache/cache.go b/telegram/cache/cache.go index 3d9608d..c4a59bf 100644 --- a/telegram/cache/cache.go +++ b/telegram/cache/cache.go @@ -19,9 +19,11 @@ type Cache struct { chats map[int64]*client.Chat users map[int64]*client.User statuses map[int64]*Status + capsVers map[int64]string chatsLock sync.Mutex usersLock sync.Mutex statusesLock sync.Mutex + capsVersLock sync.Mutex } // NewCache initializes a cache @@ -106,6 +108,15 @@ func (cache *Cache) GetStatus(id int64) (*Status, bool) { return status, ok } +// GetCapsVer retrieves capabilities verification string by id if it's present in the cache +func (cache *Cache) GetCapsVer(id int64) (string, bool) { + cache.capsVersLock.Lock() + defer cache.capsVersLock.Unlock() + + ver, ok := cache.capsVers[id] + return ver, ok +} + // SetChat stores a chat in the cache func (cache *Cache) SetChat(id int64, chat *client.Chat) { cache.chatsLock.Lock() @@ -133,3 +144,11 @@ func (cache *Cache) SetStatus(id int64, show string, status string) { Description: status, } } + +// SetCapsVer stores a capabilities verification string in the cache +func (cache *Cache) SetCapsVer(id int64, ver string) { + cache.capsVersLock.Lock() + defer cache.capsVersLock.Unlock() + + cache.capsVers[id] = ver +} diff --git a/telegram/utils.go b/telegram/utils.go index 9a247c4..7fa13d3 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -253,6 +253,20 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o newArgs = append(newArgs, gateway.SPType(presenceType)) } + ver, ok := c.cache.GetCapsVer(chatID) + if !ok { + if c.isCallable(chat, user) { + ver, err = gateway.GetCapsVer([]gateway.CapsType{gateway.CapsAudio}}) + if err != nil { + log.Errorf("", err.Error()) + } + } + c.cache.SetCapsVer(ver) + } + if ver != "" { + newArgs = append(newArgs, gateway.SPCaps(ver)) + } + return gateway.SendPresence( c.xmpp, c.jid, @@ -1248,3 +1262,23 @@ func (c *Client) prepareDiskSpace(size uint64) { } } } + +func (c *Client) isCallable(chat *client.Chat, user, *client.User) bool { + if chat == nil || user == nil { + return false + } + chatType := chat.Type.ChatTypeType() + if chatType == client.TypeChatTypePrivate { + privateType, _ := chat.Type.(*client.ChatTypePrivate) + fullInfo, err := c.client.GetUserFullInfo(&client.GetUserFullInfoRequest{ + UserId: privateType.UserId, + }) + if err == nil { + return fullInfo.CanBeCalled && (user.Username != "" || user.PhoneNumber != "") + } else { + log.Warnf("Coudln't retrieve private chat info: %v", err.Error()) + } + } + + return false +} -- cgit v1.2.3