1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-12-03 13:07:50 +01:00
MadelineProto/src/APIWrapper.php

137 lines
3.4 KiB
PHP
Raw Normal View History

2022-12-30 21:54:44 +01:00
<?php
declare(strict_types=1);
/**
* API wrapper module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
2023-01-04 12:43:01 +01:00
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
2024-06-26 18:25:54 +02:00
use Amp\Cancellation;
use Amp\TimeoutCancellation;
2020-09-23 00:57:49 +02:00
use danog\MadelineProto\Ipc\Client;
final class APIWrapper
{
2023-01-04 15:13:55 +01:00
private MTProto|Client|null $API = null;
private string $webApiTemplate = '';
/**
2020-02-26 11:47:30 +01:00
* API wrapper.
*/
2023-01-25 19:12:49 +01:00
public function __construct(
private SessionPaths $session,
) {
}
2023-01-26 14:33:30 +01:00
public function setSession(SessionPaths $session): void
{
$this->session = $session;
}
2023-01-25 19:12:49 +01:00
public function getWebApiTemplate(): string
{
2023-01-25 19:12:49 +01:00
return $this->webApiTemplate;
}
public function setWebApiTemplate(string $template): void
{
$this->webApiTemplate = $template;
}
2023-01-25 19:12:49 +01:00
public function logger(mixed $param, int $level = Logger::NOTICE, string $file = ''): void
{
($this->API->logger ?? Logger::$default)->logger($param, $level, $file);
}
public function setAPI(Client|MTProto|null $API): void
{
2023-01-26 14:33:30 +01:00
$this->API?->unreference();
2023-01-25 19:12:49 +01:00
$this->API = $API;
}
2020-10-08 21:35:32 +02:00
/**
2020-10-13 10:03:35 +02:00
* Sleep function.
2020-10-08 21:35:32 +02:00
*/
public function __sleep(): array
{
2023-01-25 19:12:49 +01:00
return ['API', 'webApiTemplate'];
2020-10-08 21:35:32 +02:00
}
/**
2020-02-26 11:47:30 +01:00
* Get MTProto instance.
*/
2023-01-25 19:12:49 +01:00
public function getAPI(): Client|MTProto|null
{
return $this->API;
}
2024-06-26 18:25:54 +02:00
private ?int $drop = null;
/**
* @internal
*/
public function getRpcDropCancellation(): Cancellation
{
return new TimeoutCancellation($this->drop ??= $this->getAPI()->getSettings()->getRpc()->getRpcDropTimeout());
}
2020-07-11 20:01:54 +02:00
/**
* Get IPC path.
*
* @internal
*/
public function getIpcPath(): string
{
return $this->session->getIpcPath();
2020-07-11 20:01:54 +02:00
}
/**
2020-07-10 14:50:00 +02:00
* Serialize session.
*/
2023-01-03 21:51:49 +01:00
public function serialize(): bool
{
2023-01-15 21:32:48 +01:00
if ($this->API === null) {
2023-01-03 21:51:49 +01:00
return false;
2020-07-11 20:01:54 +02:00
}
2020-09-23 00:57:49 +02:00
if ($this->API instanceof Client) {
2023-01-03 21:51:49 +01:00
return false;
2020-09-23 00:57:49 +02:00
}
2023-01-25 19:12:49 +01:00
$this->API->waitForInit();
2023-01-28 16:39:03 +01:00
$API = $this->API;
2023-01-03 22:07:58 +01:00
if ($API->authorized === API::LOGGED_OUT) {
return false;
}
2023-01-03 22:07:58 +01:00
$this->session->serialize(
2023-01-28 16:39:03 +01:00
$API->serializeSession($this),
2023-01-03 22:07:58 +01:00
$this->session->getSessionPath(),
);
2023-01-28 16:39:03 +01:00
$this->session->storeLightState($API);
2023-01-03 22:07:58 +01:00
if (!Magic::$suspendPeriodicLogging) {
Logger::log('Saved session!');
}
return true;
}
/**
* Get session path.
*/
public function getSession(): SessionPaths
{
return $this->session;
}
}