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
// Marshaller implementation for YamlDB
func Marshaller() ([]byte, error) {
// SessionMarshaller implementation for YamlDB
func SessionMarshaller() ([]byte, error) {
return yaml.Marshal(sessionDB.Data)
}
@ -41,9 +41,6 @@ func LoadSessions(path string) (SessionsYamlDB, error) {
return sessionDB, errors.Wrap(err, "Sessions restore error")
}
sessionDB.Transaction(func() {
}, Marshaller)
return sessionDB, nil
}

View file

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

View file

@ -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
}

View file

@ -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 {

View file

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