aboutsummaryrefslogtreecommitdiff
path: root/telegram/client.go
blob: 9a98be735f275c486014350bf549e8ef921d75ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package telegram

import (
	"github.com/pkg/errors"
	"path/filepath"
	"strconv"
	"sync"
	"time"

	"dev.narayana.im/narayana/telegabber/config"
	"dev.narayana.im/narayana/telegabber/persistence"
	"dev.narayana.im/narayana/telegabber/telegram/cache"

	"github.com/zelenin/go-tdlib/client"
	"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
}

// DelayedStatus describes an online status expiring on timeout
type DelayedStatus struct {
	TimestampOnline  int64
	TimestampExpired int64
}

// Client stores the metadata for lazily invoked TDlib instance
type Client struct {
	client     *client.Client
	authorizer *clientAuthorizer
	parameters *client.TdlibParameters
	options    []client.Option
	me         *client.User

	resource  string
	xmpp      *xmpp.Component
	jid       string
	Session   *persistence.Session
	resources map[string]bool
	content   *config.TelegramContentConfig
	cache     *cache.Cache
	online    bool

	DelayedStatuses     map[int64]*DelayedStatus
	DelayedStatusesLock sync.Mutex

	locks clientLocks
}

type clientLocks struct {
	authorizationReady sync.WaitGroup
	chatMessageLocks   map[int64]*sync.Mutex
	resourcesLock      sync.Mutex
}

// NewClient instantiates a Telegram App
func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) {
	var options []client.Option

	options = append(options, client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
		NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
	}))

	if conf.Tdlib.Client.CatchTimeout != 0 {
		options = append(options, client.WithCatchTimeout(
			time.Duration(conf.Tdlib.Client.CatchTimeout)*time.Second,
		))
	}

	apiID, err := strconv.ParseInt(conf.Tdlib.Client.APIID, 10, 32)
	if err != nil {
		return &Client{}, errors.Wrap(err, "Wrong api_id")
	}

	datadir := conf.Tdlib.Datadir
	if datadir == "" {
		datadir = "./sessions/" // ye olde defaute
	}

	parameters := client.TdlibParameters{
		UseTestDc: false,

		DatabaseDirectory: filepath.Join(datadir, jid),
		FilesDirectory:    filepath.Join(datadir, jid, "/files/"),

		UseFileDatabase:     true,
		UseChatInfoDatabase: conf.Tdlib.Client.UseChatInfoDatabase,
		UseMessageDatabase:  true,
		UseSecretChats:      conf.Tdlib.Client.UseSecretChats,

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

	resource := conf.Tdlib.Client.DeviceModel
	if resource == "" {
		resource = "telegabber"
	}

	return &Client{
		parameters: &parameters,
		resource:   resource,
		xmpp:       component,
		jid:        jid,
		Session:    session,
		resources:  make(map[string]bool),
		content:    &conf.Content,
		cache:      cache.NewCache(),
		options:    options,
		DelayedStatuses: make(map[int64]*DelayedStatus),
		locks: clientLocks{
			chatMessageLocks: make(map[int64]*sync.Mutex),
		},
	}, nil
}