protected function injectCommand(Chain $chain, $msg, $isPrivate = true) { $request = $chain->getRequest(); $request['msg'] = $msg; if ($isPrivate) { $request['to'] = $chain->getUser()->getId(); } else { $request['self'] = 1; } $chain->setRequest($request); }
/** * C-o-R pattern * @param Chain $chain input stream * @return false|null|true */ public function handleRequest(Chain $chain) { $request = $chain->getRequest(); $text = strip_tags(htmlentities(trim($request['msg']))); $msgMaxLength = DI::get()->getConfig()->msgMaxLength; if (mb_strlen($text) > $msgMaxLength) { $text = mb_strcut($text, 0, $msgMaxLength) . self::CUT; } $request['msg'] = $text; $chain->setRequest($request); }
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); } }); }; }