Asyncify (2nd pass)

This commit is contained in:
Daniil Gentili 2019-05-11 17:12:17 +02:00
parent 66f84f4b05
commit cacc792619
16 changed files with 120 additions and 129 deletions

View File

@ -403,7 +403,7 @@ class MTProto implements TLCallback
}
if ($this->authorized === self::LOGGED_IN && $this->settings['updates']['handle_updates'] && !$this->updates_state['sync_loading']) {
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['getupdates_deserialization'], Logger::NOTICE);
$this->get_updates_difference();
yield $this->get_updates_difference_async();
}
$this->datacenter->sockets[$this->settings['connection_settings']['default_dc']]->updater->start();
}
@ -865,7 +865,7 @@ class MTProto implements TLCallback
return $this->config;
}
$this->config = empty($config) ? yield $this->method_call_async_read('help.getConfig', $config, empty($options) ? ['datacenter' => $this->settings['connection_settings']['default_dc']] : $options) : $config;
yield $this->parse_config();
yield $this->parse_config_async();
return $this->config;
}
@ -893,7 +893,7 @@ class MTProto implements TLCallback
public function parse_config_async()
{
if (isset($this->config['dc_options'])) {
yield $this->parse_dc_options($this->config['dc_options']);
yield $this->parse_dc_options_async($this->config['dc_options']);
unset($this->config['dc_options']);
}
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['config_updated'], Logger::NOTICE);

View File

