Skip to content

Commit

Permalink
Fix broadcasts with bot API file IDs, fix MTProtoToBotAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Dec 15, 2023
1 parent f43782c commit a3e66c8
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ It can login with a phone number (MTProto API), or with a bot token (MTProto API
```php
<?php

// PHP 8.2+ is required.

if (!file_exists('madeline.php')) {
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
Expand Down
11 changes: 2 additions & 9 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
<PossiblyNullReference>
<code>getInputClientProxy</code>
<code>isHttp</code>
<code>refreshNext</code>
<code>refreshNext</code>
<code>refreshNextDisable</code>
<code>refreshNextEnable</code>
</PossiblyNullReference>
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$res['chat_id']]]></code>
Expand Down Expand Up @@ -1713,10 +1713,6 @@
<PossiblyNullArrayOffset>
<code><![CDATA[$res['participants']]]></code>
</PossiblyNullArrayOffset>
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$partial['InputChannel']]]></code>
<code><![CDATA[$partial['InputUser']]]></code>
</PossiblyUndefinedArrayOffset>
</file>
<file src="src/MTProtoTools/ReferenceDatabase.php">
<PossiblyNullArrayAccess>
Expand Down Expand Up @@ -1751,9 +1747,6 @@
<InaccessibleProperty>
<code><![CDATA[$last->nextSent]]></code>
</InaccessibleProperty>
<InvalidArrayOffset>
<code><![CDATA[$info['User']]]></code>
</InvalidArrayOffset>
<MissingReturnType>
<code>getUpdatesState</code>
<code>loadUpdateState</code>
Expand Down
4 changes: 2 additions & 2 deletions src/Broadcast/Action/ActionSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function act(int $broadcastId, int $peer, Cancellation $cancellation): vo
return;
}
$id = $this->API->extractMessageId($this->API->methodCallAsyncRead(
isset($message['media']['_']) &&
$message['media']['_'] !== 'messageMediaWebPage'
\is_string($message['media']) || (isset($message['media']['_']) &&
$message['media']['_'] !== 'messageMediaWebPage')
? 'messages.sendMedia'
: 'messages.sendMessage',
array_merge($message, ['peer' => $peer, 'floodWaitLimit' => 2*86400, 'cancellation' => $cancellation]),
Expand Down
21 changes: 12 additions & 9 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,19 @@ public function sendMessage(MTProtoOutgoingMessage $message): void
if (!$message->hasSerializedBody() || $message->shouldRefreshReferences()) {
$body = $message->getBody();
if ($message->shouldRefreshReferences()) {
$this->API->referenceDatabase->refreshNext(true);
$this->API->referenceDatabase->refreshNextEnable();
}
if ($message->isMethod) {
$body = $this->API->getTL()->serializeMethod($message->constructor, $body);
} else {
$body['_'] = $message->constructor;
$body = $this->API->getTL()->serializeObject(['type' => ''], $body, $message->constructor);
}
if ($message->shouldRefreshReferences()) {
$this->API->referenceDatabase->refreshNext(false);
try {
if ($message->isMethod) {
$body = $this->API->getTL()->serializeMethod($message->constructor, $body);
} else {
$body['_'] = $message->constructor;
$body = $this->API->getTL()->serializeObject(['type' => ''], $body, $message->constructor);
}
} finally {
if ($message->shouldRefreshReferences()) {
$this->API->referenceDatabase->refreshNextDisable();
}
}
$message->setSerializedBody($body);
unset($body);
Expand Down
2 changes: 1 addition & 1 deletion src/MTProtoTools/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ public function downloadToCallable(mixed $messageMedia, callable $callable, ?cal
$origCb(100, 0, 0);
return;
}
$parallel_chunks = $seekable ? $parallel_chunks : 1;
$parallel_chunks = $seekable ? $parallel_chunks : 2;
if ($params) {
$previous_promise = true;
$promises = [];
Expand Down
20 changes: 12 additions & 8 deletions src/MTProtoTools/ReferenceDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,19 +441,23 @@ private function storeReference(string $location, string $reference, int $origin

EventLoop::queue($this->flush(...), $location);
}
public function refreshNext(bool $refresh = false): void
public function refreshNextEnable(): void
{
if ($this->refreshCount === 1 && !$refresh) {
$this->refreshed = [];
$this->refreshCount--;
$this->refresh = false;
} elseif ($this->refreshCount === 0 && $refresh) {
if ($this->refreshCount === 0) {
$this->refreshed = [];
$this->refreshCount++;
$this->refresh = true;
} elseif ($this->refreshCount === 0 && !$refresh) {
} elseif ($refresh) {
} else {
$this->refreshCount++;
}
}
public function refreshNextDisable(): void
{
if ($this->refreshCount === 1) {
$this->refreshed = [];
$this->refreshCount--;
$this->refresh = false;
} elseif ($this->refreshCount === 0) {
} else {
$this->refreshCount--;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TL/Conversion/BotAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function MTProtoToBotAPI(array $data): array
if (isset($data['fwd_from']['from_id'])) {
$newd['forward_from'] = ($this->getPwrChat($data['fwd_from']['from_id'], false));
}
if ($data['fwd_from'] < 0) {
if (isset($data['fwd_from']) && $data['fwd_from'] < 0) {
try {
$newd['forward_from_chat'] = $this->getPwrChat($data['fwd_from'], false);
} catch (Throwable $e) {
Expand Down

0 comments on commit a3e66c8

Please sign in to comment.