Skip to content

Commit

Permalink
Update madelineProto
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Mar 3, 2020
1 parent e4a3212 commit f238aa2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 93 deletions.
114 changes: 40 additions & 74 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace TelegramApiServer;

use Amp\Loop;
use Amp\Promise;
use danog\MadelineProto;
use danog\MadelineProto\MTProto;
use InvalidArgumentException;
use Psr\Log\LogLevel;
use RuntimeException;
use TelegramApiServer\EventObservers\EventHandler;
use function Amp\call;

class Client
{
Expand Down Expand Up @@ -48,7 +50,7 @@ public static function getSessionName(?string $sessionFile): ?string
return $matches['sessionName'] ?? null;
}

public static function checkOrCreateSessionFolder($session): void
public static function checkOrCreateSessionFolder(string $session): void
{
$directory = dirname($session);
if ($directory && $directory !== '.' && !is_dir($directory)) {
Expand All @@ -59,12 +61,12 @@ public static function checkOrCreateSessionFolder($session): void
}
}

private static function isSessionLoggedIn($instance): bool
private static function isSessionLoggedIn(MadelineProto\API $instance): bool
{
return ($instance->API->authorized ?? MTProto::NOT_LOGGED_IN) === MTProto::LOGGED_IN;
}

public function connect($sessionFiles): void
public function connect(array $sessionFiles): void
{
warning(PHP_EOL . 'Starting MadelineProto...' . PHP_EOL);

Expand Down Expand Up @@ -147,9 +149,9 @@ public function getInstance(?string $session = null): MadelineProto\API
return $this->instances[$session];
}

private function startSessions(): void
private function startSessions(): Promise
{
Loop::defer(
return call(
function() {
foreach ($this->instances as $instance) {
if (!static::isSessionLoggedIn($instance)) {
Expand All @@ -173,14 +175,14 @@ static function() use ($instance) {
);
}

private function runSession(MadelineProto\API $instance): void
public function runSession(MadelineProto\API $instance): Promise
{
Loop::defer(
return call(
function() use ($instance) {
if (static::isSessionLoggedIn($instance)) {
yield $instance->start();
$instance->setEventHandler(EventHandler::class);
$this->loop($instance);
yield $instance->setEventHandler(EventHandler::class);
Loop::defer(fn() => $this->loop($instance));
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/EventObservers/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace TelegramApiServer\EventObservers;

use danog\MadelineProto\API;
use danog\MadelineProto\APIWrapper;
use TelegramApiServer\Client;

class EventHandler extends \danog\MadelineProto\EventHandler
{
public static array $instances = [];
private string $sessionName;

public function __construct(API $MadelineProto)
public function __construct(APIWrapper $MadelineProto)
{
$this->sessionName = Client::getSessionName($MadelineProto->session);
if (empty(static::$instances[$this->sessionName])) {
Expand Down
Loading

0 comments on commit f238aa2

Please sign in to comment.