Send queued presences, handle presence sending errors

This commit is contained in:
bodqhrohro 2019-11-16 20:44:13 +02:00
parent fc5f8d7a65
commit e4c9267347

View file

@ -3,6 +3,7 @@ package xmpp
import (
"encoding/xml"
"github.com/pkg/errors"
"time"
"dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/persistence"
@ -56,16 +57,36 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
cm := xmpp.NewStreamManager(component, nil)
go maintenance(component)
go heartbeat(component)
return cm, nil
}
func maintenance(component *xmpp.Component) {
func logPresence(err error, presence *stanza.Presence) {
log.WithFields(log.Fields{
"presence": *presence,
}).Error(errors.Wrap(err, "Couldn't send presence"))
}
func heartbeat(component *xmpp.Component) {
var err error
probeType := SPType("probe")
for jid := range sessions {
sendPresence(component, jid, probeType)
}
for {
for key, presence := range queue {
err = component.Send(presence)
if err != nil {
logPresence(err, presence)
} else {
delete(queue, key)
}
}
time.Sleep(60e9)
}
}
func loadSessions(dbPath string) error {
@ -186,7 +207,10 @@ func sendPresence(component *xmpp.Component, to string, args ...args.V) {
immed := SPImmed.Get(args)
if immed {
component.Send(presence)
err := component.Send(presence)
if err != nil {
logPresence(err, &presence)
}
} else {
queue[presence.From+presence.To] = &presence
}