Fetch user info and chats on successful authorization

This commit is contained in:
bodqhrohro 2019-11-27 00:14:06 +02:00
parent 9a292fba94
commit bcf222b53d
2 changed files with 35 additions and 5 deletions

View file

@ -36,12 +36,16 @@ func stringToLogConstant(c string) int32 {
type Client struct {
client *client.Client
authorizer *clientAuthorizer
parameters *client.TdlibParameters
logVerbosity client.Option
me *client.User
xmpp *xmpp.Component
jid string
parameters *client.TdlibParameters
Session *persistence.Session
ready chan bool
online bool
logVerbosity client.Option
}
// 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
}

View file

@ -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
}
}