function onExecuteStartWriting(\Bitrix\Main\Event $event) { $parameters = $event->getParameters(); $userId = $parameters[0]; $dialogId = $parameters[1] . $parameters[2]; if ($userId > 0) { if (!\Bitrix\Main\Loader::includeModule('pull')) { return; } \CPushManager::DeleteFromQueueBySubTag($userId, 'IM_MESS'); if (intval($dialogId) > 0) { \CPullStack::AddByUser($dialogId, array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $userId, 'dialogId' => $dialogId))); } elseif (substr($dialogId, 0, 4) == 'chat') { $chatId = substr($dialogId, 4); $arRelation = \CIMChat::GetRelationById($chatId); unset($arRelation[$userId]); $pullMessage = array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $userId, 'dialogId' => $dialogId)); \CPullStack::AddByUsers(array_keys($arRelation), $pullMessage); $orm = \Bitrix\Im\ChatTable::getById($chatId); $chat = $orm->fetch(); if ($chat['TYPE'] == IM_MESSAGE_OPEN) { \CPullWatch::AddToStack('IM_PUBLIC_' . $chatId, $pullMessage); } } } }
public static function OnFileDelete($params) { $result = IM\ChatTable::getList(array('select' => array('ID', 'AUTHOR_ID'), 'filter' => array('=AVATAR' => $params['ID']))); while ($row = $result->fetch()) { IM\ChatTable::update($row['ID'], array('AVATAR' => '')); $obCache = new CPHPCache(); $arRel = CIMChat::GetRelationById($row['ID']); foreach ($arRel as $rel) { $obCache->CleanDir('/bx/imc/recent' . CIMMessenger::GetCachePath($rel['USER_ID'])); } } }
/** * Method will be invoked after new database record inserted. * * @param array $newRecord All fields of inserted record. * * @return void */ public function afterInsertTrigger(array $newRecord) { $id = intval($newRecord['MESSAGE_ID']); if (!\Bitrix\Main\Loader::includeModule('pull')) { return; } $message = \CIMMessenger::GetById($id, array('WITH_FILES' => 'Y')); if (!$message) { return; } if ($newRecord['PARAM_NAME'] === 'LIKE' && $newRecord["PARAM_VALUE"]) { $like = $message['PARAMS']['LIKE']; $result = \Bitrix\IM\ChatTable::getList(array('filter' => array('=ID' => $message['CHAT_ID']))); $chat = $result->fetch(); $relations = \CIMMessenger::GetRelationById($id); if (!isset($relations[$newRecord["PARAM_VALUE"]])) { return; } if ($message['AUTHOR_ID'] > 0 && $message['AUTHOR_ID'] != $newRecord["PARAM_VALUE"]) { $CCTP = new \CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => "N", "BIU" => "N", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => "N", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); $message['MESSAGE'] = str_replace('<br />', ' ', $CCTP->convertText($message['MESSAGE'])); $message['MESSAGE'] = preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $message['MESSAGE']); $message['MESSAGE'] = preg_replace("/\\[[bui]\\](.*?)\\[\\/[bui]\\]/i", "\$1", $message['MESSAGE']); $message['MESSAGE'] = preg_replace("/\\[USER=([0-9]{1,})\\](.*?)\\[\\/USER\\]/i", "\$2", $message['MESSAGE']); $message['MESSAGE'] = preg_replace("/------------------------------------------------------(.*)------------------------------------------------------/mi", " [" . GetMessage('IM_QUOTE') . "] ", str_replace(array("#BR#"), array(" "), $message['MESSAGE'])); if (count($message['FILES']) > 0 && strlen($message['MESSAGE']) < 200) { foreach ($message['FILES'] as $file) { $file = " [" . GetMessage('IM_MESSAGE_FILE') . ": " . $file['name'] . "]"; if (strlen($message['MESSAGE'] . $file) > 200) { break; } $message['MESSAGE'] .= $file; } $message['MESSAGE'] = trim($message['MESSAGE']); } $isChat = $chat && strlen($chat['TITLE']) > 0; $dot = strlen($message['MESSAGE']) >= 200 ? '...' : ''; $message['MESSAGE'] = substr($message['MESSAGE'], 0, 199) . $dot; $message['MESSAGE'] = strlen($message['MESSAGE']) > 0 ? $message['MESSAGE'] : '-'; $arMessageFields = array("MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $message['AUTHOR_ID'], "FROM_USER_ID" => $newRecord["PARAM_VALUE"], "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "main", "NOTIFY_EVENT" => "rating_vote", "NOTIFY_TAG" => "RATING|IM|" . ($isChat ? 'G' : 'P') . "|" . ($isChat ? $chat['ID'] : $newRecord["PARAM_VALUE"]) . "|" . $id, "NOTIFY_MESSAGE" => GetMessage($isChat ? 'IM_MESSAGE_LIKE' : 'IM_MESSAGE_LIKE_PRIVATE', array('#MESSAGE#' => $message['MESSAGE'], '#TITLE#' => $chat['TITLE']))); \CIMNotify::Add($arMessageFields); } $arPullMessage = array('id' => $id, 'chatId' => $relations[$newRecord["PARAM_VALUE"]]['CHAT_ID'], 'senderId' => $newRecord["PARAM_VALUE"], 'users' => $like); foreach ($relations as $rel) { \CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'messageLike', 'params' => $arPullMessage)); } } }
public static function SetRecent($arParams) { $itemId = intval($arParams['ENTITY_ID']); $messageId = intval($arParams['MESSAGE_ID']); if ($itemId <= 0) { return false; } $userId = intval($arParams['USER_ID']); if ($userId <= 0) { $userId = (int) $GLOBALS['USER']->GetID(); } $chatType = IM_MESSAGE_PRIVATE; if (isset($arParams['CHAT_TYPE']) && in_array($arParams['CHAT_TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT))) { $chatType = $arParams['CHAT_TYPE']; } else { if (isset($arParams['CHAT_ID'])) { $orm = IM\ChatTable::getById($arParams['CHAT_ID']); if ($chatData = $orm->fetch()) { $chatType = $chatData['TYPE']; } } } $isChat = in_array($chatType, array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT)); if (!$isChat && $userId == $itemId) { return false; } global $DB; $strSQL = "\n\t\t\tINSERT INTO b_im_recent (USER_ID, ITEM_TYPE, ITEM_ID, ITEM_MID)\n\t\t\tVALUES (" . $userId . ", '" . $chatType . "', " . $itemId . ", " . $messageId . ")\n\t\t\tON DUPLICATE KEY UPDATE ITEM_MID = " . $messageId . "\n\t\t"; $DB->Query($strSQL, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__); $obCache = new CPHPCache(); $obCache->CleanDir('/bx/imc/recent' . CIMMessenger::GetCachePath($userId)); if ($isChat) { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_GROUP); } else { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_MESSAGE); } return true; }
public function DeleteUser($chatId, $userId, $checkPermission = true) { global $DB; $chatId = intval($chatId); $userId = intval($userId); if ($chatId <= 0 || $userId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_OR_CHAT"), "EMPTY_USER_OR_CHAT"); return false; } $strSql = "\n\t\t\tSELECT R.CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.EXTRANET CHAT_EXTRANET, C.TYPE CHAT_TYPE\n\t\t\tFROM b_im_relation R LEFT JOIN b_im_chat C ON R.CHAT_ID = C.ID\n\t\t\tWHERE R.USER_ID = " . $userId . " AND R.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "') AND R.CHAT_ID = " . $chatId; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $extranetFlag = $arRes["CHAT_EXTRANET"] == "" ? "" : ($arRes["CHAT_EXTRANET"] == "Y" ? true : false); $chatTitle = $arRes['CHAT_TITLE']; $chatType = $arRes['CHAT_TYPE']; $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']); if ($chatAuthorId == $userId) { $strSql = "\n\t\t\t\t\tSELECT R.USER_ID\n\t\t\t\t\tFROM b_im_relation R\n\t\t\t\t\tWHERE R.CHAT_ID = " . $chatId . " AND R.USER_ID <> " . $chatAuthorId; $strSql = $DB->TopSql($strSql, 1); $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $strSql = "UPDATE b_im_chat SET AUTHOR_ID = " . $arRes['USER_ID'] . " WHERE ID = " . $chatId; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } $bSelf = true; $arUsers = array($userId); if (is_object($GLOBALS["USER"]) && $GLOBALS["USER"]->GetId() != $userId) { if ($checkPermission && $chatAuthorId != $GLOBALS["USER"]->GetId()) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_KICK"), "IM_ERROR_KICK"); return false; } $bSelf = false; $arUsers[] = $GLOBALS["USER"]->GetId(); } $arOldRelation = CIMChat::GetRelationById($chatId); $arUsers = CIMContactList::GetUserData(array('ID' => array_keys($arOldRelation), 'DEPARTMENT' => 'N', 'USE_CACHE' => 'N')); $arUsers = $arUsers['users']; if ($bSelf) { $message = GetMessage("IM_CHAT_LEAVE_" . $arUsers[$userId]['gender'], array('#USER_NAME#' => htmlspecialcharsback($arUsers[$userId]['name']))); } else { $message = GetMessage("IM_CHAT_KICK_" . $arUsers[$GLOBALS["USER"]->GetId()]['gender'], array('#USER_1_NAME#' => htmlspecialcharsback($arUsers[$GLOBALS["USER"]->GetId()]['name']), '#USER_2_NAME#' => htmlspecialcharsback($arUsers[$userId]['name']))); } $CIMChat = new CIMChat($userId); $CIMChat->SetReadMessage($chatId); CIMContactList::CleanChatCache($userId); $publicPullWatch = false; if ($chatType == IM_MESSAGE_OPEN && CModule::IncludeModule("pull")) { $publicPullWatch = true; } $relationList = IM\RelationTable::getList(array("select" => array("ID", "USER_ID"), "filter" => array("=CHAT_ID" => $chatId, "=USER_ID" => $userId))); while ($relation = $relationList->fetch()) { if ($publicPullWatch && !$arUsers[$relation["USER_ID"]]['extranet']) { CPullWatch::Add($relation["USER_ID"], 'IM_PUBLIC_' . $chatId, true); } Im\RelationTable::delete($relation["ID"]); CIMContactList::DeleteRecent($chatId, true, $relation["USER_ID"]); if ($extranetFlag !== false) { $isExtranet = false; foreach ($arUsers as $userData) { if ($userData['id'] == $userId) { continue; } if ($userData['extranet']) { $isExtranet = true; break; } } if (!$isExtranet || $extranetFlag === "") { IM\ChatTable::update($chatId, array('EXTRANET' => $isExtranet ? "Y" : "N")); } $extranetFlag = $isExtranet; } } CIMDisk::ChangeFolderMembers($chatId, $userId, false); self::AddMessage(array("TO_CHAT_ID" => $chatId, "MESSAGE" => $message, "FROM_USER_ID" => $this->user_id, "SYSTEM" => 'Y')); foreach ($arOldRelation as $rel) { CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'chatUserLeave', 'params' => array('chatId' => $chatId, 'chatTitle' => $chatTitle, 'userId' => $userId, 'message' => $bSelf ? '' : htmlspecialcharsbx($message)))); } return true; } $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_USER_NOT_FOUND"), "USER_NOT_FOUND"); return false; }
public static function Like($id, $action = 'auto') { global $USER; if (!CModule::IncludeModule('pull')) return false; $action = in_array($action, Array('plus', 'minus'))? $action: 'auto'; $message = self::GetById($id, Array('WITH_FILES' => 'Y')); if (!$message) return false; $relations = CIMMessenger::GetRelationById($id); if (!isset($relations[$USER->GetId()])) return false; $isLike = false; if (isset($message['PARAMS']['LIKE'])) { $isLike = in_array($USER->GetId(), $message['PARAMS']['LIKE']); } if ($isLike && $action == 'plus') { return false; } else if (!$isLike && $action == 'minus') { return false; } $isLike = true; if (isset($message['PARAMS']['LIKE'])) { $like = $message['PARAMS']['LIKE']; $selfLike = array_search($USER->GetId(), $like); if ($selfLike !== false) { $isLike = false; unset($like[$selfLike]); } else { $like[] = $USER->GetId(); } } else { $like = Array($USER->GetId()); } sort($like); CIMMessageParam::Set($id, Array('LIKE' => $like)); $result = IM\ChatTable::getList(Array( 'filter'=>Array( '=ID' => $message['CHAT_ID'] ) )); $chat = $result->fetch(); if ($message['AUTHOR_ID'] > 0 && $message['AUTHOR_ID'] != $USER->GetId() && $isLike) { $CCTP = new CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => "N", "BIU" => "N", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => "N", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); $message['MESSAGE'] = str_replace('<br />', ' ', $CCTP->convertText($message['MESSAGE'])); if (count($message['FILES']) > 0 && strlen($message['MESSAGE']) < 200) { foreach ($message['FILES'] as $file) { $file = " [".GetMessage('IM_MESSAGE_FILE').": ".$file['name']."]"; if (strlen($message['MESSAGE'].$file) > 200) break; $message['MESSAGE'] .= $file; } $message['MESSAGE'] = trim($message['MESSAGE']); } $isChat = $chat && strlen($chat['TITLE']) > 0; $arMessageFields = array( "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $message['AUTHOR_ID'], "FROM_USER_ID" => $USER->GetId(), "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "main", "NOTIFY_EVENT" => "rating_vote", "NOTIFY_TAG" => "RATING|IM|".($isChat? 'G':'P')."|".($isChat? $chat['ID']: $USER->GetId())."|".$id, "NOTIFY_MESSAGE" => GetMessage($isChat? 'IM_MESSAGE_LIKE': 'IM_MESSAGE_LIKE_PRIVATE', Array( '#MESSAGE#' => $message['MESSAGE'], '#TITLE#' => $chat['TITLE'] )) ); CIMNotify::Add($arMessageFields); } $arPullMessage = Array( 'id' => $id, 'chatId' => $relations[$USER->GetId()]['CHAT_ID'], 'senderId' => $USER->GetId(), 'users' => $like ); foreach ($relations as $rel) { CPullStack::AddByUser($rel['USER_ID'], Array( 'module_id' => 'im', 'command' => 'messageLike', 'params' => $arPullMessage )); } return $like; }
public function Add($arParams) { global $DB; $chatTitle = ''; if (isset($arParams['TITLE'])) { $chatTitle = trim($arParams['TITLE']); } $userId = array(); if (isset($arParams['USERS'])) { $userId = $arParams['USERS']; } $callNumber = ''; if (isset($arParams['CALL_NUMBER'])) { $callNumber = $arParams['CALL_NUMBER']; } $entityType = ''; if (isset($arParams['ENTITY_TYPE'])) { $entityType = $arParams['ENTITY_TYPE']; } $entityId = ''; if (isset($arParams['ENTITY_ID'])) { $entityId = $arParams['ENTITY_ID']; } $skipUserAdd = false; if ($userId === false) { $skipUserAdd = true; } $arUserId = array(); $arUserId[$this->user_id] = $this->user_id; if (is_array($userId)) { foreach ($userId as $value) { $arUserId[intval($value)] = intval($value); } } else { if (intval($userId) > 0) { $arUserId[intval($userId)] = intval($userId); } } if (!$skipUserAdd) { if (count($arUserId) <= 2) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MIN_USER"), "MIN_USER"); return false; } if (count($arUserId) > 100) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MAX_USER", array('#COUNT#' => 100)), "MAX_USER"); return false; } if (!IsModuleInstalled('intranet') && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed()) { global $USER; $arFriendUsers = array(); $dbFriends = CSocNetUserRelations::GetList(array(), array("USER_ID" => $USER->GetID(), "RELATION" => SONET_RELATIONS_FRIEND), false, false, array("ID", "FIRST_USER_ID", "SECOND_USER_ID", "DATE_CREATE", "DATE_UPDATE", "INITIATED_BY")); while ($arFriends = $dbFriends->Fetch()) { $friendId = $USER->GetID() == $arFriends["FIRST_USER_ID"] ? $arFriends["SECOND_USER_ID"] : $arFriends["FIRST_USER_ID"]; $arFriendUsers[$friendId] = $friendId; } foreach ($arUserId as $id => $userId) { if ($userId == $USER->GetID()) { continue; } if (!isset($arFriendUsers[$userId]) && CIMSettings::GetPrivacy(CIMSettings::PRIVACY_CHAT, $userId) == CIMSettings::PRIVACY_RESULT_CONTACT) { unset($arUserId[$id]); } } if (count($arUserId) <= 2) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MIN_USER_BY_PRIVACY"), "MIN_USER_BY_PRIVACY"); return false; } } } $arUsers = CIMContactList::GetUserData(array('ID' => array_values($arUserId), 'DEPARTMENT' => 'N', 'USE_CACHE' => 'N')); $arUsers = $arUsers['users']; $arUsersName = array(); if ($chatTitle == "") { foreach ($arUserId as $userId) { $arUsersName[$userId] = htmlspecialcharsback($arUsers[$userId]['name']); } $chatTitle = implode(', ', $arUsersName); } $result = IM\ChatTable::add(array("TITLE" => substr($chatTitle, 0, 255), "TYPE" => IM_MESSAGE_GROUP, "AUTHOR_ID" => $this->user_id, "ENTITY_TYPE" => $entityType, "ENTITY_ID" => $entityId, "CALL_NUMBER" => $callNumber)); $chatId = $result->getId(); if ($chatId > 0) { $params = $result->getData(); if (intval($params['AVATAR']) > 0) { $this->lastAvatarId = $params['AVATAR']; } $arUsersName = array(); foreach ($arUserId as $userId) { if ($userId != $this->user_id) { $arUsersName[$userId] = htmlspecialcharsback($arUsers[$userId]['name']); } CIMContactList::SetRecent($chatId, 0, true, $userId); $strSql = "INSERT INTO b_im_relation (CHAT_ID, MESSAGE_TYPE, USER_ID, STATUS) VALUES (" . $chatId . ",'" . IM_MESSAGE_GROUP . "'," . $userId . ", " . IM_STATUS_READ . ")"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); CIMContactList::CleanChatCache($userId); } if (!$skipUserAdd) { $message = GetMessage("IM_CHAT_JOIN_" . $arUsers[$this->user_id]['gender'], array('#USER_1_NAME#' => htmlspecialcharsback($arUsers[$this->user_id]['name']), '#USER_2_NAME#' => implode(', ', $arUsersName))); self::AddMessage(array("TO_CHAT_ID" => $chatId, "FROM_USER_ID" => $this->user_id, "MESSAGE" => $message, "SYSTEM" => 'Y')); } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_IM_ERROR_EMPTY_USER_OR_CHAT"), "ERROR_OF_CREATE_CHAT"); return false; } return $chatId; }
/** * Method will be invoked after new database record inserted. * * @param array $newRecord All fields of inserted record. * * @return void */ public function afterInsertTrigger(array $newRecord) { $arParams = array(); $chatId = $newRecord['CHAT_ID']; $arRel = \CIMChat::GetRelationById($chatId); $arFields['MESSAGE_TYPE'] = ''; foreach ($arRel as $rel) { $arFields['MESSAGE_TYPE'] = $rel["MESSAGE_TYPE"]; break; } $arFields['PARAMS'] = array(); $arFields['FILES'] = array(); //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message_v2', '**', false); if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { foreach ($arRel as $rel) { if ($rel['USER_ID'] == $newRecord['AUTHOR_ID']) { $arFields['FROM_USER_ID'] = $rel['USER_ID']; } else { $arFields['TO_USER_ID'] = $rel['USER_ID']; } } \CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['TO_USER_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['FROM_USER_ID'])); \CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['FROM_USER_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['TO_USER_ID'])); if (\CModule::IncludeModule('pull')) { $pullMessage = array('module_id' => 'im', 'command' => 'message', 'params' => \CIMMessage::GetFormatMessage(array('ID' => $newRecord['ID'], 'CHAT_ID' => $chatId, 'TO_USER_ID' => $arFields['TO_USER_ID'], 'FROM_USER_ID' => $arFields['FROM_USER_ID'], 'SYSTEM' => $newRecord['NOTIFY_EVENT'] == 'private_system' ? 'Y' : 'N', 'MESSAGE' => $newRecord['MESSAGE'], 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES']))); $pullMessageTo = $pullMessage; if (\CPullOptions::GetPushStatus()) { if (\CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], 'im', 'message', \CIMSettings::CLIENT_PUSH)) { $pushParams = \CIMMessenger::PreparePushForPrivate(array('FROM_USER_ID' => $arFields['FROM_USER_ID'], 'MESSAGE' => $newRecord['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'], 'FILES' => $arFields['FILES'])); $pullMessageTo = array_merge($pullMessage, $pushParams); } } \CPullStack::AddByUser($arFields['TO_USER_ID'], $pullMessageTo); \CPullStack::AddByUser($arFields['FROM_USER_ID'], $pullMessage); \CPushManager::DeleteFromQueueBySubTag($arFields['FROM_USER_ID'], 'IM_MESS'); //self::SendBadges($arFields['TO_USER_ID']); } } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) { foreach ($arRel as $relation) { \CIMContactList::SetRecent(array('ENTITY_ID' => $relation['CHAT_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => $relation['MESSAGE_TYPE'], 'USER_ID' => $relation['USER_ID'])); } if (\CModule::IncludeModule('pull')) { $pullMessage = array('module_id' => 'im', 'command' => 'messageChat', 'params' => \CIMMessage::GetFormatMessage(array('ID' => $newRecord['ID'], 'CHAT_ID' => $chatId, 'TO_CHAT_ID' => $chatId, 'FROM_USER_ID' => $newRecord['AUTHOR_ID'], 'MESSAGE' => $newRecord['MESSAGE'], 'SYSTEM' => $newRecord['AUTHOR_ID'] > 0 ? 'N' : 'Y', 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES']))); $chat = \Bitrix\Im\ChatTable::getById($chatId); $chatData = $chat->fetch(); if ($chatData && \CPullOptions::GetPushStatus()) { $pushParams = \CIMMessenger::PreparePushForChat(array('CHAT_ID' => $chatId, 'CHAT_TITLE' => $chatData['TITLE'], 'FROM_USER_ID' => $newRecord['AUTHOR_ID'], 'MESSAGE' => $newRecord['MESSAGE'], 'SYSTEM' => $newRecord['AUTHOR_ID'] > 0 ? 'N' : 'Y', 'FILES' => $arFields['FILES'])); $pullMessage = array_merge($pullMessage, $pushParams); } $pullUsers = array(); $pullUsersSkip = array(); foreach ($arRel as $rel) { $pullUsers[] = $rel['USER_ID']; if ($rel['USER_ID'] == $newRecord['AUTHOR_ID']) { $pullUsersSkip[] = $rel['USER_ID']; \CPushManager::DeleteFromQueueBySubTag($newRecord['AUTHOR_ID'], 'IM_MESS'); } else { if ($rel['NOTIFY_BLOCK'] == 'Y' || !\CIMSettings::GetNotifyAccess($rel['USER_ID'], 'im', $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN ? 'openChat' : 'chat', \CIMSettings::CLIENT_PUSH)) { $pullUsersSkip[] = $rel['USER_ID']; } } } $pullMessage['push']['skip_users'] = $pullUsersSkip; \CPullStack::AddByUsers($pullUsers, $pullMessage); if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) { \CPullWatch::AddToStack('IM_PUBLIC_' . $chatId, $pullMessage); } /* TODO uncomment after fix mantis:66363 \CIMMessenger::SendMention(Array( 'CHAT_ID' => $chatId, 'CHAT_TITLE' => $chatData['TITLE'], 'CHAT_RELATION' => $arRel, 'CHAT_TYPE' => $chatData['TYPE'], 'MESSAGE' => $newRecord['MESSAGE'], 'FILES' => $arFields['FILES'], 'FROM_USER_ID' => $newRecord['AUTHOR_ID'], )); */ //\CIMMessenger::SendBadges($usersForBadges); foreach (\GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) { \ExecuteModuleEventEx($arEvent, array($newRecord['ID'], $newRecord)); } } } } }
public static function ChangeFolderMembers($chatId, $userId, $append = true) { $folderModel = self::GetFolderModel($chatId); if (!$folderModel) { return false; } $result = IM\ChatTable::getById($chatId); if (!($chat = $result->fetch())) { return false; } if (!is_array($userId)) { $userIds = array($userId); } else { $userIds = $userId; } $driver = \Bitrix\Disk\Driver::getInstance(); $rightsManager = $driver->getRightsManager(); if ($append) { $fullAccessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_FULL); $accessCodes = array(); if ($chat['TYPE'] == IM_MESSAGE_OPEN) { $users = CIMContactList::GetUserData(array('ID' => array_values($userIds), 'DEPARTMENT' => 'N', 'SHOW_ONLINE' => 'N')); foreach ($users['users'] as $userData) { if ($userData['extranet']) { $accessCodes[] = array('ACCESS_CODE' => 'U' . $userData['id'], 'TASK_ID' => $fullAccessTaskId); } } } else { foreach ($userIds as $userId) { $userId = intval($userId); if ($userId <= 0) { continue; } $accessCodes[] = array('ACCESS_CODE' => 'U' . $userId, 'TASK_ID' => $fullAccessTaskId); } } if (count($accessCodes) <= 0) { return false; } $result = $rightsManager->append($folderModel, $accessCodes); } else { $accessCodes = array(); if ($chat['TYPE'] == IM_MESSAGE_OPEN) { $users = CIMContactList::GetUserData(array('ID' => array_values($userIds), 'DEPARTMENT' => 'N', 'SHOW_ONLINE' => 'N')); foreach ($users['users'] as $userData) { if ($userData['extranet']) { $accessCodes[] = 'U' . $userData['id']; } } } else { foreach ($userIds as $userId) { $userId = intval($userId); if ($userId <= 0) { continue; } $accessCodes[] = 'U' . $userId; } } $result = $rightsManager->revokeByAccessCodes($folderModel, $accessCodes); } return $result; }
public function GetDialogInfo($phone, $chatTitle = '', $getPhoto = true) { $phoneNormalize = CVoxImplantPhone::Normalize($phone); if (!$phoneNormalize) { $phoneNormalize = preg_replace("/[^0-9\\#\\*]/i", "", $phone); } $phone = $phoneNormalize; $hrPhoto = array(); $openChat = true; $result = VI\PhoneTable::getList(array('select' => array('USER_ID', 'PHONE_MNEMONIC'), 'filter' => array('=PHONE_NUMBER' => $phone))); $userId = false; while ($row = $result->fetch()) { if (!$userId && $row['PHONE_MNEMONIC'] != 'WORK_PHONE') { $userId = $row['USER_ID']; $openChat = false; } else { if (!$userId && $row['PHONE_MNEMONIC'] == 'WORK_PHONE') { $openChat = true; } } } if ($userId == $this->userId) { $openChat = true; } $dialogId = 0; if (CModule::IncludeModule('im')) { if (CVoxImplantConfig::GetChatAction() == CVoxImplantConfig::INTERFACE_CHAT_NONE) { } else { if ($openChat) { $entityId = $phone; if (CVoxImplantConfig::GetChatAction() == CVoxImplantConfig::INTERFACE_CHAT_APPEND) { $entityId = 'UNIFY_CALL_CHAT'; $chatTitle = GetMessage('VI_CALL_CHAT_UNIFY'); } $result = IM\ChatTable::getList(array('select' => array('ID', 'AVATAR'), 'filter' => array('=ENTITY_TYPE' => 'CALL', '=ENTITY_ID' => $entityId, '=AUTHOR_ID' => $this->userId))); if ($row = $result->fetch()) { $dialogId = 'chat' . $row['ID']; $avatarId = $row['AVATAR']; } else { $CIMChat = new CIMChat($this->userId); $chatId = $CIMChat->Add(array('TITLE' => $chatTitle != '' ? $chatTitle : $phone, 'USERS' => false, 'CALL_NUMBER' => $entityId == 'UNIFY_CALL_CHAT' ? '' : $entityId, 'ENTITY_TYPE' => 'CALL', 'ENTITY_ID' => $entityId)); if ($chatId) { $dialogId = 'chat' . $chatId; $avatarId = $CIMChat->lastAvatarId; } } if ($getPhoto && intval($avatarId) > 0) { $arPhotoHrTmp = CFile::ResizeImageGet($avatarId, array('width' => 200, 'height' => 200), BX_RESIZE_IMAGE_EXACT, false, false, true); $hrPhoto[$dialogId] = empty($arPhotoHrTmp['src']) ? '/bitrix/js/im/images/hidef-avatar-v2.png' : $arPhotoHrTmp['src']; } } else { if ($userId) { if ($getPhoto) { $userData = CIMContactList::GetUserData(array('ID' => $userId, 'DEPARTMENT' => 'N', 'HR_PHOTO' => 'Y')); $hrPhoto = $userData['hrphoto']; } $dialogId = $userId; } } } } if (!$dialogId) { $this->error = new CVoxImplantError(__METHOD__, 'ERROR_NEW_CHAT', GetMessage('VI_ERROR_NEW_CHAT')); return false; } //foreach(GetModuleEvents("voximplant", "OnGetDialogInfo", true) as $arEvent) // ExecuteModuleEventEx($arEvent, array('USER_ID' => $this->userId, 'DIALOG_ID' => $dialogId)); return array('DIALOG_ID' => $dialogId, 'HR_PHOTO' => $hrPhoto); }
public static function GetChatId($fromUserId, $toUserId) { global $DB; $chatId = 0; $fromUserId = intval($fromUserId); $toUserId = intval($toUserId); if (intval($fromUserId) <= 0 || intval($toUserId) <= 0) { return $chatId; } $strSql = "\n\t\t\tSELECT RF.CHAT_ID\n\t\t\tFROM\n\t\t\t\tb_im_relation RF\n\t\t\t\tINNER JOIN b_im_relation RT on RF.CHAT_ID = RT.CHAT_ID\n\t\t\tWHERE\n\t\t\t\tRF.USER_ID = " . $fromUserId . "\n\t\t\tand RT.USER_ID = " . $toUserId . "\n\t\t\tand RF.MESSAGE_TYPE = '" . IM_MESSAGE_PRIVATE . "'\n\t\t"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); } if ($chatId <= 0) { $result = \Bitrix\IM\ChatTable::add(array('TYPE' => IM_MESSAGE_PRIVATE, 'AUTHOR_ID' => $fromUserId)); $chatId = $result->getId(); if ($chatId > 0) { IM\RelationTable::add(array("CHAT_ID" => $chatId, "MESSAGE_TYPE" => IM_MESSAGE_PRIVATE, "USER_ID" => $fromUserId, "STATUS" => IM_STATUS_READ)); IM\RelationTable::add(array("CHAT_ID" => $chatId, "MESSAGE_TYPE" => IM_MESSAGE_PRIVATE, "USER_ID" => $toUserId, "STATUS" => IM_STATUS_READ)); } } return $chatId; }
public static function GetFolderModel($chatId) { if (!self::Enabled()) return false; $folderModel = false; $result = Im\ChatTable::getById($chatId); if (!$chat = $result->fetch()) return false; $folderId = intval($chat['DISK_FOLDER_ID']); if ($folderId > 0) { $folderModel = \Bitrix\Disk\Folder::getById($folderId); if (!$folderModel || $folderModel->getStorageId() != self::GetStorageId()) { $folderId = 0; } } if (!$folderId) { $driver = \Bitrix\Disk\Driver::getInstance(); $storageModel = self::GetStorage(); if (!$storageModel) { return false; } $rightsManager = $driver->getRightsManager(); $fullAccessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_FULL); $accessCodes = array(); $accessCodes[] = Array( 'ACCESS_CODE' => 'AU', 'TASK_ID' => $fullAccessTaskId, 'NEGATIVE' => 1 ); $chatRelation = CIMChat::GetRelationById($chatId); foreach ($chatRelation as $relation) { $accessCodes[] = Array( 'ACCESS_CODE' => 'U'.$relation['USER_ID'], 'TASK_ID' => $fullAccessTaskId ); } $folderModel = $storageModel->addFolder(array('NAME' => 'chat'.$chatId, 'CREATED_BY' => self::GetUserId()), $accessCodes); if ($folderModel) Im\ChatTable::update($chatId, Array('DISK_FOLDER_ID' => $folderModel->getId())); } return $folderModel; }
public static function Like($id, $action = 'auto') { global $USER; if (!CModule::IncludeModule('pull')) { return false; } $action = in_array($action, array('plus', 'minus')) ? $action : 'auto'; $message = self::GetById($id, array('WITH_FILES' => 'Y')); if (!$message) { return false; } $relations = CIMMessenger::GetRelationById($id); if (!isset($relations[$USER->GetId()])) { return false; } $isLike = false; if (isset($message['PARAMS']['LIKE'])) { $isLike = in_array($USER->GetId(), $message['PARAMS']['LIKE']); } if ($isLike && $action == 'plus') { return false; } else { if (!$isLike && $action == 'minus') { return false; } } $isLike = true; if (isset($message['PARAMS']['LIKE'])) { $like = $message['PARAMS']['LIKE']; $selfLike = array_search($USER->GetId(), $like); if ($selfLike !== false) { $isLike = false; unset($like[$selfLike]); } else { $like[] = $USER->GetId(); } } else { $like = array($USER->GetId()); } sort($like); CIMMessageParam::Set($id, array('LIKE' => $like)); $result = IM\ChatTable::getList(array('filter' => array('=ID' => $message['CHAT_ID']))); $chat = $result->fetch(); if ($message['AUTHOR_ID'] > 0 && $message['AUTHOR_ID'] != $USER->GetId() && $isLike) { $CCTP = new CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => "N", "BIU" => "N", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => "N", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); $message['MESSAGE'] = str_replace('<br />', ' ', $CCTP->convertText($message['MESSAGE'])); $message['MESSAGE'] = preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $message['MESSAGE']); $message['MESSAGE'] = preg_replace("/\\[[bui]\\](.*?)\\[\\/[bui]\\]/i", "\$1", $message['MESSAGE']); $message['MESSAGE'] = preg_replace("/\\[USER=([0-9]{1,})\\](.*?)\\[\\/USER\\]/i", "\$2", $message['MESSAGE']); $message['MESSAGE'] = preg_replace('#\\-{54}.+?\\-{54}#s', " [" . GetMessage('IM_QUOTE') . "] ", str_replace(array("#BR#"), array(" "), $message['MESSAGE'])); if (count($message['FILES']) > 0 && strlen($message['MESSAGE']) < 200) { foreach ($message['FILES'] as $file) { $file = " [" . GetMessage('IM_MESSAGE_FILE') . ": " . $file['name'] . "]"; if (strlen($message['MESSAGE'] . $file) > 200) { break; } $message['MESSAGE'] .= $file; } $message['MESSAGE'] = trim($message['MESSAGE']); } $isChat = $chat && strlen($chat['TITLE']) > 0; $dot = strlen($message['MESSAGE']) >= 200 ? '...' : ''; $message['MESSAGE'] = substr($message['MESSAGE'], 0, 199) . $dot; $message['MESSAGE'] = strlen($message['MESSAGE']) > 0 ? $message['MESSAGE'] : '-'; $arMessageFields = array("MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $message['AUTHOR_ID'], "FROM_USER_ID" => $USER->GetId(), "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "im", "NOTIFY_EVENT" => "like", "NOTIFY_TAG" => "RATING|IM|" . ($isChat ? 'G' : 'P') . "|" . ($isChat ? $chat['ID'] : $USER->GetId()) . "|" . $id, "NOTIFY_MESSAGE" => GetMessage($isChat ? 'IM_MESSAGE_LIKE' : 'IM_MESSAGE_LIKE_PRIVATE', array('#MESSAGE#' => $message['MESSAGE'], '#TITLE#' => $isChat ? '[CHAT=' . $chat['ID'] . ']' . $chat['TITLE'] . '[/CHAT]' : $chat['TITLE'])), "NOTIFY_MESSAGE_OUT" => GetMessage($isChat ? 'IM_MESSAGE_LIKE' : 'IM_MESSAGE_LIKE_PRIVATE', array('#MESSAGE#' => $message['MESSAGE'], '#TITLE#' => $chat['TITLE']))); CIMNotify::Add($arMessageFields); } $arPullMessage = array('id' => $id, 'chatId' => $chat['ID'], 'senderId' => $USER->GetId(), 'users' => $like); foreach ($relations as $rel) { CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'messageLike', 'params' => $arPullMessage)); } if ($chat['TYPE'] == IM_MESSAGE_OPEN) { CPullWatch::AddToStack('IM_PUBLIC_' . $chat['ID'], array('module_id' => 'im', 'command' => 'messageLike', 'params' => $arPullMessage)); } return $like; }
public static function SetRecent($arParams) { global $USER; $itemId = intval($arParams['ENTITY_ID']); $messageId = intval($arParams['MESSAGE_ID']); if ($itemId <= 0) { return false; } $userId = intval($arParams['USER_ID']); if ($userId <= 0) { $userId = (int) $USER->GetID(); } $chatType = IM_MESSAGE_PRIVATE; if (isset($arParams['CHAT_TYPE']) && in_array($arParams['CHAT_TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT))) { $chatType = $arParams['CHAT_TYPE']; } else { if (isset($arParams['CHAT_ID'])) { $orm = IM\ChatTable::getById($arParams['CHAT_ID']); if ($chatData = $orm->fetch()) { $chatType = $chatData['TYPE']; } } } $isChat = in_array($chatType, array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT)); if (!$isChat && $userId == $itemId) { return false; } $connection = \Bitrix\Main\Application::getInstance()->getConnection(); $isUserAlreadyInRecent = $connection->queryScalar("SELECT 1 FROM b_im_recent WHERE USER_ID = " . $userId); $merge = $connection->getSqlHelper()->prepareMerge("b_im_recent", array('USER_ID', 'ITEM_TYPE', 'ITEM_ID'), array('USER_ID' => $userId, 'ITEM_TYPE' => $chatType, 'ITEM_ID' => $itemId, 'ITEM_MID' => $messageId), array('ITEM_MID' => $messageId)); if ($merge && $merge[0] != "") { $connection->query($merge[0]); } $obCache = new CPHPCache(); $obCache->CleanDir('/bx/imc/recent' . CIMMessenger::GetCachePath($userId)); if ($isChat) { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_GROUP); } else { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_MESSAGE); } if (!$isUserAlreadyInRecent) { $event = new \Bitrix\Main\Event("im", "OnAfterRecentAdd", array("user_id" => $userId)); $event->send(); } return true; }
public static function PreparePushForChat($params) { $params['CHAT_ID'] = intval($params['CHAT_ID']); if ($params['CHAT_ID'] <= 0) { return false; } if (!isset($params['CHAT_TITLE'])) { $orm = \Bitrix\Im\ChatTable::getById($params['CHAT_ID']); $chat = $orm->fetch(); if (!$chat) { return false; } $params['CHAT_TITLE'] = $chat['TITLE']; } $params['CHAT_TITLE'] = substr(htmlspecialcharsback($params['CHAT_TITLE']), 0, 32); if ($params['SYSTEM'] == 'Y') { $pushText = $params['CHAT_TITLE'] . ': ' . $params['MESSAGE']; } else { $userName = \Bitrix\Im\User::getInstance($params['FROM_USER_ID'])->getName(); if (!$userName) { return false; } $pushText = GetMessage('IM_PUSH_GROUP_TITLE', array('#USER#' => $userName, '#GROUP#' => $params['CHAT_TITLE'])) . ': ' . $params['MESSAGE']; } $pushText = self::PrepareMessageForPush(array('MESSAGE' => $pushText, 'FILES' => $params['FILES'], 'ATTACH' => empty($params['MESSAGE']) && $params['ATTACH'])); if (strlen($pushText) <= 0) { return false; } $result = array(); $result['push']['params'] = array('TAG' => 'IM_CHAT_' . $params['CHAT_ID'], 'CATEGORY' => 'ANSWER', 'URL' => SITE_DIR . 'mobile/ajax.php?mobile_action=im_answer', 'PARAMS' => array('RECIPIENT_ID' => 'chat' . $params['CHAT_ID'])); $result['push']['tag'] = 'IM_CHAT_' . $params['CHAT_ID']; $result['push']['sub_tag'] = 'IM_MESS'; $result['push']['app_id'] = 'Bitrix24'; $result['push']['message'] = $pushText; return $result; }