Implement lots of additional peer casting

This commit is contained in:
Daniil Gentili 2018-08-30 19:58:58 +02:00
parent 5de602a1fd
commit c86488a244
5 changed files with 88 additions and 62 deletions

View File

@ -121,7 +121,7 @@ image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png
'; ';
$header .= isset($this->td_descriptions['types'][$otype]) ? $this->td_descriptions['types'][$otype].PHP_EOL.PHP_EOL : ''; $header .= isset($this->td_descriptions['types'][$otype]) ? $this->td_descriptions['types'][$otype].PHP_EOL.PHP_EOL : '';
if (!isset($this->settings['td'])) { if (!isset($this->settings['td'])) {
if (in_array($type, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) { if (in_array($type, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputDialogPeer', 'DialogPeer', 'InputPeer', 'NotifyPeer', 'InputNotifyPeer'])) {
$header .= 'You can directly provide the [Update](Update.md) or [Message](Message.md) object here, MadelineProto will automatically extract the destination chat id. $header .= 'You can directly provide the [Update](Update.md) or [Message](Message.md) object here, MadelineProto will automatically extract the destination chat id.
The following syntaxes can also be used: The following syntaxes can also be used:
@ -143,7 +143,7 @@ $'.$type." = 'https://t.me/danogentili'; // t.me URLs
\$".$type." = 'channel#38575794'; // tg-cli style id (channels) \$".$type." = 'channel#38575794'; // tg-cli style id (channels)
``` ```
A [Chat](Chat.md), a [User](User.md), an [InputPeer](InputPeer.md), an [InputUser](InputUser.md), an [InputChannel](InputChannel.md), a [Peer](Peer.md), or a [Chat](Chat.md) object can also be used.\n\n\n"; A [Chat](Chat.md), a [User](User.md), an [InputPeer](InputPeer.md), an [InputDialogPeer](InputDialogPeer.md), an [InputNotifyPeer](InputNotifyPeer.md), an [InputUser](InputUser.md), an [InputChannel](InputChannel.md), a [Peer](Peer.md), an [DialogPeer](DialogPeer.md), [NotifyPeer](NotifyPeer.md), or a [Chat](Chat.md) object can also be used.\n\n\n";
} }
if (in_array($type, ['InputEncryptedChat'])) { if (in_array($type, ['InputEncryptedChat'])) {
$header .= 'You can directly provide the [Update](Update.md) or [EncryptedMessage](EncryptedMessage.md) object here, MadelineProto will automatically extract the destination chat id. $header .= 'You can directly provide the [Update](Update.md) or [EncryptedMessage](EncryptedMessage.md) object here, MadelineProto will automatically extract the destination chat id.

View File

@ -130,7 +130,7 @@ trait Constructors
$ptype = 'Bool'; $ptype = 'Bool';
} }
$human_ptype = $ptype; $human_ptype = $ptype;
if (strpos($type, 'Input') === 0 && in_array($ptype, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer']) && !isset($this->settings['td'])) { if (strpos($type, 'Input') === 0 && in_array($ptype, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputDialogPeer', 'DialogPeer', 'NotifyPeer', 'InputNotifyPeer', 'InputPeer']) && !isset($this->settings['td'])) {
$human_ptype = 'Username, chat ID, Update, Message or '.$ptype; $human_ptype = 'Username, chat ID, Update, Message or '.$ptype;
} }
if (strpos($type, 'Input') === 0 && in_array($ptype, ['InputMedia', 'InputDocument', 'InputPhoto']) && !isset($this->settings['td'])) { if (strpos($type, 'Input') === 0 && in_array($ptype, ['InputMedia', 'InputDocument', 'InputPhoto']) && !isset($this->settings['td'])) {

View File

@ -117,7 +117,7 @@ trait Methods
$ptype = 'Bool'; $ptype = 'Bool';
} }
$human_ptype = $ptype; $human_ptype = $ptype;
if (in_array($ptype, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer']) && !isset($this->settings['td'])) { if (in_array($ptype, ['InputDialogPeer', 'DialogPeer', 'NotifyPeer', 'InputNotifyPeer', 'User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer']) && !isset($this->settings['td'])) {
$human_ptype = 'Username, chat ID, Update, Message or '.$ptype; $human_ptype = 'Username, chat ID, Update, Message or '.$ptype;
} }
if (in_array($ptype, ['InputMedia', 'InputPhoto', 'InputDocument']) && !isset($this->settings['td'])) { if (in_array($ptype, ['InputMedia', 'InputPhoto', 'InputDocument']) && !isset($this->settings['td'])) {

View File

@ -9,7 +9,7 @@ MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY
See the GNU Affero General Public License for more details. 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. You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace danog\MadelineProto\MTProtoTools; namespace danog\MadelineProto\MTProtoTools;
@ -94,7 +94,7 @@ trait PeerHandler
case 'userEmpty': case 'userEmpty':
break; break;
default: default:
throw new \danog\MadelineProto\Exception('Invalid user provided at key '.$key.': '.var_export($user, true)); throw new \danog\MadelineProto\Exception('Invalid user provided at key ' . $key . ': ' . var_export($user, true));
break; break;
} }
} }
@ -162,7 +162,7 @@ trait PeerHandler
} }
break; break;
default: default:
throw new \danog\MadelineProto\Exception('Invalid chat provided at key '.$key.': '.var_export($chat, true)); throw new \danog\MadelineProto\Exception('Invalid chat provided at key ' . $key . ': ' . var_export($chat, true));
break; break;
} }
} }
@ -223,6 +223,18 @@ trait PeerHandler
{ {
if (is_array($id)) { if (is_array($id)) {
switch ($id['_']) { switch ($id['_']) {
case 'updateDialogPinned':
case 'updateDialogUnreadMark':
case 'updateNotifySettings':
$id = $id['peer'];
case 'updateDraftMessage':
case 'inputDialogPeer':
case 'dialogPeer':
case 'inputNotifyPeer':
case 'notifyPeer':
case 'dialog':
case 'help.proxyDataPromo':
return $this->get_info($id['peer']);
case 'inputUserSelf': case 'inputUserSelf':
case 'inputPeerSelf': case 'inputPeerSelf':
$id = $this->authorization['user']['id']; $id = $this->authorization['user']['id'];
@ -324,7 +336,7 @@ trait PeerHandler
case 'channelForbidden': case 'channelForbidden':
throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN'); throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN');
default: default:
throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($id, true)); throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($id, true));
break; break;
} }
} }
@ -333,7 +345,7 @@ trait PeerHandler
$id = $this->to_supergroup($matches[1]); $id = $this->to_supergroup($matches[1]);
} }
if (preg_match('/^chat#(\d*)/', $id, $matches)) { if (preg_match('/^chat#(\d*)/', $id, $matches)) {
$id = '-'.$matches[1]; $id = '-' . $matches[1];
} }
if (preg_match('/^user#(\d*)/', $id, $matches)) { if (preg_match('/^user#(\d*)/', $id, $matches)) {
$id = $matches[1]; $id = $matches[1];
@ -358,9 +370,9 @@ trait PeerHandler
} }
} }
if (!isset($this->settings['pwr']['requests']) || $this->settings['pwr']['requests'] === true && $recursive) { if (!isset($this->settings['pwr']['requests']) || $this->settings['pwr']['requests'] === true && $recursive) {
$dbres = json_decode(@file_get_contents('https://id.pwrtelegram.xyz/db/getusername?id='.$id, false, stream_context_create(['http' => ['timeout' => 2]])), true); $dbres = json_decode(@file_get_contents('https://id.pwrtelegram.xyz/db/getusername?id=' . $id, false, stream_context_create(['http' => ['timeout' => 2]])), true);
if (isset($dbres['ok']) && $dbres['ok']) { if (isset($dbres['ok']) && $dbres['ok']) {
$this->resolve_username('@'.$dbres['result']); $this->resolve_username('@' . $dbres['result']);
return $this->get_info($id, false); return $this->get_info($id, false);
} }
@ -413,6 +425,10 @@ trait PeerHandler
throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database'); throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database');
} }
$res['Peer'] = ['_' => 'peerUser', 'user_id' => $constructor['id']]; $res['Peer'] = ['_' => 'peerUser', 'user_id' => $constructor['id']];
$res['DialogPeer'] = ['_' => 'dialogPeer', 'peer' => $res['Peer']];
$res['NotifyPeer'] = ['_' => 'notifyPeer', 'peer' => $res['Peer']];
$res['InputDialogPeer'] = ['_' => 'inputDialogPeer', 'peer' => $res['InputPeer']];
$res['InputNotifyPeer'] = ['_' => 'inputNotifyPeer', 'peer' => $res['InputPeer']];
$res['user_id'] = $constructor['id']; $res['user_id'] = $constructor['id'];
$res['bot_api_id'] = $constructor['id']; $res['bot_api_id'] = $constructor['id'];
$res['type'] = $constructor['bot'] ? 'bot' : 'user'; $res['type'] = $constructor['bot'] ? 'bot' : 'user';
@ -420,6 +436,11 @@ trait PeerHandler
case 'chat': case 'chat':
case 'chatForbidden': case 'chatForbidden':
$res['InputPeer'] = ['_' => 'inputPeerChat', 'chat_id' => $constructor['id']]; $res['InputPeer'] = ['_' => 'inputPeerChat', 'chat_id' => $constructor['id']];
$res['DialogPeer'] = ['_' => 'dialogPeer', 'peer' => $res['Peer']];
$res['NotifyPeer'] = ['_' => 'notifyPeer', 'peer' => $res['Peer']];
$res['InputDialogPeer'] = ['_' => 'inputDialogPeer', 'peer' => $res['InputPeer']];
$res['InputNotifyPeer'] = ['_' => 'inputNotifyPeer', 'peer' => $res['InputPeer']];
$res['Peer'] = ['_' => 'peerChat', 'chat_id' => $constructor['id']]; $res['Peer'] = ['_' => 'peerChat', 'chat_id' => $constructor['id']];
$res['chat_id'] = $constructor['id']; $res['chat_id'] = $constructor['id'];
$res['bot_api_id'] = -$constructor['id']; $res['bot_api_id'] = -$constructor['id'];
@ -430,6 +451,11 @@ trait PeerHandler
throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database'); throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database');
} }
$res['InputPeer'] = ['_' => 'inputPeerChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; $res['InputPeer'] = ['_' => 'inputPeerChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
$res['DialogPeer'] = ['_' => 'dialogPeer', 'peer' => $res['Peer']];
$res['NotifyPeer'] = ['_' => 'notifyPeer', 'peer' => $res['Peer']];
$res['InputDialogPeer'] = ['_' => 'inputDialogPeer', 'peer' => $res['InputPeer']];
$res['InputNotifyPeer'] = ['_' => 'inputNotifyPeer', 'peer' => $res['InputPeer']];
$res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; $res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
$res['Peer'] = ['_' => 'peerChannel', 'channel_id' => $constructor['id']]; $res['Peer'] = ['_' => 'peerChannel', 'channel_id' => $constructor['id']];
$res['channel_id'] = $constructor['id']; $res['channel_id'] = $constructor['id'];
@ -440,7 +466,7 @@ trait PeerHandler
throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN'); throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN');
break; break;
default: default:
throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($constructor, true)); throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($constructor, true));
break; break;
} }
@ -622,7 +648,7 @@ trait PeerHandler
foreach ($filters as $filter) { foreach ($filters as $filter) {
$this->recurse_alphabet_search_participants($full['InputChannel'], $filter, $q, $total_count, $res); $this->recurse_alphabet_search_participants($full['InputChannel'], $filter, $q, $total_count, $res);
} }
$this->logger->logger('Fetched '.count($res['participants'])." out of $total_count"); $this->logger->logger('Fetched ' . count($res['participants']) . " out of $total_count");
$res['participants'] = array_values($res['participants']); $res['participants'] = array_values($res['participants']);
} }
if (!$fullfetch) { if (!$fullfetch) {
@ -642,7 +668,7 @@ trait PeerHandler
} }
for ($x = 'a'; $x !== 'aa' && $total_count > count($res['participants']); $x++) { for ($x = 'a'; $x !== 'aa' && $total_count > count($res['participants']); $x++) {
$this->recurse_alphabet_search_participants($channel, $filter, $q.$x, $total_count, $res); $this->recurse_alphabet_search_participants($channel, $filter, $q . $x, $total_count, $res);
} }
} }
@ -717,7 +743,7 @@ trait PeerHandler
} }
$res['participants'][$participant['user_id']] = $newres; $res['participants'][$participant['user_id']] = $newres;
} }
$this->logger->logger('Fetched '.count($gres['participants'])." channel participants with filter $filter, query $q, offset $offset, limit $limit, hash $hash: ".($cached ? 'cached' : 'not cached').', '.($offset + count($gres['participants'])).' participants out of '.$gres['count'].', in total fetched '.count($res['participants']).' out of '.$total_count); $this->logger->logger('Fetched ' . count($gres['participants']) . " channel participants with filter $filter, query $q, offset $offset, limit $limit, hash $hash: " . ($cached ? 'cached' : 'not cached') . ', ' . ($offset + count($gres['participants'])) . ' participants out of ' . $gres['count'] . ', in total fetched ' . count($res['participants']) . ' out of ' . $total_count);
$offset += count($gres['participants']); $offset += count($gres['participants']);
} while (count($gres['participants'])); } while (count($gres['participants']));
@ -783,7 +809,7 @@ trait PeerHandler
$id = isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id']; $id = isset($this->authorization['user']['username']) ? $this->authorization['user']['username'] : $this->authorization['user']['id'];
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://id.pwrtelegram.xyz/db'.$this->settings['pwr']['db_token'].'/addnewmadeline?d=pls&from='.$id); curl_setopt($ch, CURLOPT_URL, 'https://id.pwrtelegram.xyz/db' . $this->settings['pwr']['db_token'] . '/addnewmadeline?d=pls&from=' . $id);
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
@ -797,7 +823,7 @@ trait PeerHandler
if (file_exists($path)) { if (file_exists($path)) {
unlink($path); unlink($path);
} }
$this->logger->logger('======= COULD NOT STORE IN DB DUE TO '.$e->getMessage().' =============', \danog\MadelineProto\Logger::VERBOSE); $this->logger->logger('======= COULD NOT STORE IN DB DUE TO ' . $e->getMessage() . ' =============', \danog\MadelineProto\Logger::VERBOSE);
} }
} }
@ -806,7 +832,7 @@ trait PeerHandler
try { try {
$res = $this->method_call('contacts.resolveUsername', ['username' => str_replace('@', '', $username)], ['datacenter' => $this->datacenter->curdc]); $res = $this->method_call('contacts.resolveUsername', ['username' => str_replace('@', '', $username)], ['datacenter' => $this->datacenter->curdc]);
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
$this->logger->logger('Username resolution failed with error '.$e->getMessage(), \danog\MadelineProto\Logger::ERROR); $this->logger->logger('Username resolution failed with error ' . $e->getMessage(), \danog\MadelineProto\Logger::ERROR);
if (strpos($e->rpc, 'FLOOD_WAIT_') === 0 || $e->rpc === 'AUTH_KEY_UNREGISTERED' || $e->rpc === 'USERNAME_INVALID') { if (strpos($e->rpc, 'FLOOD_WAIT_') === 0 || $e->rpc === 'AUTH_KEY_UNREGISTERED' || $e->rpc === 'USERNAME_INVALID') {
throw $e; throw $e;
} }

View File

@ -346,7 +346,7 @@ trait TL
if ($type['type'] === 'InputMessage' && !is_array($object)) { if ($type['type'] === 'InputMessage' && !is_array($object)) {
$object = ['_' => 'inputMessageID', 'id' => $object]; $object = ['_' => 'inputMessageID', 'id' => $object];
} }
if (in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer']) && (!is_array($object) || isset($object['_']) && $this->constructors->find_by_predicate($object['_'])['type'] !== $type['type'])) { if (in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer', 'InputDialogPeer', 'InputNotifyPeer']) && (!is_array($object) || isset($object['_']) && $this->constructors->find_by_predicate($object['_'])['type'] !== $type['type'])) {
$object = $this->get_info($object); $object = $this->get_info($object);
if (!isset($object[$type['type']])) { if (!isset($object[$type['type']])) {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['peer_not_in_db']); throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['peer_not_in_db']);