Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jun 15, 2024
1 parent 0750b22 commit 92a3dcd
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 108 deletions.
32 changes: 9 additions & 23 deletions src/Broadcast/Action/ActionForward.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
use danog\MadelineProto\Broadcast\Action;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\PeerNotInDbException;
use danog\MadelineProto\RPCError\ChannelPrivateError;
use danog\MadelineProto\RPCError\ChatWriteForbiddenError;
use danog\MadelineProto\RPCError\InputUserDeactivatedError;
use danog\MadelineProto\RPCError\PeerIdInvalidError;
use danog\MadelineProto\RPCError\UserIsBlockedError;
use danog\MadelineProto\RPCError\UserIsBotError;
use danog\MadelineProto\RPCErrorException;

/** @internal */
Expand All @@ -42,7 +48,7 @@ public function act(int $broadcastId, int $peer, Cancellation $cancellation): vo
'to_peer' => $peer,
'id' => $this->ids,
'drop_author' => $this->drop_author,
'floodWaitLimit' => 2*86400,
'floodWaitLimit' => 2 * 86400,
'cancellation' => $cancellation,
],
);
Expand All @@ -62,34 +68,14 @@ public function act(int $broadcastId, int $peer, Cancellation $cancellation): vo
'id' => $id,
'unpin' => false,
'pm_oneside' => false,
'floodWaitLimit' => 2*86400,
'floodWaitLimit' => 2 * 86400,
'cancellation' => $cancellation,
],
);
} catch (RPCErrorException) {
}
}
} catch (RPCErrorException $e) {
if ($e->rpc === 'INPUT_USER_DEACTIVATED') {
return;
}
if ($e->rpc === 'USER_IS_BOT') {
return;
}
if ($e->rpc === 'CHAT_WRITE_FORBIDDEN') {
return;
}
if ($e->rpc === 'CHANNEL_PRIVATE') {
return;
}
if ($e->rpc === 'USER_IS_BLOCKED') {
return;
}
if ($e->rpc === 'PEER_ID_INVALID') {
return;
}
throw $e;
} catch (PeerNotInDbException) {
} catch (PeerNotInDbException|InputUserDeactivatedError|UserIsBotError|ChatWriteForbiddenError|ChannelPrivateError|UserIsBlockedError|PeerIdInvalidError) {
return;
}
}
Expand Down
28 changes: 7 additions & 21 deletions src/Broadcast/Action/ActionSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
use danog\MadelineProto\Broadcast\Action;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\PeerNotInDbException;
use danog\MadelineProto\RPCError\ChannelPrivateError;
use danog\MadelineProto\RPCError\ChatWriteForbiddenError;
use danog\MadelineProto\RPCError\InputUserDeactivatedError;
use danog\MadelineProto\RPCError\PeerIdInvalidError;
use danog\MadelineProto\RPCError\UserIsBlockedError;
use danog\MadelineProto\RPCError\UserIsBotError;
use danog\MadelineProto\RPCErrorException;

/** @internal */
Expand Down Expand Up @@ -61,27 +67,7 @@ public function act(int $broadcastId, int $peer, Cancellation $cancellation): vo
} catch (RPCErrorException) {
}
}
} catch (RPCErrorException $e) {
if ($e->rpc === 'INPUT_USER_DEACTIVATED') {
return;
}
if ($e->rpc === 'USER_IS_BOT') {
return;
}
if ($e->rpc === 'CHAT_WRITE_FORBIDDEN') {
return;
}
if ($e->rpc === 'CHANNEL_PRIVATE') {
return;
}
if ($e->rpc === 'USER_IS_BLOCKED') {
return;
}
if ($e->rpc === 'PEER_ID_INVALID') {
return;
}
throw $e;
} catch (PeerNotInDbException) {
} catch (PeerNotInDbException|InputUserDeactivatedError|UserIsBotError|ChatWriteForbiddenError|ChannelPrivateError|UserIsBlockedError|PeerIdInvalidError) {
return;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/DataCenterConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use danog\MadelineProto\MTProto\PermAuthKey;
use danog\MadelineProto\MTProto\TempAuthKey;
use danog\MadelineProto\MTProtoTools\Crypt;
use danog\MadelineProto\RPCError\DcIdInvalidError;
use danog\MadelineProto\Settings\Connection as ConnectionSettings;
use danog\MadelineProto\Stream\ContextIterator;
use JsonSerializable;
Expand Down Expand Up @@ -275,13 +276,11 @@ private function syncAuthorization(): void
$socket->methodCallAsyncRead('auth.importAuthorization', $exported_authorization);
$this->authorized(true);
break;
} catch (Exception $e) {
} catch (DcIdInvalidError $e) {
$logger->logger('Failure while syncing authorization from DC '.$authorized_dc_id.' to DC '.$this->datacenter.': '.$e->getMessage(), Logger::ERROR);
} catch (RPCErrorException $e) {
break;
} catch (RPCErrorException|Exception $e) {
$logger->logger('Failure while syncing authorization from DC '.$authorized_dc_id.' to DC '.$this->datacenter.': '.$e->getMessage(), Logger::ERROR);
if ($e->rpc === 'DC_ID_INVALID') {
break;
}
}
// Turns out this DC isn't authorized after all
}
Expand Down
9 changes: 3 additions & 6 deletions src/EventHandler/Message/ChannelMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Participant;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\RPCError\MsgIdInvalidError;

