Esempio n. 1
0
 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 function testInvite()
 {
     $user1 = $this->getMockUser();
     $user2 = $this->getMockUser();
     DI::get()->container()->add('eventloop', MockEventLoop::class, true);
     $dummy = function () {
     };
     // send invitation
     $inviteTimestamp = time();
     list($user1id, $time) = $this->privates->invite($user1, $user2, $dummy);
     $this->assertEquals($user1->getId(), $user1id);
     $this->assertNull($time);
     // repeat sending invitation, but it should return sign of 'already sent' by timestamp of first attempt
     list($user1id, $time) = $this->privates->invite($user1, $user2, $dummy);
     $this->assertEquals($user1->getId(), $user1id);
     $this->assertEquals($inviteTimestamp, $time);
     // accept invitation
     list($user1id, $time) = $this->privates->invite($user2, $user1, $dummy);
     $this->assertNull($user1id);
     $this->assertNull($time);
 }