diff --git a/telegram/client.go b/telegram/client.go index 072f4b9..6a738af 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -36,12 +36,16 @@ func stringToLogConstant(c string) int32 { type Client struct { client *client.Client authorizer *clientAuthorizer - xmpp *xmpp.Component - jid string parameters *client.TdlibParameters - Session *persistence.Session - online bool logVerbosity client.Option + me *client.User + + xmpp *xmpp.Component + jid string + Session *persistence.Session + + ready chan bool + online bool } // NewClient instantiates a Telegram App @@ -84,6 +88,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component jid: jid, Session: session, logVerbosity: logVerbosity, + ready: make(chan bool), }, nil } diff --git a/telegram/connect.go b/telegram/connect.go index 4f509d2..4fe262c 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -2,6 +2,7 @@ package telegram import ( "github.com/pkg/errors" + "math" "dev.narayana.im/narayana/telegabber/xmpp/gateway" @@ -9,6 +10,8 @@ import ( "github.com/zelenin/go-tdlib/client" ) +const chatsLimit int32 = 999 + type clientAuthorizer struct { TdlibParameters chan *client.TdlibParameters PhoneNumber chan string @@ -108,6 +111,7 @@ func (c *Client) Connect() error { c.client = tdlibClient c.online = true + c.ready <- true go updateHandler(c.client) @@ -153,8 +157,29 @@ func (c *Client) interactor() { log.Warn("Waiting for 2FA password...") gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp) case client.TypeAuthorizationStateReady: + var err error + + <-c.ready + log.Warn("Authorization successful!") - // TODO + + 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.Error("Could not retrieve chats") + } + + gateway.SendPresence(c.xmpp, nil, c.jid, gateway.SPStatus("Logged in "+c.Session.Login)) + return } }