Migrate some comments from Zhabogram; change queue behaviour

This commit is contained in:
bodqhrohro 2019-12-22 03:04:45 +02:00
parent 915b7fe1de
commit ed8fd6415c
4 changed files with 13 additions and 1 deletions

View file

@ -56,6 +56,8 @@ func main() {
if err != nil {
<-cleanupDone
log.Fatal(err)
// bye
os.Exit(-1)
}
}

View file

@ -129,6 +129,7 @@ func (c *Client) userStatusToText(status client.UserStatus) (string, string) {
return show, textStatus
}
// set contact status
func (c *Client) processStatusUpdate(chatID int64, status string, show string, args ...args.V) error {
if !c.Online() {
return nil

View file

@ -46,6 +46,7 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
return nil, nil, err
}
// probe all known sessions
err = loadSessions(conf.Db, component)
if err != nil {
return nil, nil, err
@ -71,7 +72,9 @@ func heartbeat(component *xmpp.Component) {
log.Info("Starting heartbeat queue")
// status updater thread
for {
time.Sleep(60e9)
for key, presence := range gateway.Queue {
err = component.Send(presence)
if err != nil {
@ -80,7 +83,6 @@ func heartbeat(component *xmpp.Component) {
delete(gateway.Queue, key)
}
}
time.Sleep(60e9)
}
}
@ -124,10 +126,12 @@ func getTelegramInstance(jid string, savedSession *persistence.Session, componen
func Close(component *xmpp.Component) {
log.Error("Disconnecting...")
// close all sessions
for _, session := range sessions {
session.Disconnect()
}
// save sessions
db.Transaction(func() bool {
for jid, session := range sessions {
db.Data.Sessions[jid] = *session.Session
@ -136,5 +140,6 @@ func Close(component *xmpp.Component) {
return true
}, persistence.SessionMarshaller)
// close stream
component.Disconnect()
}

View file

@ -148,6 +148,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
}).Warn("Presence")
log.Debugf("%#v", p)
// create session
fromJid, err := xmpp.NewJid(p.From)
if err != nil {
log.Error("Invalid from JID!")
@ -160,11 +161,14 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
}
switch p.Type {
// destroy session
case "unsubscribed", "unsubscribe":
session.Disconnect()
delete(sessions, bareFromJid)
// go offline
case "unavailable", "error":
session.Disconnect()
// go online
case "probe", "", "online":
// due to the weird implementation of go-tdlib wrapper, it won't
// return the client instance until successful authorization