Spawn TDlib instances for recovered sessions

This commit is contained in:
bodqhrohro 2019-11-12 17:50:25 +02:00
parent fbe99c65ec
commit a5c97d68e7
5 changed files with 68 additions and 32 deletions

View file

@ -27,8 +27,8 @@ type Session struct {
var sessionDB SessionsYamlDB var sessionDB SessionsYamlDB
// Marshaller implementation for YamlDB // SessionMarshaller implementation for YamlDB
func Marshaller() ([]byte, error) { func SessionMarshaller() ([]byte, error) {
return yaml.Marshal(sessionDB.Data) return yaml.Marshal(sessionDB.Data)
} }
@ -41,9 +41,6 @@ func LoadSessions(path string) (SessionsYamlDB, error) {
return sessionDB, errors.Wrap(err, "Sessions restore error") return sessionDB, errors.Wrap(err, "Sessions restore error")
} }
sessionDB.Transaction(func() {
}, Marshaller)
return sessionDB, nil return sessionDB, nil
} }

View file

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"dev.narayana.im/narayana/telegabber/config" "dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/persistence"
"github.com/zelenin/go-tdlib/client" "github.com/zelenin/go-tdlib/client"
) )
@ -35,12 +36,13 @@ type Client struct {
client *client.Client client *client.Client
jid string jid string
parameters *client.TdlibParameters parameters *client.TdlibParameters
session *persistence.Session
online bool online bool
logVerbosity client.Option logVerbosity client.Option
} }
// NewClient instantiates a Telegram App // NewClient instantiates a Telegram App
func NewClient(conf config.TelegramConfig, jid string) (Client, error) { func NewClient(conf config.TelegramConfig, jid string, session *persistence.Session) (Client, error) {
logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{ logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: stringToLogConstant(conf.Loglevel), NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
}) })
@ -76,6 +78,7 @@ func NewClient(conf config.TelegramConfig, jid string) (Client, error) {
return Client{ return Client{
parameters: &parameters, parameters: &parameters,
jid: jid, jid: jid,
session: session,
logVerbosity: logVerbosity, logVerbosity: logVerbosity,
}, nil }, nil
} }

View file

@ -1,10 +1,13 @@
package xmpp package xmpp
import ( import (
"github.com/pkg/errors"
"dev.narayana.im/narayana/telegabber/config" "dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/persistence" "dev.narayana.im/narayana/telegabber/persistence"
"dev.narayana.im/narayana/telegabber/telegram" "dev.narayana.im/narayana/telegabber/telegram"
log "github.com/sirupsen/logrus"
"gosrc.io/xmpp" "gosrc.io/xmpp"
) )
@ -24,9 +27,8 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
} }
tgConf = tc tgConf = tc
sessions = make(map[string]telegram.Client)
db, err = persistence.LoadSessions(conf.Db) err = loadSessions(conf.Db)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -50,5 +52,46 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
cm := xmpp.NewStreamManager(component, nil) cm := xmpp.NewStreamManager(component, nil)
go maintenance()
return cm, nil 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
}

View file

@ -3,8 +3,6 @@ package xmpp
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
"dev.narayana.im/narayana/telegabber/telegram"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gosrc.io/xmpp" "gosrc.io/xmpp"
"gosrc.io/xmpp/stanza" "gosrc.io/xmpp/stanza"
@ -90,14 +88,9 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
return return
} }
bareFromJid := fromJid.Bare() bareFromJid := fromJid.Bare()
session, ok := sessions[bareFromJid] session, ok := getTelegramInstance(bareFromJid, nil)
if !ok { if !ok {
client, err := telegram.NewClient(tgConf, bareFromJid) return
if err != nil {
log.Error(errors.Wrap(err, "TDlib initialization failure"))
return
}
sessions[bareFromJid] = client
} }
switch p.Type { switch p.Type {

View file

@ -18,9 +18,7 @@ type YamlDB struct {
// Transaction executes the given callback and safely saves // Transaction executes the given callback and safely saves
// the data after they are modified within the callback // the data after they are modified within the callback
func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error)) error { func (db *YamlDB) Transaction(callback func() bool, marshaller func() ([]byte, error)) error {
var err error
log.Debug("Enter transaction") log.Debug("Enter transaction")
db.lock.Lock() db.lock.Lock()
defer func() { defer func() {
@ -28,19 +26,21 @@ func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error)
log.Debug("Exit transaction") log.Debug("Exit transaction")
}() }()
callback() isDataChanged := callback()
yamlData, err := marshaller() if isDataChanged {
if err != nil { yamlData, err := marshaller()
return errors.Wrap(err, "Data marshalling error") if err != nil {
} return errors.Wrap(err, "Data marshalling error")
err = ioutil.WriteFile(db.PathNew, yamlData, 0644) }
if err != nil { err = ioutil.WriteFile(db.PathNew, yamlData, 0644)
return errors.Wrap(err, "YamlDB write failure") if err != nil {
} return errors.Wrap(err, "YamlDB write failure")
err = os.Rename(db.PathNew, db.Path) }
if err != nil { err = os.Rename(db.PathNew, db.Path)
return errors.Wrap(err, "Couldn't rewrite an old YamlDB file") if err != nil {
return errors.Wrap(err, "Couldn't rewrite an old YamlDB file")
}
} }
return nil return nil