public static function uploadHistory(User $user, $clear = null) { $channel = ChannelsCollection::get()->getChannelById($user->getChannelId()); if (!$channel) { $channel = new Channel($user->getChannelId(), 'Приват_' . $user->getChannelId()); $channel->setOwnerId($user->getId()); ChannelsCollection::get()->addChannel($channel); } $log = $channel->getHistory($user->getLastMsgId()); $client = (new UserCollection())->attach($user); $historyResponse = (new HistoryResponse())->setChannelId($user->getChannelId()); if (!$user->getLastMsgId()) { $historyResponse->setClear($clear); } foreach ($log as $response) { if ($user->getBlacklist()->isBanned($response[Channel::FROM_USER_ID])) { continue; } if (isset($response[Channel::TO_NAME])) { if ($response[Channel::FROM_USER_ID] == $user->getId() || $response[Channel::TO_NAME] == $user->getProperties()->getName()) { $historyResponse->addResponse($response); } continue; } $historyResponse->addResponse($response); } $historyResponse->setLastMsgId($channel->getLastMsgId()); $client->setResponse($historyResponse)->notify(false); }
public function getHistory(User $user) { if (!isset($this->channels[$user->getChannelId()])) { return []; } $channel = $this->channels[$user->getChannelId()]; /* @var $channel Channel */ return $channel->getHistory($user->getLastMsgId()); }
private function setAdLoop(User $user) { $config = DI::get()->getConfig(); $loop = $this->getLoop(); $timeout = $config->ads->adInterval; $timerCallback = function () use($user, $config) { if (isset($this->msgTimers[$user->getId()])) { return; } if (isset($this->lastMsgIds[$user->getId()])) { $current = (int) $user->getLastMsgId(); $last = (int) $this->lastMsgIds[$user->getId()]; $diff = (int) $config->ads->historyDiffSize; if ($current - $last < $diff) { return; } } DI::get()->getLogger()->info('Ad fired for user_id = ' . $user->getId(), ['Show_ad']); $this->lastMsgIds[$user->getId()] = $user->getLastMsgId(); $this->showAd($user); }; $timer = $loop->addPeriodicTimer($timeout, $timerCallback); $this->adTimers[$user->getId()] = $timer; }
/** * @param $sessionInfo * @param UserCollection $clients * @param Logger $logger * @param User $newUserWrapper * @return UserDAO */ private function handleKnownUser($sessionInfo, UserCollection $clients, Logger $logger, User $newUserWrapper) { $user = UserDAO::create()->getById($sessionInfo['user_id']); $lang = $newUserWrapper->getLang(); if ($oldClient = $clients->getClientById($user->getId())) { if ($timer = $oldClient->getDisconnectTimer()) { DI::get()->container()->get('eventloop')->cancelTimer($timer); $logger->info("Deffered disconnection timer canceled: connection_id = {$newUserWrapper->getConnectionId()} for user_id = {$sessionInfo['user_id']}", [__METHOD__]); if ($oldClient->getConnectionId()) { $oldClient->setAsyncDetach(false)->send(['disconnect' => 1]); $clients->detach($oldClient); $newUserWrapper->setLastMsgId(-1); } } elseif ($oldClient->getConnectionId()) { // If there is no timer set, then // 1) it's regular user visit // 2) an attempt to open another browser tab // 3) window reload $oldClient->setAsyncDetach(false)->send(['msg' => $lang->getPhrase('DuplicateConnection'), 'disconnect' => 1]); $clients->detach($oldClient); if ($oldClient->getIp() == $newUserWrapper->getIp()) { $newUserWrapper->setLastMsgId(-1); } $logger->info("Probably tabs duplication detected: detaching = {$oldClient->getConnectionId()} for user_id = {$oldClient->getId()}}", [__METHOD__]); } if ($newUserWrapper->getLastMsgId()) { $logger->info("Re-established connection for user_id = {$sessionInfo['user_id']}, lastMsgId = {$newUserWrapper->getLastMsgId()}", [__METHOD__]); } } return $user; }