/** * @param CM_Model_User $user * @param int $actionType OPTIONAL * @param int $actionVerb OPTIONAL * @param int $limitType OPTIONAL * @param int $period OPTIONAL */ public function __construct(CM_Model_User $user, $actionType = null, $actionVerb = null, $limitType = null, $period = null) { $this->_user = $user; $where = '`actorId` = ' . $user->getId(); if ($actionType) { $actionType = (int) $actionType; $where .= ' AND `type` = ' . $actionType; } if ($actionVerb) { $actionVerb = (int) $actionVerb; $where .= ' AND `verb` = ' . $actionVerb; } if ($limitType) { $limitType = (int) $limitType; $where .= ' AND `actionLimitType` = ' . $limitType; } else { $where .= ' AND `actionLimitType` IS NOT NULL'; } if ($period) { $period = (int) $period; $time = time() - $period; $where .= ' AND `createStamp` > ' . $time; } $source = new CM_PagingSource_Sql_Deferred('type, verb, createStamp', 'cm_action', $where, '`createStamp` DESC'); parent::__construct($source); }
/** * @param CM_Model_User $user * @param CM_Action_Abstract $action * @param CM_Model_Abstract $model * @param mixed|null $data */ public static function publishAction($user, CM_Action_Abstract $action, CM_Model_Abstract $model, $data = null) { if (!$user->getOnline()) { return; } $streamChannel = self::getKeyByUser($user); parent::publishAction($streamChannel, $action, $model, $data); }
/** * @param int|CM_Model_User $user * @return bool */ public function contains($user) { if ($user instanceof CM_Model_User) { $userId = $user->getId(); } else { $userId = (int) $user; } return in_array($userId, $this->getItemsRaw()); }
public function testHasViewer() { $environment = new CM_Frontend_Environment(); $this->assertFalse($environment->hasViewer()); $environment = new CM_Frontend_Environment(null, CM_Model_User::createStatic()); $this->assertTrue($environment->hasViewer()); }
/** * @return CM_Model_User|null */ public function getUser() { if (is_null($this->getUserId())) { return null; } return CM_Model_User::factory($this->getUserId()); }
public function testCreateWithLanguage() { $language = CMTest_TH::createLanguage(); /** @var CM_Model_User $user */ $user = CM_Model_User::createStatic(array('language' => $language)); $this->assertEquals($language, $user->getLanguage()); }
/** * @param CM_Model_User $user * @throws CM_Exception_Invalid * @return boolean */ public function getEnabled(CM_Model_User $user) { $cacheKey = CM_CacheConst::SplitFeature_Fixtures . '_userId:' . $user->getId(); $cacheWrite = false; $cache = CM_Cache_Local::getInstance(); if (($fixtures = $cache->get($cacheKey)) === false) { $fixtures = CM_Db_Db::select('cm_splitfeature_fixture', array('splitfeatureId', 'fixtureId'), array('userId' => $user->getId()))->fetchAllTree(); $cacheWrite = true; } if (!array_key_exists($this->getId(), $fixtures)) { $fixtureId = CM_Db_Db::replace('cm_splitfeature_fixture', array('splitfeatureId' => $this->getId(), 'userId' => $user->getId())); $fixtures[$this->getId()] = $fixtureId; $cacheWrite = true; } if ($cacheWrite) { $cache->set($cacheKey, $fixtures); } return $this->_calculateEnabled($fixtures[$this->getId()]); }
protected function _registerCallbacks() { $this->_registerClockworkCallbacks('10 seconds', array('CM_Model_User::offlineDelayed' => function () { CM_Model_User::offlineDelayed(); })); $this->_registerClockworkCallbacks('1 minute', array('CM_Model_User::offlineOld' => function () { CM_Model_User::offlineOld(); }, 'CM_ModelAsset_User_Roles::deleteOld' => function () { CM_ModelAsset_User_Roles::deleteOld(); }, 'CM_Paging_Useragent_Abstract::deleteOlder' => function () { CM_Paging_Useragent_Abstract::deleteOlder(100 * 86400); }, 'CM_File_UserContent_Temp::deleteOlder' => function () { CM_File_UserContent_Temp::deleteOlder(86400); }, 'CM_SVM_Model::deleteOldTrainings' => function () { CM_SVM_Model::deleteOldTrainings(3000); }, 'CM_Paging_Ip_Blocked::deleteOlder' => function () { CM_Paging_Ip_Blocked::deleteOld(); }, 'CM_Captcha::deleteOlder' => function () { CM_Captcha::deleteOlder(3600); }, 'CM_Session::deleteExpired' => function () { CM_Session::deleteExpired(); }, 'CM_Stream_Video::synchronize' => function () { CM_Stream_Video::getInstance()->synchronize(); }, 'CM_Stream_Video::checkStreams' => function () { CM_Stream_Video::getInstance()->checkStreams(); }, 'CM_MessageStream_Service::synchronize' => function () { CM_Service_Manager::getInstance()->getStreamMessage()->synchronize(); })); $this->_registerClockworkCallbacks('15 minutes', array('CM_Mail::processQueue' => function () { CM_Mail::processQueue(500); }, 'CM_Action_Abstract::aggregate' => function () { CM_Action_Abstract::aggregate(); }, 'CM_Action_Abstract::deleteTransgressionsOlder' => function () { CM_Action_Abstract::deleteTransgressionsOlder(3 * 31 * 86400); }, 'CM_Paging_Log_Abstract::cleanup' => function () { foreach (CM_Paging_Log_Abstract::getClassChildren() as $logClass) { /** @var CM_Paging_Log_Abstract $log */ $log = new $logClass(); $log->cleanUp(); } })); $this->_registerClockworkCallbacks('8 days', array('CMService_MaxMind::upgrade' => function () { try { $maxMind = new CMService_MaxMind(); $maxMind->upgrade(); } catch (Exception $exception) { if (!is_a($exception, 'CM_Exception')) { $exception = new CM_Exception($exception->getMessage(), ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString()]); } $exception->setSeverity(CM_Exception::FATAL); throw $exception; } })); }
/** * @return CM_Model_User|null */ public function getUser() { $userId = $this->getUserId(); if (null === $userId) { return null; } try { return CM_Model_User::factory($userId); } catch (CM_Exception_Nonexistent $ex) { return null; } }
/** * @param boolean|null $delayed * @throws CM_Exception_Invalid */ public function send($delayed = null) { $delayed = (bool) $delayed; if (empty($this->_to)) { throw new CM_Exception_Invalid('No recipient specified.'); } $verificationMissing = $this->_verificationRequired && $this->_recipient && !$this->_recipient->getEmailVerified(); if ($verificationMissing) { return; } list($subject, $html, $text) = $this->render(); if ($delayed) { $this->_queue($subject, $text, $html); } else { $this->_send($subject, $text, $html); } }
public function testGetUser() { $user = CM_Model_User::createStatic(); $user2 = CM_Model_User::createStatic(); CM_Model_Entity_Mock::createStatic(array('userId' => $user->getId(), 'foo' => 'bar1')); $entityMock = new CM_Model_Entity_Mock(1); $this->assertEquals($user->getId(), $entityMock->getUser()->getId()); $this->assertInstanceOf('CM_Model_User', $user); $this->assertNotEquals($user2, $entityMock->getUser()); CM_Db_Db::delete('cm_user', array('userId' => $user->getId())); CMTest_TH::clearCache(); try { $entityMock->getUser(); $this->fail('User not deleted'); } catch (CM_Exception_Nonexistent $ex) { $this->assertTrue(true); } $this->assertNull($entityMock->getUserIfExists()); }
/** * @param string $key * @param CM_Model_User|null $default * @throws CM_Exception_InvalidParam * @return CM_Model_User */ public function getUser($key, CM_Model_User $default = null) { $param = $this->_get($key, $default); if (ctype_digit($param) || is_int($param)) { return CM_Model_User::factory($param); } if (!$param instanceof CM_Model_User) { throw new CM_Exception_InvalidParam('Not a CM_Model_User'); } return $param; }
/** * @param CM_Model_User $user */ protected function _setUser(CM_Model_User $user) { $this->_setIdentity($user->getDisplayName()); }
protected function _registerCallbacks() { $this->_registerClockworkCallbacks('1 second', ['CM_Jobdistribution_DelayedQueue::queueOutstanding' => function () { $delayedQueue = $this->getServiceManager()->getDelayedJobQueue(); $delayedQueue->queueOutstanding(); }]); $this->_registerClockworkCallbacks('1 minute', array('CM_Model_User::offlineOld' => function () { CM_Model_User::offlineOld(); }, 'CM_ModelAsset_User_Roles::deleteOld' => function () { CM_ModelAsset_User_Roles::deleteOld(); }, 'CM_Paging_Useragent_Abstract::deleteOlder' => function () { CM_Paging_Useragent_Abstract::deleteOlder(100 * 86400); }, 'CM_File_UserContent_Temp::deleteOlder' => function () { CM_File_UserContent_Temp::deleteOlder(86400); }, 'CM_SVM_Model::deleteOldTrainings' => function () { CM_SVM_Model::deleteOldTrainings(3000); }, 'CM_Paging_Ip_Blocked::deleteOlder' => function () { CM_Paging_Ip_Blocked::deleteOld(); }, 'CM_Captcha::deleteOlder' => function () { CM_Captcha::deleteOlder(3600); }, 'CM_Session::deleteExpired' => function () { CM_Session::deleteExpired(); }, 'CM_MessageStream_Service::synchronize' => function () { CM_Service_Manager::getInstance()->getStreamMessage()->synchronize(); })); if ($this->getServiceManager()->has('janus')) { $this->_registerClockworkCallbacks('1 minute', array('CM_Janus_Service::synchronize' => function () { $this->getServiceManager()->getJanus('janus')->synchronize(); }, 'CM_Janus_Service::checkStreams' => function () { $this->getServiceManager()->getJanus('janus')->checkStreams(); })); } $this->_registerClockworkCallbacks('15 minutes', array('CM_Action_Abstract::aggregate' => function () { CM_Action_Abstract::aggregate(); }, 'CM_Action_Abstract::deleteTransgressionsOlder' => function () { CM_Action_Abstract::deleteTransgressionsOlder(3 * 31 * 86400); }, 'CM_Paging_Log::cleanup' => function () { $allLevelsList = array_values(CM_Log_Logger::getLevels()); foreach (CM_Paging_Log::getClassChildren() as $pagingLogClass) { /** @type CM_Paging_Log $log */ $log = new $pagingLogClass($allLevelsList); $log->cleanUp(); } (new CM_Paging_Log($allLevelsList, false))->cleanUp(); //deletes all untyped records })); if ($this->getServiceManager()->has('maxmind')) { $this->_registerClockworkCallbacks('8 days', array('CMService_MaxMind::upgrade' => function () { try { /** @var CMService_MaxMind $maxMind */ $maxMind = $this->getServiceManager()->get('maxmind', 'CMService_MaxMind'); $maxMind->upgrade(); } catch (Exception $exception) { if (!is_a($exception, 'CM_Exception')) { $exception = new CM_Exception($exception->getMessage(), null, ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString()]); } $exception->setSeverity(CM_Exception::FATAL); throw $exception; } })); } }
public function add(CM_Action_Abstract $action) { CM_Db_Db::insertDelayed('cm_action', array('actorId' => $this->_user->getId(), 'verb' => $action->getVerb(), 'type' => $action->getType(), 'createStamp' => time())); }
/** * @return CM_Frontend_Render */ public function getRender() { $environment = $this->_recipient ? $this->_recipient->getEnvironment() : new CM_Frontend_Environment(); $environment->setSite($this->_site); return new CM_Frontend_Render($environment); }
/** * @param CM_Model_User $user */ public function setUser(CM_Model_User $user) { $user->setOnline(true); $this->set('userId', $user->getId()); $this->regenerateId(); if ($request = $this->getRequest()) { CM_Splittest_Fixture::setUserForRequestClient($request, $user); $user->getUseragents()->addFromRequest($request); } }
public function testStart() { /** @var CM_Model_User $user */ $user = CM_Model_User::createStatic(); $activityStamp1 = time(); $session = new CM_Session(); $session->setUser($user); $sessionId = $session->getId(); unset($session); $session = new CM_Session($sessionId); $this->assertEquals($activityStamp1, $session->getUser(true)->getLatestActivity(), null, 1); CMTest_TH::timeForward(CM_Model_User::ACTIVITY_EXPIRATION / 2); $session = new CM_Session($sessionId); $session->start(); $this->assertEquals($activityStamp1, $session->getUser(true)->getLatestActivity(), null, 1); CM_Db_Db::update('cm_session', array('data' => serialize(array('userId' => $user->getId(), 'foo' => 'bar')))); unset($session); CMTest_TH::clearCache(); CMTest_TH::timeForward(CM_Model_User::ACTIVITY_EXPIRATION / 2 + 1); $activityStamp2 = time(); $session = new CM_Session($sessionId); $session->start(); $this->assertEquals($activityStamp2, $session->getUser(true)->getLatestActivity(), null, 1); CMTest_TH::timeForward($session->getLifetime() / 2); $session->start(); $this->assertEquals('bar', $session->get('foo')); CM_Db_Db::update('cm_session', array('data' => serialize(array('userId' => $user->getId(), 'foo' => 'foo')))); unset($session); CMTest_TH::clearCache(); $session = new CM_Session($sessionId); $this->assertEquals('bar', $session->get('foo')); }
/** * @param CM_Model_User $user */ public function __construct(CM_Model_User $user) { parent::__construct('Blocked', null, array('messagePublic' => '{$username} has blocked you.', 'messagePublicVariables' => array('username' => $user->getDisplayName()))); }
public function canSubscribe(CM_Model_User $user, $allowedUntil) { return $user->getId() != 1 ? $allowedUntil + 100 : $allowedUntil; }
public function testParseTemplateContent() { $viewer = CM_Model_User::createStatic(); $render = new CM_Frontend_Render(); $render->getEnvironment()->setViewer($viewer); $content = '{$viewer->getId()} {$foo} normal-text'; $expected = $viewer->getId() . ' bar normal-text'; $this->assertSame($expected, $render->parseTemplateContent($content, ['foo' => 'bar'])); }
public function testCreateType() { $user = CM_Model_Abstract::createType(CM_Model_User::getTypeStatic()); $this->assertInstanceOf('CM_Model_User', $user); }
/** * @param CM_Http_Request_Abstract $request * @param CM_Model_User $user */ public static function setUserForRequestClient(CM_Http_Request_Abstract $request, CM_Model_User $user) { $requestClientId = $request->getClientId(); $userId = $user->getId(); CM_Db_Db::updateIgnore('cm_splittestVariation_fixture', array('userId' => $userId), array('requestClientId' => $requestClientId)); }
/** * @param CM_Model_User $user * @return CM_Queue */ protected function _getTrackingQueue(CM_Model_User $user) { return new CM_Queue(get_called_class() . '::_getTrackingQueue:' . $user->getId()); }
/** * @param CM_Model_User $user * @return CM_Queue */ protected function _getTrackingQueue(CM_Model_User $user) { return new CM_Queue(__METHOD__ . ':' . $user->getId()); }
public function testOfflineDelayed() { $user1 = CMTest_TH::createUser(); $user2 = CMTest_TH::createUser(); $user3 = CMTest_TH::createUser(); $user1->setOnline(); $user2->setOnline(); $user3->setOnline(); $user1->setOfflineStamp(); $user2->setOfflineStamp(); CMTest_TH::timeForward(CM_Model_User::OFFLINE_DELAY); $user3->setOfflineStamp(); $user2->setOnline(); $userOnlinePaging = new CM_Paging_User_Online(); $this->assertEquals([$user1, $user2, $user3], $userOnlinePaging); CM_Model_User::offlineDelayed(); $userOnlinePaging = new CM_Paging_User_Online(); $this->assertEquals([$user1, $user2, $user3], $userOnlinePaging); CMTest_TH::timeForward(1); CM_Model_User::offlineDelayed(); $userOnlinePaging = new CM_Paging_User_Online(); $this->assertEquals([$user2, $user3], $userOnlinePaging); }
/** * @return CM_Model_User */ public static function createUser() { return CM_Model_User::createStatic(); }
/** * @param CM_Model_User $user * @param CM_Model_Stream_Abstract|null $excludedStream * @return bool */ public function isPublisher(CM_Model_User $user, CM_Model_Stream_Abstract $excludedStream = null) { /** @var $stream CM_Model_Stream_Publish */ foreach ($this->getStreamPublishs() as $stream) { if (!$stream->equals($excludedStream) && $stream->getUserId() == $user->getId()) { return true; } } return false; }
/** * @param string $useragent */ public function add($useragent) { $useragent = (string) $useragent; CM_Db_Db::replaceDelayed('cm_useragent', array('userId' => $this->_user->getId(), 'useragent' => $useragent, 'createStamp' => time())); }
public static function offlineOld() { $res = CM_Db_Db::exec(' SELECT `o`.`userId` FROM `cm_user_online` `o` LEFT JOIN `cm_user` `u` USING(`userId`) WHERE `u`.`activityStamp` < ? OR `u`.`userId` IS NULL', array(time() - self::ONLINE_EXPIRATION)); while ($userId = $res->fetchColumn()) { try { $user = CM_Model_User::factory($userId); $user->setOnline(false); } catch (CM_Exception_Nonexistent $e) { CM_Db_Db::delete('cm_user_online', array('userId' => $userId)); } } }