/**
* Represents an incoming or outgoing channel message.
Expand Down Expand Up @@ -54,11 +54,8 @@ public function getDiscussion(): ?GroupMessage
$v = $this->getClient()->wrapMessage($r);
\assert($v instanceof GroupMessage);
return $v;
} catch (RPCErrorException $e) {
if ($e->rpc == 'MSG_ID_INVALID') {
return null;
}
throw $e;
} catch (MsgIdInvalidError) {
return null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/MTProto.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ private function upgradeMadelineProto(): void
foreach ($this->secretChats as $chat) {
try {
$chat->notifyLayer();
} catch (RPCErrorException $e) {
} catch (RPCErrorException) {
}
}

Expand Down
14 changes: 5 additions & 9 deletions src/MTProtoTools/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use danog\MadelineProto\FileRedirect;
use danog\MadelineProto\Logger;
use danog\MadelineProto\MTProtoTools\Crypt\IGE;
use danog\MadelineProto\RPCError\FileTokenInvalidError;
use danog\MadelineProto\RPCError\FloodWaitError;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\SecurityException;
Expand Down Expand Up @@ -1211,15 +1212,10 @@ private function downloadPart(array &$messageMedia, bool &$cdn, int &$datacenter
$datacenter = $e->dc;
} catch (FloodWaitError $e) {
delay(1, cancellation: $cancellation);
} catch (RPCErrorException $e) {
switch ($e->rpc) {
case 'FILE_TOKEN_INVALID':
$cdn = false;
$datacenter = $this->authorized_dc;
continue 3;
default:
throw $e;
}
} catch (FileTokenInvalidError) {
$cdn = false;
$datacenter = $this->authorized_dc;
continue 2;
}
} while (true);
$cancellation?->throwIfRequested();
Expand Down
9 changes: 3 additions & 6 deletions src/MTProtoTools/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@
use danog\MadelineProto\ParseMode;
use danog\MadelineProto\PeerNotInDbException;
use danog\MadelineProto\ResponseException;
use danog\MadelineProto\RPCError\ChannelPrivateError;
use danog\MadelineProto\RPCError\FloodWaitError;
use danog\MadelineProto\RPCError\MsgIdInvalidError;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\Settings;
use danog\MadelineProto\TL\TL;
Expand Down Expand Up @@ -1109,12 +1111,7 @@ public function saveUpdate(array $update): void
if ($this->getSettings()->getDb()->getEnableFullPeerDb()) {
$this->peerDatabase->expireFull($id);
}
} catch (PeerNotInDbException) {
} catch (FloodWaitError) {
} catch (RPCErrorException $e) {
if ($e->rpc !== 'CHANNEL_PRIVATE' && $e->rpc !== 'MSG_ID_INVALID') {
throw $e;
}
} catch (ChannelPrivateError|MsgIdInvalidError|PeerNotInDbException|FloodWaitError) {
}
});
}
Expand Down
8 changes: 3 additions & 5 deletions src/SecretChats/AuthKeyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
use danog\MadelineProto\Logger;
use danog\MadelineProto\Loop\Update\UpdateLoop;
use danog\MadelineProto\MTProtoTools\Crypt;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\RPCError\EncryptionAlreadyAcceptedError;
use danog\MadelineProto\RPCError\EncryptionAlreadyDeclinedError;
use danog\MadelineProto\SecretPeerNotInDbException;
use danog\MadelineProto\SecurityException;
use danog\MadelineProto\Tools;
Expand Down Expand Up @@ -246,10 +247,7 @@ public function discardSecretChat(int $chat, bool $deleteHistory = false): void
}
try {
$this->methodCallAsyncRead('messages.discardEncryption', ['chat_id' => $chat, 'delete_history' => $deleteHistory]);
} catch (RPCErrorException $e) {
if ($e->rpc !== 'ENCRYPTION_ALREADY_DECLINED') {
throw $e;
}
} catch (EncryptionAlreadyAcceptedError|EncryptionAlreadyDeclinedError) {
}
}
}
43 changes: 17 additions & 26 deletions src/VoIPController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use danog\Loop\Loop;
use danog\MadelineProto\Loop\VoIP\DjLoop;
use danog\MadelineProto\MTProtoTools\Crypt;
use danog\MadelineProto\RPCError\CallAlreadyAcceptedError;
use danog\MadelineProto\RPCError\CallAlreadyDeclinedError;
use danog\MadelineProto\VoIP\CallState;
use danog\MadelineProto\VoIP\DiscardReason;
use danog\MadelineProto\VoIP\Endpoint;
Expand Down Expand Up @@ -243,17 +245,13 @@ public function confirm(array $params): bool
'g_a' => $this->call['g_a'],
'protocol' => self::CALL_PROTOCOL,
]))['phone_call'];
} catch (RPCErrorException $e) {
if ($e->rpc === 'CALL_ALREADY_ACCEPTED') {
$this->log(sprintf(Lang::$current_lang['call_already_accepted'], $params['id']));
return true;
}
if ($e->rpc === 'CALL_ALREADY_DECLINED') {
$this->log(Lang::$current_lang['call_already_declined']);
$this->discard(DiscardReason::HANGUP);
return false;
}
throw $e;
} catch (CallAlreadyAcceptedError) {
$this->log(sprintf(Lang::$current_lang['call_already_accepted'], $params['id']));
return true;
} catch (CallAlreadyDeclinedError) {
$this->log(Lang::$current_lang['call_already_declined']);
$this->discard(DiscardReason::HANGUP);
return false;
}
$visualization = [];
$length = new BigInteger(\count(Magic::$emojis));
Expand Down Expand Up @@ -305,17 +303,13 @@ public function accept(): self
'g_b' => $g_b->toBytes(),
'protocol' => self::CALL_PROTOCOL,
]);
} catch (RPCErrorException $e) {
if ($e->rpc === 'CALL_ALREADY_ACCEPTED') {
$this->log(sprintf(Lang::$current_lang['call_already_accepted'], $this->public->callID));
return $this;
}
if ($e->rpc === 'CALL_ALREADY_DECLINED') {
$this->log(Lang::$current_lang['call_already_declined']);
$this->discard(DiscardReason::HANGUP);
return $this;
}
throw $e;
} catch (CallAlreadyAcceptedError) {
$this->log(sprintf(Lang::$current_lang['call_already_accepted'], $this->public->callID));
return $this;
} catch (CallAlreadyDeclinedError) {
$this->log(Lang::$current_lang['call_already_declined']);
$this->discard(DiscardReason::HANGUP);
return $this;
}
$this->call['b'] = $b;

Expand Down Expand Up @@ -417,10 +411,7 @@ public function discard(DiscardReason $reason = DiscardReason::HANGUP, ?int $rat
DiscardReason::DISCONNECTED => 'phoneCallDiscardReasonDisconnect',
DiscardReason::MISSED => 'phoneCallDiscardReasonMissed'
}]]);
} catch (RPCErrorException $e) {
if (!\in_array($e->rpc, ['CALL_ALREADY_DECLINED', 'CALL_ALREADY_ACCEPTED'], true)) {
throw $e;
}
} catch (CallAlreadyAcceptedError|CallAlreadyDeclinedError) {
}
if ($rating !== null) {
$this->log(sprintf('Setting rating for call %s...', $this->call), Logger::VERBOSE);
Expand Down
9 changes: 3 additions & 6 deletions src/Wrappers/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use danog\MadelineProto\MTProto\PermAuthKey;
use danog\MadelineProto\MTProto\TempAuthKey;
use danog\MadelineProto\MTProtoTools\PasswordCalculator;
use danog\MadelineProto\RPCError\PasswordHashInvalidError;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\Settings;
use danog\MadelineProto\TL\Types\LoginQrCode;
Expand Down Expand Up @@ -331,12 +332,8 @@ public function complete2faLogin(string $password): array
$this->logger->logger(Lang::$current_lang['login_user'], Logger::NOTICE);
try {
$res = $this->methodCallAsyncRead('auth.checkPassword', ['password' => $password], $this->authorized_dc);
} catch (RPCErrorException $e) {
if ($e->rpc === 'PASSWORD_HASH_INVALID') {
$res = $this->methodCallAsyncRead('auth.checkPassword', ['password' => $password], $this->authorized_dc);
} else {
throw $e;
}
} catch (PasswordHashInvalidError) {
$res = $this->methodCallAsyncRead('auth.checkPassword', ['password' => $password], $this->authorized_dc);
}
return $this->processAuthorization($res);
}
Expand Down

0 comments on commit 92a3dcd

Please sign in to comment.