@ -226,7 +226,7 @@ trait Files
return $this->gen_all_file($constructor, $regenerate);
}
public function get_download_info($message_media)
public function get_download_info_async($message_media)
{
if (is_string($message_media)) {
$message_media = $this->unpack_file_id($message_media)['MessageMedia'];
@ -290,7 +290,7 @@ trait Files
}
}
if (!isset($res['ext'])) {
$res['ext'] = $this->get_extension_from_location($res['InputFileLocation'], $this->get_extension_from_mime(isset($res['mime']) ? $res['mime'] : 'image/jpeg'));
$res['ext'] = yield $this->get_extension_from_location_async($res['InputFileLocation'], $this->get_extension_from_mime(isset($res['mime']) ? $res['mime'] : 'image/jpeg'));
}
if (!isset($res['mime'])) {
$res['mime'] = $this->get_mime_from_extension($res['ext'], 'image/jpeg');
@ -348,7 +348,7 @@ trait Files
case 'fileLocation':
$res['name'] = $message_media['volume_id'].'_'.$message_media['local_id'];
$res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $message_media['volume_id'], 'local_id' => $message_media['local_id'], 'secret' => $message_media['secret'], 'dc_id' => $message_media['dc_id'], 'file_reference' => $this->wait($this->referenceDatabase->getReference(ReferenceDatabase::PHOTO_LOCATION_LOCATION, $message_media))];
$res['ext'] = $this->get_extension_from_location($res['InputFileLocation'], '.jpg');
$res['ext'] = yield $this->get_extension_from_location_async($res['InputFileLocation'], '.jpg');
$res['mime'] = $this->get_mime_from_extension($res['ext'], 'image/jpeg');
return $res;
@ -382,7 +382,7 @@ trait Files
}
$res['InputFileLocation'] = ['_' => 'inputDocumentFileLocation', 'id' => $message_media['document']['id'], 'access_hash' => $message_media['document']['access_hash'], 'version' => isset($message_media['document']['version']) ? $message_media['document']['version'] : 0, 'dc_id' => $message_media['document']['dc_id'], 'file_reference' => $this->wait($this->referenceDatabase->getReference(ReferenceDatabase::DOCUMENT_LOCATION_LOCATION, $message_media['document']))];
if (!isset($res['ext'])) {
$res['ext'] = $this->get_extension_from_location($res['InputFileLocation'], $this->get_extension_from_mime($message_media['document']['mime_type']));
$res['ext'] = yield $this->get_extension_from_location_async($res['InputFileLocation'], $this->get_extension_from_mime($message_media['document']['mime_type']));
}
if (!isset($res['name'])) {
$res['name'] = $message_media['document']['access_hash'];
@ -411,7 +411,7 @@ trait Files
return $this->download_to_file($message_media, $dir.'/'.$message_media['name'].$message_media['ext'], $cb);
}
public function download_to_file($message_media, $file, $cb = null)
public function download_to_file_async($message_media, $file, $cb = null)
{
if (is_object($file) && class_implements($file)['danog\MadelineProto\FileCallbackInterface']) {
$cb = $file;
@ -429,7 +429,7 @@ trait Files
flock($stream, LOCK_EX);
try {
$this->download_to_stream($message_media, $stream, $cb, $size, -1);
yield $this->download_to_stream_async($message_media, $stream, $cb, $size, -1);
} finally {
flock($stream, LOCK_UN);
fclose($stream);

View File

@ -90,7 +90,7 @@ trait PeerHandler
}
}
public function add_chat($chat)
public function add_chat_async($chat)
{
switch ($chat['_']) {
case 'chat':
@ -126,7 +126,7 @@ trait PeerHandler
$this->chats[$bot_api_id] = $chat;
if ($this->settings['peer']['full_fetch'] && (!isset($this->full_chats[$bot_api_id]) || $this->full_chats[$bot_api_id]['full']['participants_count'] !== $this->get_full_info($bot_api_id)['full']['participants_count'])) {
if ($this->settings['peer']['full_fetch'] && (!isset($this->full_chats[$bot_api_id]) || $this->full_chats[$bot_api_id]['full']['participants_count'] !== yield $this->get_full_info_async($bot_api_id)['full']['participants_count'])) {
$this->cache_pwr_chat($bot_api_id, $this->settings['peer']['full_fetch'], true);
}
}
@ -150,10 +150,10 @@ trait PeerHandler
});
}
public function peer_isset($id)
public function peer_isset_async($id)
{
try {
return isset($this->chats[$this->get_info($id)['bot_api_id']]);
return isset($this->chats[yield $this->get_info_async($id)['bot_api_id']]);
} catch (\danog\MadelineProto\Exception $e) {
return false;
} catch (\danog\MadelineProto\RPCErrorException $e) {
@ -384,9 +384,9 @@ trait PeerHandler
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);
if (isset($dbres['ok']) && $dbres['ok']) {
$this->resolve_username('@'.$dbres['result']);
yield $this->resolve_username_async('@'.$dbres['result']);
return $this->get_info($id, false);
return yield $this->get_info_async($id, false);
}
}
if ($tried_simple && isset($this->caching_possible_username[$id])) {
@ -394,7 +394,7 @@ trait PeerHandler
$user = $this->caching_possible_username[$id];
unset($this->caching_possible_username[$id]);
return $this->get_info($user);
return yield $this->get_info_async($user);
}
throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database');
}
@ -404,7 +404,7 @@ trait PeerHandler
} else {
$invite = yield $this->method_call_async_read('messages.checkChatInvite', ['hash' => $matches[2]], ['datacenter' => $this->datacenter->curdc]);
if (isset($invite['chat'])) {
return $this->get_info($invite['chat']);
return yield $this->get_info_async($invite['chat']);
} else {
throw new \danog\MadelineProto\Exception('You have not joined this chat');
}
@ -412,14 +412,14 @@ trait PeerHandler
}
$id = strtolower(str_replace('@', '', $id));
if ($id === 'me') {
return $this->get_info($this->authorization['user']['id']);
return yield $this->get_info_async($this->authorization['user']['id']);
}
if ($id === 'support') {
if (!$this->supportUser) {
yield $this->method_call_async_read('help.getSupport', [], ['datacenter' => $this->settings['connection_settings']['default_dc']]);
}
return $this->get_info($this->supportUser);
return yield $this->get_info_async($this->supportUser);
}
foreach ($this->chats as $chat) {
if (isset($chat['username']) && strtolower($chat['username']) === $id) {
@ -427,9 +427,9 @@ trait PeerHandler
}
}
if ($recursive) {
$this->resolve_username($id);
yield $this->resolve_username_async($id);
return $this->get_info($id, false);
return yield $this->get_info_async($id, false);
}
throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database');
@ -501,7 +501,7 @@ trait PeerHandler
public function get_full_info_async($id)
{
$partial = $this->get_info($id);
$partial = yield $this->get_info_async($id);
if (time() - $this->full_chat_last_updated($partial['bot_api_id']) < (isset($this->settings['peer']['full_info_cache_time']) ? $this->settings['peer']['full_info_cache_time'] : 0)) {
return array_merge($partial, $this->full_chats[$partial['bot_api_id']]);
}
@ -524,14 +524,14 @@ trait PeerHandler
$res['last_update'] = time();
$this->full_chats[$partial['bot_api_id']] = $res;
$partial = $this->get_info($id);
$partial = yield $this->get_info_async($id);
return array_merge($partial, $res);
}
public function get_pwr_chat($id, $fullfetch = true, $send = true)
public function get_pwr_chat_async($id, $fullfetch = true, $send = true)
{
$full = $fullfetch ? $this->get_full_info($id) : $this->get_info($id);
$full = $fullfetch ? yield $this->get_full_info_async($id) : yield $this->get_info_async($id);
$res = ['id' => $full['bot_api_id'], 'type' => $full['type']];
switch ($full['type']) {
case 'user':
@ -547,19 +547,8 @@ trait PeerHandler
}
}
if (isset($full['full']['profile_photo']['sizes'])) {
$res['photo'] = $this->photosize_to_botapi(end($full['full']['profile_photo']['sizes']), $full['full']['profile_photo']);
$res['photo'] = yield $this->photosize_to_botapi_async(end($full['full']['profile_photo']['sizes']), $full['full']['profile_photo']);
}
/*$bio = '';
if ($full['type'] === 'user' && isset($res['username']) && !isset($res['about']) && $fullfetch) {
if (preg_match('/meta property="og:description" content=".+/', file_get_contents('https://telegram.me/'.$res['username']), $biores)) {
$bio = html_entity_decode(preg_replace_callback('/(&#[0-9]+;)/', function ($m) {
return mb_convert_encoding($m[1], 'UTF-8', 'HTML-ENTITIES');
}, str_replace(['meta property="og:description" content="', '">'], '', $biores[0])));
}
if ($bio != '' && $bio != 'You can contact @'.$res['username'].' right away.') {
$res['about'] = $bio;
}
}*/
break;
case 'chat':
foreach (['title', 'participants_count', 'admin', 'admins_enabled'] as $key) {
@ -576,7 +565,7 @@ trait PeerHandler
$res['all_members_are_administrators'] = !$res['admins_enabled'];
}
if (isset($full['full']['chat_photo']['sizes'])) {
$res['photo'] = $this->photosize_to_botapi(end($full['full']['chat_photo']['sizes']), $full['full']['chat_photo']);
$res['photo'] = yield $this->photosize_to_botapi_async(end($full['full']['chat_photo']['sizes']), $full['full']['chat_photo']);
}
if (isset($full['full']['exported_invite']['link'])) {
$res['invite'] = $full['full']['exported_invite']['link'];
@ -598,7 +587,7 @@ trait PeerHandler
}
}
if (isset($full['full']['chat_photo']['sizes'])) {
$res['photo'] = $this->photosize_to_botapi(end($full['full']['chat_photo']['sizes']), $full['full']['chat_photo']);
$res['photo'] = yield $this->photosize_to_botapi_async(end($full['full']['chat_photo']['sizes']), $full['full']['chat_photo']);
}
if (isset($full['full']['exported_invite']['link'])) {
$res['invite'] = $full['full']['exported_invite']['link'];
@ -656,7 +645,7 @@ trait PeerHandler
$limit = 200;
$filters = ['channelParticipantsAdmins', 'channelParticipantsBots'];
foreach ($filters as $filter) {
$this->fetch_participants($full['InputChannel'], $filter, '', $total_count, $res);
yield $this->fetch_participants_async($full['InputChannel'], $filter, '', $total_count, $res);
}
$q = '';
@ -677,9 +666,9 @@ trait PeerHandler
return $res;
}
public function recurse_alphabet_search_participants($channel, $filter, $q, $total_count, &$res)
public function recurse_alphabet_search_participants_async($channel, $filter, $q, $total_count, &$res)
{
if (!$this->fetch_participants($channel, $filter, $q, $total_count, $res)) {
if (!yield $this->fetch_participants_async($channel, $filter, $q, $total_count, $res)) {
return false;
}

View File

@ -552,17 +552,19 @@ trait ResponseHandler
return;
}
if (isset($request['botAPI']) && $request['botAPI']) {
$response = $this->MTProto_to_botAPI($response);
}
$botAPI = isset($request['botAPI']) && $request['botAPI'];
unset($request);
$this->got_response_for_outgoing_message_id($request_id, $datacenter);
Loop::defer(function () use ($request_id, $response, $datacenter) {
Loop::defer(function () use ($request_id, $response, $datacenter, $botAPI) {
$this->call((function ()use ($request_id, $response, $datacenter, $botAPI) {
$r = isset($response['_']) ? $response['_'] : json_encode($response);
$this->logger->logger("Deferred: sent $r to deferred");
if ($botAPI) {
$response = yield $this->MTProto_to_botAPI_async($response);
}
$this->datacenter->sockets[$datacenter]->outgoing_messages[$request_id]['promise']->resolve($response);
unset($this->datacenter->sockets[$datacenter]->outgoing_messages[$request_id]['promise']);
})());
});
}
@ -583,7 +585,7 @@ trait ResponseHandler
}
}
public function handle_updates($updates, $actual_updates = null)
public function handle_updates_async($updates, $actual_updates = null)
{
if (!$this->settings['updates']['handle_updates']) {
return;
@ -627,14 +629,14 @@ trait ResponseHandler
$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'])) {
$this->logger->logger('getDifference: good - getting user for updateShortMessage', \danog\MadelineProto\Logger::VERBOSE);
$this->get_updates_difference();
yield $this->get_updates_difference_async();
}
$message = $updates;
$message['_'] = 'message';
$message['from_id'] = $from_id;
try {
$message['to_id'] = $this->get_info($to_id)['Peer'];
$message['to_id'] = yield $this->get_info_async($to_id)['Peer'];
} catch (\danog\MadelineProto\Exception $e) {
$this->logger->logger('Still did not get user in database, postponing update', \danog\MadelineProto\Logger::ERROR);
//$this->pending_updates[] = $updates;
@ -651,7 +653,7 @@ trait ResponseHandler
//$this->set_update_state(['date' => $updates['date']]);
break;
case 'updatesTooLong':
$this->get_updates_difference();
yield $this->get_updates_difference_async();
break;
default:
throw new \danog\MadelineProto\ResponseException('Unrecognized update received: '.var_export($updates, true));

View File

@ -150,7 +150,7 @@ trait UpdateHandler
$this->postpone_updates = true;
try {
$input = $this->get_info('channel#'.$channel);
$input = yield $this->get_info_async('channel#'.$channel);
if (!isset($input['InputChannel'])) {
throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database');
}
@ -179,7 +179,7 @@ trait UpdateHandler
$this->logger->logger($e->getMessage());
unset($this->channels_state[$channel]);
return false; //$this->get_channel_difference($channel);
return false; //yield $this->get_channel_difference_async($channel);
} finally {
$this->postpone_updates = false;
$this->load_channel_state($channel)['sync_loading'] = false;
@ -204,7 +204,7 @@ trait UpdateHandler
}
if (!$difference['final']) {
unset($difference);
$this->get_channel_difference($channel);
yield $this->get_channel_difference_async($channel);
}
break;
case 'updates.channelDifferenceTooLong':
@ -220,7 +220,7 @@ trait UpdateHandler
$this->postpone_updates = false;
$this->load_channel_state($channel)['sync_loading'] = false;
}
$this->get_channel_difference($channel);
yield $this->get_channel_difference_async($channel);
break;
default:
throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true));
@ -229,7 +229,7 @@ trait UpdateHandler
$this->handle_pending_updates();
}
public function set_update_state($data)
public function set_update_state_async($data)
{
if (isset($data['pts']) && $data['pts'] !== 0) {
$this->load_update_state()['pts'] = $data['pts'];
@ -244,7 +244,7 @@ trait UpdateHandler
$this->load_update_state()['date'] = $data['date'];
}
}
public function reset_update_state()
public function reset_update_state_async()
{
$this->load_update_state()['pts'] = 1;
$this->load_update_state()['qts'] = 0;
@ -255,14 +255,14 @@ trait UpdateHandler
}
$this->msg_ids = [];
}
public function &load_update_state()
public function &load_update_state_async()
{
if (!isset($this->updates_state['qts'])) {
$this->updates_state['qts'] = 0;
}
if (!$this->got_state) {
$this->got_state = true;
$this->set_update_state($this->get_updates_state());
$this->set_update_state(yield $this->get_updates_state_async());
}
return $this->updates_state;
@ -319,7 +319,7 @@ trait UpdateHandler
$this->set_update_state($difference['intermediate_state']);
unset($difference);
$this->updates_state['sync_loading'] = false;
$this->get_updates_difference();
yield $this->get_updates_difference_async();
break;
default:
throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true));
@ -356,7 +356,7 @@ trait UpdateHandler
return $data;
}
public function handle_update($update, $options = [])
public function handle_update_async($update, $options = [])
{
if (!$this->settings['updates']['handle_updates']) {
return;
@ -400,7 +400,7 @@ trait UpdateHandler
}*/
switch ($update['_']) {
case 'updateChannelTooLong':
$this->get_channel_difference($channel_id);
yield $this->get_channel_difference_async($channel_id);
return false;
case 'updateNewMessage':
@ -424,9 +424,9 @@ trait UpdateHandler
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))) {
$this->get_channel_difference($channel_id);
yield $this->get_channel_difference_async($channel_id);
} else {
$this->get_updates_difference();
yield $this->get_updates_difference_async();
}
return false;
@ -456,9 +456,9 @@ 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))) {
$this->get_channel_difference($channel_id);
yield $this->get_channel_difference_async($channel_id);
} else {
$this->get_updates_difference();
yield $this->get_updates_difference_async();
}
return false;
@ -483,7 +483,7 @@ trait UpdateHandler
$seq_start = isset($options['seq_start']) ? $options['seq_start'] : $options['seq'];
if ($seq_start != $cur_state['seq'] + 1 && $seq_start > $cur_state['seq']) {
$this->logger->logger('Seq hole. seq_start: '.$seq_start.' != cur seq: '.$cur_state['seq'].' + 1', \danog\MadelineProto\Logger::ERROR);
$this->get_updates_difference();
yield $this->get_updates_difference_async();
return false;
}
@ -494,7 +494,7 @@ trait UpdateHandler
}
}
}
$this->save_update($update);
yield $this->save_update_async($update);
}
public function handle_multiple_update($updates, $options = [], $channel = false)
@ -532,11 +532,11 @@ trait UpdateHandler
if (in_array($update['_'], ['updateUserName', 'updateUserPhone', 'updateUserBlocked', 'updateUserPhoto', 'updateContactRegistered', 'updateContactLink'])) {
$id = $this->get_id($update);
$this->full_chats[$id]['last_update'] = 0;
$this->get_full_info($id);
yield $this->get_full_info_async($id);
}
if ($update['_'] === 'updateDcOptions') {
$this->logger->logger('Got new dc options', \danog\MadelineProto\Logger::VERBOSE);
$this->parse_dc_options($update['dc_options']);
yield $this->parse_dc_options_async($update['dc_options']);
return;
}
@ -557,13 +557,13 @@ trait UpdateHandler
$update['phone_call'] = $this->calls[$update['phone_call']['id']] = $controller;
break;
case 'phoneCallAccepted':
if (!$this->confirm_call($update['phone_call'])) {
if (!yield $this->confirm_call_async($update['phone_call'])) {
return;
}
$update['phone_call'] = $this->calls[$update['phone_call']['id']];
break;
case 'phoneCall':
if (!$this->complete_call($update['phone_call'])) {
if (!yield $this->complete_call_async($update['phone_call'])) {
return;
}
$update['phone_call'] = $this->calls[$update['phone_call']['id']];
@ -588,7 +588,7 @@ trait UpdateHandler
}
if ($update['qts'] > $cur_state['qts'] + 1) {
$this->logger->logger('Qts hole. Fetching updates manually: update qts: '.$update['qts'].' > current qts '.$cur_state['qts'].'+1, chat id: '.$update['message']['chat_id'], \danog\MadelineProto\Logger::ERROR);
$this->get_updates_difference();
yield $this->get_updates_difference_async();
return false;
}
@ -610,7 +610,7 @@ trait UpdateHandler
return;
}
$this->logger->logger('Accepting secret chat '.$update['chat']['id'], \danog\MadelineProto\Logger::NOTICE);
$this->accept_secret_chat($update['chat']);
yield $this->accept_secret_chat_async($update['chat']);
break;
case 'encryptedChatDiscarded':
$this->logger->logger('Deleting secret chat '.$update['chat']['id'].' because it was revoked by the other user', \danog\MadelineProto\Logger::NOTICE);
@ -627,7 +627,7 @@ trait UpdateHandler
break;
case 'encryptedChat':
$this->logger->logger('Completing creation of secret chat '.$update['chat']['id'], \danog\MadelineProto\Logger::NOTICE);
$this->complete_secret_chat($update['chat']);
yield $this->complete_secret_chat_async($update['chat']);
break;
}
//$this->logger->logger($update, \danog\MadelineProto\Logger::NOTICE);

