/**
  * @Route ("/api/messages/send")
  * @HttpMethod ({"POST"})
  *
  * Expects the following GET|POST variables:
  *     privatekey=XXXXXXXX
  *     message=string
  *     userid=999
  *     targetuserid=999
  *
  * @param array $params
  * @return Response
  */
 public function sendMessage(array $params)
 {
     $privateMessageService = PrivateMessageService::instance();
     $chatIntegrationService = ChatIntegrationService::instance();
     $userService = UserService::instance();
     $response = array();
     try {
         FilterParams::required($params, 'privatekey');
         FilterParams::required($params, 'message');
         FilterParams::required($params, 'userid');
         FilterParams::required($params, 'targetuserid');
         if (!$this->checkPrivateKey($params['privatekey'])) {
             throw new Exception('Invalid shared private key.');
         }
         if ($params['userid'] == $params['targetuserid']) {
             throw new Exception('Cannot send messages to yourself.');
         }
         $ban = $userService->getUserActiveBan($params['userid']);
         if (!empty($ban)) {
             throw new Exception("privmsgbanned");
         }
         $oldEnough = $userService->isUserOldEnough($params['userid']);
         if (!$oldEnough) {
             throw new Exception("privmsgaccounttooyoung");
         }
         $user = $userService->getUserById($params['userid']);
         $credentials = new SessionCredentials($user);
         $credentials->addRoles($userService->getUserRolesByUserId($params['userid']));
         $targetuser = $userService->getUserById($params['targetuserid']);
         if (empty($targetuser)) {
             throw new Exception('notfound');
         }
         $canSend = $privateMessageService->canSend($credentials, $params['targetuserid']);
         if (!$canSend) {
             throw new Exception("throttled");
         }
         if (empty($user)) {
             throw new Exception('notfound');
         }
         $message = array('userid' => $params['userid'], 'targetuserid' => $params['targetuserid'], 'message' => $params['message'], 'isread' => 0);
         $message['id'] = $privateMessageService->addMessage($message);
         $chatIntegrationService->publishPrivateMessage(array('messageid' => $message['id'], 'message' => $message['message'], 'username' => $user['username'], 'userid' => $user['userId'], 'targetusername' => $targetuser['username'], 'targetuserid' => $targetuser['userId']));
         $response = new Response(Http::STATUS_NO_CONTENT);
     } catch (Exception $e) {
         $response['success'] = false;
         $response['error'] = $e->getMessage();
         $response = new Response(Http::STATUS_BAD_REQUEST, json_encode($response));
         $response->addHeader(Http::HEADER_CONTENTTYPE, MimeType::JSON);
     }
     return $response;
 }
Exemple #2
0
 /**
  * @Route ("/")
  * @Route ("/home")
  *
  * @param ViewModel $model
  * @return string
  */
 public function home(ViewModel $model)
 {
     if (Session::hasRole(UserRole::USER)) {
         $userid = $userId = Session::getCredentials()->getUserId();
         $privateMessageService = PrivateMessageService::instance();
         $model->unreadMessageCount = $privateMessageService->getUnreadMessageCount($userid);
     }
     $app = Application::instance();
     $cacheDriver = $app->getCacheDriver();
     $model->articles = $cacheDriver->fetch('recentblog');
     $model->summoners = $cacheDriver->fetch('summoners');
     $model->tweets = $cacheDriver->fetch('twitter');
     $model->music = $cacheDriver->fetch('recenttracks');
     $model->playlist = $cacheDriver->fetch('youtubeplaylist');
     $model->broadcasts = $cacheDriver->fetch('pastbroadcasts');
     $model->streamInfo = $cacheDriver->fetch('streaminfo');
     return 'home';
 }
 /**
  * @Route ("/profile/messages/{targetuserid}")
  * @Secure ({"USER"})
  * @HttpMethod ({"GET"})
  *
  * @param array $params
  * @return Response
  */
 public function message(array $params, ViewModel $viewModel)
 {
     FilterParams::required($params, 'targetuserid');
     $privateMessageService = PrivateMessageService::instance();
     $userService = UserService::instance();
     $userId = Session::getCredentials()->getUserId();
     $username = Session::getCredentials()->getUsername();
     $targetuser = $userService->getUserById($params['targetuserid']);
     if (empty($targetuser)) {
         throw new Exception('Invalid user');
     }
     $messages = $privateMessageService->getMessagesBetweenUserIdAndTargetUserId($userId, $params['targetuserid'], 0, 1000);
     $privateMessageService->markMessagesRead($userId, $params['targetuserid']);
     $viewModel->targetuser = $targetuser;
     $viewModel->messages = $messages;
     $viewModel->username = $username;
     $viewModel->userId = $userId;
     $viewModel->title = 'Message';
     return 'profile/message';
 }