Spawn TDlib instances for recovered sessions
This commit is contained in:
parent
fbe99c65ec
commit
a5c97d68e7
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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: ¶meters,
|
parameters: ¶meters,
|
||||||
jid: jid,
|
jid: jid,
|
||||||
|
session: session,
|
||||||
logVerbosity: logVerbosity,
|
logVerbosity: logVerbosity,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
@ -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,15 +88,10 @@ 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)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(errors.Wrap(err, "TDlib initialization failure"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sessions[bareFromJid] = client
|
|
||||||
}
|
|
||||||
|
|
||||||
switch p.Type {
|
switch p.Type {
|
||||||
case "unsubscribed":
|
case "unsubscribed":
|
||||||
|
|
|
@ -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,8 +26,9 @@ func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error)
|
||||||
log.Debug("Exit transaction")
|
log.Debug("Exit transaction")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
callback()
|
isDataChanged := callback()
|
||||||
|
|
||||||
|
if isDataChanged {
|
||||||
yamlData, err := marshaller()
|
yamlData, err := marshaller()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Data marshalling error")
|
return errors.Wrap(err, "Data marshalling error")
|
||||||
|
@ -42,6 +41,7 @@ func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Couldn't rewrite an old YamlDB file")
|
return errors.Wrap(err, "Couldn't rewrite an old YamlDB file")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue