aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--telegram/client.go4
-rw-r--r--telegram/connect.go23
-rw-r--r--telegram/utils.go8
3 files changed, 22 insertions, 13 deletions
diff --git a/telegram/client.go b/telegram/client.go
index f15d07d..5e9dd9f 100644
--- a/telegram/client.go
+++ b/telegram/client.go
@@ -44,6 +44,7 @@ type Client struct {
parameters *client.TdlibParameters
logVerbosity client.Option
me *client.User
+ listener *client.Listener
xmpp *xmpp.Component
jid string
@@ -51,8 +52,7 @@ type Client struct {
content *config.TelegramContentConfig
cache *cache
- locks clientLocks
- online bool
+ locks clientLocks
}
type clientLocks struct {
diff --git a/telegram/connect.go b/telegram/connect.go
index 7e9154c..b19e4ed 100644
--- a/telegram/connect.go
+++ b/telegram/connect.go
@@ -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))
-
- return
+ case client.TypeAuthorizationStateClosed:
+ 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()
+}
diff --git a/telegram/utils.go b/telegram/utils.go
index eb7b905..ac100fd 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -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
}