Skip to content

Commit

Permalink
Misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Dec 18, 2023
1 parent a3ddd49 commit 54dbb53
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 143 files
6 changes: 5 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,6 @@
</MissingClosureParamType>
<MissingParamType>
<code>$messages</code>
<code>$update</code>
</MissingParamType>
<MissingReturnType>
<code>feed</code>
Expand Down Expand Up @@ -1251,6 +1250,8 @@
<code>$difference</code>
<code>$difference</code>
<code>$difference</code>
<code>$difference</code>
<code>$difference</code>
</PossiblyUndefinedVariable>
<RedundantConditionGivenDocblockType>
<code><![CDATA[$this->API->feeders[$channelId]?->resume()]]></code>
Expand Down Expand Up @@ -1693,6 +1694,9 @@
<InaccessibleProperty>
<code><![CDATA[$last->nextSent]]></code>
</InaccessibleProperty>
<MissingParamType>
<code>$update</code>
</MissingParamType>
<MissingReturnType>
<code>getUpdatesState</code>
<code>loadUpdateState</code>
Expand Down
4 changes: 2 additions & 2 deletions src/DocsBuilder/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ public function mkMethods(): void
}
$type_or_bare_type = ctype_upper(Tools::end(explode('.', $param[$type_or_subtype]))[0]) || \in_array($param[$type_or_subtype], ['!X', 'X', 'bytes', 'true', 'false', 'double', 'string', 'Bool', 'int', 'long', 'int128', 'int256', 'int512', 'int53'], true) ? 'types' : 'constructors';
if (isset($this->tdDescriptions['methods'][$method])) {
$table .= '|'.self::markdownEscape($param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.self::markdownEscape($human_ptype).'](/API_docs/'.$type_or_bare_type.'/'.$ptype.'.md) | '.$this->tdDescriptions['methods'][$method]['params'][$param['name']].' | '.(isset($param['pow']) || $param['type'] === 'int' || $param['type'] === 'double' || ($id = $this->TL->getConstructors()->findByPredicate(lcfirst($param['type']).'Empty')) && $id['type'] === $param['type'] || ($id = $this->TL->getConstructors()->findByPredicate('input'.$param['type'].'Empty')) && $id['type'] === $param['type'] ? 'Optional' : 'Yes').'|';
$table .= '|'.self::markdownEscape($param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.self::markdownEscape($human_ptype).'](/API_docs/'.$type_or_bare_type.'/'.$ptype.'.md) | '.$this->tdDescriptions['methods'][$method]['params'][$param['name']].' | '.(isset($param['pow']) || $param['type'] === 'int' || $param['type'] === 'string' || $param['type'] === 'double' || ($id = $this->TL->getConstructors()->findByPredicate(lcfirst($param['type']).'Empty')) && $id['type'] === $param['type'] || ($id = $this->TL->getConstructors()->findByPredicate('input'.$param['type'].'Empty')) && $id['type'] === $param['type'] ? 'Optional' : 'Yes').'|';
} else {
$table .= '|'.self::markdownEscape($param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.self::markdownEscape($human_ptype).'](/API_docs/'.$type_or_bare_type.'/'.$ptype.'.md) | '.(isset($param['pow']) || ($param['type'] === 'long' && $param['name'] === 'hash')|| ($id = $this->TL->getConstructors()->findByPredicate(lcfirst($param['type']).'Empty')) && $id['type'] === $param['type'] || ($id = $this->TL->getConstructors()->findByPredicate('input'.$param['type'].'Empty')) && $id['type'] === $param['type'] ? 'Optional' : 'Yes').'|';
$table .= '|'.self::markdownEscape($param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.self::markdownEscape($human_ptype).'](/API_docs/'.$type_or_bare_type.'/'.$ptype.'.md) | '.(isset($param['pow']) || $param['type'] === 'int' || $param['type'] === 'string' || ($param['type'] === 'long' && $param['name'] === 'hash')|| ($id = $this->TL->getConstructors()->findByPredicate(lcfirst($param['type']).'Empty')) && $id['type'] === $param['type'] || ($id = $this->TL->getConstructors()->findByPredicate('input'.$param['type'].'Empty')) && $id['type'] === $param['type'] ? 'Optional' : 'Yes').'|';
}
$table .= PHP_EOL;
$pptype = \in_array($ptype, ['string', 'bytes'], true) ? "'".$ptype."'" : '$'.$ptype;
Expand Down
2 changes: 1 addition & 1 deletion src/EventHandlerIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __toString(): string

public function log(): void
{
Logger::log((string) $this, Logger::FATAL_ERROR);
Logger::log((string) $this, $this->severe ? Logger::FATAL_ERROR : Logger::ERROR);
}

public function getHTML(): string
Expand Down
7 changes: 2 additions & 5 deletions src/Loop/Update/FeedLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public function parse(array $updates): void
$logger('PTS OK');
$this->state->pts($update['pts']);
}
$this->save($update);

$this->parsedUpdates[] = $update;
}
}
public function feed(array $updates)
Expand Down Expand Up @@ -258,10 +259,6 @@ public function feedSingle(array $update)
$this->incomingUpdates[] = $update;
return $this->channelId;
}
public function save($update): void
{
$this->parsedUpdates[] = $update;
}
public function saveMessages($messages): void
{
foreach ($messages as $message) {
Expand Down
12 changes: 12 additions & 0 deletions src/Loop/Update/UpdateLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public function loop(): ?float
}
$result += ($this->feeder->feed($difference['other_updates']));
$state->update($difference);
if ($difference['new_messages']) {
$result[$this->channelId] = true;
}
$this->feeder->saveMessages($difference['new_messages']);
if (!$difference['final']) {
if ($difference['pts'] >= $toPts) {
Expand All @@ -155,6 +158,9 @@ public function loop(): ?float
$difference['pts'] = $difference['dialog']['pts'];
}
$state->update($difference);
if ($difference['messages']) {
$result[$this->channelId] = true;
}
$this->feeder->saveMessages($difference['messages']);
unset($difference);
break;
Expand Down Expand Up @@ -191,6 +197,9 @@ public function loop(): ?float
$result += ($this->feeder->feed($difference['other_updates']));
$result += ($this->feeder->feed($difference['new_encrypted_messages']));
$state->update($difference['state']);
if ($difference['new_messages']) {
$result[$this->channelId] = true;
}
$this->feeder->saveMessages($difference['new_messages']);
unset($difference);
break 2;
Expand All @@ -202,6 +211,9 @@ public function loop(): ?float
$result += ($this->feeder->feed($difference['other_updates']));
$result += ($this->feeder->feed($difference['new_encrypted_messages']));
$state->update($difference['intermediate_state']);
if ($difference['new_messages']) {
$result[$this->channelId] = true;
}
$this->feeder->saveMessages($difference['new_messages']);
if ($difference['intermediate_state']['pts'] >= $toPts) {
unset($difference);
Expand Down
29 changes: 26 additions & 3 deletions src/MTProto.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
use danog\MadelineProto\Wrappers\Start;
use Psr\Log\LoggerInterface;
use Revolt\EventLoop;
use SplQueue;
use Throwable;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -125,7 +126,7 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter
* @internal
* @var int
*/
public const V = 179;
public const V = 180;
/**
* Bad message error codes.
*
Expand Down Expand Up @@ -397,6 +398,7 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter
protected static array $dbProperties = [
'sponsoredMessages' => ['innerMadelineProto' => true],
'channelParticipants' => ['innerMadelineProto' => true],
'getUpdatesQueue' => ['innerMadelineProto' => true],
'session' => ['innerMadelineProto' => true, 'enableCache' => false],
];

Expand Down Expand Up @@ -479,6 +481,9 @@ public function __construct(Settings|SettingsEmpty $settings, ?APIWrapper $wrapp
$this->wrapper = $wrapper;
self::$references[$this->getSessionName()] = $this;
}
$q = new SplQueue;
$q->setIteratorMode(SplQueue::IT_MODE_DELETE);
$this->updateQueue ??= $q;

$initDeferred = new DeferredFuture;
$this->initPromise = $initDeferred->getFuture();
Expand Down Expand Up @@ -612,8 +617,9 @@ public function __sleep(): array
'event_handler',
'event_handler_instance',
'pluginInstances',
'updates',
'updates_key',
'updateQueue',
'getUpdatesQueue',
'getUpdatesQueueKey',
'webhookUrl',

'updateHandlerType',
Expand Down Expand Up @@ -838,6 +844,12 @@ private function stopLoops(): void
*/
private function cleanupProperties(): void
{
if (!isset($this->updateQueue)) {
$q = new SplQueue;
$q->setIteratorMode(SplQueue::IT_MODE_DELETE);
$this->updateQueue = $q;
}

$this->acceptChatMutex ??= new LocalKeyedMutex;
$this->confirmChatMutex ??= new LocalKeyedMutex;
$this->channels_state ??= new CombinedUpdatesState;
Expand Down Expand Up @@ -915,6 +927,13 @@ private function upgradeMadelineProto(): void
} catch (RPCErrorException $e) {
}
}

if (isset($this->updates) && \is_array($this->updates)) {
foreach ($this->updates as $update) {
$this->updateQueue->enqueue($update);
}
unset($this->updates);
}
}
/**
* Post-deserialization initialization function.
Expand Down Expand Up @@ -1294,6 +1313,10 @@ public function startUpdateSystem(bool $anyway = false): void
foreach ($this->secretChats as $chat) {
$chat->startFeedLoop();
}

foreach ($this->updateQueue as $update) {
$this->handleUpdate($update);
}
}
/**
* Store shared phone config.
Expand Down
Loading

0 comments on commit 54dbb53

Please sign in to comment.