public function addChannel(Channel $channel)
 {
     if (!isset($this->channels[$channel->getId()])) {
         $this->channels[$channel->getId()] = $channel;
     }
     return $this;
 }
 public static function joinPrivate(ChainContainer $chain)
 {
     $users = DI::get()->getUsers();
     $user = $chain->getFrom();
     $lang = $user->getLang();
     if (!($desiredUser = self::checkRestrictions($chain, $users))) {
         return;
     }
     $privates = PendingPrivates::get();
     list($inviterUserId, $time) = $privates->invite($user, $desiredUser, self::getTimeoutCallableResponse());
     $remainingTime = time() - $time;
     if ($remainingTime < $privates->getTTL() && $inviterUserId) {
         RespondError::make($user, [PropertiesDAO::USER_ID => $lang->getPhrase('YouAlreadySentInvitation', $privates->getTTL() - $remainingTime)]);
         return;
     } elseif (!$time && !$inviterUserId) {
         $newChatRoomId = uniqid('_', 1);
         $desiredUser->setChannelId($newChatRoomId);
         $desiredUser->save();
         $channel = new Channel($newChatRoomId, 'Приват_' . $newChatRoomId);
         $channel->setOwnerId($user->getId());
         ChannelsCollection::get()->addChannel($channel);
         $user->setChannelId($newChatRoomId);
         $user->save();
         self::sendMatchResponse($users->getUsersByChatId($newChatRoomId), MsgToken::create('InvitationAccepted'));
         return;
     }
     self::sendPendingResponse($user, MsgToken::create('SendInvitationFor', $desiredUser->getProperties()->getName()));
     self::sendPendingResponse($desiredUser, MsgToken::create('UserInvitesYou', $user->getProperties()->getName(), $user->getId()));
     ChannelNotifier::updateChannelInfo($users, ChannelsCollection::get());
 }
 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 static function run(ChainContainer $chain)
 {
     $duals = PendingDuals::get();
     $users = DI::get()->getUsers();
     $user = $chain->getFrom();
     $lang = $user->getLang();
     if ($user->getProperties()->getTim()->getId() == TimEnum::ANY) {
         $user->send(['msg' => $lang->getPhrase('SelectTIMinProfile')]);
         return;
     }
     if ($user->isInPrivateChat()) {
         $user->send(['msg' => $lang->getPhrase('ThisFunctionWorkInPublicOnly')]);
         return;
     }
     if ($duals->getUserPosition($user)) {
         $user->send(['msg' => $lang->getPhrase('YouAlreadySentRequestOnSearch')]);
         return;
     }
     if ($dualUserId = $duals->matchDual($user)) {
         $dualUser = $users->getClientById($dualUserId);
         $oldChatId = $user->getChannelId();
         $newChatRoomId = uniqid('_', 1);
         $channel = new Channel($newChatRoomId, 'Приват_' . $newChatRoomId);
         $channel->setOwnerId($user->getId());
         ChannelsCollection::get()->addChannel($channel);
         $dualUser->setChannelId($newChatRoomId);
         $dualUser->save();
         $user->setChannelId($newChatRoomId);
         $user->save();
         self::sendMatchResponse($users->getUsersByChatId($newChatRoomId), MsgToken::create('DualIsFound'));
         self::renewGuestsList($oldChatId, MsgToken::create('DualizationStarted'));
         self::sendRenewPositions($duals->getUsersByDual($user));
         return;
     }
     self::sendPendingResponse($user, MsgToken::create('DualPending'), true);
     self::dualGuestsList($user);
 }