2019-11-03 22:15:43 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2019-11-29 00:51:41 +00:00
|
|
|
"sync"
|
2019-11-03 22:15:43 +00:00
|
|
|
|
|
|
|
"dev.narayana.im/narayana/telegabber/config"
|
2019-11-12 15:50:25 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/persistence"
|
2019-11-03 22:15:43 +00:00
|
|
|
|
|
|
|
"github.com/zelenin/go-tdlib/client"
|
2019-11-24 17:10:29 +00:00
|
|
|
"gosrc.io/xmpp"
|
2019-11-03 22:15:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var logConstants = map[string]int32{
|
2019-11-05 00:09:07 +00:00
|
|
|
":fatal": 0,
|
|
|
|
":error": 1,
|
|
|
|
":warn": 2,
|
|
|
|
":info": 3,
|
|
|
|
":debug": 4,
|
|
|
|
":verbose": 5,
|
|
|
|
":all": 1023,
|
2019-11-03 22:15:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 21:47:44 +00:00
|
|
|
type cache struct {
|
2019-11-29 00:51:41 +00:00
|
|
|
chats map[int64]*client.Chat
|
|
|
|
users map[int32]*client.User
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:15:43 +00:00
|
|
|
func stringToLogConstant(c string) int32 {
|
|
|
|
level, ok := logConstants[c]
|
|
|
|
if !ok {
|
|
|
|
level = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return level
|
|
|
|
}
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
// Client stores the metadata for lazily invoked TDlib instance
|
|
|
|
type Client struct {
|
2019-11-03 22:15:43 +00:00
|
|
|
client *client.Client
|
2019-11-25 19:42:11 +00:00
|
|
|
authorizer *clientAuthorizer
|
2019-11-03 22:15:43 +00:00
|
|
|
parameters *client.TdlibParameters
|
|
|
|
logVerbosity client.Option
|
2019-11-26 22:14:06 +00:00
|
|
|
me *client.User
|
|
|
|
|
|
|
|
xmpp *xmpp.Component
|
|
|
|
jid string
|
|
|
|
Session *persistence.Session
|
2019-12-01 13:13:45 +00:00
|
|
|
content *config.TelegramContentConfig
|
2019-12-04 21:47:44 +00:00
|
|
|
cache *cache
|
2019-11-26 22:14:06 +00:00
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
locks clientLocks
|
2019-11-26 22:14:06 +00:00
|
|
|
online bool
|
2019-11-03 22:15:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
type clientLocks struct {
|
|
|
|
authorizationReady sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:15:43 +00:00
|
|
|
// NewClient instantiates a Telegram App
|
2019-11-24 17:10:29 +00:00
|
|
|
func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) {
|
2019-11-03 22:15:43 +00:00
|
|
|
logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
|
|
|
|
NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
|
|
|
|
})
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
apiID, err := strconv.Atoi(conf.Tdlib.Client.APIID)
|
2019-11-03 22:15:43 +00:00
|
|
|
if err != nil {
|
2019-11-24 17:10:29 +00:00
|
|
|
return &Client{}, errors.Wrap(err, "Wrong api_id")
|
2019-11-03 22:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parameters := client.TdlibParameters{
|
|
|
|
UseTestDc: false,
|
|
|
|
|
|
|
|
DatabaseDirectory: filepath.Join("./sessions/", jid),
|
|
|
|
FilesDirectory: filepath.Join("./sessions/", jid, "/files/"),
|
|
|
|
|
|
|
|
UseFileDatabase: true,
|
|
|
|
UseChatInfoDatabase: conf.Tdlib.Client.UseChatInfoDatabase,
|
|
|
|
UseMessageDatabase: true,
|
|
|
|
UseSecretChats: conf.Tdlib.Client.UseSecretChats,
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
ApiId: int32(apiID),
|
2019-11-03 22:15:43 +00:00
|
|
|
ApiHash: conf.Tdlib.Client.APIHash,
|
|
|
|
|
|
|
|
SystemLanguageCode: "en",
|
|
|
|
DeviceModel: conf.Tdlib.Client.DeviceModel,
|
|
|
|
SystemVersion: "1.0.0",
|
|
|
|
ApplicationVersion: conf.Tdlib.Client.ApplicationVersion,
|
|
|
|
|
|
|
|
EnableStorageOptimizer: true,
|
|
|
|
IgnoreFileNames: false,
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
return &Client{
|
2019-12-04 21:47:44 +00:00
|
|
|
parameters: ¶meters,
|
|
|
|
xmpp: component,
|
|
|
|
jid: jid,
|
|
|
|
Session: session,
|
|
|
|
content: &conf.Content,
|
|
|
|
cache: &cache{
|
|
|
|
chats: map[int64]*client.Chat{},
|
|
|
|
users: map[int32]*client.User{},
|
|
|
|
},
|
2019-11-03 22:15:43 +00:00
|
|
|
logVerbosity: logVerbosity,
|
2019-11-29 00:51:41 +00:00
|
|
|
locks: clientLocks{},
|
2019-11-03 22:15:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|