View File

@ -59,7 +59,7 @@ trait AuthKeyHandler
public function request_secret_chat_async($user)
{
$user = $this->get_info($user);
$user = yield $this->get_info_async($user);
if (!isset($user['InputUser'])) {
throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database');
}
@ -74,7 +74,7 @@ trait AuthKeyHandler
$res = yield $this->method_call_async_read('messages.requestEncryption', ['user_id' => $user, 'g_a' => $g_a->toBytes()], ['datacenter' => $this->datacenter->curdc]);
$this->temp_requested_secret_chats[$res['id']] = $a;
$this->handle_pending_updates();
$this->get_updates_difference();
yield $this->get_updates_difference_async();
$this->logger->logger('Secret chat '.$res['id'].' requested successfully!', \danog\MadelineProto\Logger::NOTICE);
return $res['id'];
@ -96,7 +96,7 @@ trait AuthKeyHandler
$key['fingerprint'] = substr(sha1($key['auth_key'], true), -8);
//$this->logger->logger($key);
if ($key['fingerprint'] !== $params['key_fingerprint']) {
$this->discard_secret_chat($params['id']);
yield $this->discard_secret_chat_async($params['id']);
throw new \danog\MadelineProto\SecurityException('Invalid key fingerprint!');
}
@ -136,7 +136,7 @@ trait AuthKeyHandler
$this->secret_chats[$chat]['rekeying'] = [1, $e];
yield $this->method_call_async_read('messages.sendEncryptedService', ['peer' => $chat, 'message' => ['_' => 'decryptedMessageService', 'action' => ['_' => 'decryptedMessageActionRequestKey', 'g_a' => $g_a->toBytes(), 'exchange_id' => $e]]], ['datacenter' => $this->datacenter->curdc]);
$this->handle_pending_updates();
$this->get_updates_difference();
yield $this->get_updates_difference_async();
return $e;
}
@ -172,7 +172,7 @@ trait AuthKeyHandler
$this->check_G($g_b, $dh_config['p']);
yield $this->method_call_async_read('messages.sendEncryptedService', ['peer' => $chat, 'message' => ['_' => 'decryptedMessageService', 'action' => ['_' => 'decryptedMessageActionAcceptKey', 'g_b' => $g_b->toBytes(), 'exchange_id' => $params['exchange_id'], 'key_fingerprint' => $key['fingerprint']]]], ['datacenter' => $this->datacenter->curdc]);
$this->handle_pending_updates();
$this->get_updates_difference();
yield $this->get_updates_difference_async();
}
public function commit_rekey_async($chat, $params)
@ -203,7 +203,7 @@ trait AuthKeyHandler
$this->secret_chats[$chat]['ttr'] = 100;
$this->secret_chats[$chat]['updated'] = time();
$this->handle_pending_updates();
$this->get_updates_difference();
yield $this->get_updates_difference_async();
}
public function complete_rekey_async($chat, $params)

