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 { type Client struct {
client *client.Client client *client.Client
authorizer *clientAuthorizer authorizer *clientAuthorizer
xmpp *xmpp.Component
jid string
parameters *client.TdlibParameters parameters *client.TdlibParameters
Session *persistence.Session
online bool
logVerbosity client.Option logVerbosity client.Option
me *client.User
xmpp *xmpp.Component
jid string
Session *persistence.Session
ready chan bool
online bool
} }
// NewClient instantiates a Telegram App // NewClient instantiates a Telegram App
@ -84,6 +88,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
jid: jid, jid: jid,
Session: session, Session: session,
logVerbosity: logVerbosity, logVerbosity: logVerbosity,
ready: make(chan bool),
}, nil }, nil
} }

View file

@ -2,6 +2,7 @@ package telegram
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
"math"
"dev.narayana.im/narayana/telegabber/xmpp/gateway" "dev.narayana.im/narayana/telegabber/xmpp/gateway"
@ -9,6 +10,8 @@ import (
"github.com/zelenin/go-tdlib/client" "github.com/zelenin/go-tdlib/client"
) )
const chatsLimit int32 = 999
type clientAuthorizer struct { type clientAuthorizer struct {
TdlibParameters chan *client.TdlibParameters TdlibParameters chan *client.TdlibParameters
PhoneNumber chan string PhoneNumber chan string
@ -108,6 +111,7 @@ func (c *Client) Connect() error {
c.client = tdlibClient c.client = tdlibClient
c.online = true c.online = true
c.ready <- true
go updateHandler(c.client) go updateHandler(c.client)
@ -153,8 +157,29 @@ func (c *Client) interactor() {
log.Warn("Waiting for 2FA password...") log.Warn("Waiting for 2FA password...")
gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp) gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp)
case client.TypeAuthorizationStateReady: case client.TypeAuthorizationStateReady:
var err error
<-c.ready
log.Warn("Authorization successful!") 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 return
} }
} }