6if (Loader::includeModule(
'replica'))
8 class RelationHandler
extends \Bitrix\Replica\Client\BaseHandler
10 protected $tableName =
"b_im_relation";
11 protected $moduleId =
"im";
12 protected $className =
"\\Bitrix\\Im\\Model\\RelationTable";
13 protected $primary = array(
14 "ID" =>
"auto_increment",
16 protected $predicates = array(
17 "CHAT_ID" =>
"b_im_chat.ID",
18 "USER_ID" =>
"b_user.ID",
20 protected $translation = array(
21 "ID" =>
"b_im_relation.ID",
22 "CHAT_ID" =>
"b_im_chat.ID",
23 "USER_ID" =>
"b_user.ID",
24 "START_ID" =>
"b_im_message.ID",
25 "LAST_ID" =>
"b_im_message.ID",
26 "LAST_SEND_ID" =>
"b_im_message.ID",
28 protected $fields = array(
29 "LAST_READ" =>
"datetime",
41 public function beforeInsertTrigger(array &$newRecord)
43 if ($newRecord[
"CHAT_ID"] <= 0 || $newRecord[
"USER_ID"] <= 0)
45 return array(
"ID" => 0);
48 isset($newRecord[
"MESSAGE_TYPE"])
49 && $newRecord[
"MESSAGE_TYPE"] ===
"S"
52 $chatList = \Bitrix\Im\Model\RelationTable::getList(array(
54 "=USER_ID" => $newRecord[
"USER_ID"],
55 "=CHAT_ID" => $newRecord[
"CHAT_ID"],
56 "=MESSAGE_TYPE" =>
"S",
59 $oldRecord = $chatList->fetch();
75 public function beforeLogUpdate(array $record)
77 if ($record[
"MESSAGE_TYPE"] ===
"S")
89 public function beforeLogInsert(array $record)
91 if (\
Bitrix\Im\User::getInstance($record[
"USER_ID"])->isBot())
109 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
112 $newRecord[
"MESSAGE_TYPE"] ===
"P"
113 && intval($oldRecord[
"LAST_ID"]) < intval($newRecord[
"LAST_ID"])
116 $oldLastRead = $oldRecord[
"LAST_READ"] instanceof \Bitrix\Main\Type\DateTime? $oldRecord[
"LAST_READ"]:
false;
117 $newLastRead = $newRecord[
"LAST_READ"] instanceof \Bitrix\Main\Type\DateTime? $newRecord[
"LAST_READ"]:
false;
118 if ($oldLastRead < $newLastRead)
120 if (\
Bitrix\Main\Loader::includeModule(
'pull'))
122 $relationList = \Bitrix\Im\Model\RelationTable::getList(array(
123 "select" => array(
"ID",
"USER_ID"),
125 "=CHAT_ID" => $newRecord[
"CHAT_ID"],
126 "!=USER_ID" => $newRecord[
"USER_ID"],
129 if ($relation = $relationList->fetch())
131 \Bitrix\Pull\Event::add($relation[
'USER_ID'], Array(
133 'command' =>
'readMessageOpponent',
136 'dialogId' => intval($newRecord[
'USER_ID']),
137 'chatId' => intval($newRecord[
'CHAT_ID']),
138 'userId' => intval($newRecord[
'USER_ID']),
139 'chatMessageStatus' =>
''
141 'extra' => \
Bitrix\Im\Common::getPullExtra()