View File

@ -35,7 +35,7 @@ trait MessageHandler
$this->secret_chats[$chat_id]['ttr']--;
if ($this->secret_chats[$chat_id]['layer'] > 8) {
if (($this->secret_chats[$chat_id]['ttr'] <= 0 || time() - $this->secret_chats[$chat_id]['updated'] > 7 * 24 * 60 * 60) && $this->secret_chats[$chat_id]['rekeying'][0] === 0) {
$this->rekey($chat_id);
yield $this->rekey_async($chat_id);
}
$message = ['_' => 'decryptedMessageLayer', 'layer' => $this->secret_chats[$chat_id]['layer'], 'in_seq_no' => $this->generate_secret_in_seq_no($chat_id), 'out_seq_no' => $this->generate_secret_out_seq_no($chat_id), 'message' => $message];
$this->secret_chats[$chat_id]['out_seq_no']++;
@ -61,7 +61,7 @@ trait MessageHandler
return $message;
}
public function handle_encrypted_update($message, $test = false)
public function handle_encrypted_update_async($message, $test = false)
{
if (!isset($this->secret_chats[$message['message']['chat_id']])) {
$this->logger->logger(sprintf(\danog\MadelineProto\Lang::$current_lang['secret_chat_skipping'], $message['message']['chat_id']));
@ -73,13 +73,13 @@ trait MessageHandler
if ($auth_key_id !== $this->secret_chats[$message['message']['chat_id']]['key']['fingerprint']) {
if (isset($this->secret_chats[$message['message']['chat_id']]['old_key']['fingerprint'])) {
if ($auth_key_id !== $this->secret_chats[$message['message']['chat_id']]['old_key']['fingerprint']) {
$this->discard_secret_chat($message['message']['chat_id']);
yield $this->discard_secret_chat_async($message['message']['chat_id']);
throw new \danog\MadelineProto\SecurityException(\danog\MadelineProto\Lang::$current_lang['fingerprint_mismatch']);
}
$old = true;
} else {
$this->discard_secret_chat($message['message']['chat_id']);
yield $this->discard_secret_chat_async($message['message']['chat_id']);
throw new \danog\MadelineProto\SecurityException(\danog\MadelineProto\Lang::$current_lang['fingerprint_mismatch']);
}
@ -114,12 +114,12 @@ trait MessageHandler
$deserialized = $this->deserialize($message_data, ['type' => '']);
$this->secret_chats[$message['message']['chat_id']]['ttr']--;
if (($this->secret_chats[$message['message']['chat_id']]['ttr'] <= 0 || time() - $this->secret_chats[$message['message']['chat_id']]['updated'] > 7 * 24 * 60 * 60) && $this->secret_chats[$message['message']['chat_id']]['rekeying'][0] === 0) {
$this->rekey($message['message']['chat_id']);
yield $this->rekey_async($message['message']['chat_id']);
}
unset($message['message']['bytes']);
$message['message']['decrypted_message'] = $deserialized;
$this->secret_chats[$message['message']['chat_id']]['incoming'][$this->secret_chats[$message['message']['chat_id']]['in_seq_no']] = $message['message'];
$this->handle_decrypted_update($message);
yield $this->handle_decrypted_update_async($message);
}
public function try_mtproto_v1_decrypt($message_key, $chat_id, $old, $encrypted_data)

View File

@ -34,15 +34,15 @@ trait ResponseHandler
case 'decryptedMessageService':
switch ($update['message']['decrypted_message']['action']['_']) {
case 'decryptedMessageActionRequestKey':
$this->accept_rekey($update['message']['chat_id'], $update['message']['decrypted_message']['action']);
yield $this->accept_rekey_async($update['message']['chat_id'], $update['message']['decrypted_message']['action']);
return;
case 'decryptedMessageActionAcceptKey':
$this->commit_rekey($update['message']['chat_id'], $update['message']['decrypted_message']['action']);
yield $this->commit_rekey_async($update['message']['chat_id'], $update['message']['decrypted_message']['action']);
return;
case 'decryptedMessageActionCommitKey':
$this->complete_rekey($update['message']['chat_id'], $update['message']['decrypted_message']['action']);
yield $this->complete_rekey_async($update['message']['chat_id'], $update['message']['decrypted_message']['action']);
return;
case 'decryptedMessageActionNotifyLayer':
@ -58,7 +58,7 @@ trait ResponseHandler
case 'decryptedMessageActionSetMessageTTL':
$this->secret_chats[$update['message']['chat_id']]['ttl'] = $update['message']['decrypted_message']['action']['ttl_seconds'];
$this->save_update($update);
yield $this->save_update_async($update);
return;
case 'decryptedMessageActionNoop':
@ -78,12 +78,12 @@ trait ResponseHandler
return;
default:
// $this->save_update(['_' => 'updateNewDecryptedMessage', 'peer' => $this->secret_chats[$update['message']['chat_id']]['InputEncryptedChat'], 'in_seq_no' => $this->get_in_seq_no($update['message']['chat_id']), 'out_seq_no' => $this->get_out_seq_no($update['message']['chat_id']), 'message' => $update['message']['decrypted_message']]);
$this->save_update($update);
// yield $this->save_update_async(['_' => 'updateNewDecryptedMessage', 'peer' => $this->secret_chats[$update['message']['chat_id']]['InputEncryptedChat'], 'in_seq_no' => $this->get_in_seq_no($update['message']['chat_id']), 'out_seq_no' => $this->get_out_seq_no($update['message']['chat_id']), 'message' => $update['message']['decrypted_message']]);
yield $this->save_update_async($update);
}
break;
case 'decryptedMessage':
$this->save_update($update);
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'])) {
@ -95,7 +95,7 @@ trait ResponseHandler
}
}
$update['message']['decrypted_message'] = $update['message']['decrypted_message']['message'];
$this->handle_decrypted_update($update);
yield $this->handle_decrypted_update_async($update);
}
break;
default:

View File

@ -24,14 +24,14 @@ namespace danog\MadelineProto\SecretChats;
*/
trait SeqNoHandler
{
public function check_secret_in_seq_no($chat_id, $seqno)
public function check_secret_in_seq_no_async($chat_id, $seqno)
{
$seqno = ($seqno - $this->secret_chats[$chat_id]['out_seq_no_x']) / 2;
$last = 0;
foreach ($this->secret_chats[$chat_id]['incoming'] as $message) {
if (isset($message['decrypted_message']['in_seq_no'])) {
if (($message['decrypted_message']['in_seq_no'] - $this->secret_chats[$chat_id]['out_seq_no_x']) / 2 < $last) {
$this->discard_secret_chat($chat_id);
yield $this->discard_secret_chat_async($chat_id);
throw new \danog\MadelineProto\SecurityException('in_seq_no is not increasing');
}
@ -39,7 +39,7 @@ trait SeqNoHandler
}
}
if ($seqno > $this->secret_chats[$chat_id]['out_seq_no'] + 1) {
$this->discard_secret_chat($chat_id);
yield $this->discard_secret_chat_async($chat_id);
throw new \danog\MadelineProto\SecurityException('in_seq_no is too big');
}
@ -47,14 +47,14 @@ trait SeqNoHandler
return true;
}
public function check_secret_out_seq_no($chat_id, $seqno)
public function check_secret_out_seq_no_async($chat_id, $seqno)
{
$seqno = ($seqno - $this->secret_chats[$chat_id]['in_seq_no_x']) / 2;
$C = 0;
foreach ($this->secret_chats[$chat_id]['incoming'] as $message) {
if (isset($message['decrypted_message']['out_seq_no']) && $C < $this->secret_chats[$chat_id]['in_seq_no']) {
if (($message['decrypted_message']['out_seq_no'] - $this->secret_chats[$chat_id]['in_seq_no_x']) / 2 !== $C) {
$this->discard_secret_chat($chat_id);
yield $this->discard_secret_chat_async($chat_id);
throw new \danog\MadelineProto\SecurityException('out_seq_no hole: should be '.$C.', is '.($message['decrypted_message']['out_seq_no'] - $this->secret_chats[$chat_id]['in_seq_no_x']) / 2);
} else {
@ -71,7 +71,7 @@ trait SeqNoHandler
}
if ($seqno > $C) {
// > C+1
$this->discard_secret_chat($chat_id);
yield $this->discard_secret_chat_async($chat_id);
throw new \danog\MadelineProto\SecurityException('WARNING: out_seq_no gap detected ('.$seqno.' > '.$C.')!');
}

View File

@ -160,7 +160,7 @@ trait BotAPI
$newd = [];
if (!isset($data['_'])) {
foreach ($data as $key => $element) {
$newd[$key] = $this->MTProto_to_botAPI($element, $sent_arguments);
$newd[$key] = yield $this->MTProto_to_botAPI_async($element, $sent_arguments);
}
return $newd;
@ -175,16 +175,16 @@ trait BotAPI
}
$newd['chat'] = $this->get_pwr_chat($sent_arguments['peer']);
if (isset($data['entities'])) {
$newd['entities'] = $this->MTProto_to_botAPI($data['entities'], $sent_arguments);
$newd['entities'] = yield $this->MTProto_to_botAPI_async($data['entities'], $sent_arguments);
}
if (isset($data['media'])) {
$newd = array_merge($newd, $this->MTProto_to_botAPI($data['media'], $sent_arguments));
$newd = array_merge($newd, yield $this->MTProto_to_botAPI_async($data['media'], $sent_arguments));
}
return $newd;
case 'updateNewChannelMessage':
case 'updateNewMessage':
return $this->MTProto_to_botAPI($data['message']);
return yield $this->MTProto_to_botAPI_async($data['message']);
case 'message':
$newd['message_id'] = $data['id'];
$newd['date'] = $data['date'];
@ -196,7 +196,7 @@ trait BotAPI
}
$newd['chat'] = $this->get_pwr_chat($data['to_id']);
if (isset($data['entities'])) {
$newd['entities'] = $this->MTProto_to_botAPI($data['entities'], $sent_arguments);
$newd['entities'] = yield $this->MTProto_to_botAPI_async($data['entities'], $sent_arguments);
}
if (isset($data['views'])) {
$newd['views'] = $data['views'];
@ -220,7 +220,7 @@ trait BotAPI
$newd['forward_from_message_id'] = $data['fwd_from']['channel_post'];
}
if (isset($data['media'])) {
$newd = array_merge($newd, $this->MTProto_to_botAPI($data['media'], $sent_arguments));
$newd = array_merge($newd, yield $this->MTProto_to_botAPI_async($data['media'], $sent_arguments));
}
return $newd;
@ -287,7 +287,7 @@ trait BotAPI
}
$res['photo'] = [];
foreach ($data['photo']['sizes'] as $key => $photo) {
$res['photo'][$key] = $this->photosize_to_botapi($photo, $data['photo']);
$res['photo'][$key] = yield $this->photosize_to_botapi_async($photo, $data['photo']);
}
return $res;
@ -297,7 +297,7 @@ trait BotAPI
$type_name = 'document';
$res = [];
if ($data['document']['thumb']['_'] === 'photoSize') {
$res['thumb'] = $this->photosize_to_botapi($data['document']['thumb'], [], true);
$res['thumb'] = yield $this->photosize_to_botapi_async($data['document']['thumb'], [], true);
}
foreach ($data['document']['attributes'] as $attribute) {
switch ($attribute['_']) {
@ -396,7 +396,7 @@ trait BotAPI
return $arguments;
}
public function parse_node($node, &$entities, &$new_message, &$offset)
public function parse_node_async($node, &$entities, &$new_message, &$offset)
{
switch ($node->nodeName) {
case 'br':
@ -455,7 +455,7 @@ trait BotAPI
$length = $this->mb_strlen($text);
$href = $node->getAttribute('href');
if (preg_match('|mention:(.*)|', $href, $matches) || preg_match('|tg://user\?id=(.*)|', $href, $matches)) {
$mention = $this->get_info($matches[1]);
$mention = yield $this->get_info_async($matches[1]);
if (!isset($mention['InputUser'])) {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['peer_not_in_db']);
}

View File

@ -72,7 +72,7 @@ trait BotAPIFiles
public function photosize_to_botapi_async($photoSize, $photo, $thumbnail = false)
{
$ext = $this->get_extension_from_location(['_' => 'inputFileLocation', 'volume_id' => $photoSize['location']['volume_id'], 'local_id' => $photoSize['location']['local_id'], 'secret' => $photoSize['location']['secret'], 'dc_id' => $photoSize['location']['dc_id']], '.jpg');
$ext = yield $this->get_extension_from_location_async(['_' => 'inputFileLocation', 'volume_id' => $photoSize['location']['volume_id'], 'local_id' => $photoSize['location']['local_id'], 'secret' => $photoSize['location']['secret'], 'dc_id' => $photoSize['location']['dc_id']], '.jpg');
$photoSize['location']['access_hash'] = isset($photo['access_hash']) ? $photo['access_hash'] : 0;
$photoSize['location']['id'] = isset($photo['id']) ? $photo['id'] : 0;
$photoSize['location']['_'] = $thumbnail ? 'bot_thumbnail' : 'bot_photo';

View File

@ -80,7 +80,7 @@ trait TD
return $this->td_to_tdcli($this->mtproto_to_td($params));
}
public function mtproto_to_td(&$params)
public function mtproto_to_td_async(&$params)
{
if (!is_array($params)) {
return $params;
@ -100,7 +100,7 @@ trait TD
} else {
switch (end($mtproto)) {
case 'choose_chat_id_from_botapi':
$newparams[$td] = $this->get_info($params[$mtproto[0]])['bot_api_id'] == $this->authorization['user']['id'] ? $params['from_id'] : $this->get_info($params[$mtproto[0]])['bot_api_id'];
$newparams[$td] = yield $this->get_info_async($params[$mtproto[0]])['bot_api_id'] == $this->authorization['user']['id'] ? $params['from_id'] : yield $this->get_info_async($params[$mtproto[0]])['bot_api_id'];
break;
case 'choose_incoming_or_sent':
$newparams[$td] = ['_' => $params['out'] ? 'messageIsSuccessfullySent' : 'messageIsIncoming'];

View File

@ -430,7 +430,7 @@ trait TL
$concat = $constructorData['id'];
}
return $concat.yield $this->serialize_params($constructorData, $object, '', $layer);
return $concat.yield $this->serialize_params_async($constructorData, $object, '', $layer);
}
public function serialize_object($type, $object, $ctx, $layer = -1)
@ -483,7 +483,7 @@ trait TL
}
}
} elseif (in_array($method, ['messages.addChatUser', 'messages.deleteChatUser', 'messages.editChatAdmin', 'messages.editChatPhoto', 'messages.editChatTitle', 'messages.getFullChat', 'messages.exportChatInvite', 'messages.editChatAdmin', 'messages.migrateChat']) && isset($arguments['chat_id']) && (!is_numeric($arguments['chat_id']) || $arguments['chat_id'] < 0)) {
$res = $this->get_info($arguments['chat_id']);
$res = yield $this->get_info_async($arguments['chat_id']);
if ($res['type'] !== 'chat') {
throw new \danog\MadelineProto\Exception('chat_id is not a chat id (only normal groups allowed, not supergroups)!');
}
@ -513,7 +513,7 @@ trait TL
throw new Exception(\danog\MadelineProto\Lang::$current_lang['method_not_found'].$method);
}
return $tl['id'].yield $this->serialize_params($tl, $arguments, $method);
return $tl['id'].yield $this->serialize_params_async($tl, $arguments, $method);
}
public function serialize_params_async($tl, $arguments, $ctx, $layer = -1)
@ -553,7 +553,7 @@ trait TL
continue;
}
if ($current_argument['name'] === 'data' && isset($tl['method']) && in_array($tl['method'], ['messages.sendEncrypted', 'messages.sendEncryptedFile', 'messages.sendEncryptedService']) && isset($arguments['message'])) {
$serialized .= yield $this->serialize_object_async($current_argument, $this->encrypt_secret_message($arguments['peer']['chat_id'], $arguments['message']), 'data');
$serialized .= yield $this->serialize_object_async($current_argument, yield $this->encrypt_secret_message_async($arguments['peer']['chat_id'], $arguments['message']), 'data');
continue;
}
if ($current_argument['name'] === 'random_id') {
@ -617,7 +617,7 @@ trait TL
if ($current_argument['type'] === 'InputEncryptedChat' && (!is_array($arguments[$current_argument['name']]) || isset($arguments[$current_argument['name']]['_']) && $this->constructors->find_by_predicate($arguments[$current_argument['name']]['_'])['type'] !== $current_argument['type'])) {
if (is_array($arguments[$current_argument['name']])) {
$arguments[$current_argument['name']] = $this->get_info($arguments[$current_argument['name']])['InputEncryptedChat'];
$arguments[$current_argument['name']] = yield $this->get_info_async($arguments[$current_argument['name']])['InputEncryptedChat'];
} else {
if (!isset($this->secret_chats[$arguments[$current_argument['name']]])) {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['sec_peer_not_in_db']);

View File

@ -39,7 +39,7 @@ trait AuthKeyHandler
$controller->discard();
}
});
$user = $this->get_info($user);
$user = yield $this->get_info_async($user);
if (!isset($user['InputUser']) || $user['InputUser']['_'] === 'inputUserSelf') {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['peer_not_in_db']);
}
@ -57,7 +57,7 @@ trait AuthKeyHandler
$controller->setCall($res['phone_call']);
$this->calls[$res['phone_call']['id']] = $controller;
$this->handle_pending_updates();
$this->get_updates_difference();
yield $this->get_updates_difference_async();
return $controller;
}
@ -94,7 +94,7 @@ trait AuthKeyHandler
}
if ($e->rpc === 'CALL_ALREADY_DECLINED') {
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['call_already_declined']);
$this->discard_call($call['id'], 'phoneCallDiscardReasonHangup');
yield $this->discard_call_async($call['id'], 'phoneCallDiscardReasonHangup');
return false;
}
@ -103,7 +103,7 @@ trait AuthKeyHandler
}
$this->calls[$res['phone_call']['id']]->storage['b'] = $b;
$this->handle_pending_updates();
$this->get_updates_difference();
yield $this->get_updates_difference_async();
return true;
}

