private function onResponse(Chain $chain) { return function (Response $response) use($chain) { BufferedSink::createPromise($response)->then(function ($body) use($chain) { $logger = DI::get()->getLogger(); $logger->info('Got http response: ' . $body); if (!($json = json_decode($body, 1))) { return; } $logger->info('JSON decoded'); $channelId = $chain->getUser()->getChannelId(); /** @var UserCollection $users */ $users = DI::get()->getUsers(); $response = (new MusicResponse())->setInfo($json)->setChannelId($channelId); $loop = DI::get()->container()->get('eventloop'); $loop->addTimer(1, function () use($users, $response, $logger) { $users->setResponse($response)->notify(false); $logger->info('Sent MusicResponse!'); }); $channel = ChannelsCollection::get()->getChannelById($channelId); $history = $channel->getHistory(0); foreach ($history as $k => $part) { /** @var Msg $msgObj */ $msgObj = $part['msg']; $string = $msgObj->getMsg($chain->getUser()->getLang()); if (!preg_match('~id="music-(' . $json['track_id'] . ')"~u', $string)) { continue; } $logger->info('Replacing corresponding history row'); $string = str_replace('id="music-' . $json['track_id'] . '" data-src=""><span class="glyphicon glyphicon-play-circle">' . '</span> <span class="audio-title">...</span>', 'id="music-' . $json['track_id'] . '" data-src="' . $json['url'] . '" bind-play-click="1">' . '<span class="glyphicon glyphicon-play-circle"></span> ' . '<span class="audio-title">' . $json['artist'] . ' - ' . $json['track'] . '</span>', $string); $part['msg'] = MsgRaw::create($string); $channel->setRow($k, $part); } }); }; }
private function showAd(User $user) { $msg = MsgRaw::create('<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:inline-block;width:320px;height:50px" data-ad-client="ca-pub-1352019659330191" data-ad-slot="9172675664"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>'); $response = (new MessageResponse())->setChannelId($user->getChannelId())->setMsg($msg); (new UserCollection())->setResponse($response)->attach($user)->notify(false); }
public function pushRawResponse($response) { if (!isset($response[self::MSG])) { return; } $msg = $response[self::MSG]; if (mb_strpos($msg, '|')) { $msg = call_user_func_array([MsgToken::class, 'create'], explode('|', $msg)); } else { $msg = MsgRaw::create($msg); } $response[self::MSG] = $msg; $this->history[$this->lastMsgId] = $response; $this->lastMsgId++; }
private function guestResponse(User $user) { $response = (new MessageResponse())->setMsg(MsgRaw::create(''))->setTime(null)->setChannelId($user->getChannelId())->setGuests(DI::get()->getUsers()->getUsersByChatId($user->getChannelId())); (new UserCollection())->attach($user)->setResponse($response)->notify(false); }
/** * @param User $user */ private function handleHistory(User $user) { ChannelNotifier::uploadHistory($user); $ds = DIRECTORY_SEPARATOR; if (file_exists(ROOT . $ds . 'www' . $ds . 'motd.txt') && !$user->getLastMsgId()) { $motd = file_get_contents(ROOT . $ds . 'www' . $ds . 'motd.txt'); $motd .= "<br>Доступны каналы:<br>"; foreach (ChannelsCollection::get()->getChannels() as $channel) { if ($channel->isPrivate()) { continue; } $motd .= '(' . DI::get()->getUsers()->getClientsCount($channel->getId()) . ') ' . $channel->getName() . '<br>'; } $client = (new UserCollection())->attach($user); $response = (new MessageResponse())->setChannelId($user->getChannelId())->setMsg(MsgRaw::create($motd)); $client->setResponse($response)->notify(false); } }