Migrate some comments from Zhabogram; change queue behaviour
This commit is contained in:
parent
915b7fe1de
commit
ed8fd6415c
|
@ -56,6 +56,8 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
<-cleanupDone
|
<-cleanupDone
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
// bye
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -129,6 +129,7 @@ func (c *Client) userStatusToText(status client.UserStatus) (string, string) {
|
||||||
return show, textStatus
|
return show, textStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set contact status
|
||||||
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
|
||||||
|
|
|
@ -46,6 +46,7 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// probe all known sessions
|
||||||
err = loadSessions(conf.Db, component)
|
err = loadSessions(conf.Db, component)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
@ -71,7 +72,9 @@ func heartbeat(component *xmpp.Component) {
|
||||||
|
|
||||||
log.Info("Starting heartbeat queue")
|
log.Info("Starting heartbeat queue")
|
||||||
|
|
||||||
|
// status updater thread
|
||||||
for {
|
for {
|
||||||
|
time.Sleep(60e9)
|
||||||
for key, presence := range gateway.Queue {
|
for key, presence := range gateway.Queue {
|
||||||
err = component.Send(presence)
|
err = component.Send(presence)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -80,7 +83,6 @@ func heartbeat(component *xmpp.Component) {
|
||||||
delete(gateway.Queue, key)
|
delete(gateway.Queue, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(60e9)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,10 +126,12 @@ func getTelegramInstance(jid string, savedSession *persistence.Session, componen
|
||||||
func Close(component *xmpp.Component) {
|
func Close(component *xmpp.Component) {
|
||||||
log.Error("Disconnecting...")
|
log.Error("Disconnecting...")
|
||||||
|
|
||||||
|
// close all sessions
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
session.Disconnect()
|
session.Disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// save sessions
|
||||||
db.Transaction(func() bool {
|
db.Transaction(func() bool {
|
||||||
for jid, session := range sessions {
|
for jid, session := range sessions {
|
||||||
db.Data.Sessions[jid] = *session.Session
|
db.Data.Sessions[jid] = *session.Session
|
||||||
|
@ -136,5 +140,6 @@ func Close(component *xmpp.Component) {
|
||||||
return true
|
return true
|
||||||
}, persistence.SessionMarshaller)
|
}, persistence.SessionMarshaller)
|
||||||
|
|
||||||
|
// close stream
|
||||||
component.Disconnect()
|
component.Disconnect()
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,6 +148,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
|
||||||
}).Warn("Presence")
|
}).Warn("Presence")
|
||||||
log.Debugf("%#v", p)
|
log.Debugf("%#v", p)
|
||||||
|
|
||||||
|
// create session
|
||||||
fromJid, err := xmpp.NewJid(p.From)
|
fromJid, err := xmpp.NewJid(p.From)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Invalid from JID!")
|
log.Error("Invalid from JID!")
|
||||||
|
@ -160,11 +161,14 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch p.Type {
|
switch p.Type {
|
||||||
|
// destroy session
|
||||||
case "unsubscribed", "unsubscribe":
|
case "unsubscribed", "unsubscribe":
|
||||||
session.Disconnect()
|
session.Disconnect()
|
||||||
delete(sessions, bareFromJid)
|
delete(sessions, bareFromJid)
|
||||||
|
// go offline
|
||||||
case "unavailable", "error":
|
case "unavailable", "error":
|
||||||
session.Disconnect()
|
session.Disconnect()
|
||||||
|
// go online
|
||||||
case "probe", "", "online":
|
case "probe", "", "online":
|
||||||
// due to the weird implementation of go-tdlib wrapper, it won't
|
// due to the weird implementation of go-tdlib wrapper, it won't
|
||||||
// return the client instance until successful authorization
|
// return the client instance until successful authorization
|
||||||
|
|
Loading…
Reference in a new issue