aboutsummaryrefslogtreecommitdiff
path: root/xmpp
diff options
context:
space:
mode:
Diffstat (limited to 'xmpp')
-rw-r--r--xmpp/component.go47
-rw-r--r--xmpp/handlers.go11
2 files changed, 47 insertions, 11 deletions
diff --git a/xmpp/component.go b/xmpp/component.go
index fcef91b..a341a2e 100644
--- a/xmpp/component.go
+++ b/xmpp/component.go
@@ -1,10 +1,13 @@
package xmpp
import (
+ "github.com/pkg/errors"
+
"dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/persistence"
"dev.narayana.im/narayana/telegabber/telegram"
+ log "github.com/sirupsen/logrus"
"gosrc.io/xmpp"
)
@@ -24,9 +27,8 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
}
tgConf = tc
- sessions = make(map[string]telegram.Client)
- db, err = persistence.LoadSessions(conf.Db)
+ err = loadSessions(conf.Db)
if err != nil {
return nil, err
}
@@ -50,5 +52,46 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
cm := xmpp.NewStreamManager(component, nil)
+ go maintenance()
+
return cm, nil
}
+
+func maintenance() {
+ // TODO
+}
+
+func loadSessions(dbPath string) error {
+ var err error
+
+ sessions = make(map[string]telegram.Client)
+
+ db, err = persistence.LoadSessions(dbPath)
+ if err != nil {
+ return err
+ }
+
+ db.Transaction(func() bool {
+ for jid, session := range db.Data.Sessions {
+ getTelegramInstance(jid, &session)
+ }
+
+ return false
+ }, persistence.SessionMarshaller)
+
+ return nil
+}
+
+func getTelegramInstance(jid string, savedSession *persistence.Session) (telegram.Client, bool) {
+ session, ok := sessions[jid]
+ if !ok {
+ session, err := telegram.NewClient(tgConf, jid, savedSession)
+ if err != nil {
+ log.Error(errors.Wrap(err, "TDlib initialization failure"))
+ return session, false
+ }
+ sessions[jid] = session
+ }
+
+ return session, true
+}
diff --git a/xmpp/handlers.go b/xmpp/handlers.go
index ad6e3ba..c19a4ef 100644
--- a/xmpp/handlers.go
+++ b/xmpp/handlers.go
@@ -3,8 +3,6 @@ package xmpp
import (
"github.com/pkg/errors"
- "dev.narayana.im/narayana/telegabber/telegram"
-
log "github.com/sirupsen/logrus"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
@@ -90,14 +88,9 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
return
}
bareFromJid := fromJid.Bare()
- session, ok := sessions[bareFromJid]
+ session, ok := getTelegramInstance(bareFromJid, nil)
if !ok {
- client, err := telegram.NewClient(tgConf, bareFromJid)
- if err != nil {
- log.Error(errors.Wrap(err, "TDlib initialization failure"))
- return
- }
- sessions[bareFromJid] = client
+ return
}
switch p.Type {