From 462a537021f471579877eceb1dd6c47154d8052d Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Fri, 31 Dec 2021 18:34:04 -0500 Subject: [PATCH] Fix subscriptions after logging in --- telegram/connect.go | 50 +++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/telegram/connect.go b/telegram/connect.go index 9937970..7221453 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -117,9 +117,31 @@ func (c *Client) Connect() error { c.client = tdlibClient c.locks.authorizationReady.Done() - c.online = true + + // stage 3: if a client is succesfully created, AuthorizationStateReady is already reached + log.Warn("Authorization successful!") + + c.me, err = c.client.GetMe() + if err != nil { + log.Error("Could not retrieve me info") + } else if c.Session.Login == "" { + c.Session.Login = c.me.PhoneNumber + } go c.updateHandler() + c.online = true + + _, err = c.client.GetChats(&client.GetChatsRequest{ + OffsetOrder: client.JsonInt64(math.MaxInt64), + Limit: chatsLimit, + }) + if err != nil { + log.Errorf("Could not retrieve chats: %v", err) + } + + gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribe")) + gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribed")) + gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login)) return nil } @@ -179,32 +201,6 @@ func (c *Client) interactor() { case client.TypeAuthorizationStateWaitPassword: log.Warn("Waiting for 2FA password...") gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp) - // stage 3: auth completed - case client.TypeAuthorizationStateReady: - var err error - - c.locks.authorizationReady.Wait() - - log.Warn("Authorization successful!") - - c.me, err = c.client.GetMe() - if err != nil { - log.Error("Could not retrieve me info") - } else if c.Session.Login == "" { - c.Session.Login = c.me.PhoneNumber - } - - _, err = c.client.GetChats(&client.GetChatsRequest{ - OffsetOrder: client.JsonInt64(math.MaxInt64), - Limit: chatsLimit, - }) - if err != nil { - log.Errorf("Could not retrieve chats: %v", err) - } - - gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in "+c.Session.Login)) - - return } } }