Пример #1
0
	public function SetReadMessage($chatId, $lastId = null)
	{
		global $DB;

		$chatId = intval($chatId);
		if ($chatId <= 0)
			return false;

		$sqlLastId = '';
		if (intval($lastId) > 0)
			$sqlLastId = "AND M.ID <= ".intval($lastId);

		$strSql = "
			SELECT COUNT(M.ID) CNT, MAX(M.ID) ID, M.CHAT_ID
			FROM b_im_message M
			INNER JOIN b_im_relation R1 ON M.ID > R1.LAST_ID ".$sqlLastId." AND M.CHAT_ID = R1.CHAT_ID
			WHERE R1.CHAT_ID = ".$chatId." AND R1.USER_ID = ".$this->user_id."
			GROUP BY M.CHAT_ID
		";
		$dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		if ($arRes = $dbRes->Fetch())
		{
			$bReadMessage = CIMMessage::SetLastId($chatId, $this->user_id, $arRes['ID']);
			if ($bReadMessage)
			{
				//CUserCounter::Decrement($this->user_id, 'im_chat_v2', '**', false, $arRes['CNT']);
				CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_GROUP);
				if (CModule::IncludeModule("pull"))
				{
					CPushManager::DeleteFromQueueBySubTag($this->user_id, 'IM_GROUP');
					CPullStack::AddByUser($this->user_id, Array(
						'module_id' => 'im',
						'command' => 'readMessageChat',
						'params' => Array(
							'chatId' => $chatId,
							'lastId' => $arRes['ID'],
							'count' => $arRes['CNT']
						),
					));
					CIMMessenger::SendBadges($this->user_id);
				}
				return true;
			}
		}

		return false;
	}
