Fix breaking the TDlib data on graceful exit

This commit is contained in:
bodqhrohro 2019-12-15 04:26:07 +02:00
parent 0274f47433
commit e43a0c3144
3 changed files with 22 additions and 13 deletions

View file

@ -44,6 +44,7 @@ type Client struct {
parameters *client.TdlibParameters
logVerbosity client.Option
me *client.User
listener *client.Listener
xmpp *xmpp.Component
jid string
@ -52,7 +53,6 @@ type Client struct {
cache *cache
locks clientLocks
online bool
}
type clientLocks struct {

View file

@ -87,7 +87,7 @@ func (stateHandler *clientAuthorizer) Close() {
// Connect starts TDlib connection
func (c *Client) Connect() error {
if c.online {
if c.Online() {
return nil
}
@ -113,8 +113,8 @@ func (c *Client) Connect() error {
}
c.client = tdlibClient
c.online = true
c.locks.authorizationReady.Done()
c.listener = tdlibClient.GetListener()
go c.updateHandler()
@ -124,7 +124,7 @@ func (c *Client) Connect() error {
// Disconnect drops TDlib connection
func (c *Client) Disconnect() {
// already disconnected
if !c.online {
if !c.Online() {
return
}
@ -140,14 +140,17 @@ func (c *Client) Disconnect() {
)
}
c.client.Destroy()
c.online = false
_, err := c.client.Close()
if err != nil {
log.Fatalf("Couldn't close the Telegram instance: %#v", c)
}
}
func (c *Client) interactor() {
for {
state, ok := <-c.authorizer.State
if !ok {
log.Error("Interactor is disconnected")
return
}
@ -192,8 +195,14 @@ func (c *Client) interactor() {
}
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in "+c.Session.Login))
case client.TypeAuthorizationStateClosed:
log.Warn("Closing the updates listener")
c.listener.Close()
}
}
}
return
}
}
// Online checks if the updates listener is alive
func (c *Client) Online() bool {
return c.listener != nil && c.listener.IsActive()
}

View file

@ -30,7 +30,7 @@ const newlineChar string = "\n"
// GetContactByUsername resolves username to user id retrieves user and chat information
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
if !c.online {
if !c.Online() {
return nil, nil, errOffline
}
@ -47,7 +47,7 @@ func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.Us
// GetContactByID gets user and chat information from cache (or tries to retrieve it, if missing)
func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *client.User, error) {
if !c.online {
if !c.Online() {
return nil, nil, errOffline
}
@ -130,7 +130,7 @@ func (c *Client) userStatusToText(status client.UserStatus) (string, string) {
}
func (c *Client) processStatusUpdate(chatID int64, status string, show string, args ...args.V) error {
if !c.online {
if !c.Online() {
return nil
}
@ -432,7 +432,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
}
}
if !c.online {
if !c.Online() {
// we're offline
return
}