Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
relationhandler.php
1<?php
2namespace Bitrix\Im\Replica;
3
5
6if (Loader::includeModule('replica'))
7{
8 class RelationHandler extends \Bitrix\Replica\Client\BaseHandler
9 {
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",
15 );
16 protected $predicates = array(
17 "CHAT_ID" => "b_im_chat.ID",
18 "USER_ID" => "b_user.ID",
19 );
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",
27 );
28 protected $fields = array(
29 "LAST_READ" => "datetime",
30 );
31
41 public function beforeInsertTrigger(array &$newRecord)
42 {
43 if ($newRecord["CHAT_ID"] <= 0 || $newRecord["USER_ID"] <= 0)
44 {
45 return array("ID" => 0);
46 }
47 if (
48 isset($newRecord["MESSAGE_TYPE"])
49 && $newRecord["MESSAGE_TYPE"] === "S"
50 )
51 {
52 $chatList = \Bitrix\Im\Model\RelationTable::getList(array(
53 "filter" => array(
54 "=USER_ID" => $newRecord["USER_ID"],
55 "=CHAT_ID" => $newRecord["CHAT_ID"],
56 "=MESSAGE_TYPE" => "S",
57 ),
58 ));
59 $oldRecord = $chatList->fetch();
60 if ($oldRecord)
61 {
62 return $oldRecord;
63 }
64 }
65 return null;
66 }
67
75 public function beforeLogUpdate(array $record)
76 {
77 if ($record["MESSAGE_TYPE"] === "S")
78 return false;
79 else
80 return true;
81 }
82
89 public function beforeLogInsert(array $record)
90 {
91 if (\Bitrix\Im\User::getInstance($record["USER_ID"])->isBot())
92 {
93 return false;
94 }
95 else
96 {
97 return true;
98 }
99 }
100
109 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
110 {
111 if (
112 $newRecord["MESSAGE_TYPE"] === "P"
113 && intval($oldRecord["LAST_ID"]) < intval($newRecord["LAST_ID"])
114 )
115 {
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)
119 {
120 if (\Bitrix\Main\Loader::includeModule('pull'))
121 {
122 $relationList = \Bitrix\Im\Model\RelationTable::getList(array(
123 "select" => array("ID", "USER_ID"),
124 "filter" => array(
125 "=CHAT_ID" => $newRecord["CHAT_ID"],
126 "!=USER_ID" => $newRecord["USER_ID"],
127 ),
128 ));
129 if ($relation = $relationList->fetch())
130 {
131 \Bitrix\Pull\Event::add($relation['USER_ID'], Array(
132 'module_id' => 'im',
133 'command' => 'readMessageOpponent',
134 'expiry' => 3600,
135 'params' => Array(
136 'dialogId' => intval($newRecord['USER_ID']),
137 'chatId' => intval($newRecord['CHAT_ID']),
138 'userId' => intval($newRecord['USER_ID']),
139 'chatMessageStatus' => ''
140 ),
141 'extra' => \Bitrix\Im\Common::getPullExtra()
142 ));
143 }
144 }
145 }
146 }
147 }
148 }
149}