-
Notifications
You must be signed in to change notification settings - Fork 132
/
SystemApiExtensions.php
165 lines (142 loc) · 4.66 KB
/
SystemApiExtensions.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace TelegramApiServer\MadelineProtoExtensions;
use danog\MadelineProto;
use danog\MadelineProto\API;
use InvalidArgumentException;
use Revolt\EventLoop;
use TelegramApiServer\Client;
use TelegramApiServer\Files;
use Throwable;
use function Amp\async;
use function Amp\File\deleteFile;
use function Amp\Future\awaitAll;
final class SystemApiExtensions
{
private Client $client;
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* @return array<string, array>
*/
public function healthcheck(): array
{
$results = [];
['sessions' => $sessions] = $this->getSessionList();
foreach ($sessions as $sessionKey => $session) {
$instance = $this->client->instances[$sessionKey];
if ($instance->getAuthorization() === API::LOGGED_IN) {
$results[$sessionKey] = $instance->getSelf();
}
}
return $results;
}
public function addSession(string $session, array $settings = []): array
{
if (!empty($settings['app_info']['api_id'])) {
$settings['app_info']['api_id'] = (int) $settings['app_info']['api_id'];
}
$instance = $this->client->addSession($session, $settings);
/** @var null|MadelineProto\Settings $fullSettings */
$fullSettings = $instance->getSettings();
try {
if ($fullSettings !== null && $instance->getAuthorization() !== API::LOGGED_IN) {
$fullSettings->getAppInfo()->getApiId();
$fullSettings->getAppInfo()->getApiHash();
}
} catch (Throwable $e) {
unset($fullSettings, $instance);
$this->removeSession($session);
$this->unlinkSessionFile($session);
throw $e;
}
$this->client->startLoggedInSession($session);
return $this->getSessionList();
}
public function removeSession(string $session): array
{
$this->client->removeSession($session);
return $this->getSessionList();
}
public function getSessionList(): array
{
$sessions = [];
foreach ($this->client->instances as $session => $instance) {
$authorized = $instance->getAuthorization();
switch ($authorized) {
case API::NOT_LOGGED_IN:
$status = 'NOT_LOGGED_IN';
break;
case API::WAITING_CODE:
$status = 'WAITING_CODE';
break;
case API::WAITING_SIGNUP:
$status = 'WAITING_SIGNUP';
break;
case API::WAITING_PASSWORD:
$status = 'WAITING_PASSWORD';
break;
case API::LOGGED_IN:
$status = 'LOGGED_IN';
break;
case API::LOGGED_OUT:
$status = 'LOGGED_OUT';
break;
default:
$status = $authorized;
break;
}
$sessions[$session] = [
'session' => $session,
'file' => Files::getSessionFile($session),
'status' => $status,
];
}
return [
'sessions' => $sessions,
'memory' => $this->bytesToHuman(\memory_get_usage(true)),
];
}
public function unlinkSessionFile($session): string
{
$file = Files::getSessionFile($session);
if (\is_file($file)) {
$futures = [];
foreach (\glob("$file*") as $file) {
$futures[] = async(fn () => deleteFile($file));
}
awaitAll($futures);
} else {
throw new InvalidArgumentException('Session file not found');
}
$this->unlinkSessionSettings($session);
return 'ok';
}
public function saveSessionSettings(string $session, array $settings = [])
{
Files::saveSessionSettings($session, $settings);
return 'ok';
}
public function unlinkSessionSettings($session): string
{
$settings = Files::getSessionFile($session, Files::SETTINGS_EXTENSION);
if (\is_file($settings)) {
deleteFile($settings);
}
return 'ok';
}
public function exit(): string
{
EventLoop::defer(static fn () => exit());
return 'ok';
}
private function bytesToHuman($bytes): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
for ($i = 0; $bytes > 1024; $i++) {
$bytes /= 1024;
}
return \round($bytes, 2) . ' ' . $units[$i];
}
}