Esempio n. 1
0
 /**
  * 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));
                 }
             }
         }
     }
 }