Fix breaking the TDlib data on graceful exit
This commit is contained in:
parent
0274f47433
commit
e43a0c3144
|
@ -44,6 +44,7 @@ type Client struct {
|
||||||
parameters *client.TdlibParameters
|
parameters *client.TdlibParameters
|
||||||
logVerbosity client.Option
|
logVerbosity client.Option
|
||||||
me *client.User
|
me *client.User
|
||||||
|
listener *client.Listener
|
||||||
|
|
||||||
xmpp *xmpp.Component
|
xmpp *xmpp.Component
|
||||||
jid string
|
jid string
|
||||||
|
@ -52,7 +53,6 @@ type Client struct {
|
||||||
cache *cache
|
cache *cache
|
||||||
|
|
||||||
locks clientLocks
|
locks clientLocks
|
||||||
online bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientLocks struct {
|
type clientLocks struct {
|
||||||
|
|
|
@ -87,7 +87,7 @@ func (stateHandler *clientAuthorizer) Close() {
|
||||||
|
|
||||||
// Connect starts TDlib connection
|
// Connect starts TDlib connection
|
||||||
func (c *Client) Connect() error {
|
func (c *Client) Connect() error {
|
||||||
if c.online {
|
if c.Online() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,8 +113,8 @@ func (c *Client) Connect() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.client = tdlibClient
|
c.client = tdlibClient
|
||||||
c.online = true
|
|
||||||
c.locks.authorizationReady.Done()
|
c.locks.authorizationReady.Done()
|
||||||
|
c.listener = tdlibClient.GetListener()
|
||||||
|
|
||||||
go c.updateHandler()
|
go c.updateHandler()
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ func (c *Client) Connect() error {
|
||||||
// Disconnect drops TDlib connection
|
// Disconnect drops TDlib connection
|
||||||
func (c *Client) Disconnect() {
|
func (c *Client) Disconnect() {
|
||||||
// already disconnected
|
// already disconnected
|
||||||
if !c.online {
|
if !c.Online() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,14 +140,17 @@ func (c *Client) Disconnect() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.client.Destroy()
|
_, err := c.client.Close()
|
||||||
c.online = false
|
if err != nil {
|
||||||
|
log.Fatalf("Couldn't close the Telegram instance: %#v", c)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) interactor() {
|
func (c *Client) interactor() {
|
||||||
for {
|
for {
|
||||||
state, ok := <-c.authorizer.State
|
state, ok := <-c.authorizer.State
|
||||||
if !ok {
|
if !ok {
|
||||||
|
log.Error("Interactor is disconnected")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,8 +195,14 @@ func (c *Client) interactor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in "+c.Session.Login))
|
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in "+c.Session.Login))
|
||||||
|
case client.TypeAuthorizationStateClosed:
|
||||||
return
|
log.Warn("Closing the updates listener")
|
||||||
|
c.listener.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Online checks if the updates listener is alive
|
||||||
|
func (c *Client) Online() bool {
|
||||||
|
return c.listener != nil && c.listener.IsActive()
|
||||||
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ const newlineChar string = "\n"
|
||||||
|
|
||||||
// GetContactByUsername resolves username to user id retrieves user and chat information
|
// GetContactByUsername resolves username to user id retrieves user and chat information
|
||||||
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
|
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
|
||||||
if !c.online {
|
if !c.Online() {
|
||||||
return nil, nil, errOffline
|
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)
|
// 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) {
|
func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *client.User, error) {
|
||||||
if !c.online {
|
if !c.Online() {
|
||||||
return nil, nil, errOffline
|
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 {
|
func (c *Client) processStatusUpdate(chatID int64, status string, show string, args ...args.V) error {
|
||||||
if !c.online {
|
if !c.Online() {
|
||||||
return nil
|
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
|
// we're offline
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue