Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2018-03-29 21:05:15 +00:00 committed by StyleCI Bot
parent 91fa43094d
commit acb73834d4
2 changed files with 70 additions and 58 deletions

122
magna.php
View File

@ -36,12 +36,12 @@ $MadelineProto->session = 'session.madeline';
$MadelineProto->inputEncryptedFileAudio = $MadelineProto->upload_encrypted('tests/mosconi.mp3'); $MadelineProto->inputEncryptedFileAudio = $MadelineProto->upload_encrypted('tests/mosconi.mp3');
}*/ }*/
class EventHandler extends \danog\MadelineProto\EventHandler class EventHandler extends \danog\MadelineProto\EventHandler
{ {
private $times = []; private $times = [];
private $calls = []; private $calls = [];
private $my_users = []; private $my_users = [];
public function configureCall($call) public function configureCall($call)
{ {
include 'songs.php'; include 'songs.php';
@ -59,6 +59,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
$call->parseConfig(); $call->parseConfig();
$call->playOnHold($songs); $call->playOnHold($songs);
} }
public function handleMessage($chat_id, $from_id, $message) public function handleMessage($chat_id, $from_id, $message)
{ {
try { try {
@ -100,7 +101,7 @@ Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'p
} }
} }
if ($message === '/broadcast' && $from_id === 101374607) { if ($message === '/broadcast' && $from_id === 101374607) {
$time = time()+100; $time = time() + 100;
foreach ($this->get_dialogs() as $peer) { foreach ($this->get_dialogs() as $peer) {
if (isset($peer['user_id'])) { if (isset($peer['user_id'])) {
$this->programmed_call[] = [$peer['user_id'], $time]; $this->programmed_call[] = [$peer['user_id'], $time];
@ -135,6 +136,7 @@ Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'p
$message = isset($update['message']['message']) ? $update['message']['message'] : ''; $message = isset($update['message']['message']) ? $update['message']['message'] : '';
$this->handleMessage($chat_id, $from_id, $message); $this->handleMessage($chat_id, $from_id, $message);
} }
public function onUpdateNewEncryptedMessage($update) public function onUpdateNewEncryptedMessage($update)
{ {
return; return;
@ -143,73 +145,81 @@ Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'p
$message = isset($update['message']['decrypted_message']['message']) ? $update['message']['decrypted_message']['message'] : ''; $message = isset($update['message']['decrypted_message']['message']) ? $update['message']['decrypted_message']['message'] : '';
$this->handleMessage($chat_id, $from_id, $message); $this->handleMessage($chat_id, $from_id, $message);
} }
public function onUpdateEncryption($update) {
public function onUpdateEncryption($update)
{
return; return;
try {
if ($update['chat']['_'] !== 'encryptedChat') return; try {
$chat_id = $this->get_info($update)['InputEncryptedChat']; if ($update['chat']['_'] !== 'encryptedChat') {
$from_id = $this->get_secret_chat($chat_id)['user_id']; return;
$message = ''; }
} catch (\danog\MadelineProto\Exception $e) { return; } $chat_id = $this->get_info($update)['InputEncryptedChat'];
$from_id = $this->get_secret_chat($chat_id)['user_id'];
$message = '';
} catch (\danog\MadelineProto\Exception $e) {
return;
}
$this->handleMessage($chat_id, $from_id, $message); $this->handleMessage($chat_id, $from_id, $message);
} }
public function onUpdatePhoneCall($update) public function onUpdatePhoneCall($update)
{ {
if (is_object($update['phone_call']) && isset($update['phone_call']->madeline) && $update['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) { if (is_object($update['phone_call']) && isset($update['phone_call']->madeline) && $update['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) {
$this->configureCall($update['phone_call']); $this->configureCall($update['phone_call']);
if ($update['phone_call']->accept() === false) { if ($update['phone_call']->accept() === false) {
echo 'DID NOT ACCEPT A CALL'; echo 'DID NOT ACCEPT A CALL';
}
$this->calls[$update['phone_call']->getOtherID()] = $update['phone_call'];
try {
$this->times[$update['phone_call']->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $update['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL])['id']];
} catch (\danog\MadelineProto\RPCErrorException $e) {
}
}
}
public function onLoop()
{
foreach ($this->programmed_call as $key => $pair) {
list($user, $time) = $pair;
if ($time < time()) {
if (!isset($this->calls[$user])) {
try {
$call = $this->request_call($user);
$this->configureCall($call);
$this->calls[$call->getOtherID()] = $call;
$this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']];
} catch (\danog\MadelineProto\RPCErrorException $e) {
try {
if ($e->rpc === 'USER_PRIVACY_RESTRICTED') {
$e = 'Please disable call privacy settings to make me call you';
} elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) {
$t = str_replace('FLOOD_WAIT_', '', $e->rpc);
$this->programmed_call[] = [$user, time() + 1 + $t];
$e = "I'll call you back in $t seconds.\nYou can also call me right now.";
}
$this->messages->sendMessage(['peer' => $user, 'message' => (string) $e]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
}
}
} }
unset($this->programmed_call[$key]); $this->calls[$update['phone_call']->getOtherID()] = $update['phone_call'];
}
}
foreach ($this->calls as $key => $call) {
if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
unset($this->calls[$key]);
} elseif (isset($this->times[$call->getOtherID()]) && $this->times[$call->getOtherID()][0] < time()) {
$this->times[$call->getOtherID()][0] += 30 + count($this->calls);
try { try {
$this->messages->editMessage(['id' => $this->times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()]); $this->times[$update['phone_call']->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $update['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL])['id']];
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
echo $e;
} }
} }
} }
public function onLoop()
{
foreach ($this->programmed_call as $key => $pair) {
list($user, $time) = $pair;
if ($time < time()) {
if (!isset($this->calls[$user])) {
try {
$call = $this->request_call($user);
$this->configureCall($call);
$this->calls[$call->getOtherID()] = $call;
$this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']];
} catch (\danog\MadelineProto\RPCErrorException $e) {
try {
if ($e->rpc === 'USER_PRIVACY_RESTRICTED') {
$e = 'Please disable call privacy settings to make me call you';
} elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) {
$t = str_replace('FLOOD_WAIT_', '', $e->rpc);
$this->programmed_call[] = [$user, time() + 1 + $t];
$e = "I'll call you back in $t seconds.\nYou can also call me right now.";
}
$this->messages->sendMessage(['peer' => $user, 'message' => (string) $e]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
}
}
}
unset($this->programmed_call[$key]);
}
}
foreach ($this->calls as $key => $call) {
if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
unset($this->calls[$key]);
} elseif (isset($this->times[$call->getOtherID()]) && $this->times[$call->getOtherID()][0] < time()) {
$this->times[$call->getOtherID()][0] += 30 + count($this->calls);
try {
$this->messages->editMessage(['id' => $this->times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
echo $e;
}
}
}
} }
} }

View File

@ -19,10 +19,12 @@ namespace danog\MadelineProto\Wrappers;
trait Loop trait Loop
{ {
private $loop_callback; private $loop_callback;
public function setLoopCallback($callback) public function setLoopCallback($callback)
{ {
$this->loop_callback = $callback; $this->loop_callback = $callback;
} }
public function loop($max_forks = 0) public function loop($max_forks = 0)
{ {
if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) { if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) {
@ -51,7 +53,7 @@ trait Loop
die; die;
} }
} }
if ($this->loop_callback !== NULL) { if ($this->loop_callback !== null) {
($this->loop_callback)(); ($this->loop_callback)();
} }
} }
@ -62,7 +64,7 @@ trait Loop
$offset = $update['update_id'] + 1; $offset = $update['update_id'] + 1;
$this->settings['updates']['callback']($update['update']); $this->settings['updates']['callback']($update['update']);
} }
if ($this->loop_callback !== NULL) { if ($this->loop_callback !== null) {
($this->loop_callback)(); ($this->loop_callback)();
} }
} }