diff options
Diffstat (limited to 'xmpp/component.go')
-rw-r--r-- | xmpp/component.go | 47 |
1 files changed, 45 insertions, 2 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 +} |