/**
  * Get the conversation history for the desk
  *
  * <b>Request Type: </b>GET<br/>
  * <b>Request Endpoint: </b>http://{server-domain}/api/chat/conversations?deskId={deskId}<br/>
  * <b>Summary: </b>This api is for get the conversation history of a specific history.<br/>
  *
  * <b>Request Endpoint:</b><br/>
  *     deskId: string, the id of the desk.<br/>
  *
  * <b>Response Example:</b><br/>
  * <pre>
  * [
  *      {
  *          "conversationId": "54b4bb34db4c0e396a8b456f",
  *          "client": {
  *              "nick": "guest-e4p5pj2t6u",
  *              "source": "website",
  *              "openId": "e4p5pj2t6u"
  *          },
  *          "desk": {
  *              "id": "54ae3ce5db4c0e92278b4573",
  *              "badge": "T00001",
  *              "email": "*****@*****.**",
  *              "avatar": "/images/management/image_hover_default_avatar.png"
  *          },
  *          "channelName": "wm-chat-54ae3ce5db4c0e92278b4573-e4p5pj2t6u",
  *          "messageHistory": [
  *              {
  *                  "content": {
  *                      "msgType": "TEXT",
  *                      "body": "Hello!!!"
  *                  },
  *                  "isReply": false,
  *                  "sentTime": 1421130559000
  *              },
  *              {
  *                  "content": {
  *                      "msgType": "TEXT",
  *                      "body": ":))))))"
  *                  },
  *                  "isReply": true,
  *                  "sentTime": 1421130578000
  *              },
  *              {
  *                  "content": {
  *                      "msgType": "TEXT",
  *                      "body": "123"
  *                  },
  *                  "isReply": true,
  *                  "sentTime": 1421130707000
  *              }
  *          ]
  *      },
  *      {
  *          "conversationId": "54b4bb57db4c0eea6e8b4568",
  *          "client": {
  *              "nick": "vincent",
  *              "avatar": "http://wx.qlogo.cn/mmopen/hrpNToPUkXQteILqJ6tXsrMWsQQZBAt6NUibwxoFyTMRNu6jQje9K6JLibbrkqewwBqU1fU45EwsQgS98Zd2DM34Kia2iaiat9Rl/0",
  *              "openId": "549d01e3e4b00e1d5ff9259d",
  *              "source": "wechat",
  *              "sourceChannel": "549cd113e4b00e1d5ff92599",
  *              "wechatAccountInfo": {
  *                  "type": "SERVICE_AUTH_ACCOUNT",
  *                  "name": "Hardy-Test"
  *              }
  *          },
  *          "desk": {
  *              "id": "54ae3ce5db4c0e92278b4573",
  *              "badge": "T00001",
  *              "email": "*****@*****.**",
  *              "avatar": "/images/management/image_hover_default_avatar.png"
  *          },
  *          "channelName": "wm-chat-54ae3ce5db4c0e92278b4573-549d01e3e4b00e1d5ff9259d",
  *          "messageHistory": []
  *      }
  *  ]
  * </pre>
  */
 public function actionIndex()
 {
     $deskId = $this->getQuery("deskId");
     $accountId = $this->getAccountId();
     $conversations = ChatConversation::findOpenByDeskId(new \MongoId($deskId), $accountId);
     $result = [];
     foreach ($conversations as $conversation) {
         $lastMessages = [];
         if ($conversation->status == ChatConversation::STATUS_OPEN) {
             $lastMessages = ChatMessage::findByConversation(new \MongoId($conversation->_id));
         }
         $conversationVO = ['conversationId' => (string) $conversation->_id, 'client' => $conversation->client, 'desk' => ['id' => (string) $conversation->desk['id'], 'name' => $conversation->desk['name'], 'badge' => $conversation->desk['badge'], 'email' => $conversation->desk['email'], 'avatar' => $conversation->desk['avatar']], 'channel' => ChatConversation::getChannelName($deskId, $conversation->client['openId']), 'chatTimes' => ChatConversation::getChatTimes($conversation->client['openId'], $accountId), 'lastChatTime' => ChatConversation::getLastChatTime($conversation->desk['id'], $conversation->client['openId'], $accountId), 'previousChatTime' => ChatConversation::getLastChatTime($conversation->desk['id'], $conversation->client['openId'], $accountId, 1), 'lastMessages' => $lastMessages, 'startTime' => TimeUtil::msTime()];
         $result[] = $conversationVO;
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * HelpDesk offline
  * @param string $deskId
  * @param MongoId $accountId
  * @param  Object $extra
  * @return array
  */
 public static function leave($deskId, $accountId, $extra)
 {
     $cache = Yii::$app->cache;
     //remove it from conversation caches
     $conversations = $cache->get('conversations' . $accountId);
     unset($conversations[$deskId]);
     $cache->set('conversations' . $accountId, $conversations);
     //remove it from online caches
     self::cacheOnlineDesks($deskId, (string) $accountId, ChatConversation::EVENT_USER_REMOVED);
     //remove the helpdesk ping time
     $onlineHelpDesks = $cache->get(HelpDesk::CACHE_PREFIX_HELPDESK_PING . $accountId);
     unset($onlineHelpDesks[$deskId]);
     $cache->set(HelpDesk::CACHE_PREFIX_HELPDESK_PING . $accountId, $onlineHelpDesks);
     //get the conversation records in db according to the deskId
     $conversationInstances = ChatConversation::findOpenByDeskId(new \MongoId($deskId), $accountId);
     foreach ($conversationInstances as $conversationInstance) {
         $clientTemp = $conversationInstance->client;
         $desk = $conversationInstance->desk;
         //trigger deskLeft event
         $data = ['conversationId' => (string) $conversationInstance['_id'], 'desk' => $desk, 'client' => $clientTemp, 'extra' => $extra, 'sentTime' => TimeUtil::msTime()];
         $channels = [ChatConversation::getChannelName($deskId, $clientTemp['openId'])];
         Yii::$app->tuisongbao->triggerEvent(ChatConversation::EVENT_DESK_LEFT, $data, $channels);
         //set the status of the chatConversation to "closed"
         ChatConversation::closeById($conversationInstance['_id']);
         HelpDesk::sendSystemReplyByType($clientTemp, $accountId, isset($extra['type']) ? $extra['type'] : HelpDeskSetting::REPLY_CLOSE);
     }
     //flush the client count in db
     helpDesk::flushClientCount(new \MongoId($deskId));
     LogUtil::info(['event' => 'deskLeft', 'deskId' => $deskId], 'helpdesk');
     return ['status' => 'ok'];
 }