View File

@ -59,7 +59,7 @@ trait DialogHandler
$last_id = $dialog['top_message'];
}
foreach ($res['messages'] as $message) {
if (yield $this->get_info($message)['Peer'] === $last_peer && $last_id === $message['id']) {
if (yield $this->get_info_async($message)['Peer'] === $last_peer && $last_id === $message['id']) {
$last_date = $message['date'];
break;
}

View File

@ -61,27 +61,27 @@ trait Start
} else {
if ($this->authorized === self::NOT_LOGGED_IN) {
if (isset($_POST['phone_number'])) {
yield $this->web_phone_login();
yield $this->web_phone_login_async();
} elseif (isset($_POST['token'])) {
yield $this->web_bot_login();
yield $this->web_bot_login_async();
} else {
$this->web_echo();
}
} elseif ($this->authorized === self::WAITING_CODE) {
if (isset($_POST['phone_code'])) {
yield $this->web_complete_phone_login();
yield $this->web_complete_phone_login_async();
} else {
$this->web_echo("You didn't provide a phone code!");
}
} elseif ($this->authorized === self::WAITING_PASSWORD) {
if (isset($_POST['password'])) {
yield $this->web_complete_2fa_login();
yield $this->web_complete_2fa_login_async();
} else {
$this->web_echo("You didn't provide the password!");
}
} elseif ($this->authorized === self::WAITING_SIGNUP) {
if (isset($_POST['first_name'])) {
yield $this->web_complete_signup();
yield $this->web_complete_signup_async();
} else {
$this->web_echo("You didn't provide the first name!");
}