public function actionConversation()
 {
     $messageId = $this->getQuery('messageId');
     if (empty($messageId)) {
         throw new BadRequestHttpException('missing params');
     }
     $chatMessage = ChatMessage::findByPk(new \MongoId($messageId));
     if (empty($chatMessage)) {
         throw new BadRequestHttpException('Invalid message id');
     }
     $conversation = ChatConversation::findByPk($chatMessage->conversationId);
     $message['content'] = $chatMessage->content;
     $message['sentTime'] = $chatMessage->sentTime;
     $result = [];
     if (empty(!$conversation)) {
         $accountId = $conversation->accountId;
         $deskId = $conversation->desk['id'];
         $openId = $conversation->client['openId'];
         $result = ['messageId' => $chatMessage->_id . '', 'conversationId' => $conversation->_id . '', 'client' => $conversation->client, 'desk' => ['id' => $deskId . '', 'name' => $conversation->desk['name'], 'badge' => $conversation->desk['badge'], 'email' => $conversation->desk['email'], 'avatar' => $conversation->desk['avatar']], 'channel' => $conversation->conversation, 'chatTimes' => ChatConversation::getChatTimes($deskId, $openId, $accountId), 'lastChatTime' => ChatConversation::getLastChatTime($deskId, $openId, $accountId), 'chatMessage' => $message, 'isReply' => $chatMessage->isReply];
     }
     return $result;
 }
 /**
  * Tranfer client to another helpdesk
  *
  * <b>Request Type: </b>POST<br/>
  * <b>Request Endpoint: </b>http://{server-domain}/api/chat/conversation/transfer
  * <b>Summary: </b> This api is for transfer helpdesk.<br/>
  *
  * <b>Request Parameters: </b><br/>
  *     accesstoken: string<br/>
  *     deskId: string, id of the desk.<br/>
  *     clientOpenId: string, the openId of the client.<br/>
  *     conversationId: string, the id of chatConversation.<br/>
  *     targetDeskId: string, the id of the target desk
  *
  * <b>Response Example: </b><br/>
  * {
  *     "status" => "ok"
  * }
  */
 public function actionTransfer()
 {
     $cache = Yii::$app->cache;
     $targetDeskId = $this->getParams('targetDeskId');
     $conversationId = $this->getParams('conversationId');
     $accountId = $this->getAccountId();
     $conversations = $cache->get('conversations' . $accountId);
     if ($conversations) {
         $lastChatConversation = ChatConversation::findByPk(new \MongoId($conversationId));
         if ($lastChatConversation->status === ChatConversation::STATUS_CLOSED) {
             throw new BadRequestHttpException('ChatConversation has been closed');
         }
         $client = $lastChatConversation->client;
         $clientOpenId = $lastChatConversation->client['openId'];
         $deskMongoId = $lastChatConversation->desk['id'];
         $deskId = (string) $deskMongoId;
         $targetDeskMongoId = new \MongoId($targetDeskId);
         $helpDesk = HelpDesk::findByPk($deskMongoId);
         $targetHelpDesk = HelpDesk::findByPk($targetDeskMongoId);
         $maxClientLimit = HelpDeskSetting::getMaxClientCount($accountId);
         if ($targetHelpDesk->clientCount >= $maxClientLimit) {
             throw new BadRequestHttpException('Target helpdesk has exceed the max serve number');
         }
         // Set the previous chat conversation status to 'closed'
         $lastChatConversation->status = ChatConversation::STATUS_CLOSED;
         if (!$lastChatConversation->update()) {
             LogUtil::error(['message' => 'Update previous helpdesk chatConversation status failed', 'error' => $lastChatConversation->errors], 'helpdesk');
             throw new ServerErrorHttpException('Update previous helpdesk chatConversation status failed');
         } else {
             HelpDesk::decClientCount($deskMongoId);
         }
         // Remove the client from original desk client list, and add to transfered desk client list
         foreach ($conversations[$deskId] as $index => $openId) {
             if ($openId == $clientOpenId) {
                 unset($conversations[$deskId][$index]);
             }
         }
         $conversations[$targetDeskId][] = $clientOpenId;
         Yii::$app->cache->set('conversations' . $accountId, $conversations);
         // Create chatConversation record in db
         $chatConversation = ChatConversation::saveRecord($targetHelpDesk, $client, ChatConversation::STATUS_OPEN, $accountId);
         // Generate response data
         $newChannelName = ChatConversation::getChannelName($targetDeskId, $clientOpenId);
         $lastChatTime = ChatConversation::getLastChatTime($targetDeskMongoId, $clientOpenId, $accountId);
         $chatTimes = ChatConversation::getChatTimes($clientOpenId, $accountId);
         $previousDesk = $lastChatConversation->desk;
         $previousDesk['id'] = $deskId;
         $desk = $chatConversation->desk;
         $desk['id'] = $targetDeskId;
         $data = ['conversationId' => (string) $chatConversation->_id, 'desk' => $desk, 'previousDesk' => $previousDesk, 'client' => $client, 'channel' => $newChannelName, 'lastChatTime' => $lastChatTime, 'chatTimes' => $chatTimes, 'startTime' => TimeUtil::msTime()];
         // Trigger transfer event to global channel, and the desk-client channel to make the client change listening channel
         Yii::$app->tuisongbao->triggerEvent(ChatConversation::EVENT_DESK_TRANSFER, $data, [ChatConversation::CHANNEL_GLOBAL . $accountId, $lastChatConversation->conversation]);
         // Push state
         $pushExtra = ['openId' => $clientOpenId, 'conversationId' => (string) $chatConversation->_id, 'previousDeskId' => $deskId, 'sentTime' => TimeUtil::msTime()];
         $previousDeskName = empty($previousDesk['name']) ? '' : $previousDesk['name'];
         $pushMessage = str_replace('{desk}', $previousDeskName, ChatConversation::PUSH_MESSAGE_TRANSFER);
         ChatConversation::pushMessage($previousDesk['id'], ChatConversation::EVENT_DESK_TRANSFER, $pushExtra);
         ChatConversation::pushMessage($desk['id'], ChatConversation::EVENT_DESK_TRANSFER, $pushExtra, $pushMessage);
         LogUtil::info(['event' => ChatConversation::EVENT_DESK_TRANSFER, 'desk' => $desk, 'previousDesk' => $previousDesk, 'client' => $client], 'helpdesk');
         return array_merge($data, ['status' => 'ok']);
     }
 }
Beispiel #3
0
 /**
  * Destroy the conversation between helpdesk and client
  * @param  string $conversationId the conversation UUID
  * @param  MongoId $accountId the account UUID
  * @param  array $extra extra information got from request
  * @return array success status for conversation disconnection
  */
 public static function disconnect($conversationId, $extra = null)
 {
     //query the desk information in db
     $chatConversation = ChatConversation::findByPk($conversationId);
     $conversations = Yii::$app->cache->get('conversations' . $chatConversation->accountId);
     if (empty($chatConversation)) {
         throw new BadRequestHttpException("Incorrect conversationId");
     }
     if ($chatConversation['status'] === ChatConversation::STATUS_CLOSED) {
         return ['status' => 'ok'];
     }
     return self::destroyConnection($chatConversation, $extra);
 }