Пример #2
0
 public function GetUnreadMessage($arParams = array())
 {
     global $DB;
     $bSpeedCheck = isset($arParams['SPEED_CHECK']) && $arParams['SPEED_CHECK'] == 'N' ? false : true;
     $lastId = !isset($arParams['LAST_ID']) || $arParams['LAST_ID'] == null ? null : IntVal($arParams['LAST_ID']);
     $order = isset($arParams['ORDER']) && $arParams['ORDER'] == 'ASC' ? 'ASC' : 'DESC';
     $loadDepartment = isset($arParams['LOAD_DEPARTMENT']) && $arParams['LOAD_DEPARTMENT'] == 'N' ? false : true;
     $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N' ? false : true;
     $bGroupByChat = isset($arParams['GROUP_BY_CHAT']) && $arParams['GROUP_BY_CHAT'] == 'Y' ? true : false;
     $bUserLoad = isset($arParams['USER_LOAD']) && $arParams['USER_LOAD'] == 'N' ? false : true;
     $bFileLoad = isset($arParams['FILE_LOAD']) && $arParams['FILE_LOAD'] == 'N' ? false : true;
     $arExistUserData = isset($arParams['EXIST_USER_DATA']) && is_array($arParams['EXIST_USER_DATA']) ? $arParams['EXIST_USER_DATA'] : array();
     $messageType = isset($arParams['MESSAGE_TYPE']) && in_array($arParams['MESSAGE_TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT)) ? $arParams['MESSAGE_TYPE'] : 'ALL';
     $arMessages = array();
     $arUnreadMessage = array();
     $arUsersMessage = array();
     $arResult = array('message' => array(), 'unreadMessage' => array(), 'usersMessage' => array(), 'users' => array(), 'userInGroup' => array(), 'woUserInGroup' => array(), 'files' => array(), 'countMessage' => 0, 'chat' => array(), 'userChatBlockStatus' => array(), 'userInChat' => array(), 'result' => false);
     $bLoadMessage = $bSpeedCheck ? CIMMessenger::SpeedFileExists($this->user_id, IM_SPEED_GROUP) : false;
     $count = CIMMessenger::SpeedFileGet($this->user_id, IM_SPEED_GROUP);
     if (!$bLoadMessage || $bLoadMessage && intval($count) > 0) {
         $ssqlLastId = "R1.LAST_ID";
         $ssqlStatus = " AND R1.STATUS < " . IM_STATUS_READ;
         if (!is_null($lastId) && intval($lastId) > 0 && !CIMMessenger::CheckXmppStatusOnline()) {
             $ssqlLastId = intval($lastId);
             $ssqlStatus = "";
         }
         $arRelations = array();
         if (strlen($ssqlStatus) > 0) {
             $strSql = "\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tR1.USER_ID,\n\t\t\t\t\t\tR1.CHAT_ID,\n\t\t\t\t\t\tR1.LAST_ID\n\t\t\t\t\tFROM\n\t\t\t\t\t\tb_im_relation R1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tR1.USER_ID = " . $this->user_id . "\n\t\t\t\t\t\t" . ($messageType == 'ALL' ? "AND R1.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "')" : "AND R1.MESSAGE_TYPE = '" . $messageType . "'") . "\n\t\t\t\t\t\t" . $ssqlStatus . "\n\t\t\t\t";
             $dbSubRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             while ($arRes = $dbSubRes->Fetch()) {
                 $arRelations[] = $arRes;
             }
         }
         $arMessageId = array();
         $arMessageChatId = array();
         $arLastMessage = array();
         $arMark = array();
         $arChat = array();
         $arPrepareResult = array();
         $arFilteredResult = array();
         if (!empty($arRelations)) {
             if (!$bTimeZone) {
                 CTimeZone::Disable();
             }
             $strSql = "\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tM.ID,\n\t\t\t\t\t\tM.CHAT_ID,\n\t\t\t\t\t\tM.MESSAGE,\n\t\t\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\t\tM.AUTHOR_ID,\n\t\t\t\t\t\tR1.STATUS R1_STATUS,\n\t\t\t\t\t\tR1.MESSAGE_TYPE MESSAGE_TYPE\n\t\t\t\t\tFROM b_im_message M\n\t\t\t\t\tINNER JOIN b_im_relation R1 ON M.ID > " . $ssqlLastId . " AND M.CHAT_ID = R1.CHAT_ID AND R1.USER_ID != M.AUTHOR_ID\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tR1.USER_ID = " . $this->user_id . "\n\t\t\t\t\t\t" . ($messageType == 'ALL' ? "AND R1.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "')" : "AND R1.MESSAGE_TYPE = '" . $messageType . "'") . "\n\t\t\t\t\t\t" . $ssqlStatus . "\n\t\t\t\t";
             if (!$bTimeZone) {
                 CTimeZone::Enable();
             }
             $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             $CCTP = new CTextParser();
             $CCTP->MaxStringLen = 200;
             $CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink ? "N" : "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $this->bHideLink ? "N" : "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N");
             while ($arRes = $dbRes->Fetch()) {
                 $arPrepareResult[$arRes['CHAT_ID']][$arRes['ID']] = $arRes;
             }
             foreach ($arPrepareResult as $chatId => $arRes) {
                 if (count($arPrepareResult[$chatId]) > 100) {
                     $arPrepareResult[$chatId] = array_slice($arRes, -100, 100);
                 }
                 $arFilteredResult = array_merge($arFilteredResult, $arPrepareResult[$chatId]);
             }
             unset($arPrepareResult);
         }
         foreach ($arFilteredResult as $arRes) {
             $arUsers[] = $arRes['AUTHOR_ID'];
             $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => $arRes['DATE_CREATE'], 'text' => $arRes['MESSAGE'], 'messageType' => $arRes['MESSAGE_TYPE']);
             if ($bGroupByChat) {
                 $arMessages[$arRes['ID']]['conversation'] = $arRes['CHAT_ID'];
                 $arMessages[$arRes['ID']]['unread'] = $this->user_id != $arRes['AUTHOR_ID'] ? 'Y' : 'N';
             } else {
                 $arUsersMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
                 if ($this->user_id != $arRes['AUTHOR_ID']) {
                     $arUnreadMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
                 }
             }
             if ($arRes['R1_STATUS'] == IM_STATUS_UNREAD && (!isset($arMark[$arRes["CHAT_ID"]]) || $arMark[$arRes["CHAT_ID"]] < $arRes["ID"])) {
                 $arMark[$arRes["CHAT_ID"]] = $arRes["ID"];
             }
             if (!isset($arLastMessage[$arRes["CHAT_ID"]]) || $arLastMessage[$arRes["CHAT_ID"]] < $arRes["ID"]) {
                 $arLastMessage[$arRes["CHAT_ID"]] = $arRes["ID"];
             }
             $arChat[$arRes["CHAT_ID"]] = $arRes["CHAT_ID"];
             $arMessageId[] = $arRes['ID'];
             $arMessageChatId[$arRes['CHAT_ID']][$arRes["ID"]] = $arRes["ID"];
         }
         $params = CIMMessageParam::Get($arMessageId);
         if ($bFileLoad) {
             foreach ($arMessageChatId as $chatId => $messages) {
                 $files = array();
                 foreach ($messages as $messageId) {
                     $arMessages[$messageId]['params'] = $params[$messageId];
                     if (isset($params[$messageId]['FILE_ID'])) {
                         foreach ($params[$messageId]['FILE_ID'] as $fileId) {
                             $files[$fileId] = $fileId;
                         }
                     }
                 }
                 $arMessageFiles = CIMDisk::GetFiles($chatId, $files);
                 foreach ($arMessageFiles as $key => $value) {
                     $arResult['files'][$chatId][$key] = $value;
                 }
             }
         } else {
             foreach ($params as $messageId => $param) {
                 $arMessages[$messageId]['params'] = $param;
             }
         }
         if (!empty($arMessages)) {
             foreach ($arMark as $chatId => $lastSendId) {
                 CIMMessage::SetLastSendId($chatId, $this->user_id, $lastSendId);
             }
         } else {
             foreach ($arRelations as $relation) {
                 CIMMessage::SetLastId($relation['CHAT_ID'], $relation['USER_ID']);
             }
         }
         if ($bGroupByChat) {
             foreach ($arMessages as $key => $value) {
                 $arMessages[$arLastMessage[$value['conversation']]]['counter']++;
                 if ($arLastMessage[$value['conversation']] != $value['id']) {
                     unset($arMessages[$key]);
                 } else {
                     $arMessages[$key]['text'] = $CCTP->convertText(htmlspecialcharsbx($value['text']));
                     $arMessages[$key]['text_mobile'] = strip_tags(preg_replace("/<img.*?data-code=\"([^\"]*)\".*?>/i", "\$1", $CCTP->convertText(htmlspecialcharsbx(preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $value['text'])))), '<br>');
                     $arUsersMessage[$value['conversation']][] = $value['id'];
                     if ($value['unread'] == 'Y') {
                         $arUnreadMessage[$value['conversation']][] = $value['id'];
                     }
                     unset($arMessages[$key]['conversation']);
                     unset($arMessages[$key]['unread']);
                 }
             }
         } else {
             foreach ($arMessages as $key => $value) {
                 $arMessages[$key]['text'] = $CCTP->convertText(htmlspecialcharsbx($value['text']));
                 $arMessages[$key]['text_mobile'] = strip_tags(preg_replace("/<img.*?data-code=\"([^\"]*)\".*?>/i", "\$1", $CCTP->convertText(htmlspecialcharsbx(preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $value['text'])))), '<br>');
             }
         }
         $arResult['message'] = $arMessages;
         $arResult['unreadMessage'] = $arUnreadMessage;
         $arResult['usersMessage'] = $arUsersMessage;
         $arChat = self::GetChatData(array('ID' => $arChat, 'USE_CACHE' => 'N'));
         if (!empty($arChat)) {
             $arResult['chat'] = $arChat['chat'];
             $arResult['userChatBlockStatus'] = $arChat['userChatBlockStatus'];
             $arResult['userInChat'] = $arChat['userInChat'];
             foreach ($arChat['userInChat'] as $value) {
                 $arUsers[] = $value;
             }
         }
         if ($bUserLoad && !empty($arUsers)) {
             $arUserData = CIMContactList::GetUserData(array('ID' => array_diff(array_unique($arUsers), $arExistUserData), 'DEPARTMENT' => $loadDepartment ? 'Y' : 'N'));
             $arResult['users'] = $arUserData['users'];
             $arResult['userInGroup'] = $arUserData['userInGroup'];
             $arResult['woUserInGroup'] = $arUserData['woUserInGroup'];
         } else {
             $arResult['users'] = array();
             $arResult['userInGroup'] = array();
             $arResult['userInGroup'] = array();
         }
         $arResult['countMessage'] = CIMMessenger::GetMessageCounter($this->user_id, $arResult);
         if (!$bGroupByChat) {
             CIMMessenger::SpeedFileCreate($this->user_id, $arResult['countMessage'], IM_SPEED_GROUP);
         }
         $arResult['result'] = true;
     } else {
         $arResult['countMessage'] = CIMMessenger::GetMessageCounter($this->user_id, $arResult);
     }
     return $arResult;
 }
Пример #3
0
 public function SetReadMessage($chatId, $lastId = null)
 {
     global $DB;
     $chatId = intval($chatId);
     if ($chatId <= 0) {
         return false;
     }
     $bReadMessage = false;
     if ($lastId == null) {
         $strSql = "\n\t\t\t\tSELECT MAX(M.ID) ID, M.CHAT_ID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_relation R1 ON M.ID >= R1.LAST_ID AND M.CHAT_ID = R1.CHAT_ID\n\t\t\t\tWHERE R1.CHAT_ID = " . $chatId . " AND R1.USER_ID = " . $this->user_id . "\n\t\t\t\tGROUP BY M.CHAT_ID\n\t\t\t";
         $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         if ($arRes = $dbRes->Fetch()) {
             $bReadMessage = CIMMessage::SetLastId($chatId, $this->user_id, $arRes['ID']);
         }
         $lastId = $arRes['ID'];
     } else {
         $bReadMessage = CIMMessage::SetLastId($chatId, $this->user_id, intval($lastId));
     }
     if ($bReadMessage) {
         CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_GROUP);
         if (CModule::IncludeModule("pull")) {
             CPushManager::DeleteFromQueue($this->user_id, 'IM_GROUP_' . $chatId);
             CPullStack::AddByUser($this->user_id, array('module_id' => 'im', 'command' => 'readMessageChat', 'params' => array('chatId' => $chatId, 'lastId' => $lastId)));
         }
         return true;
     }
     return false;
 }