Asyncify (3rd pass)

This commit is contained in:
Daniil Gentili 2019-05-11 17:16:13 +02:00
parent cacc792619
commit d17daf902a
7 changed files with 70 additions and 70 deletions

View File

@ -241,14 +241,14 @@ trait Files
case 'updateNewChannelMessage':
$message_media = $message_media['message'];
case 'message':
return $this->get_download_info($message_media['media']);
return yield $this->get_download_info_async($message_media['media']);
case 'updateNewEncryptedMessage':
$message_media = $message_media['message'];
// Secret media
case 'encryptedMessage':
if ($message_media['decrypted_message']['media']['_'] === 'decryptedMessageMediaExternalDocument') {
return $this->get_download_info($message_media['decrypted_message']['media']);
return yield $this->get_download_info_async($message_media['decrypted_message']['media']);
}
$res['InputFileLocation'] = ['_' => 'inputEncryptedFileLocation', 'id' => $message_media['file']['id'], 'access_hash' => $message_media['file']['access_hash'], 'dc_id' => $message_media['file']['dc_id']];
$res['size'] = $message_media['decrypted_message']['media']['size'];
@ -304,7 +304,7 @@ trait Files
case 'wallPaper':
$photo = end($message_media['sizes']);
return array_merge($res, $this->get_download_info($photo));
return array_merge($res, yield $this->get_download_info_async($photo));
// Photos
case 'photo':
case 'messageMediaPhoto':
@ -316,11 +316,11 @@ trait Files
$photo = end($message_media['photo']['sizes']);
}
return array_merge($res, $this->get_download_info($photo));
return array_merge($res, yield $this->get_download_info_async($photo));
case 'userProfilePhoto':
case 'chatPhoto':
return array_merge($res, $this->get_download_info($message_media['photo_big']));
return array_merge($res, yield $this->get_download_info_async($message_media['photo_big']));
case 'photoCachedSize':
$res['size'] = strlen($message_media['bytes']);
@ -331,12 +331,12 @@ trait Files
$res['mime'] = $this->get_mime_from_buffer($res['data']);
$res['ext'] = $this->get_extension_from_mime($res['mime']);
} else {
$res = array_merge($res, $this->get_download_info($message_media['location']));
$res = array_merge($res, yield $this->get_download_info_async($message_media['location']));
}
return $res;
case 'photoSize':
$res = $this->get_download_info($message_media['location']);
$res = yield $this->get_download_info_async($message_media['location']);
if (isset($message_media['size'])) {
$res['size'] = $message_media['size'];
}
@ -399,16 +399,16 @@ trait Files
}
}
public function download_to_dir($message_media, $dir, $cb = null)
public function download_to_dir_async($message_media, $dir, $cb = null)
{
if (is_object($dir) && class_implements($dir)['danog\MadelineProto\FileCallbackInterface']) {
$cb = $dir;
$dir = $dir->getFile();
}
$message_media = $this->get_download_info($message_media);
$message_media = yield $this->get_download_info_async($message_media);
return $this->download_to_file($message_media, $dir.'/'.$message_media['name'].$message_media['ext'], $cb);
return yield $this->download_to_file_async($message_media, $dir.'/'.$message_media['name'].$message_media['ext'], $cb);
}
public function download_to_file_async($message_media, $file, $cb = null)
@ -422,7 +422,7 @@ trait Files
touch($file);
}
$file = realpath($file);
$message_media = $this->get_download_info($message_media);
$message_media = yield $this->get_download_info_async($message_media);
$stream = fopen($file, 'r+b');
$size = fstat($stream)['size'];
$this->logger->logger('Waiting for lock of file to download...');
@ -452,7 +452,7 @@ trait Files
};
}
$message_media = $this->get_download_info($message_media);
$message_media = yield $this->get_download_info_async($message_media);
try {
if (stream_get_meta_data($stream)['seekable']) {

View File

@ -141,7 +141,7 @@ trait PeerHandler
{
Loop::defer(function () use ($id, $full_fetch, $send) {
try {
$this->get_pwr_chat($id, $full_fetch, $send);
yield $this->get_pwr_chat_async($id, $full_fetch, $send);
} catch (\danog\MadelineProto\Exception $e) {
$this->logger->logger("While caching: ".$e->getMessage(), \danog\MadelineProto\Logger::WARNING);
} catch (\danog\MadelineProto\RPCErrorException $e) {
@ -168,12 +168,12 @@ trait PeerHandler
}
}
public function entities_peer_isset($entities)
public function entities_peer_isset_async($entities)
{
try {
foreach ($entities as $entity) {
if ($entity['_'] === 'messageEntityMentionName' || $entity['_'] === 'inputMessageEntityMentionName') {
if (!$this->peer_isset($entity['user_id'])) {
if (!yield $this->peer_isset_async($entity['user_id'])) {
return false;
}
}
@ -185,13 +185,13 @@ trait PeerHandler
return true;
}
public function fwd_peer_isset($fwd)
public function fwd_peer_isset_async($fwd)
{
try {
if (isset($fwd['user_id']) && !$this->peer_isset($fwd['user_id'])) {
if (isset($fwd['user_id']) && !yield $this->peer_isset_async($fwd['user_id'])) {
return false;
}
if (isset($fwd['channel_id']) && !$this->peer_isset($this->to_supergroup($fwd['channel_id']))) {
if (isset($fwd['channel_id']) && !yield $this->peer_isset_async($this->to_supergroup($fwd['channel_id']))) {
return false;
}
} catch (\danog\MadelineProto\Exception $e) {
@ -600,15 +600,15 @@ trait PeerHandler
if (isset($res['participants']) && $fullfetch) {
foreach ($res['participants'] as $key => $participant) {
$newres = [];
$newres['user'] = $this->get_pwr_chat($participant['user_id'], false, true);
$newres['user'] = yield $this->get_pwr_chat_async($participant['user_id'], false, true);
if (isset($participant['inviter_id'])) {
$newres['inviter'] = $this->get_pwr_chat($participant['inviter_id'], false, true);
$newres['inviter'] = yield $this->get_pwr_chat_async($participant['inviter_id'], false, true);
}
if (isset($participant['promoted_by'])) {
$newres['promoted_by'] = $this->get_pwr_chat($participant['promoted_by'], false, true);
$newres['promoted_by'] = yield $this->get_pwr_chat_async($participant['promoted_by'], false, true);
}
if (isset($participant['kicked_by'])) {
$newres['kicked_by'] = $this->get_pwr_chat($participant['kicked_by'], false, true);
$newres['kicked_by'] = yield $this->get_pwr_chat_async($participant['kicked_by'], false, true);
}
if (isset($participant['date'])) {
$newres['date'] = $participant['date'];
@ -651,7 +651,7 @@ trait PeerHandler
$filters = ['channelParticipantsSearch', 'channelParticipantsKicked', 'channelParticipantsBanned'];
foreach ($filters as $filter) {
$this->recurse_alphabet_search_participants($full['InputChannel'], $filter, $q, $total_count, $res);
yield $this->recurse_alphabet_search_participants_async($full['InputChannel'], $filter, $q, $total_count, $res);
}
$this->logger->logger('Fetched '.count($res['participants'])." out of $total_count");
$res['participants'] = array_values($res['participants']);
@ -673,7 +673,7 @@ trait PeerHandler
}
for ($x = 'a'; $x !== 'aa' && $total_count > count($res['participants']); $x++) {
$this->recurse_alphabet_search_participants($channel, $filter, $q.$x, $total_count, $res);
yield $this->recurse_alphabet_search_participants_async($channel, $filter, $q.$x, $total_count, $res);
}
}
@ -710,15 +710,15 @@ trait PeerHandler
foreach ($gres['participants'] as $participant) {
$newres = [];
$newres['user'] = $this->get_pwr_chat($participant['user_id'], false, true);
$newres['user'] = yield $this->get_pwr_chat_async($participant['user_id'], false, true);
if (isset($participant['inviter_id'])) {
$newres['inviter'] = $this->get_pwr_chat($participant['inviter_id'], false, true);
$newres['inviter'] = yield $this->get_pwr_chat_async($participant['inviter_id'], false, true);
}
if (isset($participant['kicked_by'])) {
$newres['kicked_by'] = $this->get_pwr_chat($participant['kicked_by'], false, true);
$newres['kicked_by'] = yield $this->get_pwr_chat_async($participant['kicked_by'], false, true);
}
if (isset($participant['promoted_by'])) {
$newres['promoted_by'] = $this->get_pwr_chat($participant['promoted_by'], false, true);
$newres['promoted_by'] = yield $this->get_pwr_chat_async($participant['promoted_by'], false, true);
}
if (isset($participant['date'])) {
$newres['date'] = $participant['date'];

View File

@ -568,7 +568,7 @@ trait ResponseHandler
});
}
public function handle_pending_updates()
public function handle_pending_updates_async()
{
if ($this->postpone_updates) {
return false;
@ -579,7 +579,7 @@ trait ResponseHandler
if (isset($this->pending_updates[$key])) {
$updates = $this->pending_updates[$key];
unset($this->pending_updates[$key]);
$this->handle_updates($updates);
yield $this->handle_updates_async($updates);
}
}
}
@ -617,17 +617,17 @@ trait ResponseHandler
case 'updates':
case 'updatesCombined':
foreach ($updates['updates'] as $update) {
$this->handle_update($update, $opts);
yield $this->handle_update_async($update, $opts);
}
break;
case 'updateShort':
$this->handle_update($updates['update'], $opts);
yield $this->handle_update_async($updates['update'], $opts);
break;
case 'updateShortMessage':
case 'updateShortChatMessage':
$from_id = isset($updates['from_id']) ? $updates['from_id'] : ($updates['out'] ? $this->authorization['user']['id'] : $updates['user_id']);
$to_id = isset($updates['chat_id']) ? -$updates['chat_id'] : ($updates['out'] ? $updates['user_id'] : $this->authorization['user']['id']);
if (!$this->peer_isset($from_id) || !$this->peer_isset($to_id) || isset($updates['via_bot_id']) && !$this->peer_isset($updates['via_bot_id']) || isset($updates['entities']) && !$this->entities_peer_isset($updates['entities']) || isset($updates['fwd_from']) && !$this->fwd_peer_isset($updates['fwd_from'])) {
if (!yield $this->peer_isset_async($from_id) || !yield $this->peer_isset_async($to_id) || isset($updates['via_bot_id']) && !yield $this->peer_isset_async($updates['via_bot_id']) || isset($updates['entities']) && !$this->entities_peer_isset($updates['entities']) || isset($updates['fwd_from']) && !$this->fwd_peer_isset($updates['fwd_from'])) {
$this->logger->logger('getDifference: good - getting user for updateShortMessage', \danog\MadelineProto\Logger::VERBOSE);
yield $this->get_updates_difference_async();
}
@ -647,10 +647,10 @@ trait ResponseHandler
break;
}
$update = ['_' => 'updateNewMessage', 'message' => $message, 'pts' => $updates['pts'], 'pts_count' => $updates['pts_count']];
$this->handle_update($update, $opts);
yield $this->handle_update_async($update, $opts);
break;
case 'updateShortSentMessage':
//$this->set_update_state(['date' => $updates['date']]);
//yield $this->set_update_state_async(['date' => $updates['date']]);
break;
case 'updatesTooLong':
yield $this->get_updates_difference_async();

View File

@ -262,7 +262,7 @@ trait UpdateHandler
}
if (!$this->got_state) {
$this->got_state = true;
$this->set_update_state(yield $this->get_updates_state_async());
yield $this->set_update_state_async(yield $this->get_updates_state_async());
}
return $this->updates_state;
@ -301,22 +301,22 @@ trait UpdateHandler
try {
switch ($difference['_']) {
case 'updates.differenceEmpty':
$this->set_update_state($difference);
yield $this->set_update_state_async($difference);
break;
case 'updates.difference':
$this->updates_state['sync_loading'] = true;
$this->handle_multiple_update($difference['other_updates']);
foreach ($difference['new_encrypted_messages'] as $encrypted) {
$this->handle_encrypted_update(['_' => 'updateNewEncryptedMessage', 'message' => $encrypted], true);
yield $this->handle_encrypted_update_async(['_' => 'updateNewEncryptedMessage', 'message' => $encrypted], true);
}
$this->handle_update_messages($difference['new_messages']);
$this->set_update_state($difference['state']);
yield $this->set_update_state_async($difference['state']);
break;
case 'updates.differenceSlice':
$this->updates_state['sync_loading'] = true;
$this->handle_multiple_update($difference['other_updates']);
$this->handle_update_messages($difference['new_messages']);
$this->set_update_state($difference['intermediate_state']);
yield $this->set_update_state_async($difference['intermediate_state']);
unset($difference);
$this->updates_state['sync_loading'] = false;
yield $this->get_updates_difference_async();
@ -411,9 +411,9 @@ trait UpdateHandler
$from = false;
$via_bot = false;
$entities = false;
if (($from = isset($update['message']['from_id']) && !$this->peer_isset($update['message']['from_id'])) ||
($to = !$this->peer_isset($update['message']['to_id'])) ||
($via_bot = isset($update['message']['via_bot_id']) && !$this->peer_isset($update['message']['via_bot_id'])) ||
if (($from = isset($update['message']['from_id']) && !yield $this->peer_isset_async($update['message']['from_id'])) ||
($to = !yield $this->peer_isset_async($update['message']['to_id'])) ||
($via_bot = isset($update['message']['via_bot_id']) && !yield $this->peer_isset_async($update['message']['via_bot_id'])) ||
($entities = isset($update['message']['entities']) && !$this->entities_peer_isset($update['message']['entities'])) // ||
//isset($update['message']['fwd_from']) && !$this->fwd_peer_isset($update['message']['fwd_from'])
) {
@ -423,7 +423,7 @@ trait UpdateHandler
if ($via_bot) $log .= "via_bot {$update['message']['via_bot_id']}, ";
if ($entities) $log .= "entities ".json_encode($update['message']['entities']).", ";
$this->logger->logger("Not enough data: for message update $log, getting difference...", \danog\MadelineProto\Logger::VERBOSE);
if ($channel_id !== false && $this->peer_isset($this->to_supergroup($channel_id))) {
if ($channel_id !== false && yield $this->peer_isset_async($this->to_supergroup($channel_id))) {
yield $this->get_channel_difference_async($channel_id);
} else {
yield $this->get_updates_difference_async();
@ -433,7 +433,7 @@ trait UpdateHandler
}
break;
default:
if ($channel_id !== false && !$this->peer_isset($this->to_supergroup($channel_id))) {
if ($channel_id !== false && !yield $this->peer_isset_async($this->to_supergroup($channel_id))) {
$this->logger->logger('Skipping update, I do not have the channel id '.$channel_id, \danog\MadelineProto\Logger::ERROR);
return false;
@ -455,7 +455,7 @@ trait UpdateHandler
}
if ($cur_state['pts'] + (isset($update['pts_count']) ? $update['pts_count'] : 0) !== $update['pts']) {
$logger("PTS hole");
if ($channel_id !== false && $this->peer_isset($this->to_supergroup($channel_id))) {
if ($channel_id !== false && yield $this->peer_isset_async($this->to_supergroup($channel_id))) {
yield $this->get_channel_difference_async($channel_id);
} else {
yield $this->get_updates_difference_async();
@ -497,29 +497,29 @@ trait UpdateHandler
yield $this->save_update_async($update);
}
public function handle_multiple_update($updates, $options = [], $channel = false)
public function handle_multiple_update_async($updates, $options = [], $channel = false)
{
if (!$this->settings['updates']['handle_updates']) {
return;
}
if ($channel === false) {
foreach ($updates as $update) {
$this->handle_update($update, $options);
yield $this->handle_update_async($update, $options);
}
} else {
foreach ($updates as $update) {
$this->handle_update($update);
yield $this->handle_update_async($update);
}
}
}
public function handle_update_messages($messages, $channel = false)
public function handle_update_messages_async($messages, $channel = false)
{
if (!$this->settings['updates']['handle_updates']) {
return;
}
foreach ($messages as $message) {
$this->handle_update(['_' => $channel === false ? 'updateNewMessage' : 'updateNewChannelMessage', 'message' => $message, 'pts' => $channel === false ? $this->load_update_state()['pts'] : $this->load_channel_state($channel)['pts'], 'pts_count' => 0]);
yield $this->handle_update_async(['_' => $channel === false ? 'updateNewMessage' : 'updateNewChannelMessage', 'message' => $message, 'pts' => $channel === false ? $this->load_update_state()['pts'] : $this->load_channel_state($channel)['pts'], 'pts_count' => 0]);
}
}
@ -594,7 +594,7 @@ trait UpdateHandler
}
$this->logger->logger('Applying qts: '.$update['qts'].' over current qts '.$cur_state['qts'].', chat id: '.$update['message']['chat_id'], \danog\MadelineProto\Logger::VERBOSE);
yield $this->method_call_async_read('messages.receivedQueue', ['max_qts' => $cur_state['qts'] = $update['qts']], ['datacenter' => $this->settings['connection_settings']['default_dc']]);
$this->handle_encrypted_update($update);
yield $this->handle_encrypted_update_async($update);
return;
}

View File

@ -86,7 +86,7 @@ trait ResponseHandler
yield $this->save_update_async($update);
break;
case 'decryptedMessageLayer':
if ($this->check_secret_out_seq_no($update['message']['chat_id'], $update['message']['decrypted_message']['out_seq_no']) && $this->check_secret_in_seq_no($update['message']['chat_id'], $update['message']['decrypted_message']['in_seq_no'])) {
if (yield $this->check_secret_out_seq_no_async($update['message']['chat_id'], $update['message']['decrypted_message']['out_seq_no']) && yield $this->check_secret_in_seq_no_async($update['message']['chat_id'], $update['message']['decrypted_message']['in_seq_no'])) {
$this->secret_chats[$update['message']['chat_id']]['in_seq_no']++;
if ($update['message']['decrypted_message']['layer'] >= 17) {
$this->secret_chats[$update['message']['chat_id']]['layer'] = $update['message']['decrypted_message']['layer'];

View File

@ -171,9 +171,9 @@ trait BotAPI
$newd['date'] = $data['date'];
$newd['text'] = $sent_arguments['message'];
if ($data['out']) {
$newd['from'] = $this->get_pwr_chat($this->authorization['user']);
$newd['from'] = yield $this->get_pwr_chat_async($this->authorization['user']);
}
$newd['chat'] = $this->get_pwr_chat($sent_arguments['peer']);
$newd['chat'] = yield $this->get_pwr_chat_async($sent_arguments['peer']);
if (isset($data['entities'])) {
$newd['entities'] = yield $this->MTProto_to_botAPI_async($data['entities'], $sent_arguments);
}
@ -192,9 +192,9 @@ trait BotAPI
$newd['post'] = $data['post'];
$newd['silent'] = $data['silent'];
if (isset($data['from_id'])) {
$newd['from'] = $this->get_pwr_chat($data['from_id']);
$newd['from'] = yield $this->get_pwr_chat_async($data['from_id']);
}
$newd['chat'] = $this->get_pwr_chat($data['to_id']);
$newd['chat'] = yield $this->get_pwr_chat_async($data['to_id']);
if (isset($data['entities'])) {
$newd['entities'] = yield $this->MTProto_to_botAPI_async($data['entities'], $sent_arguments);
}
@ -205,13 +205,13 @@ trait BotAPI
$newd['edit_date'] = $data['edit_date'];
}
if (isset($data['via_bot_id'])) {
$newd['via_bot'] = $this->get_pwr_chat($data['via_bot_id']);
$newd['via_bot'] = yield $this->get_pwr_chat_async($data['via_bot_id']);
}
if (isset($data['fwd_from']['from_id'])) {
$newd['froward_from'] = $this->get_pwr_chat($data['fwd_from']['from_id']);
$newd['forward_from'] = yield $this->get_pwr_chat_async($data['fwd_from']['from_id']);
}
if (isset($data['fwd_from']['channel_id'])) {
$newd['forward_from_chat'] = $this->get_pwr_chat($data['fwd_from']['channel_id']);
$newd['forward_from_chat'] = yield $this->get_pwr_chat_async($data['fwd_from']['channel_id']);
}
if (isset($data['fwd_from']['date'])) {
$newd['forward_date'] = $data['fwd_from']['date'];
@ -277,7 +277,7 @@ trait BotAPI
case 'messageEntityMentionName':
unset($data['_']);
$data['type'] = 'text_mention';
$data['user'] = $this->get_pwr_chat($data['user_id']);
$data['user'] = yield $this->get_pwr_chat_async($data['user_id']);
unset($data['user_id']);
return $data;
@ -447,7 +447,7 @@ trait BotAPI
break;
case 'p':
foreach ($node->childNodes as $node) {
$this->parse_node($node, $entities, $new_message, $offset);
yield $this->parse_node_async($node, $entities, $new_message, $offset);
}
break;
case 'a':
@ -485,7 +485,7 @@ trait BotAPI
}
}
public function parse_mode($arguments)
public function parse_mode_async($arguments)
{
if ($arguments['message'] === '' || !isset($arguments['message']) || !isset($arguments['parse_mode'])) {
return $arguments;
@ -511,7 +511,7 @@ trait BotAPI
}
$offset = 0;
foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $node) {
$this->parse_node($node, $arguments['entities'], $new_message, $offset);
yield $this->parse_node_async($node, $arguments['entities'], $new_message, $offset);
}
if (isset($arguments['entities']['buttons'])) {
$arguments['reply_markup'] = $this->build_rows($arguments['entities']['buttons']);

View File

@ -44,7 +44,7 @@ trait TD
return $params;
}
public function td_to_mtproto($params)
public function td_to_mtproto_async($params)
{
$newparams = ['_' => self::REVERSE[$params['_']]];
foreach (self::TD_PARAMS_CONVERSION[$newparams['_']] as $td => $mtproto) {
@ -66,7 +66,7 @@ trait TD
default:
$newparams[$mtproto[0]] = isset($params[$td]) ? $params[$td] : null;
if (is_array($newparams[$mtproto[0]])) {
$newparams[$mtproto[0]] = $this->mtproto_to_td($newparams[$mtproto[0]]);
$newparams[$mtproto[0]] = yield $this->mtproto_to_td_async($newparams[$mtproto[0]]);
}
}
}
@ -75,9 +75,9 @@ trait TD
return $newparams;
}
public function mtproto_to_tdcli($params)
public function mtproto_to_tdcli_async($params)
{
return $this->td_to_tdcli($this->mtproto_to_td($params));
return $this->td_to_tdcli(yield $this->mtproto_to_td_async($params));
}
public function mtproto_to_td_async(&$params)
@ -138,7 +138,7 @@ trait TD
if ($params['message'] !== '') {
$newparams[$td] = ['_' => 'messageText', 'text' => $params['message']];
if (isset($params['media']['_']) && $params['media']['_'] === 'messageMediaWebPage') {
$newparams[$td]['web_page'] = $this->mtproto_to_td($params['media']['webpage']);
$newparams[$td]['web_page'] = yield $this->mtproto_to_td_async($params['media']['webpage']);
}
if (isset($params['entities'])) {
$newparams[$td]['entities'] = $params['entities'];
@ -154,7 +154,7 @@ trait TD
$newparams[$td] = isset($params[$mtproto[0]]) ? $params[$mtproto[0]] : null;
}
if (is_array($newparams[$td])) {
$newparams[$td] = $this->mtproto_to_td($newparams[$td]);
$newparams[$td] = yield $this->mtproto_to_td_async($newparams[$td]);
}
}
}