aboutsummaryrefslogtreecommitdiff
path: root/xmpp
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-11-16 21:44:13 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-11-16 21:44:13 +0300
commite4c9267347c4aebf3fab55ef9d90056f265ef6f0 (patch)
tree87d51185a4601a9f0c8266eb195abd30af9627c5 /xmpp
parentfc5f8d7a65d088947d4357b7c1787f0d746fca4a (diff)
Send queued presences, handle presence sending errors
Diffstat (limited to 'xmpp')
-rw-r--r--xmpp/component.go30
1 files changed, 27 insertions, 3 deletions
diff --git a/xmpp/component.go b/xmpp/component.go
index 3e85f28..55e7a7b 100644
--- a/xmpp/component.go
+++ b/xmpp/component.go
@@ -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
}