telegabber/telegram/client.go

118 lines
2.7 KiB
Go
Raw Normal View History

package telegram
import (
"github.com/pkg/errors"
"path/filepath"
"strconv"
2019-11-29 00:51:41 +00:00
"sync"
2019-12-16 01:02:53 +00:00
"time"
"dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/persistence"
2019-12-28 02:35:40 +00:00
"dev.narayana.im/narayana/telegabber/telegram/cache"
2022-01-17 20:45:40 +00:00
"github.com/zelenin/go-tdlib/client"
2019-11-24 17:10:29 +00:00
"gosrc.io/xmpp"
)
var logConstants = map[string]int32{
":fatal": 0,
":error": 1,
":warn": 2,
":info": 3,
":debug": 4,
":verbose": 5,
":all": 1023,
}
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-12-16 01:02:53 +00:00
client *client.Client
authorizer *clientAuthorizer
parameters *client.TdlibParameters
options []client.Option
me *client.User
2022-01-03 03:54:13 +00:00
xmpp *xmpp.Component
jid string
Session *persistence.Session
resources map[string]bool
content *config.TelegramContentConfig
cache *cache.Cache
online bool
locks clientLocks
}
2019-11-29 00:51:41 +00:00
type clientLocks struct {
authorizationReady sync.WaitGroup
chatMessageLocks map[int64]*sync.Mutex
2022-01-03 03:54:13 +00:00
resourcesLock sync.Mutex
2019-11-29 00:51:41 +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-12-16 01:02:53 +00:00
var options []client.Option
options = append(options, client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
2019-12-16 01:02:53 +00:00
}))
if conf.Tdlib.Client.CatchTimeout != 0 {
options = append(options, client.WithCatchTimeout(
time.Duration(conf.Tdlib.Client.CatchTimeout)*time.Second,
))
}
2022-01-17 20:45:40 +00:00
apiID, err := strconv.ParseInt(conf.Tdlib.Client.APIID, 10, 32)
if err != nil {
2019-11-24 17:10:29 +00:00
return &Client{}, errors.Wrap(err, "Wrong api_id")
}
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,
2022-01-17 20:45:40 +00:00
ApiId: int32(apiID),
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: &parameters,
xmpp: component,
jid: jid,
Session: session,
2022-01-03 03:54:13 +00:00
resources: make(map[string]bool),
2019-12-04 21:47:44 +00:00
content: &conf.Content,
2019-12-28 02:35:40 +00:00
cache: cache.NewCache(),
options: options,
locks: clientLocks{
chatMessageLocks: make(map[int64]*sync.Mutex),
},
}, nil
}